RE: Why doesn’t the simple optimizer fuse casts?

Simon Peyton Jones simonpj at microsoft.com
Tue Apr 28 22:20:53 UTC 2020


Good spot.  The very simple optimiser is, well, very simple.   So let's not do anything complicated. 

But could you submit a ticket, and patch, to replace that Cast with mkCast?  Thanks!

Simon

|  -----Original Message-----
|  From: ghc-devs <ghc-devs-bounces at haskell.org> On Behalf Of Alexis King
|  Sent: 27 April 2020 22:58
|  To: Richard Eisenberg <rae at richarde.dev>
|  Cc: ghc-devs <ghc-devs at haskell.org>
|  Subject: Re: Why doesn’t the simple optimizer fuse casts?
|  
|  Hi Richard,
|  
|  Nested casts can absolutely happen after the simple optimizer runs.
|  Suppose we produce a desugared expression like:
|  
|      let { y = x `cast` co1 } in y `cast` co2
|  
|  The simple optimizer will inline y, since it only appears once, so now
|  we have a nested cast. Perhaps you mean you expect the casts to be
|  merged when the simple optimizer gets to the Cast expression itself,
|  but they aren’t, as it doesn’t use mkCast to reconstruct the result:
|  
|      -- from GHC.Core.SimpleOpt.simple_opt_expr
|      go (Cast e co)      | isReflCo co' = go e
|                          | otherwise    = Cast (go e) co'
|  
|  I suppose a really simple fix would be to just use mkCast here instead
|  of Cast, but that wouldn’t be completely satisfying, since the merged
|  coercion wouldn’t be optimized. So you’d have to do something slightly
|  more complicated to detect if the result of `go e` was a cast
|  expression and combine the coercions before calling optCoercion.
|  
|  Whether or not doing that would be a good idea is precisely what I’m
|  asking about. :)
|  
|  Alexis
|  
|  > On Apr 27, 2020, at 16:22, Richard Eisenberg <rae at richarde.dev> wrote:
|  >
|  > Hi Alexis,
|  >
|  > Nested casts shouldn't happen. The mkCast function gets rid of them.
|  > Someone somewhere is forgetting to call it. If you have a concrete
|  > program that leads to nested casts, post a bug report. :)
|  >
|  > Thanks!
|  > Richard
|  _______________________________________________
|  ghc-devs mailing list
|  ghc-devs at haskell.org
|  https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmail.has
|  kell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fghc-
|  devs&data=02%7C01%7Csimonpj%40microsoft.com%7C825fe41065dd450030e908d
|  7eaf61d19%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637236215077850647
|  &sdata=w%2Fk4JG%2FOmpwPoUAAGqdC62ssQSYcLQioY8T9%2BZ3XS8E%3D&reser
|  ved=0


More information about the ghc-devs mailing list