<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Nov 4, 2021 at 5:59 PM Hongtao Yu <<a href="mailto:hoy@fb.com" target="_blank">hoy@fb.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-US">
<div>
<p class="MsoNormal">Hello Teresa, Snehasish and David,<u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.75in"> <u></u><u></u></p>
<p class="MsoNormal">Thanks for the RFC and follow-up clarification. I have a few questions regarding the use of the metadata and how they are manipulated.</p></div></div></blockquote><div><br></div><div>Hi Hongtao, responses below.</div><div> </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-US"><div><p class="MsoNormal"><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:27pt"> <u></u><u></u></p>
<ol style="margin-top:0in" start="1" type="1">
<li style="margin-left:0in">While the proposed !callsite metadata and its compression through chaining sounds efficient, have you considered using the existing !dbg metadata as an alternative?</li></ol></div></div></blockquote><div>We could potentially do so for the !callsite metadata, but not for the stack ids in the !memprof metadata's stack context (which could be in another module), and we need to be able to correlate these (i.e. identify the callsites corresponding to the stack context list on the !memprof metadata), using ThinLTO if they are across module boundaries. This idea and its downside is mentioned briefly near the beginning of the Metadata Format section:</div><div><br></div><div>> <span style="color:rgb(0,0,0);font-family:Arial,sans-serif">Another option would be to represent the stack entries using existing debug metadata. However, for stack entries in another module we would need to synthesize additional debug location metadata in the module containing MIB profile data that references that stack context entry.</span></div><div><span style="color:rgb(0,0,0);font-family:Arial,sans-serif"><br></span></div><div><font color="#000000" face="Arial, sans-serif">It is possible that if we used stack ids that were generated from the MD5 hash of the debug location, we could actually generate this id for callsites on the fly (from its debug metadata). However, having the !callsite metadata shows explicitly which callsites we need to consider for the MemProf optimizations (and also which to summarize in the ThinLTO summary in order to perform cross-module context disambiguation).</font></div><div><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-US"><div><ol style="margin-top:0in" start="1" type="1"><li style="margin-left:0in">Do !callisite metadata need to be maintained on MIR?</li></ol></div></div></blockquote><div>We don't plan to as the targeted transformations would be in LLVM IR (e.g. context disambiguation via cloning or the like, modification of allocation calls).</div><div> </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-US"><div><ol style="margin-top:0in" start="1" type="1"><li style="margin-left:0in">For the !callsite metadata, in order to make sure the metadata not accidentally dropped, how much extra care is needed in passes such as tail call optimization?</li></ol></div></div></blockquote><div>I believe tail call elimination happens in code gen so after we would be done with this metadata. Tail recursion elimination is earlier but we probably can and will want to handle direct recursion in the contexts specially anyway.</div><div><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-US"><div><ol style="margin-top:0in" start="1" type="1"><li style="margin-left:0in">When two callsites are merged by some CFG optimizations, how are their !callsite metadata handled?</li></ol></div></div></blockquote><div>Initially we can prevent this type of merging until after context disambiguation is complete, i.e. while they have !callsite metadata (in fact, we might need to keep the calls separate anyway if their respective contexts result in different behavior at leaf allocation sites). If this becomes too limiting we could probably extend the !callsite metadata to include "aliased" callsite ids from merged callsites.</div><div><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-US"><div><ol style="margin-top:0in" start="1" type="1"><li style="margin-left:0in">How do we identify a call path in the presence of indirect call sites?</li></ol></div></div></blockquote><div>If the indirect callsites aren't already speculatively devirtualized via the value profile info, we can do so in the process of cloning for context disambiguation, because we know the caller from the full stack context.</div><div><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-US"><div><ol style="margin-top:0in" start="1" type="1"><li style="margin-left:0in">I was wondering how the !memprof metadata is consumed. Will they be passed into the runtime allocator in some way?</li></ol></div></div></blockquote><div>We plan to use it to set hints on allocations that are consumed by the allocator. See for example the recent patch to TCMalloc that adds hot/cold hints:</div><div><br></div><div>[3] Implement interfaces for providing access frequency hints to TCMalloc (<a href="https://github.com/google/tcmalloc/commit/ab87cf382dc56784f783f3aaa43d6d0465d5f385">https://github.com/google/tcmalloc/commit/ab87cf382dc56784f783f3aaa43d6d0465d5f385</a>)<br></div><div><br></div><div>Thanks!</div><div>Teresa</div><div> </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-US"><div><p class="MsoNormal" style="margin-left:27pt"> <u></u><u></u></p>
<p class="MsoNormal">Thanks!<u></u><u></u></p>
<p class="MsoNormal" style="margin-left:27pt"> <u></u><u></u></p>
<p class="MsoNormal">Hongtao<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0in 0in">
<p class="MsoNormal" style="margin-right:0in;margin-bottom:12pt;margin-left:0.5in">
<b><span style="font-size:12pt;color:black">From: </span></b><span style="font-size:12pt;color:black">Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a>><br>
<b>Date: </b>Monday, November 1, 2021 at 12:06 PM<br>
<b>To: </b>llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Cc: </b>Snehasish Kumar <<a href="mailto:snehasishk@google.com" target="_blank">snehasishk@google.com</a>>, David Li <<a href="mailto:davidxl@google.com" target="_blank">davidxl@google.com</a>>, Vedant Kumar <<a href="mailto:vsk@apple.com" target="_blank">vsk@apple.com</a>>, Wenlei He <<a href="mailto:wenlei@fb.com" target="_blank">wenlei@fb.com</a>>, Andrey Bokhanko <<a href="mailto:andreybokhanko@gmail.com" target="_blank">andreybokhanko@gmail.com</a>>, Hongtao Yu <<a href="mailto:hoy@fb.com" target="_blank">hoy@fb.com</a>><br>
<b>Subject: </b>Re: RFC: IR metadata format for MemProf<u></u><u></u></span></p>
</div>
<div>
<div>
<p class="MsoNormal" style="margin-left:0.5in">One change below along with another clarification. Also, you can view the original RFC here with better formatting for the examples: <a href="https://groups.google.com/g/llvm-dev/c/aWHsdMxKAfE/m/WtEmRqyhAgAJ" target="_blank">https://groups.google.com/g/llvm-dev/c/aWHsdMxKAfE/m/WtEmRqyhAgAJ</a><u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal" style="margin-left:0.5in">On Wed, Oct 27, 2021 at 1:01 PM Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">This RFC describes the IR metadata format for the sanitizer-based heap profiler (MemProf) data when it is fed back into a subsequent compile for profile guided heap optimization. Additional background
 can be found in the following RFCs:</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:1in;vertical-align:baseline">
<u></u><span style="font-size:10pt;font-family:Symbol;color:black"><span>·<span style="font:7pt "Times New Roman"">
</span></span></span><u></u><span style="font-family:Arial,sans-serif;color:black">RFC: Sanitizer-based Heap Profiler [1]<u></u><u></u></span></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:1in;vertical-align:baseline">
<u></u><span style="font-size:10pt;font-family:Symbol;color:black"><span>·<span style="font:7pt "Times New Roman"">
</span></span></span><u></u><span style="font-family:Arial,sans-serif;color:black">RFC: A binary serialization format for MemProf [2]<u></u><u></u></span></p>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><span style="font-family:Arial,sans-serif;color:black"><br>
<br>
</span><u></u><u></u></p>
</div>
<div>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">We look forward to your feedback.</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><span style="font-family:Arial,sans-serif;color:black">Authors:
</span><a href="mailto:tejohnson@google.com" target="_blank"><span style="font-family:Arial,sans-serif">tejohnson@google.com</span></a><span style="font-family:Arial,sans-serif;color:black">,
</span><a href="mailto:snehasishk@google.com" target="_blank"><span style="font-family:Arial,sans-serif">snehasishk@google.com</span></a><span style="font-family:Arial,sans-serif;color:black">,
</span><a href="mailto:davidxl@google.com" target="_blank"><span style="font-family:Arial,sans-serif">davidxl@google.com</span></a><u></u><u></u></p>
</div>
<h1 style="margin-right:0in;margin-bottom:6pt;margin-left:0.5in">
<span style="font-size:20pt;font-family:Arial,sans-serif;color:black;font-weight:normal">Requirements</span><u></u><u></u></h1>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">The profile format will need to be reasonably compact within IR, but also facilitate efficiently identifying callsites within the stack contexts for use in interprocedural (and cross module) optimization
 for context disambiguation. These optimizations will require transformations to disambiguate the context at a particular allocation, which will require call graph based analysis and optimization.</span><u></u><u></u></p>
<h1 style="margin-right:0in;margin-bottom:6pt;margin-left:0.5in">
<span style="font-size:20pt;font-family:Arial,sans-serif;color:black;font-weight:normal">Input Profile Format</span><u></u><u></u></h1>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">The profile will be in an index binary format, which is detailed in [2]. In the index format, the profiles for each allocation site will be located with the profile data for the function containing the
 allocation call site. Each allocation may have multiple profile entries (known as a MIB, or Memory Info Block) uniquely identified by stack context. The entries in the stack context will be symbolized and include file, line and discriminator.</span><u></u><u></u></p>
<h1 style="margin-right:0in;margin-bottom:6pt;margin-left:0.5in">
<span style="font-size:20pt;font-family:Arial,sans-serif;color:black;font-weight:normal">Metadata Format</span><u></u><u></u></h1>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">Similar to branch weights and value profile data from regular PGO, the PGHO profile will be annotated as metadata onto relevant instructions. A natural instruction to attach the profile metadata is on
 the allocation callsite, so these allocation calls can be identified and handled by the subsequent heap optimization pass. As an example, this profile data can be used to enable automatic application of hot and cold hints to allocations, for use by a runtime
 allocator such as tcmalloc, where support for such allocation hints was recently added [3].</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">However, in order to identify ancestor callsites within an allocation’s call stack context that require modification for disambiguating the context at the allocation site, e.g. via cloning, we will also
 want to attach metadata to these callsites. This is particularly important for contexts that cross module boundaries, so that we can identify them in ThinLTO summaries for cross module coordination of context transformations.</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">To identify and correlate entries in a context, we will use a unique identifier for each stack entry. Specifically, we will use the 64-bit value from the stack entry table in the indexed profile format
 which is formed from the index into the file path table along with the line and discriminator. Another option would be to represent the stack entries using existing debug metadata. However, for stack entries in another module we would need to synthesize additional
 debug location metadata in the module containing MIB profile data that references that stack context entry.</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">Assume the following working example. For simplicity, all are shown as being in the same module, however, these function definitions could theoretically be located in multiple different modules.</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">x.cc</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)">  1 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">main() {</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)">  2 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">   foo();  // stack entry id: 123</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)">  3 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">}</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)">  4 </span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)">  5 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">foo() {</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)">  6 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">   baz();  // stack entry id: 234</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)">  7 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">}  </span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)">  8 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">      </span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)">  9 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">baz() {</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)"> 10 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">   if (x)</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)"> 11 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">      bar();  // stack entry id: 345</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)"> 12 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">   else</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)"> 13 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">      bar();  // stack entry id: 456</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)"> 14 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">}     </span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)"> 15 </span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)"> 16 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">bar() {</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)"> 17 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">   malloc(4);  // stack entry id: 567</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:rgb(193,101,28)"> 18 </span>
