wanted: constant folding for symbol constants

Bug #1337069 reported by Jingyi Hou on 2014-07-03
This bug affects 1 person
Affects Status Importance Assigned to Milestone

Bug Description

This issue is inspired by @stassats's and @csr21-cantab's comments on lp#1335732.

We didn't do constant folding on defined constants for +/*, as evidenced by the following forms:

(defun foo (x)
  (* x 2.1 3.1))

(defconstant +two-point-one+ 2.1)
(defconstant +three-point-one+ 3.1)
(defun bar (x)
  (* x +two-point-one+ +three-point-one+))

(= (foo 1.1) (bar 1.1)) => NIL

As we do not allow redefining constants in SBCL, I believe it should be OK to do constant folding for defined constants (at least on integer types).

On the other hand, the above forms also suggest it might be a little inaccurate to do this kind of constant folding on floating point numbers. I really can't decide if this is a serious issue or not, though.

Jingyi Hou (houjingyi) on 2014-07-03
description: updated
Stas Boukarev (stassats) wrote :

Shouldn't it be the other way around? (* x 2.1 3.1) shouldn't be folded into (* x (* 2.1 3.1))?

Jingyi Hou (houjingyi) wrote :

@stassats, that was what I meant by the "On the other hand" part.

I just couldn't decide if that was serious enough to disable this kind of constant folding for floating point numbers.

Besides this, I believe we didn't do constant folding for integer constants, too:

(defconstant +two+ 2)
(defconstant +three+ 3)
(defun baz (x)
  (* x +two+ +three+))

It's just there's no easy way to identify this, that's why I use floating point constants to report this "bug".

Jingyi Hou (houjingyi) wrote :

In that case, I think it might be better to do constant folding for defined constants (both integer type and floating point type), too.

I'm not quite sure how to implement this, though. I tried to tweak REDUCED-CONSTANTS in src/compiler/srctran.lisp again, not working so far, :-)

Stas Boukarev (stassats) on 2014-07-03
Changed in sbcl:
status: New → Fix Committed
Stas Boukarev (stassats) wrote :

c688e5e163f864590ea9f309ab940b881382ad77 makes this happen. Although there are still cases, like

(defun foo (z)
  (let ((x 2.1)
        (y 3.1))
    (* z x y)))
where constant propagation happens at a later stage.

Jingyi Hou (houjingyi) wrote :

That was fast.

Now I see where I went wrong, thanks.

Stas Boukarev <email address hidden> writes:

> Turns out,
> http://www.lispworks.com/reference/HyperSpec/Body/12_aaaa.htm allows
> this.

There's also the potential issue that we would not want the host lisp
compiler doing a cross-compile of sbcl itself to be involved in the
constant-folding of floating point numbers: we don't control things like
its rounding mode, or the existence of negative zero, and so we should
probably prefer no float optimizations of this nature in the
cross-compiler. (We should also mostly not be writing expressions that
would benefit from folding in the cross-compiler, I think, so it might
be appropriate to issue a cross-compiler-only warning for that case.



Changed in sbcl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers