<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I think talking about inlining of $ may not be addressing the crux of the problem here.<br><br>The issue seems to be about functions like the one in the first message. For instance:<br>
<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">    loop :: (Int -> Int) -> Int<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">    loop g = sum . map g $ [1..1000000]<br>
<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Suppose for argument that we have a fusion framework that would handle this. The problem is that this does not actually turn into a loop over integers, because the constant [1..1000000] gets floated out. It instead builds a list/vector/whatever.<br>
<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">By contrast, if we write:<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">    loop' :: Int<br>
</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">    loop' = sum . map (+1) $ [1..1000000]<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">this does turn into a loop over integers, with no intermediate list. Presumably this is due to there being no work to be saved ever by floating the list out. These are the examples people usually test fusion with.<br>
<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">And if loop is small enough to inline, it turns out that the actual code that gets run will be the same as loop', because everything will get inlined and fused. But it is also possible to make loop big enough to not inline, and then the floating will pessimize the overall code.<br>
<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">So the core issue is that constant floating blocks some fusion opportunities. It is trying to save the work of building the structure more than once, but fusion can cause the structure to not be built at all. And the floating happens before fusion can reasonably be expected to work.<br>
<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Can anything be done about this?<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I've verified that this kind of situation also affects vector. And it seems to be an issue even if loop is written:<br>
<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">    loop g = sum (map g [1..1000000])<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">-- Dan<br></div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Aug 27, 2014 at 3:38 PM, Simon Peyton Jones <span dir="ltr"><<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You'll have to do more detective work! In your dump I see "Inactive unfolding $".  So that's why it's not being inlined.  That message comes from CoreUnfold, line 941 or so.  The Boolean active_unfolding is passed in to callSiteInline from Simplify, line 1408 or so.  It is generated by the function activeUnfolding, defined in SimplUtils.<br>

