<div dir="ltr"><div>We have:<br></div><div>* wiki: <a href="https://gitlab.haskell.org/ghc/ghc/wikis/commentary/rts/storage/gc/cafs">https://gitlab.haskell.org/ghc/ghc/wikis/commentary/rts/storage/gc/cafs</a></div><div>* a huge Note in CmmBuildInfoTables: <a href="https://gitlab.haskell.org/ghc/ghc/blob/master/compiler%2Fcmm%2FCmmBuildInfoTables.hs#L42">https://gitlab.haskell.org/ghc/ghc/blob/master/compiler%2Fcmm%2FCmmBuildInfoTables.hs#L42</a></div><div><br></div><div>Maybe we need links to these from other places?<br></div><div><br></div><div>Omer's question is referring specifically to the [FUN] optimisation described in the Note.</div><div><br></div><div>Cheers</div><div>Simon<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, 6 Jan 2020 at 17:50, Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com">simonpj@microsoft.com</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">





<div lang="EN-GB">
<div class="gmail-m_-614858106689129571WordSection1">
<p class="MsoNormal"><span>Omer,<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>I think I’m not understanding all the details, but I have a clear “big picture”.   Simon can correct me if I’m wrong.<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="gmail-m_-614858106689129571MsoListParagraph" style="margin-right:0cm;margin-bottom:6pt;margin-left:36pt">
<u></u><span style="font-family:Symbol"><span>·<span style="font:7pt "Times New Roman"">       
</span></span></span><u></u><span>The <b>info table</b> for any
<b>closure</b> (top-level or otherwise) has a (possibly empty) Static Reference Table,
<b>SRT</b>.<u></u><u></u></span></p>
<p class="gmail-m_-614858106689129571MsoListParagraph" style="margin-right:0cm;margin-bottom:6pt;margin-left:36pt">
<u></u><span style="font-family:Symbol"><span>·<span style="font:7pt "Times New Roman"">       
</span></span></span><u></u><span>The SRT for an info table identifies the static top level closures that the
<b>code</b> for that info table mentions.   (In principle the garbage collector could parse the code! But it’s easier to find these references if they in a dedicated table alongside the code.)<u></u><u></u></span></p>
<p class="gmail-m_-614858106689129571MsoListParagraph" style="margin-right:0cm;margin-bottom:6pt;margin-left:36pt">
<u></u><span style="font-family:Symbol"><span>·<span style="font:7pt "Times New Roman"">       
</span></span></span><u></u><span>A top level closure is a
<b>CAF</b> if it is born updatable.<u></u><u></u></span></p>
<p class="gmail-m_-614858106689129571MsoListParagraph" style="margin-right:0cm;margin-bottom:6pt;margin-left:36pt">
<u></u><span style="font-family:Symbol"><span>·<span style="font:7pt "Times New Roman"">       
</span></span></span><u></u><span>A top level closure is
<b>CAFFY</b> if it is a CAF, or mentions another CAFFY closure.<u></u><u></u></span></p>
<p class="gmail-m_-614858106689129571MsoListParagraph" style="margin-right:0cm;margin-bottom:6pt;margin-left:36pt">
<u></u><span style="font-family:Symbol"><span>·<span style="font:7pt "Times New Roman"">       
</span></span></span><u></u><span>An entry in the SRT can point<u></u><u></u></span></p>
<p class="gmail-m_-614858106689129571MsoListParagraph" style="margin-right:0cm;margin-bottom:6pt;margin-left:72pt">
<u></u><span style="font-family:"Courier New""><span>o<span style="font:7pt "Times New Roman"">  
</span></span></span><u></u><span>To a top-level updatable closure. This may now point into the dynamic heap, and is what we want to keep alive.  If the closure hasn’t been updated, we should keep alive anything its SRT
 points to.<u></u><u></u></span></p>
