GNU bug report logs - #75216
Miscompilation of code with numeric predicates

Previous Next

Package: guile;

Reported by: "Thompson, David" <dthompson2 <at> worcester.edu>

Date: Tue, 31 Dec 2024 00:18:02 UTC

Severity: normal

To reply to this bug, email your comments to 75216 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-guile <at> gnu.org:
bug#75216; Package guile. (Tue, 31 Dec 2024 00:18:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to "Thompson, David" <dthompson2 <at> worcester.edu>:
New bug report received and forwarded. Copy sent to bug-guile <at> gnu.org. (Tue, 31 Dec 2024 00:18:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: "Thompson, David" <dthompson2 <at> worcester.edu>
To: bug-guile <at> gnu.org
Subject: Miscompilation of code with numeric predicates
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 <wingo <at> 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




Information forwarded to bug-guile <at> gnu.org:
bug#75216; Package guile. (Fri, 28 Feb 2025 20:23:01 GMT) Full text and rfc822 format available.

Message #8 received at 75216 <at> debbugs.gnu.org (full text, mbox):

From: Ludovic Courtès <ludo <at> gnu.org>
To: "Thompson, David" <dthompson2 <at> worcester.edu>
Cc: 75216 <at> debbugs.gnu.org
Subject: Re: bug#75216: Miscompilation of code with numeric predicates
Date: Fri, 28 Feb 2025 21:22:05 +0100
"Thompson, David" <dthompson2 <at> worcester.edu> skribis:

> 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.

Bug confirmed on c8a169d38825d5a21da5392b355ca5fc9f33fa55 (post 3.0.10).

Ludo’.




This bug report was last modified 103 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.