<span style="font-size:9pt;font-family:"Courier New";color:black">}  </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">The call to malloc has 2 possible calling contexts:</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:1in;vertical-align:baseline">
<u></u><span style="font-family:Arial,sans-serif;color:black"><span>1.<span style="font:7pt "Times New Roman"">   
</span></span></span><u></u><span style="font-family:Arial,sans-serif;color:black">main -> foo (x.cc:2) -> baz (x.cc:6) -> bar (x.cc:11) -> malloc (x.cc:17)<u></u><u></u></span></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:1in;vertical-align:baseline">
<u></u><span style="font-family:Arial,sans-serif;color:black"><span>2.<span style="font:7pt "Times New Roman"">   
</span></span></span><u></u><span style="font-family:Arial,sans-serif;color:black">main -> foo (x.cc:2) -> baz (x.cc:6) -> bar (x.cc:13) -> malloc (x.cc:17)<u></u><u></u></span></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">where the stack entry id for each callsite, taken from the profile’s stack entry table contents, is shown in the code comments. The corresponding full contexts in terms of stack entry ids, listed from
 the leaf allocation callsite up to the root are:</span><u></u><u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:1in;vertical-align:baseline">
<u></u><span style="font-family:Arial,sans-serif;color:black"><span>1.<span style="font:7pt "Times New Roman"">   
</span></span></span><u></u><span style="font-family:Arial,sans-serif;color:black">567, 345, 234, 123<u></u><u></u></span></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:1in;vertical-align:baseline">
<u></u><span style="font-family:Arial,sans-serif;color:black"><span>2.<span style="font:7pt "Times New Roman"">   
</span></span></span><u></u><span style="font-family:Arial,sans-serif;color:black">567, 456, 234, 123<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">Assuming both contexts execute at runtime, the allocation will end up with 2 MIBs in the profile, one for each of the above contexts.</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">To represent this in the IR, we propose 2 new metadata attachment types, as described below.</span><u></u><u></u></p>
<h2 style="margin-right:0in;margin-bottom:6pt;margin-left:0.5in">
<span style="font-size:16pt;font-family:Arial,sans-serif;color:black;font-weight:normal">Callsite metadata</span><u></u><u></u></h2>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">The </span><span style="font-family:"Courier New";color:black">!callsite</span><span style="font-family:Arial,sans-serif;color:black"> metadata is used to associate a callsite with its corresponding
 references in MIB stack contexts. It contains the associated 64-bit stack entry table value for that callsite from the indexed profile, and is initially only on non-allocation callsites. As will be described later, after inlining it can contain multiple entry
 ids or be propagated onto allocation callsites.</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">In the above example, for the call to foo(), which had stack entry id 123, the IR callsite would be decorated with a
