[GHC] #9291: Don't reconstruct sum types if the type subtly changes

GHC ghc-devs at haskell.org
Thu Jul 10 13:49:15 UTC 2014


#9291: Don't reconstruct sum types if the type subtly changes
----------------------------------------------+----------------------------
       Reporter:  schyler                     |             Owner:
           Type:  feature request             |            Status:  new
       Priority:  normal                      |         Milestone:
      Component:  Compiler                    |           Version:  7.8.2
       Keywords:                              |  Operating System:
   Architecture:  Unknown/Multiple            |  Unknown/Multiple
     Difficulty:  Moderate (less than a day)  |   Type of failure:
     Blocked By:                              |  None/Unknown
Related Tickets:                              |         Test Case:
                                              |          Blocking:
----------------------------------------------+----------------------------
 Consider this test case:

 {{{
 module Main (main) where

 fun :: Either Int String -> Either String String
 fun x = case x of
     Left int -> Left (show int)
     Right str -> Right str
 {-# NOINLINE fun #-}

 main :: IO ()
 main = do
     l <- getLine
     print $ fun (Right l)
 }}}

 The core we get for fun looks like:

 {{{
 Main.fun [InlPrag=NOINLINE]
   :: Data.Either.Either GHC.Types.Int GHC.Base.String
      -> Data.Either.Either GHC.Base.String GHC.Base.String
 [GblId, Arity=1, Caf=NoCafRefs, Str=DmdType <S,1*U>]
 Main.fun =
   \ (x_aur :: Data.Either.Either GHC.Types.Int GHC.Base.String) ->
     case x_aur of _ [Occ=Dead] {
       Data.Either.Left int_aus ->
         Data.Either.Left
           @ GHC.Base.String
           @ GHC.Base.String
           (GHC.Show.$fShowInt_$cshow int_aus);
       Data.Either.Right str_aCG ->
         Data.Either.Right @ GHC.Base.String @ GHC.Base.String str_aCG
     }
 }}}

 There would be less allocations and probably perform better if we just
 coerced `x` into the new type. Because the coercion is common code, this
 also means that if hypothetically some other sum type which had 15
 constructors, and only 3 had subtle type changes, 12 of the case branches
 could be CSE'd into the single coerce greatly reducing code generated and
 also hinting the inliner better.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/9291>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list