Opened 21 months ago

Last modified 18 months ago

#7109 new bug

Inlining depends on datatype size, even with INLINE pragmas

Reported by: dreixel Owned by: simonpj
Priority: normal Milestone: 7.8.3
Component: Compiler Version: 7.5
Keywords: Cc: nfrisby
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:


Consider the following code:

data Logic = T | F
           | Not Logic

instance GEq Logic

testEqLogic = geq (Not T) (Not F)

With a proper definitions of generic equality geq in class GEq, and an instance Generic Logic, we get the following core code with -O1:

Rec {
Bug.$fGEqLogic_$cgeq [Occ=LoopBreaker]
  :: Bug.Logic -> Bug.Logic -> GHC.Types.Bool
[GblId, Arity=2, Caf=NoCafRefs, Str=DmdType SS]
Bug.$fGEqLogic_$cgeq =
  \ (x_ap1 :: Bug.Logic) (y_ap2 :: Bug.Logic) ->
    case x_ap1 of _ {
      Bug.T ->
        case y_ap2 of _ {
          Bug.T -> GHC.Types.True;
          Bug.F -> GHC.Types.False;
          Bug.Not g1_aBc_ayJ -> GHC.Types.False
      Bug.F ->
        case y_ap2 of _ {
          Bug.T -> GHC.Types.False;
          Bug.F -> GHC.Types.True;
          Bug.Not g1_aBc_ayJ -> GHC.Types.False
      Bug.Not g1_aBc_ayJ ->
        case y_ap2 of _ {
          __DEFAULT -> GHC.Types.False;
          Bug.Not g1_aBc1_XAu -> Bug.$fGEqLogic_$cgeq g1_aBc_ayJ g1_aBc1_XAu
end Rec }

Nice and simple, looking just like what we would expect for an equality function for datatype Logic.

Now we add one more constructor to datatype Logic (and adapt the Generic instance accordingly):

data Logic = T | F
           | Not Logic 
           | And Logic Logic

GHC (HEAD) now generates 3000 lines of core code for the Bug.$fGEqLogic_$cgeq function, instead of something only slightly longer than above.

Why is this? The second version of our Logic datatype is as easy to optimise as the first version; only the terms involved will be slightly longer. Attached file Bug2.hs is the input which gives the correct behaviour, while Bug3.hs is the input with one added constructor. (You might wonder if it has to do with the fact that the added constructor has more than one argument, but this is not the source of the problem.) Both files have INLINE pragmas pretty much everywhere (in fact, we're not deriving Generic so that we can put INLINE pragmas on to and from).

Attachments (2)

Bug2.hs (2.3 KB) - added by dreixel 21 months ago.
Bug3.hs (2.7 KB) - added by dreixel 21 months ago.

Download all attachments as: .zip

Change History (4)

Changed 21 months ago by dreixel

Changed 21 months ago by dreixel

comment:1 Changed 20 months ago by nfrisby

  • Cc nfrisby added

comment:2 Changed 18 months ago by igloo

  • Difficulty set to Unknown
  • Milestone set to 7.8.1
  • Owner set to simonpj

Simon, is some sort of heuristic coming into play?

Note: See TracTickets for help on using tickets.