</span><span style="font-family:"Courier New";color:black">!callsite</span><span style="font-family:Arial,sans-serif;color:black"> metadata containing that stack entry id:</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:black">  tail call void @_Z3foov(), !dbg !12,
</span><span style="font-size:9pt;font-family:"Courier New";color:red">!callsite !14</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-size:9pt;font-family:"Courier New";color:red">!14 = !{i64 123}</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">Note that this call may be in a different module initially than the referencing MIB metadata. In order to disambiguate the context across modules, some form of LTO would be required. ThinLTO summary support
 will be added to reflect the cross-module contexts and enable cross module optimization of the contexts.</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">Also, while for MemProf the ids will be assigned uniquely using information from the MemProf profile, other types of context sensitive profiles could simply reuse the same id after matching with line
 table information, or at least leverage the same metadata attachment to assign their own unique ids if there is no MemProf profile.</span><u></u><u></u></p>
</div>
</blockquote>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in">To clarify, the callsite metadata value can be any globally unique identifier. While in this proposal we simply describe using the indexed profile's associated 64-bit stack entry table value, an alternative could
 be to compute this from the MD5 hash of the debug information (file:line:discriminator). It doesn't affect the format and its usage described in this RFC.<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<h2 style="margin-right:0in;margin-bottom:6pt;margin-left:0.5in">
