<div dir="ltr"><div dir="ltr">On Wed, Oct 13, 2021 at 4:20 PM Ellis Hoag via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi all,<br><br>In recent weeks I've been looking into fixing a few bugs in the Dwarf emitted by LLVM related to when functions are inlined.<br>* <a href="https://bugs.llvm.org/show_bug.cgi?id=30637" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=30637</a> (static variables)<br>  * Fixed in <a href="https://reviews.llvm.org/D108492" target="_blank">https://reviews.llvm.org/D108492</a> (in review)<br>* <a href="https://bugs.llvm.org/show_bug.cgi?id=52159" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=52159</a> (imported declaration)<br>  * Fixed in <a href="https://reviews.llvm.org/D110294" target="_blank">https://reviews.llvm.org/D110294</a> (in review)<br>In these bugs `getOrCreateSubprogramDIE()` is used to find the SP DIE that will become the parent of a GV DIE or the reference of an imported declaration DIE. The problem is if the function is inlined and removed, the concrete SP DIE will not be created and `getOrCreateSubprogramDIE()` will return an empty DIE. Instead, I think we should be using the abstract origin DIE of the SP which is created after processing an inlined scope.<br><br>Here is a concrete example from <a href="https://bugs.llvm.org/show_bug.cgi?id=52159" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=52159</a><br>```<br>namespace ns {<br>inline __attribute__((always_inline))<br>void foo() { int a = 4; }<br>}<br><br>void goo() {<br>  using ns::foo;<br>  foo();<br>}<br>```<br><br>This produces an imported declaration DIE that references an empty SP DIE even though there already exists one for foo.<br>```<br>// Abstract origin<br>0x0000002f:     DW_TAG_subprogram<br>                  DW_AT_linkage_name      ("_ZN2ns3fooEv")<br>                  DW_AT_name       ("foo")<br>// Empty concrete <br>0x00000047:     DW_TAG_subprogram<br>0x00000048:     NULL<br>// Import declaration<br>0x00000069:     DW_TAG_imported_declaration<br>                  DW_AT_import       (0x00000047)<br>```<br><br>One fix is to reference the abstract origin DIE.<br>```<br>0x00000069:     DW_TAG_imported_declaration<br>                  DW_AT_import      (0x0000002f)<br>```<br><br>Another fix is to do what gcc does and fill out a specification DIE that the import references.<br>```<br>// Import declaration<br>0x0000004f:     DW_TAG_imported_declaration<br>                  DW_AT_import        (0x00000063)<br>// Specification <br>0x00000063:     DW_TAG_subprogram<br>                  DW_AT_name     ("foo")<br>                  DW_AT_declaration (true)<br>// Abstract origin <br>0x00000070:   DW_TAG_subprogram<br>                DW_AT_specification      (0x00000063 "_ZN2ns3fooEv")<br>```<br><br>Since I'm relatively new to debug info, I thought I'd ask some questions on the mailing list.<br>1. A simple solution to this class of bugs is to reference the abstract origin SP DIE where appropriate. Do we have any rules for when to reference the abstract origin if it exists?<br></div></blockquote><div><br>That's the difficult problem here - the abstract origin only exists if there's at least one instance of the function being inlined - and we don't know that until we've processed all the functions. The way this was/is currently implemented is to create the concrete definition subprogram when we see the concrete function definition, but not fill out the attributes that would be inherited from an abstract origin if there was one - then wait until we get to the end of the module and if we've created an abstract origin (because an inlined instance was found/produced), then we add the abstract_origin attribute to the concrete definition subprogram, and if we haven't created an abstract origin (because there were no inlined instances) then we put the attributes (name, decl file/line/etc) on the concrete definition without creating an abstract origin.<br><br>For these other cases (variables, imported declarations, maybe function local types too?) - I think, at least consistent with the current design, we have to defer producing them until we get to the end - the same as for the name/decl file/line attributes, etc.<br><br>Though deferring them presents other challenges - we can't reference them if they're deferred, so if some other debug info like the type of a function parameter has to be produced, how do we produce that if we have deferred the type production?<br><br>We can't/shouldn't/it's difficult to create the DIEs but to defer attaching those DIEs to the DIE tree - because there's some logic that uses the DIE parent chain/which CU a DIE is in to determine certain issues of encoding (whether CU-local references can be used or the like). Maybe we can get rid of that constraint (at which point we could create DIEs but defer adding them to the DIE tree) but if I recall correctly it's pretty deeply embedded/important.<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 dir="ltr">2. Would it be better to follow gcc's solution and fill out specifications in these cases?<br></div></blockquote><div><br>I guess that doesn't address the function-local static variable case, or the case of an imported declaration being inside a subprogram? (we could in theory put those in a declaration DIE, but it'd probably be extra confusing to consumers)<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 dir="ltr">3. Are they more known cases/bugs to consider?</div></blockquote><div><br>Function local types /probably/ have a similar problem. Maybe call_site tags too? (since they reference function declarations to say which function the call_site is calling) Perhaps some other stuff - but those are ones that come to my mind at least.<br><br>Thanks for looking into this - sorry it's all a bit thorny, though.<br><br>- Dave<br> </div></div></div>