<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br></div><div><br>On Dec 23, 2016, at 18:36, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Fri, Dec 23, 2016 at 11:47 AM Duncan P. N. Exon Smith <<a href="mailto:dexonsmith@apple.com">dexonsmith@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg">A few disjoint thoughts; sorry they're so delayed (I skimmed the responses below, and I think these are still relevant/not covered elsewhere).<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Firstly, why *should* DISubprogram definitions be distinct? There were two reasons this was valuable (this was from before there was a cu: link).</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">- It helped to fix long-standing bugs in the IRLinker, where uniqued-DISubprograms in different compile units would coalesce.</div></div></blockquote><div><br></div><div>Any idea why that ^ was a problem/bugs?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"> Now that the Function <-> DISubprogram connection has been reversed, I'm not entirely sure it's still a necessary precaution.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">- It broke a several uniquing cycles involving DISubprogram. The existence of the uniquing cycles meant that affected DISubprograms weren't really uniqued, anyway (since we don't bother to solve graph isomorphism); and breaking the cycles sped up the IRLinker and reduced memory waste.</div></div></blockquote><div><br>Ah, fair point - my trivial example didn't have any local variables, so didn't hit this problem. They do indeed form cycles and I haven't tested, but seems totally reasonable/likely that that would break my simple example because cycles break uniquing and all that.<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"> Making DISubprogram uniqued again would either have no real effect (because distinct nodes they reference aren't uniqued anymore either) or bring back uniquing cycles (which would have effects... but not the desired one).</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">But there ought to be a way to get coalescing in the right places. We just need to ensure that the uniqued/coalesced parts of the graph:</div><div class="gmail_msg">- are acyclic, and</div><div class="gmail_msg">- reference only coalesced nodes (note that leaves can happily include the strangely-coalesced distinct DICompositeType beast I created).</div></div></blockquote><div><br>Perhaps before I dive into your suggestion (which, it seems, is to separate out the definition but let the declaration/locals form cycles</div></div></div></div></blockquote><div><br></div><div>No (new) cycles; in fact this breaks some. </div><div>- the Function itself references the defn and the locals;</div><div>- the locals and the defn reference (transitively) the decl; and</div><div>- the decl doesn't reference any of those.</div><div><br></div><div>The only cycle is the decl <-> composite type one that already exists (and that odr magic (mostly) fixes). Maybe that one could be removed somehow too, but it isn't directly relevant to the problem you're looking at. </div><div><br></div><div>(Note that this is somewhat inspired by Reid's point in his recent debug info talk, that CodeView avoids type cycles by breaking types into two records.)</div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_quote"><div> - that would at least be dropped because the definition would only be kept due to it being referenced from the llvm::Function), what about something simpler:<br><br>What if scope chains didn't go all the way to the top (the subprogram) but stopped short of that?<br><br>Now, granted - this was part of the great assertion I added several years back that caught all kinds of silliness - mostly around inlined call sites not having debugloc and leading to scope chains that lead to distinct roots. But the alternative to having and maintaining an invariant - is just to make things true by construction. All debug info scope chains within a function with an associated DISubprogram have an implicit last element of that DISubprogram?<br></div></div></div></div></blockquote><div><br></div><div><div><span style="background-color: rgba(255, 255, 255, 0);">That seems reasonable, I think, now that the function points directly at its subprogram.</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">I'm not sure it's simpler to get right though. Dropping the scope runs the risk of coalescing things incorrectly. I imagine the backend relies on pointer distinctness of two local variables called "a" that are in (or are parameters of) unrelated subprograms. I think in some situations they could be coalesced incorrectly if they had no scope. </span></div></div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_quote"><div>That would mean it'd be harder to catch the bugs I found</div></div></div></div></blockquote><div><br></div><div>A shame if so, but if we can find another way to catch it or design it away, as you mention, it doesn't much matter. </div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_quote"><div> - but I think most of them came from that specific inlining situation (I can go back and do some archaeology and see if there's other exposure where we could sure up some defenses as well) which can be caught more directly (I forget if the inliner now catches it itself, or if we enhanced the debug info verifier to catch it after the inliner runs - if it's the verifier, then that wouldn't suffice if we made the change I'm suggesting - and we'd have to hook it up in the inliner itself)<br><br>Thoughts?</div></div></div></div></blockquote><blockquote type="cite"><div dir="ltr"><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">I've been thinking idly whether we can/should break DISubprogram apart into uniqued (coalesce-able) and distinct parts. Still being someone ignorant of DWARF itself (and CodeView, for that matter), I'm not entirely sure. But...</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Here's my understanding of the current uses of DISubprogram (where I'm using DISubprogramDecl for the usually uniqued declarations, and DISubprogramDefn for the always distinct definitions):</div><div class="gmail_msg"><br class="gmail_msg"></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class="gmail_msg"><div class="gmail_msg"><font face="Menlo" class="gmail_msg">// !dbg</font></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg">Function -> DISubprogram</font></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg"><br class="gmail_msg"></font></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg">// Declaration (optional, only for member functions).</font></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg">DISubprogramDefn -> DISubprogramDecl</font></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg"><br class="gmail_msg"></font></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg">// Variables:</font></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg">DISubprogramDefn -> MDNode -> DILocalVariable</font></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg"><br class="gmail_msg"></font></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg">// Terminator for scope chains:</font></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg">DILocalVariable [-> DILocalScope [-> ...]]</font><span style="font-family:Menlo" class="gmail_msg"> -></span><span style="font-family:Menlo" class="gmail_msg"> DISubprogramDefn</span></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg">DILocation </font><span style="font-family:Menlo" class="gmail_msg">[-> DILocalScope [-> ...]]</span><span style="font-family:Menlo" class="gmail_msg"> -></span><span style="font-family:Menlo" class="gmail_msg"> DISubprogramDefn</span></div><div class="gmail_msg"><span style="font-family:Menlo" class="gmail_msg">DICompositeType -> DISubprogramDefn // function-local type</span></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><span style="font-family:Menlo" class="gmail_msg">// Elements (member functions):</span></div><div class="gmail_msg"><font face="Menlo" class="gmail_msg">DICompositeType -> MDNode -> DISubprogramDecl</font></div><div class="gmail_msg"><div class="gmail_msg"><span style="font-family:Menlo" class="gmail_msg">DISubprogramDecl -> DICompositeType</span></div></div><div class="gmail_msg"><span style="font-family:Menlo" class="gmail_msg">DISubprogramDefn -> DICompositeType</span></div></blockquote><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">And here are the changes I would consider:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">1. Split DISubprogramDecl and DISubprogramDefn into different types.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">2. Remove definition-specific fields from DISubprogramDecl.</div><div class="gmail_msg">- variables, a "dangerous" source of uniquing cycles.</div><div class="gmail_msg">- declaration, which it will never have.</div><div class="gmail_msg">- scopeLine, which it will never have.</div><div class="gmail_msg">- unit, which the Defn will have when it's relevant.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">3. Make a DISubprogramDecl mandatory for DISubprogramDefn.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">4. Remove redundant fields from DISubprogramDefn.</div><div class="gmail_msg">- name and linkageName are on the declaration.</div><div class="gmail_msg">- type is redundant.</div><div class="gmail_msg">- flags is likely redundant?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Nothing so far has made any real changes, I think. DISubprogramDecl should coalesce nicely between modules though.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">5. Migrate links to point at DISubprogramDecl instead of DISubprogramDefn.</div><div class="gmail_msg">- DILocalVariable scope chain.</div><div class="gmail_msg">- DILocation scope chain.</div><div class="gmail_msg">- DICompositeType scope.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I don't think this will create any uniquing cycles. DISubprogramDecl won't point at any of these. Also, DILocalVariable should start coalescing with each other. </div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Since DISubprogramDefn is only referenced from the Function !dbg attachment, only one should survive function-coalescing.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">One open question, since I don't know the backend code, is: can you emit the "correct" DWARF after step (5)? I'm hoping you only need the Defn of the Function that has instructions referencing the DILocalVariable/DILocation. In which case, we suddenly get a lot of coalescing.</div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><blockquote type="cite" class="gmail_msg">On 2016-Dec-15, at 10:54, David Blaikie <<a href="mailto:dblaikie@gmail.com" class="gmail_msg" target="_blank">dblaikie@gmail.com</a>> wrote:<br class="gmail_msg"><br class="gmail_msg">Branching off from a discussion of improvements to DIGlobalVariable representations that Adrian's working on - got me thinking about related changes that have already been made to DISubprogram.<br class="gmail_msg"><br class="gmail_msg">To reduce duplicate debug info when things like linkonce_odr functions were deduplicated in LTO linking, the relationship between a CU and DISubprogram was inverted (instead of a CU maintaining a list of subprograms, subprograms specify which CU they come from - and the llvm::Function references the DISubprogram, so if the llvm::Function goes away, so does the associated DISubprogram)<br class="gmail_msg"><br class="gmail_msg">I'm not sure if this caused a regression, but at least seems to miss a possible improvement:<br class="gmail_msg"><br class="gmail_msg">During IR linking (for LTO, ThinLTO, etc) these distinct DISubprogram definitions (& their CU link, even if they weren't marked 'distinct', the CU link would cause them to effectively be so) remain separate - this means that inlined versions in one CU don't refer to an existing subprogram definition in another CU.<br class="gmail_msg"><br class="gmail_msg">To demonstrate:<br class="gmail_msg">inl.h:<br class="gmail_msg">void f1();<br class="gmail_msg">inline __attribute__((always_inline)) void f2() {<br class="gmail_msg"> f1();<br class="gmail_msg">}<br class="gmail_msg">inl1.cpp:<br class="gmail_msg">#include "inl.h"<br class="gmail_msg">void c1() {<br class="gmail_msg"> f2();<br class="gmail_msg">}<br class="gmail_msg">inl2.cpp:<br class="gmail_msg">#include "inl.h"<br class="gmail_msg">void c2() {<br class="gmail_msg"> f2();<br class="gmail_msg">}<br class="gmail_msg"><br class="gmail_msg">Compile to IR, llvm-link the result. The DWARF you get is basically the same as the DWARF you'd get without linking:<br class="gmail_msg"><br class="gmail_msg">DW_TAG_compile_unit<br class="gmail_msg"> DW_AT_name "inl1.cpp"<br class="gmail_msg"> DW_TAG_subprogram #0<br class="gmail_msg"> DW_AT_name "f2"<br class="gmail_msg"> DW_TAG_subprogram<br class="gmail_msg"> DW_AT_name "c1"<br class="gmail_msg"> DW_TAG_inlined_subroutine<br class="gmail_msg"> DW_TAG_abstract_origin #0 "f2"<br class="gmail_msg">DW_TAG_compile_unit<br class="gmail_msg"> DW_AT_name "inl2.cpp"<br class="gmail_msg"> DW_TAG_subprogram #1<br class="gmail_msg"> DW_AT_name "f2"<br class="gmail_msg"> DW_TAG_subprogram<br class="gmail_msg"> DW_AT_name "c2"<br class="gmail_msg"> DW_TAG_inlined_subroutine<br class="gmail_msg"> DW_TAG_abstract_origin #1 "f2"<br class="gmail_msg"><br class="gmail_msg">Instead of something more like this:<br class="gmail_msg"><br class="gmail_msg">DW_TAG_compile_unit<br class="gmail_msg"> DW_AT_name "inl1.cpp"<br class="gmail_msg"> DW_TAG_subprogram #0<br class="gmail_msg"> DW_AT_name "f2"<br class="gmail_msg"> DW_TAG_subprogram<br class="gmail_msg"> DW_AT_name "c1"<br class="gmail_msg"> DW_TAG_inlined_subroutine<br class="gmail_msg"> DW_TAG_abstract_origin #0 "f2"<br class="gmail_msg">DW_TAG_compile_unit<br class="gmail_msg"> DW_AT_name "inl2.cpp"<br class="gmail_msg"> DW_TAG_subprogram<br class="gmail_msg"> DW_AT_name "c2"<br class="gmail_msg"> DW_TAG_inlined_subroutine<br class="gmail_msg"> DW_TAG_abstract_origin #0 "f2"<br class="gmail_msg"><br class="gmail_msg">(note that only one abstract definition of f2 is produced here)<br class="gmail_msg"><br class="gmail_msg">Any thoughts? I imagine this is probably worth a reasonable amount of savings in an optimized build. Not huge, but not nothing. (probably not the top of anyone's list though, I realize)<br class="gmail_msg"><br class="gmail_msg">Should we remove the CU link from a non-internal linkage subprogram (& this may have an effect on the GV representation issue originally being discussed) and just emit it into whichever CU happens to need it first?<br class="gmail_msg"><br class="gmail_msg">This might be slightly sub-optimal, due to, say, the namespace being foreign to that CU. But it's how we do types currently, I think? So at least it'd be consistent and probably cheap enough/better.<br class="gmail_msg"></blockquote><br class="gmail_msg"></div></div></blockquote></div></div>
</blockquote></body></html>