<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif">Tom</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Ha!  I could not resist having a look.  See <a href="https://gitlab.haskell.org/ghc/ghc/-/issues/24371">https://gitlab.haskell.org/ghc/ghc/-/issues/24371</a></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Simon<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 23 Jan 2024 at 21:22, Tom Ellis <<a href="mailto:tom-lists-haskell-cafe-2023@jaguarpaw.co.uk">tom-lists-haskell-cafe-2023@jaguarpaw.co.uk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello GHC devs,<br>
<br>
I'm trying to understand why my code is not being optimized in the way<br>
I would expect.  I'm completely stuck and I think I need the advice of<br>
an expert.<br>
<br>
I'm writing an effect system on top of transformers.  The effect<br>
system wraps monad transformers in a newtype that encodes the<br>
composition structure of the transformers at the type level.  Because<br>
it's a newtype all of the class members are inherited directly from<br>
the underlying type using coerce.  When I implement something using<br>
this effect system I would expect to generate exactly the same code as<br>
if I had written it using transformers directly.  However, it<br>
generates significantly worse code, even in a very simple case.<br>
<br>
Firstly, a case where I do get the same code.  All of these compile to<br>
the constant 1.  Hooray!<br>
<br>
<a href="https://github.com/tomjaguarpaw/ad/blob/cd0d876ddb448fe611515e8768dee66dc02ed650/Eff-Optimize/Main.hs#L26-L54" rel="noreferrer" target="_blank">https://github.com/tomjaguarpaw/ad/blob/cd0d876ddb448fe611515e8768dee66dc02ed650/Eff-Optimize/Main.hs#L26-L54</a><br>
<br>
Secondly, a simple cases where I do not get the same code.  `mySumMTL`<br>
and `mySumNewtype` yield the same code, as expected.  After all,<br>
`mySumNewtype` does exactly the same thing as `mySumMTL`, it's just<br>
wrapped in some newtypes.  However, `mySumEff` yields worse code,<br>
despite *also* being the same thing as `mySumMTL` just wrapped in some<br>
newtypes.<br>
<br>
<a href="https://github.com/tomjaguarpaw/ad/blob/cd0d876ddb448fe611515e8768dee66dc02ed650/Eff-Optimize/Main.hs#L62-L94" rel="noreferrer" target="_blank">https://github.com/tomjaguarpaw/ad/blob/cd0d876ddb448fe611515e8768dee66dc02ed650/Eff-Optimize/Main.hs#L62-L94</a><br>
<br>
You can compare the generated loops at:<br>
<br>
<a href="https://github.com/tomjaguarpaw/ad/blob/cd0d876ddb448fe611515e8768dee66dc02ed650/Eff-Optimize/optimizer-output.md" rel="noreferrer" target="_blank">https://github.com/tomjaguarpaw/ad/blob/cd0d876ddb448fe611515e8768dee66dc02ed650/Eff-Optimize/optimizer-output.md</a><br>
<br>
Does anyone have a clue what's going wrong in the optimizer here?  I<br>
don't think the singleton that I pass around to access the type level<br>
index at runtime has anything to do with it.  That seems to be<br>
optimized away by inlining.  Is the simplifier confused by all the<br>
coercions?<br>
<br>
Thanks for any help anyone may be able to shed,<br>
<br>
Tom<br>
_______________________________________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br>
</blockquote></div>