<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, May 20, 2020 at 9:17 AM Adrian Prantl <<a href="mailto:aprantl@apple.com">aprantl@apple.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 style="overflow-wrap: break-word;"><br><div><br><blockquote type="cite"><div>On May 19, 2020, at 3:03 PM, Vedant Kumar <<a href="mailto:vedant_kumar@apple.com" target="_blank">vedant_kumar@apple.com</a>> wrote:</div><br><div><div style="overflow-wrap: break-word;">Hi,<div><br></div><div>In a recent review (<a href="https://reviews.llvm.org/D79967" target="_blank">https://reviews.llvm.org/D79967</a>), David Blaikie suggested that we have a broader design discussion about how support for DW_TAG_call_site is supported in clang, so I’ll kick off the discussion.</div><div><br></div><div>Some topics to discuss:</div><div><br></div><div>1) Under LTO, if we emit a declaration DISubprogram for a function in one TU, and another TU defines the function with __attribute__((nodebug)), would the declaration DISubprogram get attached to the definition?</div><div><br></div><div>My own thoughts on this: firstly, as posed in <a href="https://reviews.llvm.org/D79967" target="_blank">D79967</a>, I think David was asking about the case where the declaration subprogram does not have an attached unit. I’m not sure why it wouldn’t, though, so I’d appreciate some clarification on that. Second, as far as I know, there isn’t a mechanism for attaching a declaration subprogram to a Function which doesn’t have debug info.</div><div><br></div><div>2) Should declaration subprograms emitted in support of DW_TAG_call_site be kept in the CU’s retainedTypes field?</div><div><br></div><div>David pointed out that these declarations a) persist through optimization and b) don't get deduplicated against the definitions, which means there’s potential to bloat debug info. As an alternative, we might attach a declaration subprogram to the corresponding Function (which we do already), but leave it out of the CU’s retainedTypes field.</div></div></div></blockquote><div><br></div><div>I'm having trouble visualizing this example: We are talking about forward declaration DISubprograms created for call sites of functions that are not defined in the CU where the call site is, correct?</div></div></div></blockquote><div><br></div><div>Right</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 style="overflow-wrap: break-word;"><div><div> What does "attaching a declaration to the corresponding function" mean?</div></div></div></blockquote><div><br>I think "attaching the declaration DISubprogram to the corresponding llvm::Function" would be a more precise statement. The same way definition DISubprograms are attached to llvm::Functions, using the Function's !dbg/getSubprogram field.<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 style="overflow-wrap: break-word;"><div><div> Why are we currently entering those declarations into retainedTypes?</div></div></div></blockquote><div><br>Because we're not currently attaching those DISubprogram declarations to the llvm::Function declarations - or any other part of the IR.<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 style="overflow-wrap: break-word;"><div><div> I would have expected the reference from the call site to hold on to them. </div></div></div></blockquote><div><br>That would be a 3rd option - think attaching them to the llvm::Function would /probably/ be better. (since the IR already has the infrastructure for that attachment, whereas the call instruction doesn't - it just has the DebugLoc attachment)<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 style="overflow-wrap: break-word;"><div><div>I guess it's unclear to me how call sites are represented at the moment.</div></div></div></blockquote><div><br>Right, probably a good place to start.<br><br>If the call is to a function defined in the same module - there's no IR representation for the call site - the CallInst refers the llvm::Function that in turn refers to the definition DISubprogram. So the DWARF emission code looks that up to create the call site description.<br><br>But if it's a function that /isn't/ defined in the same module, the declaration DISubprogram is created and attached to the DICompileUnit's retainedTypes list to make sure it doesn't get dropped (because it's not referenced from anywhere else in the IR/metadata/debug info) and... it looks like I misunderstood.<br><br>No, the declaration DISubprogram /is/ attached to the llvm::Function via its !dbg attachment, it's just the IR syntax wasn't what I was expecting:<br><br>For a definition llvm::Function, the !dbg is rendered at the end of the line:<br>





<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco;color:rgb(242,242,242);background-color:rgba(0,0,0,0.85)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">define dso_local void @_Z2f2v() local_unnamed_addr #</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures;color:rgb(251,0,255)">0</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"> !dbg !</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures;color:rgb(251,0,255)">11</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"> {</span></p> <br>But for a declaration llvm::Function, the !dbg is rendered nearer the beginning:<br>





<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco;color:rgb(242,242,242);background-color:rgba(0,0,0,0.85)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">declare !dbg !</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures;color:rgb(251,0,255)">4</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"> dso_local void @_Z2f1v() local_unnamed_addr #</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures;color:rgb(251,0,255)">1</span></p><br>Quirky, but OK.<br><br>OK, so now my question becomes "why are these in the retainedTypes list at all"? Because it looks like they're attached in entirely the good/proper/appropriate way that will keep them when we need them, drop them when we don't, merge them in LTO as desired, etc. (except for them being in the retainedTypes list - which thwarts a bunch of that and doesn't seem to be providing any value that I can see - nothing iterates the retainedTypes list expecting to find them there/look for them there, so they're just kept alive for no use I can see)<br><br><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 style="overflow-wrap: break-word;"><div><div><br></div><div>-- adrian</div><br><blockquote type="cite"><div><div style="overflow-wrap: break-word;"><div><br></div><div>My own thoughts: as I understand it, if a function ends up dead (all call sites to it are optimized out), we won’t emit a DW_TAG_call_site that references the function, and so we won't emit unnecessary declaration subprograms. This is demonstrated in the LTO case in llvm/test/DebugInfo/X86/lto-cross-cu-call-origin-ref.ll (added in <a href="https://reviews.llvm.org/D70350" target="_blank">https://reviews.llvm.org/D70350</a>), where the declaration subprograms for func_from_b, noinline_func_in_a, and always_inline_helper_in_a_that_calls_foo are elided. The same test demonstrates that llvm can emit cross-CU references to definition subprograms within DW_TAG_call_site. However, all of this just pertains to the final DWARF. There may still be some cost (in terms of metadata) to preserving a declaration subprogram throughout optimization when all references to the declaration are removed. Before changing the representation, it’d be helpful to measure how much it costs (either in terms of compile-time, or metadata size) to keep unreferenced declaration subprograms around (say, on a stage2 clang build).</div></div></div></blockquote></div></div></blockquote><div>I'm not too interested in the cost, just the principle really - why are they tehre? (I'm ever more confused why they're there now that I see they are attached to the llvm::Function's !dbg, and that I can't find any code using their presence in RetainedTypes) <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 style="overflow-wrap: break-word;"><div><blockquote type="cite"><div><div style="overflow-wrap: break-word;"><div><br></div><div>As for why declaration subprograms are inserted into a CU’s retainedTypes field in the first place: I don’t think this was intentionally changed just for DW_TAG_call_site support. Adrian (cc’d) changed EmitFunctionDecl to retain declaration subprograms in r266445, and the new code path which calls into EmitFunctionDecl adopted the same behavior. (Hopefully that clears up some of the history, that’s not to say that it’s not worth revisiting.)</div></div></div></blockquote></div></div></blockquote><div>Ah, OK, so thanks for pointing to the revision that introduced this. <br><br>"commit e76bda544bbf52d9ff3b55e6018b494a1e6bbc00<br>Author: Adrian Prantl <<a href="mailto:aprantl@apple.com">aprantl@apple.com</a>><br>Date:   Fri Apr 15 15:55:45 2016 +0000<br><br>    Update to match LLVM changes for PR27284.<br>    (Reverse the ownership between DICompileUnit and DISubprogram.)<br>    <br>    <a href="http://reviews.llvm.org/D19034">http://reviews.llvm.org/D19034</a><br>    <rdar://problem/25256815><br>    <br>    llvm-svn: 266445"<br><br>Adrian - do you have any context on why this change ended up adding declaration DISubprogram's to the retained types list? (perhaps something discussed in the rdar?) It doesn't look like the clang part of this was reviewed.<br><br>I think it's probably best to just remove it & see if anything breaks - I don't see anything that depends on it, at least at a cursory glance.<br><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 style="overflow-wrap: break-word;"><div><blockquote type="cite"><div><div style="overflow-wrap: break-word;"><div>best,</div><div>vedant</div></div></div></blockquote></div><br></div></blockquote></div></div>