<p class="gmail-m_-614858106689129571MsoListParagraph" style="margin-right:0cm;margin-bottom:6pt;margin-left:72pt">
<u></u><span style="font-family:"Courier New""><span>o<span style="font:7pt "Times New Roman"">  
</span></span></span><u></u><span>Directly to another SRT (or info table?) for a CAFFY top-level closure, which is a bit faster if we know the thing is non-updatable.<u></u><u></u></span></p>
<p class="gmail-m_-614858106689129571MsoListParagraph" style="margin-right:0cm;margin-bottom:6pt;margin-left:36pt">
<u></u><span style="font-family:Symbol"><span>·<span style="font:7pt "Times New Roman"">       
</span></span></span><u></u><span>If a function f calls a top-level function g, and g is CAFFY, then f’s SRT should point to g’s closure or (if g is not a CAF) directly to its SRT.<u></u><u></u></span></p>
<p class="gmail-m_-614858106689129571MsoListParagraph" style="margin-right:0cm;margin-bottom:6pt;margin-left:36pt">
<u></u><span style="font-family:Symbol"><span>·<span style="font:7pt "Times New Roman"">       
</span></span></span><u></u><span>If f is top level, and calls itself, there is no need to include a pointer to f’s closure in f’s own SRT.<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
<span>I think this last point is the one you are asking, but I’m not certain.<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
<span>All this should be written down somewhere, and perhaps is.  But where?<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
<span>Simon<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<div style="border-color:currentcolor currentcolor currentcolor blue;border-style:none none none solid;border-width:medium medium medium 1.5pt;padding:0cm 0cm 0cm 4pt">
<div>
<div style="border-color:rgb(225,225,225) currentcolor currentcolor;border-style:solid none none;border-width:1pt medium medium;padding:3pt 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> ghc-devs <<a href="mailto:ghc-devs-bounces@haskell.org" target="_blank">ghc-devs-bounces@haskell.org</a>>
<b>On Behalf Of </b>Simon Marlow<br>
<b>Sent:</b> 06 January 2020 08:17<br>
<b>To:</b> Ömer Sinan Ağacan <<a href="mailto:omeragacan@gmail.com" target="_blank">omeragacan@gmail.com</a>><br>
<b>Cc:</b> ghc-devs <<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a>><br>
<b>Subject:</b> Re: Code generation/SRT question<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
There's no need to set the srt field of f_info if f_closure is the SRT, since any reference to f_info in the code will give rise to a reference to f_closure in the SRT corresponding to that code fragment. Does that make sense?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
<u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
The use of a closure as an SRT is really quite a nice optimisation actually.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
<u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
Cheers<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
Simon<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
<u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
On Wed, 1 Jan 2020 at 09:35, Ömer Sinan Ağacan <<a href="mailto:omeragacan@gmail.com" target="_blank">omeragacan@gmail.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
Hi Simon,<br>
<br>
In Cmm if I have a recursive group of functions f and g, and I'm using f's<br>
closure as the SRT for this group, should f's entry block's info table have<br>
f_closure as its SRT?<br>
<br>
In Cmm syntax<br>
<br>
     f_entry() {<br>
             { info_tbls: [...<br>
                           (c1vn,<br>
                            label: ...<br>
                            rep: ...<br>
                            srt: ??????]<br>
               stack_info: ...<br>
             }<br>
         {offset<br>
           c1vn:<br>
             ...<br>
         }<br>
     }<br>
<br>
Here should I have `f_closure` in the srt field?<br>
<br>
I'd expect yes, but looking at the current SRT code, in<br>
CmmBuildInfoTables.updInfoSRTs, we have this:<br>
<br>
  (newInfo, srtEntries) = case mapLookup (g_entry g) funSRTEnv of<br>
<br>
    Nothing -><br>
      -- if we don't add SRT entries to this closure, then we<br>
      -- want to set the srt field in its info table as usual<br>
      (info_tbl { cit_srt = mapLookup (g_entry g) srt_env }, [])<br>
<br>
    Just srtEntries -> srtTrace "maybeStaticFun" (ppr res)<br>
      (info_tbl { cit_rep = new_rep }, res)<br>
      where res = [ CmmLabel lbl | SRTEntry lbl <- srtEntries ]<br>
<br>
Here we only update SRT field of the block if we're not adding SRT entries to<br>
the function's closure, so in the example above, because we're using the<br>
function as SRT (and adding SRT entries to its closure) SRT field of c1vn won't<br>
be updated.<br>
<br>
Am I missing anything?<br>
<br>
Thanks,<br>
<br>
Ömer<u></u><u></u></p>
</blockquote>
</div>
</div>
</div>
</div>

</blockquote></div></div>