Skip to content

Conversation

akshayrdeodhar
Copy link
Contributor

DebugInfoMetadata.h has a way to reach the scope of the deepest caller location, but not to the location itself. Having it is handy for debugging and debug printing.

@llvmbot
Copy link
Member

llvmbot commented Oct 2, 2025

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-debuginfo

Author: Akshay Deodhar (akshayrdeodhar)

Changes

DebugInfoMetadata.h has a way to reach the scope of the deepest caller location, but not to the location itself. Having it is handy for debugging and debug printing.


Full diff: https://github.com/llvm/llvm-project/pull/161696.diff

1 Files Affected:

  • (modified) llvm/include/llvm/IR/DebugInfoMetadata.h (+12-3)
diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h
index 6652e303a6648..d63e71d246fe0 100644
--- a/llvm/include/llvm/IR/DebugInfoMetadata.h
+++ b/llvm/include/llvm/IR/DebugInfoMetadata.h
@@ -2600,14 +2600,23 @@ class DILocation : public MDNode {
   StringRef getDirectory() const { return getScope()->getDirectory(); }
   std::optional<StringRef> getSource() const { return getScope()->getSource(); }
 
+
+  /// Get the location where this is inlined
+  ///
+  /// Walk through \a getInlinedAt() and return the \a DILocation where this is inlined.
+  DILocation *getInlinedAtLocation() const {
+    DILocation *Current = this, Next = nullptr;
+    while (Next = Current->getInlinedAt())
+      Current = Next;
+    return Current;
+  }
+
   /// Get the scope where this is inlined.
   ///
   /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
   /// location.
   DILocalScope *getInlinedAtScope() const {
-    if (auto *IA = getInlinedAt())
-      return IA->getInlinedAtScope();
-    return getScope();
+    return getInlinedAtLocation()->getScope();
   }
 
   /// Get the DWARF discriminator.

Copy link

github-actions bot commented Oct 2, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@OCHyams OCHyams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM, I'm surprised this doesn't already exist.

Could you add some unittest coverage? getInlinedAtScope doesn't look particularly well covered as is, but is at least called in llvm/unittests/Transforms/Utils/CloningTest.cpp - checking multiple levels of inlining would probably be ideal, but doing something similar to the getInlinedAtScope should probably be a minimum.

@akshayrdeodhar
Copy link
Contributor Author

Could you add some unittest coverage?

I'm planning to add a test to DebugInfoTest.cpp- does that work?

Copy link
Contributor

@OCHyams OCHyams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a couple nits

@akshayrdeodhar akshayrdeodhar enabled auto-merge (squash) October 6, 2025 21:18
@akshayrdeodhar akshayrdeodhar enabled auto-merge (squash) October 6, 2025 21:44
@akshayrdeodhar akshayrdeodhar merged commit e6174eb into llvm:main Oct 6, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants