#5237: Inefficient code generated for x^2
-------------------------------+--------------------------------------------
    Reporter:  scpmw           |        Owner:                         
        Type:  bug             |       Status:  patch                  
    Priority:  normal          |    Milestone:  7.4.1                  
   Component:  libraries/base  |      Version:  7.0.3                  
    Keywords:                  |     Testcase:                         
   Blockedby:                  |   Difficulty:                         
          Os:  Linux           |     Blocking:                         
Architecture:  x86_64 (amd64)  |      Failure:  Runtime performance bug
-------------------------------+--------------------------------------------

Comment(by daniel.is.fischer):

 Not sure I understand correctly. What we want is that occurrences of `expr
 ^ 2` or `expr ^ (2 :: Type)` in source code are rewritten to `expr * expr`
 (similarly for exponents 3 and 4).

 With the rules, that works fine if the exponent isn't given an explicit
 type or the type is `Int` or `Integer` - unless the type of `expr` is
 `Integer` (or `Int` if the exponent is specified as `Int`). For those
 types, `(^)` is specialised, and in the core replaced by the specialised
 version, then `GHC.Real.^_^2 expr 2` doesn't match the rule anymore^1^.

 Making `(^)` `{-# NOINLINE [1] #-}` instead of `{-# INLINABLE #-}` would
 cost a lot in other places, so that's not a good option; out of sheer
 morbid curiosity I tried it anyway, it didn't help. But if `INLINABLE`
 supports phase control (that isn't mentioned in the users guide, the
 syntax is accepted, however), perhaps `(^)` should become `{-# INLINABLE
 [1] #-}`?

 With `Word` exponents, the problem seems to be that the exponent is moved
 to a top-level binding and the rule-matcher sees `expr ^ lvl_fooBar`
 instead of `expr ^ (3 :: Word)`.
 But I don't think specifying the exponent as `Word` is common, mostly the
 exponent will not be given an explicit type and defaulted to `Integer` or
 specified as `Int` for better performance.
 So I'd just leave out the rules for `Word`.

 ^1^ Perhaps the compiler just considers the specialisation more specific
 than the rules and hence that wins? Compiling with extra rules for the
 specialised types to check.

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5237#comment:6>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to