Opened 8 years ago

Last modified 4 years ago

#5298 new bug

Inlined functions aren't fully specialised

Reported by: reinerp Owned by:
Priority: low Milestone:
Component: Compiler Version: 7.0.3
Keywords: Cc: hackage.haskell.org@…, pho@…, jpm@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Runtime performance bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

When a function is inlined, it can expose other functions as candidates for specialisation, but GHC doesn't specialise them.

For instance, given the two modules

module A where

{-# INLINABLE fac #-}
fac :: Num a => a -> a
fac 0 = 1
fac n = n * fac (n-1)

{-# INLINE f #-}
f :: Num a => a -> a
f a = fac a
module B where

import A

g :: Int -> Int
g x = f x

we see that f is inlined, but fac isn't specialised for Ints:

B.g :: Int -> Int
[GblId,
 Arity=1,

 Unf=Unf{Src=<vanilla>, TopLvl=True, Arity=1, Value=True,
         ConLike=True, Cheap=True, Expandable=True,
         Guidance=IF_ARGS [0] 30 0}]
B.g =
  \ (x_ary :: Int) ->
    A.fac @ Int $fNumInt x_ary

Removing the INLINE pragma on f doesn't help.

Either of the following changes will cause fac to be specialised:

  • adding {-# SPECIALISE f :: Int -> Int #-} to module B
  • defining "g x = fac x" instead

This happens with both GHC 7.0.3 and GHC HEAD

Change History (10)

comment:1 Changed 8 years ago by liyang

Cc: hackage.haskell.org@… added

comment:2 Changed 8 years ago by PHO

Cc: pho@… added

comment:3 Changed 8 years ago by dreixel

Cc: jpm@… added

comment:4 Changed 8 years ago by igloo

Milestone: 7.4.1

Confirmed: with g x = fac x and

ghc -O B -ddump-simpl

we get

[...]
B.g [InlPrag=INLINE[0]] :: GHC.Types.Int -> GHC.Types.Int
[...]
"SPEC A.fac [GHC.Types.Int]" [ALWAYS]
    forall {$dNum_srl :: GHC.Num.Num GHC.Types.Int}
      A.fac @ GHC.Types.Int $dNum_srl
      = B.$sfac

comment:5 Changed 8 years ago by igloo

Milestone: 7.4.17.6.1
Priority: normallow

comment:6 Changed 7 years ago by igloo

Milestone: 7.6.17.6.2

comment:7 Changed 5 years ago by thoughtpolice

Milestone: 7.6.27.10.1

Moving to 7.10.1.

comment:8 Changed 5 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:9 Changed 4 years ago by thoughtpolice

Milestone: 7.12.18.0.1

Milestone renamed

comment:10 Changed 4 years ago by thomie

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