Opened 8 years ago

Last modified 23 months ago

#4101 new bug

Primitive constant unfolding

Reported by: malosh Owned by: schyler
Priority: low Milestone:
Component: Compiler Version: 6.12.2
Keywords: Cc: alexey.skladnoy@…, bjornbm, anton.nik@…, carter.schonwald@…, schyler
Operating System: Unknown/Multiple Architecture: x86
Type of failure: Runtime performance bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by igloo)

Examining the core generated by ghc with -O2 on a numerical code, I saw things like :

case GHC.Prim.<## x_aB9 (GHC.Prim.**## 2.0 -1021.0) of _  {...

This code being executed each time my code performs an addition ! GHC does not seem to unfold the constants right with at least ##. By the way, it should definitely be possible to specify a Double# constant in hexadecimal.

Change History (19)

comment:1 Changed 8 years ago by igloo

Description: modified (diff)

comment:2 Changed 8 years ago by igloo

Milestone: 6.16.1

Test case:

module Q where

foo x = x < (2 ** (-1021) :: Double)

comment:3 Changed 8 years ago by simonmar

There are quite a few floating-point operations that we don't constant fold currently - all the trig functions, **, log, sqrt, and so on. One trickiness with implementing this is that the internal representation for Float/Double in the compiler is Rational, and that doesn't support these operations. We'd have to cast down to Double to do the operation and back to Rational again, and only do it when not cross-compiling.

comment:4 in reply to:  3 Changed 7 years ago by Khudyakov

Replying to simonmar:

There is one more problem. Floats and Doubles have special values +∞, -∞ and NaN. They could be represented as Rational (1%0, -1%0, 0%0) but Data.Ratio doesn't allow to construct such values.

comment:5 Changed 7 years ago by Khudyakov

Cc: alexey.skladnoy@… added

comment:6 Changed 6 years ago by bjornbm

Cc: bjornbm added

comment:7 Changed 6 years ago by lelf

Cc: anton.nik@… added

comment:8 Changed 6 years ago by igloo

Milestone: 7.4.17.6.1
Priority: normallow

comment:9 Changed 5 years ago by igloo

Milestone: 7.6.17.6.2

comment:10 Changed 5 years ago by carter

Cc: carter.schonwald@… added

comment:11 Changed 4 years ago by jstolarek

Cc: jan.stolarek@… added

comment:12 Changed 4 years ago by schyler

Cc: schyler added
difficulty: Unknown
Owner: set to schyler

comment:13 Changed 4 years ago by carter

@schyler

please please be very careful about anything regarding evaluating floating point, I think before we add optimizations and constant propagation for floating point computations we really need to enrich GHC's internal data model of floating point. a Naive constant progagation alg will not be correct for floating point. I'd in fact argue that any constant propagtion stuff first needs that internal model cleaned up first to be correct.

Relatedly: many floating point libs do a lot of work that exploits the specific structure of floating point, for example edward kmett's compensated lib http://hackage.haskell.org/package/compensated

I think the first step would be adding better support for writing double# and float# literals, perhaps with hexadecimal notation support, as this ticket suggests, as a prereq to the subsequent parts of the ticket. Perhaps that literal support should be spun out into its own ticket and your first task is adding that literals support?

comment:14 Changed 4 years ago by carter

So one thing that also maybe needs to be done is add proper way of tracking NAN and other exception values (and coding them correctly too) as a prereq for constant evaluation

comment:15 Changed 3 years ago by thoughtpolice

Milestone: 7.6.27.10.1

Moving to 7.10.1.

comment:16 Changed 3 years ago by thoughtpolice

Milestone: 7.10.17.12.1

Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

comment:17 Changed 2 years ago by thoughtpolice

Milestone: 7.12.18.0.1

Milestone renamed

comment:18 Changed 2 years ago by jstolarek

Cc: jan.stolarek@… removed

comment:19 Changed 23 months ago by thomie

Milestone: 8.0.1
Note: See TracTickets for help on using tickets.