[llvm-dev] Mapping a retained DILocalVariable back to its Function

William Woodruff via llvm-dev llvm-dev at lists.llvm.org
Thu Apr 30 08:44:23 PDT 2020

Hi all,

I'm dealing with LLVM's debug information metadata, and have run into
an interesting edge case.

Under normal circumstances, every `DILocalVariable` has a `User` in
the form of the `llvm.dbg.*` intrinsic that produced it. Knowing this,
I can go from `DILocalVariable` to `MetadataAsValue`, grab the users,
and end up at the corresponding instruction + function.

In some cases, however, LLVM will optimize the IR such that those
intrinsics are no longer called, and their corresponding metadata is
stashed in the `RetainedNodes` of each `DISubprogram` instead.

For example:

    ; Function Attrs: nounwind readnone uwtable
    define dso_local i32 @foobar(i32) local_unnamed_addr #0 !dbg !13 {
      ; no calls to llvm.dbg.* for the locals in this func

With the corresponding debug nodes:

    !13 = distinct !DISubprogram(name: "foobar", scope: !1, file: !1,
line: 7, type: !14, isLocal: false, isDefinition: true, scopeLine: 7,
flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes:
    !14 = !DISubroutineType(types: !15)
    !15 = !{!4, !4}
    !16 = !{!17, !18}
    !17 = !DILocalVariable(name: "x", arg: 1, scope: !13, file: !1,
line: 7, type: !4)
    ; this one in particular, since it's a non-argument local
    !18 = !DILocalVariable(name: "y", scope: !13, file: !1, line: 13, type: !19)

I can iterate over these retained local variables as a special case,
and would like to map them back to their corresponding IR function
(which I don't have direct access to). My plan for doing so was to
grab their `DISubprogram` and do a similar `MetadataAsValue -> users`
trick to reach the `llvm::Function`,  but this results in an empty
use/users list (presumably because the `!dbg !13` isn't recorded as a

Any ideas? I'm in a context where the only state I have is the
DILocalVariable (as MetadataAsValue, so I have an LLVMContext).


More information about the llvm-dev mailing list