Skip to content

Inefficient code generated for x^2

Okay, this one is quite surprising to me. Suppose the following trivial Haskell program:

main = print (2.0 ^ 2)

This innocent example actually ends up generating multiple pages of Core! To see what's going on we just have to look at a few type signatures:

$wg1_r11f  :: Double# -> Type.Integer -> Double# -> Double#
$wf1 :: Double# -> Type.Integer -> Double#

The function $wf1 is what is called to perform the calculation. First thing to note is that it actually compiles ^2 into a loop (that's what $wg1_r11f is for). Second, to make matters worse, it ends up using Integer of all things as the index type (defaulting at work, probably).

This has obviously drastic performance implications. A more complicated example program actually got about 4 times faster just by replacing all instances of x^2 with x*x.

I guess this probably is not easy to fix in the general case (speculative loop unrolling?). But couldn't we have, say, a rule that catches the worst cases?

Trac metadata
Trac field Value
Version 7.0.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information