unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#75216: Miscompilation of code with numeric predicates
@ 2024-12-31  0:17 Thompson, David
  0 siblings, 0 replies; only message in thread
From: Thompson, David @ 2024-12-31  0:17 UTC (permalink / raw)
  To: 75216

Consider this contrived procedure:

(define (foo x)
  (cond
   ((integer? x) 42)
   ((and (number? x) (inexact? x)) 69)
   (else 138)))

This procedure is kind of silly but it resembles some real world code
I was debugging today.  A call of (foo 8.2) should return 69 and
indeed it does so on Guile 3.0.9. Not so on Guile 3.0.10 where it
returns 138.

The bytecode generated on 3.0.9 vs. 3.0.10 is very different, ~80
instructions vs. ~40, respectively. I suspected a bug in the type
inference pass or related code and after a 'git bisect' it seems that
is indeed the case:

55256ab33f14cd75778f089c5d96ea42f5b44397 is the first bad commit
commit 55256ab33f14cd75778f089c5d96ea42f5b44397
Author: Andy Wingo <wingo@pobox.com>
Date:   Fri Sep 15 15:21:26 2023 +0200
    Better compilation for rational?, exact?, and so on

    These numeric predicates now have CPS branching primcalls, which allows
    type inference and folding to reduce them to less-strong instructions.

    * module/language/cps/effects-analysis.scm (heap-numbers-equal?): Put
    all the number predicates together.  None have type checks.
    * module/language/cps/guile-vm/lower-primcalls.scm
    (define-branching-primcall-alias): New helper.
    (complex?): Same as number?.
    * module/language/cps/guile-vm/lower-primcalls.scm (real?)
    (rational?, integer?, exact-integer?, exact?, inexact?): Define
    lowerers.
    * module/language/cps/type-fold.scm (number?, complex?, real?)
    (rational?, integer?, exact-integer?, exact?, inexact?): Add folders and
    reducers for all of these.
    * module/language/cps/type.scm (number?, complex?, real?)
    (rational?, integer?, exact-integer?, exact?, inexact?): Add type
    inference for these.
    * module/language/tree-il/compile-cps.scm (convert): Add number? checks
    before exact? and inexact?.  Remove the eager lowering of
    exact-integer?; instead rely on folders.
    * module/language/tree-il/cps-primitives.scm (number?, complex?)
    (real?, rational?, integer?, exact-integer?, exact?, inexact?): Add
    primitive decls.  Define as "number-type-predicates?", meaning they need
    a number? guard.

- Dave





^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-12-31  0:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-31  0:17 bug#75216: Miscompilation of code with numeric predicates Thompson, David

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).