[GHC] #5731: Bad code for Double literals
GHC
cvs-ghc at haskell.org
Sat Dec 31 17:28:57 CET 2011
#5731: Bad code for Double literals
---------------------------------+------------------------------------------
Reporter: rl | Owner:
Type: bug | Status: new
Priority: normal | Milestone:
Component: Compiler | Version: 7.3
Keywords: | Os: Unknown/Multiple
Architecture: Unknown/Multiple | Failure: Runtime performance bug
Difficulty: Unknown | Testcase:
Blockedby: | Blocking:
Related: |
---------------------------------+------------------------------------------
Changes (by simonpj):
* difficulty: => Unknown
Comment:
You are right that the RULES you mention are redundant; would you like to
remove them?
The right fix is to add two more rules (for Float and Double) to
`builtinIntegerRules` in `prelude/PrelRules`. Follow the pattern for the
rules `"integerToWord"` and `"integerToInt"`. Here's how they work,
taking `Int` as the example:
* Occurrences of an integer literal start life as `(fromInteger 3)`,
where the "3" is an Integer literal. This literal is, as you say, held as
a literal right through the compiler to the back end.
* The `Num Int` instance in `GHC.Num` has this definition for
`fromInteger`:
{{{
instance Num Int where
{-# INLINE fromInteger #-} -- Just to be sure!
fromInteger i = I# (integerToInt i)
}}}
* So `(fromInteger 3)` turns into `I# (integerToInt 3)`. The function
`integerToInt` is defined in package `integer-gmp`, module
`GHC.Integer.Type`.
* Now there is a builtin rule in `PrelRules`, so that `integerToInt 3` is
rewritten to `3#`, a literal of type `Int#`.
All that is needed is to play the same game for `Double` and `Float`, just
as is done for `Int` and `Word`. Would you feel like doing that?
Two side notes
* The reason why one program gives good code and the other does not is
because the type checker short-circuits the entire fol-de-rol if it see
the rigth answer on the spot.
* I see that `GHC.Integer.Type.integerToInt` also has a RULE that will
never fire, so it too could be removed.
It would be good to transfer this knowledge into a `Note [Integer
literals]` somewhere; but I'm not sure where!
Simon
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5731#comment:3>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the Glasgow-haskell-bugs
mailing list