<span style="font-size:16pt;font-family:Arial,sans-serif;color:black;font-weight:normal">Memprof metadata</span><u></u><u></u></h2>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">The </span><span style="font-family:"Courier New";color:black">!memprof</span><span style="font-family:Arial,sans-serif;color:black"> metadata describes the MIBs for the leaf allocation callsite it
 is attached to. If there are multiple stack contexts leading to that allocation, it will have a single
</span><span style="font-family:"Courier New";color:black">!memprof</span><span style="font-family:Arial,sans-serif;color:black"> metadata attachment, with a level of indirection used to list all related MIB, as shown in the later example.</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">As with the indexed profile format, we need to be able to add or modify fields of the MIB entries while maintaining backwards compatibility with older bitcode. Therefore, we use a schema format with the
 MIB profile entry fields described by a “Memprof Schema” module level metadata, for example:</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">!llvm.module.flags = !{!1}</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">!1 = !{i32 1, !”Memprof Schema”,!”Stack”, !”AllocCount”, !”AveSize”, !”MinSize”, !”MaxSize”, !”AveAccessCount”, !”MinAccessCount”, !”MaxAccessCount”, !”AveLifetime”, !”MinLifetime”, !”MaxLifetime”, !”NumMigration”,
 !”NumLifetimeOverlaps” }</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p style="margin-right:0in;margin-bottom:0in;margin-left:0.5in">
