Subject: Miscompilation of code with numeric predicates

From: "Thompson, David" <[email-masked]>
Date: Mon, 30 Dec 2024 19:17:08 -0500

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 <[email-masked]>
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