<br>
But you have probably change the "CompilerPhase" data type, so I can't guess what is happening.  But if you just follow it through I'm sure you'll find it.<br>
<span class="HOEnZb"><font color="#888888"><br>
Simon<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
| -----Original Message-----<br>
| From: David Feuer [mailto:<a href="mailto:david.feuer@gmail.com">david.feuer@gmail.com</a>]<br>
| Sent: 27 August 2014 17:22<br>
| To: Simon Peyton Jones<br>
| Cc: ghc-devs<br>
| Subject: Re: Why isn't ($) inlining when I want?<br>
|<br>
| I just ran that (results attached), and as far as I can tell, it<br>
| doesn't even *consider* inlining ($) until phase 2.<br>
|<br>
| On Wed, Aug 27, 2014 at 4:03 AM, Simon Peyton Jones<br>
| <<a href="mailto:simonpj@microsoft.com">simonpj@microsoft.com</a>> wrote:<br>
| > It's hard to tell since you are using a modified compiler.  Try running<br>
| with -ddump-occur-anal -dverbose-core2core -ddump-inlinings.  That will<br>
| show you every inlining, whether failed or successful. You can see the<br>
| attempt to inline ($) and there is some info with the output that may<br>
| help to explain why it did or did not work.<br>
| ><br>
| > Try that<br>
| ><br>
| > Simon<br>
| ><br>
| > | -----Original Message-----<br>
| > | From: ghc-devs [mailto:<a href="mailto:ghc-devs-bounces@haskell.org">ghc-devs-bounces@haskell.org</a>] On Behalf Of<br>
| David<br>
| > | Feuer<br>
| > | Sent: 27 August 2014 04:50<br>
| > | To: ghc-devs; Carter Schonwald<br>
| > | Subject: Why isn't ($) inlining when I want?<br>
| > |<br>
| > | tl;dr  I added a simplifier run with inlining enabled between<br>
| > | specialization and floating out. It seems incapable of inlining<br>
| > | saturated applications of ($), and I can't figure out why. These are<br>
| > | inlined later, when phase 2 runs. Am I running the simplifier wrong<br>
| or<br>
| > | something?<br>
| > |<br>
| > |<br>
| > | I'm working on this simple little fusion pipeline:<br>
| > |<br>
| > | {-# INLINE takeWhile #-}<br>
| > | takeWhile p xs = build builder<br>
| > |   where<br>
| > |     builder c n = foldr go n xs<br>
| > |       where<br>
| > |         go x r = if p x then x `c` r else n<br>
| > |<br>
| > | foo c n x = foldr c n . takeWhile (/= (1::Int)) $ [-9..10]<br>
| > |<br>
| > | There are some issues with the enumFrom definition that break things.<br>
| > | If I use a fusible unfoldr that produces some numbers instead, that<br>
| > | issue goes away. Part of that problem (but not all of it) is that the<br>
| > | simplifier doesn't run to apply rules between specialization and full<br>
| > | laziness, so there's no opportunity for the specialization of<br>
| > | enumFromTo to Int to trigger the rewrite to a build form and fusion<br>
| > | with foldr before full laziness tears things apart. The other problem<br>
| > | is that inlining doesn't happen at all before full laziness, so<br>
| things<br>
| > | defined using foldr and/or build aren't actually exposed as such<br>
| until<br>
| > | afterwards. Therefore I decided to try adding a simplifier run with<br>
| > | inlining between specialization and floating out:<br>
| > |<br>
| > |         runWhen do_specialise CoreDoSpecialising,<br>
| > |<br>
| > |         runWhen full_laziness $ CoreDoSimplify max_iter<br>
| > |                        (base_mode { sm_phase = InitialPhase<br>
| > |                                   , sm_names = ["PostGentle"]<br>
| > |                                   , sm_rules = rules_on<br>
| > |                                   , sm_inline = True<br>
| > |                                   , sm_case_case = False }),<br>
| > |<br>
| > |         runWhen full_laziness $<br>
| > |            CoreDoFloatOutwards FloatOutSwitches {<br>
| > |                                  floatOutLambdas   = Just 0,<br>
| > |                                  floatOutConstants = True,<br>
| > |                                  floatOutPartialApplications = False<br>
| },<br>
| > |<br>
| > | The weird thing is that for some reason this doesn't inline ($), even<br>
| > | though it appears to be saturated. Using the modified thing with (my<br>
| > | version of) unfoldr:<br>
| > |<br>
| > | foo c n x = (foldr c n . takeWhile (/= (1::Int))) $ unfoldr (potato<br>
| 10)<br>
| > | (-9)<br>
| > |<br>
| > | potato :: Int -> Int -> Maybe (Int, Int)<br>
| > | potato n m | m <= n = Just (m, m)<br>
| > |            | otherwise = Nothing<br>
| > |<br>
| > |<br>
| > | I get this out of the specializer:<br>
| > |<br>
| > | foo<br>
| > | foo =<br>
| > |   \ @ t_a1Za @ c_a1Zb c_a1HT n_a1HU _ -><br>
| > |     $ (. (foldr c_a1HT n_a1HU)<br>
| > |          (takeWhile<br>
| > |             (let {<br>
| > |                ds_s21z<br>
| > |                ds_s21z = I# 1 } in<br>
| > |              \ ds_d1Zw -> neInt ds_d1Zw ds_s21z)))<br>
| > |       (let {<br>
| > |          n_s21x<br>
| > |          n_s21x = I# 10 } in<br>
| > |        unfoldr<br>
| > |          (\ m_a1U7 -><br>
| > |             case leInt m_a1U7 n_s21x of _ {<br>
| > |               False -> Nothing;<br>
| > |               True -> Just (m_a1U7, m_a1U7)<br>
| > |             })<br>
| > |          ($fNumInt_$cnegate (I# 9)))<br>
| > |<br>
| > |<br>
| > | and then I get this out of my extra simplifier run:<br>
| > |<br>
| > | foo<br>
| > | foo =<br>
| > |   \ @ t_a1Za @ c_a1Zb c_a1HT n_a1HU _ -><br>
| > |     $ (\ x_a20f -><br>
| > |          foldr<br>
| > |            (\ x_a1HR r_a1HS -><br>
| > |               case case x_a1HR of _ { I# x_a20R -><br>
| > |                    tagToEnum#<br>
| > |                      (case x_a20R of _ {<br>
| > |                         __DEFAULT -> 1;<br>
| > |                         1 -> 0<br>
| > |                       })<br>
| > |                    }<br>
| > |               of _ {<br>
| > |                 False -> n_a1HU;<br>
| > |                 True -> c_a1HT x_a1HR r_a1HS<br>
| > |               })<br>
| > |            n_a1HU<br>
| > |            x_a20f)<br>
| > |       (let {<br>
| > |          b'_a1ZS<br>
| > |          b'_a1ZS = $fNumInt_$cnegate (I# 9) } in<br>
| > |        $ (build)<br>
| > |          (\ @ b1_a1ZU c_a1ZV n_a1ZW -><br>
| > |             letrec {<br>
| > |               go_a1ZX<br>
| > |               go_a1ZX =<br>
| > |                 \ b2_a1ZY -><br>
| > |                   case case case b2_a1ZY of _ { I# x_a218 -><br>
| > |                             tagToEnum# (<=# x_a218 10)<br>
| > |                             }<br>
| > |                        of _ {<br>
| > |                          False -> Nothing;<br>
| > |                          True -> Just (b2_a1ZY, b2_a1ZY)<br>
| > |                        }<br>
| > |                   of _ {<br>
| > |                     Nothing -> n_a1ZW;<br>
| > |                     Just ds_a203 -><br>
| > |                       case ds_a203 of _ { (a1_a207, new_b_a208) -><br>
| > |                       c_a1ZV a1_a207 (go_a1ZX new_b_a208)<br>
| > |                       }<br>
| > |                   }; } in<br>
| > |             go_a1ZX b'_a1ZS))<br>
| > |<br>
| > |<br>
| > | That is, neither the $ in the code nor the $ that was inserted when<br>
| > | inlining unfoldr got inlined themselves, even though both appear to<br>
| be<br>
| > | saturated. As a result, foldr/build doesn't fire, and full laziness<br>
| > | tears things apart. Later on, in simplifier phase 2, $ gets inlined.<br>
| > | What's preventing this from happening in the PostGentle phase I<br>
| added?<br>
| > |<br>
| > | David Feuer<br>
| > | _______________________________________________<br>
| > | ghc-devs mailing list<br>
| > | <a href="mailto:ghc-devs@haskell.org">ghc-devs@haskell.org</a><br>
| > | <a href="http://www.haskell.org/mailman/listinfo/ghc-devs" target="_blank">http://www.haskell.org/mailman/listinfo/ghc-devs</a><br>
_______________________________________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org">ghc-devs@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/ghc-devs" target="_blank">http://www.haskell.org/mailman/listinfo/ghc-devs</a><br>
</div></div></blockquote></div><br></div>