<span style="font-family:Arial,sans-serif;color:black">The first (merge behavior) field is 1 (ModFlagBehavior::Error), meaning that it is an error to merge modules with different values, or in other words, merging modules compiled with different profiles
 generated with different versions of the indexed profile format.</span><u></u><u></u></p>
</div>
</blockquote>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in">Using module flags for the schema doesn't work, because it only supports a single string tag and an integer flag value, not arbitrary contents. Instead, I have implemented this using a new named metadata:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in">!memprof.schema = !{!0}<br>
!0 = !{!"Stack", !"AllocCount", !"AveSize", !"MinSize", !"MaxSize", !"AveAccessCount", !"MinAccessCount", !"MaxAccessCount", !"AveLifetime", !"MinLifetime", !"MaxLifetime", !"NumMigration", !"NumLifetimeOverlaps"}<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in">Named metadata must only hold metadata nodes as operands. Here we use a single operand to point to metadata that describes the schema.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in">The advantage of using a single metadata operand in the new !memprof.schema metadata, vs for example a list of metadata operands each pointing to a single MDString schema field, is that it simplifies detection of
 different schemas when modules are merged for LTO. If the schemas are identical, the merged !memprof.schema metadata will continue to hold a single metadata node as the node holding the schema (!0 above) will be shared. If they are not identical, the merged
 module's !memprof.schema metadata will hold more than one metadata operand, one for each unique schema.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in">An alternative, which is what I originally had in a prototype, was to include the MDString field tags in each !memprof metadata, for example:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in">!274 = !{!"Stack", !273, !"AllocCount", i32 1, !"AveSize", i32 4, !"MinSize", i32 4, !"MaxSize", i32 4, !"AveAccessCount", i64 5, !"MinAccessCount", i64 1, !"MaxAccessCount", i64 10, !"AveLifetime", i32 10, !"MinLifetime",
 i32 10, !"MaxLifetime", i32 10, !"NumMigration", i32 0, !"NumLifetimeOverlaps", i32 0}<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in">This provides maximal flexibility of merging modules using different schemas, at the expense of additional overhead in operands (doubling the number of operands of !memprof metadata). But if we need to support merging
 modules with different schemas, we could alternatively just support unifying the named metadata schemas and fixing up the associated !memprof metadata during the merging process, rather than carrying this extra overhead.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
</div>
<div>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
<p class="MsoNormal" style="margin-left:0.5in">-- <u></u><u></u></p>
<div>
<div>
<div>
<table border="0" cellspacing="0" cellpadding="0" style="margin-left:0.5in">
<tbody>
<tr>
<td nowrap style="border-right:none;border-bottom:none;border-left:none;border-top:1.5pt solid rgb(213,15,37);padding:0in">
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(85,85,85)">Teresa Johnson |<u></u><u></u></span></p>
</td>
<td nowrap style="border-right:none;border-bottom:none;border-left:none;border-top:1.5pt solid rgb(51,105,232);padding:0in">
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(85,85,85)"> Software Engineer |<u></u><u></u></span></p>
</td>
<td nowrap style="border-right:none;border-bottom:none;border-left:none;border-top:1.5pt solid rgb(0,153,57);padding:0in">
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(85,85,85)"> <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |<u></u><u></u></span></p>
</td>
<td nowrap style="border-right:none;border-bottom:none;border-left:none;border-top:1.5pt solid rgb(238,178,17);padding:0in">
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>

</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><span style="font-family:Times;font-size:medium"><table cellspacing="0" cellpadding="0"><tbody><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small"><td nowrap style="border-top:2px solid rgb(213,15,37)">Teresa Johnson |</td><td nowrap style="border-top:2px solid rgb(51,105,232)"> Software Engineer |</td><td nowrap style="border-top:2px solid rgb(0,153,57)"> <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |</td><td nowrap style="border-top:2px solid rgb(238,178,17)"><br></td></tr></tbody></table></span></div></div></div></div>