<div dir="ltr"><div>I believe the only way to do this is to go forward from llvm::Functions to DISubprograms - so probably your best bet is to walk all the llvm::Functions in your module either searching for the associated DISubprogram, or creating a map from Function to DISubprogram to do lookups on later if you want to do several of these.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Apr 30, 2020 at 8:44 AM William Woodruff via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</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">Hi all,<br>
<br>
I'm dealing with LLVM's debug information metadata, and have run into<br>
an interesting edge case.<br>
<br>
Under normal circumstances, every `DILocalVariable` has a `User` in<br>
the form of the `llvm.dbg.*` intrinsic that produced it. Knowing this,<br>
I can go from `DILocalVariable` to `MetadataAsValue`, grab the users,<br>
and end up at the corresponding instruction + function.<br>
<br>
In some cases, however, LLVM will optimize the IR such that those<br>
intrinsics are no longer called, and their corresponding metadata is<br>
stashed in the `RetainedNodes` of each `DISubprogram` instead.<br>
<br>
For example:<br>
<br>
    ; Function Attrs: nounwind readnone uwtable<br>
    define dso_local i32 @foobar(i32) local_unnamed_addr #0 !dbg !13 {<br>
      ; no calls to llvm.dbg.* for the locals in this func<br>
    }<br>
<br>
With the corresponding debug nodes:<br>
<br>
    !13 = distinct !DISubprogram(name: "foobar", scope: !1, file: !1,<br>
line: 7, type: !14, isLocal: false, isDefinition: true, scopeLine: 7,<br>
flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes:<br>
!16)<br>
    !14 = !DISubroutineType(types: !15)<br>
    !15 = !{!4, !4}<br>
    !16 = !{!17, !18}<br>
    !17 = !DILocalVariable(name: "x", arg: 1, scope: !13, file: !1,<br>
line: 7, type: !4)<br>
    ; this one in particular, since it's a non-argument local<br>
    !18 = !DILocalVariable(name: "y", scope: !13, file: !1, line: 13, type: !19)<br>
<br>
I can iterate over these retained local variables as a special case,<br>
and would like to map them back to their corresponding IR function<br>
(which I don't have direct access to). My plan for doing so was to<br>
grab their `DISubprogram` and do a similar `MetadataAsValue -> users`<br>
trick to reach the `llvm::Function`,  but this results in an empty<br>
use/users list (presumably because the `!dbg !13` isn't recorded as a<br>
"use").<br>
<br>
Any ideas? I'm in a context where the only state I have is the<br>
DILocalVariable (as MetadataAsValue, so I have an LLVMContext).<br>
<br>
Thanks,<br>
William<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>