[llvm] [DebugInfo] Helper method for finding the deepest inlining location (PR #161696)
Akshay Deodhar via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 6 14:40:42 PDT 2025
https://github.com/akshayrdeodhar updated https://github.com/llvm/llvm-project/pull/161696
>From 394b18e397b2bf7aae5c1d0aeafcf66ffcf4c734 Mon Sep 17 00:00:00 2001
From: Akshay Deodhar <adeodhar at nvidia.com>
Date: Thu, 25 Sep 2025 21:21:01 +0000
Subject: [PATCH 1/4] [DebugInfo] Helper method for finding the deepest
inlining location
---
llvm/include/llvm/IR/DebugInfoMetadata.h | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h
index 6652e303a6648..3536fcdbb911f 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.
+ const DILocation *getInlinedAtLocation() const {
+ 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.
>From 7edcc06f0993ba978a85e3ab1f1193cf65aa9b30 Mon Sep 17 00:00:00 2001
From: Akshay Deodhar <adeodhar at nvidia.com>
Date: Fri, 3 Oct 2025 23:11:57 +0000
Subject: [PATCH 2/4] address review comments
---
llvm/include/llvm/IR/DebugInfoMetadata.h | 15 ++---
llvm/unittests/IR/DebugInfoTest.cpp | 75 ++++++++++++++++++++++++
2 files changed, 80 insertions(+), 10 deletions(-)
diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h
index 3536fcdbb911f..55c925ae2086c 100644
--- a/llvm/include/llvm/IR/DebugInfoMetadata.h
+++ b/llvm/include/llvm/IR/DebugInfoMetadata.h
@@ -2600,21 +2600,16 @@ 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.
+ /// Walk through \a getInlinedAt() and return the \a DILocation of the outermost
+ /// call site in the inlining chain.
const DILocation *getInlinedAtLocation() const {
- const DILocation *Current = this, *Next = nullptr;
- while (Next = Current->getInlinedAt())
+ const DILocation *Current = this;
+ while (const DILocation *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.
+ // Return the \a DILocalScope of the outermost call site in the inlining chain.
DILocalScope *getInlinedAtScope() const {
return getInlinedAtLocation()->getScope();
}
diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp
index 03333d5872d2f..273e6825e9943 100644
--- a/llvm/unittests/IR/DebugInfoTest.cpp
+++ b/llvm/unittests/IR/DebugInfoTest.cpp
@@ -1367,4 +1367,79 @@ TEST(DIBuilder, DynamicOffsetAndSize) {
EXPECT_EQ(Field->getRawSizeInBits(), Len);
}
+TEST(DILocationTest, InlinedAtMethodsWithMultipleLevels) {
+ LLVMContext C;
+
+ // Create IR with 3 levels of inlining:
+ // main() calls inline1() which calls inline2() which calls inline3()
+ // We'll test from the perspective of code in inline3()
+ std::unique_ptr<Module> M = parseIR(C, R"(
+ define void @main() !dbg !10 {
+ ret void, !dbg !20
+ }
+
+ !llvm.dbg.cu = !{!0}
+ !llvm.module.flags = !{!2}
+
+ !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1)
+ !1 = !DIFile(filename: "test.c", directory: "/test")
+ !2 = !{i32 2, !"Debug Info Version", i32 3}
+
+ ; Subprograms for each function in the call chain
+ !10 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 100, unit: !0)
+ !11 = distinct !DISubprogram(name: "inline1", scope: !1, file: !1, line: 200, unit: !0)
+ !12 = distinct !DISubprogram(name: "inline2", scope: !1, file: !1, line: 300, unit: !0)
+ !13 = distinct !DISubprogram(name: "inline3", scope: !1, file: !1, line: 400, unit: !0)
+
+ ; Location in inline3 (line 401), inlined at location !21
+ !20 = !DILocation(line: 401, column: 5, scope: !13, inlinedAt: !21)
+
+ ; Location in inline2 (line 301) where inline3 was called, inlined at !22
+ !21 = !DILocation(line: 301, column: 10, scope: !12, inlinedAt: !22)
+
+ ; Location in inline1 (line 201) where inline2 was called, inlined at !23
+ !22 = !DILocation(line: 201, column: 15, scope: !11, inlinedAt: !23)
+
+ ; Location in main (line 101) where inline1 was called (no more inlinedAt)
+ !23 = !DILocation(line: 101, column: 3, scope: !10)
+ )");
+
+ ASSERT_TRUE(M);
+
+ Function *MainFunc = M->getFunction("main");
+ ASSERT_TRUE(MainFunc);
+ Instruction &RetInst = MainFunc->getEntryBlock().front();
+
+ // Use getDebugLoc() to get the location from the ret instruction.
+ const DILocation *InnermostLoc = RetInst.getDebugLoc().get();
+ ASSERT_TRUE(InnermostLoc);
+
+ // Test getScope() - should return the immediate scope (inline3).
+ DILocalScope *ImmediateScope = InnermostLoc->getScope();
+ ASSERT_TRUE(ImmediateScope);
+ EXPECT_TRUE(isa<DISubprogram>(ImmediateScope));
+ EXPECT_EQ(cast<DISubprogram>(ImmediateScope)->getName(), "inline3");
+
+ // Test getInlinedAt() - should return the next level in the inlining chain.
+ const DILocation *NextLevel = InnermostLoc->getInlinedAt();
+ ASSERT_TRUE(NextLevel);
+ EXPECT_EQ(NextLevel->getLine(), 301u);
+ EXPECT_EQ(cast<DISubprogram>(NextLevel->getScope())->getName(), "inline2");
+
+ // Test getInlinedAtLocation() - should return the outermost location.
+ const DILocation *OutermostLoc = InnermostLoc->getInlinedAtLocation();
+ ASSERT_TRUE(OutermostLoc);
+ EXPECT_EQ(OutermostLoc->getLine(), 101u);
+ EXPECT_EQ(OutermostLoc->getColumn(), 3u);
+ EXPECT_EQ(OutermostLoc->getInlinedAt(), nullptr);
+ EXPECT_EQ(cast<DISubprogram>(OutermostLoc->getScope())->getName(), "main");
+
+ // Test getInlinedAtScope() - should return the scope of the outermost location.
+ DILocalScope *InlinedAtScope = InnermostLoc->getInlinedAtScope();
+ ASSERT_TRUE(InlinedAtScope);
+ EXPECT_TRUE(isa<DISubprogram>(InlinedAtScope));
+ EXPECT_EQ(cast<DISubprogram>(InlinedAtScope)->getName(), "main");
+ EXPECT_EQ(InlinedAtScope, OutermostLoc->getScope());
+}
+
} // end namespace
>From b2cbf6dd943c3986dbe784c035240410d6a12808 Mon Sep 17 00:00:00 2001
From: Akshay Deodhar <adeodhar at nvidia.com>
Date: Fri, 3 Oct 2025 23:40:35 +0000
Subject: [PATCH 3/4] clang-format
---
.clang-format | 1 -
llvm/.clang-format | 1 -
llvm/include/llvm/IR/DebugInfoMetadata.h | 7 ++++---
llvm/unittests/IR/DebugInfoTest.cpp | 3 ++-
4 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/.clang-format b/.clang-format
index ecb44bfabd9aa..9b3aa8b7213b2 100644
--- a/.clang-format
+++ b/.clang-format
@@ -1,2 +1 @@
BasedOnStyle: LLVM
-LineEnding: LF
diff --git a/llvm/.clang-format b/llvm/.clang-format
index ecb44bfabd9aa..9b3aa8b7213b2 100644
--- a/llvm/.clang-format
+++ b/llvm/.clang-format
@@ -1,2 +1 @@
BasedOnStyle: LLVM
-LineEnding: LF
diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h
index 55c925ae2086c..7c6e709f2e1f6 100644
--- a/llvm/include/llvm/IR/DebugInfoMetadata.h
+++ b/llvm/include/llvm/IR/DebugInfoMetadata.h
@@ -2600,8 +2600,8 @@ class DILocation : public MDNode {
StringRef getDirectory() const { return getScope()->getDirectory(); }
std::optional<StringRef> getSource() const { return getScope()->getSource(); }
- /// Walk through \a getInlinedAt() and return the \a DILocation of the outermost
- /// call site in the inlining chain.
+ /// Walk through \a getInlinedAt() and return the \a DILocation of the
+ /// outermost call site in the inlining chain.
const DILocation *getInlinedAtLocation() const {
const DILocation *Current = this;
while (const DILocation *Next = Current->getInlinedAt())
@@ -2609,7 +2609,8 @@ class DILocation : public MDNode {
return Current;
}
- // Return the \a DILocalScope of the outermost call site in the inlining chain.
+ // Return the \a DILocalScope of the outermost call site in the inlining
+ // chain.
DILocalScope *getInlinedAtScope() const {
return getInlinedAtLocation()->getScope();
}
diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp
index 273e6825e9943..98084d2711107 100644
--- a/llvm/unittests/IR/DebugInfoTest.cpp
+++ b/llvm/unittests/IR/DebugInfoTest.cpp
@@ -1434,7 +1434,8 @@ TEST(DILocationTest, InlinedAtMethodsWithMultipleLevels) {
EXPECT_EQ(OutermostLoc->getInlinedAt(), nullptr);
EXPECT_EQ(cast<DISubprogram>(OutermostLoc->getScope())->getName(), "main");
- // Test getInlinedAtScope() - should return the scope of the outermost location.
+ // Test getInlinedAtScope() - should return the scope of the outermost
+ // location.
DILocalScope *InlinedAtScope = InnermostLoc->getInlinedAtScope();
ASSERT_TRUE(InlinedAtScope);
EXPECT_TRUE(isa<DISubprogram>(InlinedAtScope));
>From 3a5ea6aa0b45d2ebeb7e4f9a9eeb6fc1341cc05e Mon Sep 17 00:00:00 2001
From: Akshay Deodhar <adeodhar at nvidia.com>
Date: Mon, 6 Oct 2025 21:14:33 +0000
Subject: [PATCH 4/4] address review comments
---
.clang-format | 1 +
llvm/.clang-format | 1 +
llvm/unittests/IR/DebugInfoTest.cpp | 152 ++++++++++++++--------------
3 files changed, 78 insertions(+), 76 deletions(-)
diff --git a/.clang-format b/.clang-format
index 9b3aa8b7213b2..ecb44bfabd9aa 100644
--- a/.clang-format
+++ b/.clang-format
@@ -1 +1,2 @@
BasedOnStyle: LLVM
+LineEnding: LF
diff --git a/llvm/.clang-format b/llvm/.clang-format
index 9b3aa8b7213b2..ecb44bfabd9aa 100644
--- a/llvm/.clang-format
+++ b/llvm/.clang-format
@@ -1 +1,2 @@
BasedOnStyle: LLVM
+LineEnding: LF
diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp
index 98084d2711107..475e0a915fa28 100644
--- a/llvm/unittests/IR/DebugInfoTest.cpp
+++ b/llvm/unittests/IR/DebugInfoTest.cpp
@@ -1250,6 +1250,82 @@ TEST(MetadataTest, DbgVariableRecordConversionRoutines) {
EXPECT_EQ(DVI2->getExpression(), Expr2);
}
+TEST(MetadataTest, InlinedAtMethodsWithMultipleLevels) {
+ LLVMContext C;
+
+ // Create IR with 3 levels of inlining:
+ // main() calls inline1() which calls inline2() which calls inline3()
+ // We'll test from the perspective of code in inline3()
+ std::unique_ptr<Module> M = parseIR(C, R"(
+ define void @main() !dbg !10 {
+ ret void, !dbg !20
+ }
+
+ !llvm.dbg.cu = !{!0}
+ !llvm.module.flags = !{!2}
+
+ !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1)
+ !1 = !DIFile(filename: "test.c", directory: "/test")
+ !2 = !{i32 2, !"Debug Info Version", i32 3}
+
+ ; Subprograms for each function in the call chain
+ !10 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 100, unit: !0)
+ !11 = distinct !DISubprogram(name: "inline1", scope: !1, file: !1, line: 200, unit: !0)
+ !12 = distinct !DISubprogram(name: "inline2", scope: !1, file: !1, line: 300, unit: !0)
+ !13 = distinct !DISubprogram(name: "inline3", scope: !1, file: !1, line: 400, unit: !0)
+
+ ; Location in inline3 (line 401), inlined at location !21
+ !20 = !DILocation(line: 401, column: 5, scope: !13, inlinedAt: !21)
+
+ ; Location in inline2 (line 301) where inline3 was called, inlined at !22
+ !21 = !DILocation(line: 301, column: 10, scope: !12, inlinedAt: !22)
+
+ ; Location in inline1 (line 201) where inline2 was called, inlined at !23
+ !22 = !DILocation(line: 201, column: 15, scope: !11, inlinedAt: !23)
+
+ ; Location in main (line 101) where inline1 was called (no more inlinedAt)
+ !23 = !DILocation(line: 101, column: 3, scope: !10)
+ )");
+
+ ASSERT_TRUE(M);
+
+ Function *MainFunc = M->getFunction("main");
+ ASSERT_TRUE(MainFunc);
+ Instruction &RetInst = MainFunc->getEntryBlock().front();
+
+ // Use getDebugLoc() to get the location from the ret instruction.
+ const DILocation *InnermostLoc = RetInst.getDebugLoc().get();
+ ASSERT_TRUE(InnermostLoc);
+
+ // Test getScope() - should return the immediate scope (inline3).
+ DILocalScope *ImmediateScope = InnermostLoc->getScope();
+ ASSERT_TRUE(ImmediateScope);
+ EXPECT_TRUE(isa<DISubprogram>(ImmediateScope));
+ EXPECT_EQ(cast<DISubprogram>(ImmediateScope)->getName(), "inline3");
+
+ // Test getInlinedAt() - should return the next level in the inlining chain.
+ const DILocation *NextLevel = InnermostLoc->getInlinedAt();
+ ASSERT_TRUE(NextLevel);
+ EXPECT_EQ(NextLevel->getLine(), 301u);
+ EXPECT_EQ(cast<DISubprogram>(NextLevel->getScope())->getName(), "inline2");
+
+ // Test getInlinedAtLocation() - should return the outermost location.
+ const DILocation *OutermostLoc = InnermostLoc->getInlinedAtLocation();
+ ASSERT_TRUE(OutermostLoc);
+ EXPECT_EQ(OutermostLoc->getLine(), 101u);
+ EXPECT_EQ(OutermostLoc->getColumn(), 3u);
+ EXPECT_EQ(OutermostLoc->getInlinedAt(), nullptr);
+ EXPECT_EQ(cast<DISubprogram>(OutermostLoc->getScope())->getName(), "main");
+
+ // Test getInlinedAtScope() - should return the scope of the outermost
+ // location.
+ DILocalScope *InlinedAtScope = InnermostLoc->getInlinedAtScope();
+ ASSERT_TRUE(InlinedAtScope);
+ EXPECT_TRUE(isa<DISubprogram>(InlinedAtScope));
+ EXPECT_EQ(cast<DISubprogram>(InlinedAtScope)->getName(), "main");
+ EXPECT_EQ(InlinedAtScope, OutermostLoc->getScope());
+}
+
// Test that the hashing function for DISubprograms representing methods produce
// the same result after replacing their scope (the type containing the
// subprogram) from a temporary DIType with the permanent one.
@@ -1367,80 +1443,4 @@ TEST(DIBuilder, DynamicOffsetAndSize) {
EXPECT_EQ(Field->getRawSizeInBits(), Len);
}
-TEST(DILocationTest, InlinedAtMethodsWithMultipleLevels) {
- LLVMContext C;
-
- // Create IR with 3 levels of inlining:
- // main() calls inline1() which calls inline2() which calls inline3()
- // We'll test from the perspective of code in inline3()
- std::unique_ptr<Module> M = parseIR(C, R"(
- define void @main() !dbg !10 {
- ret void, !dbg !20
- }
-
- !llvm.dbg.cu = !{!0}
- !llvm.module.flags = !{!2}
-
- !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1)
- !1 = !DIFile(filename: "test.c", directory: "/test")
- !2 = !{i32 2, !"Debug Info Version", i32 3}
-
- ; Subprograms for each function in the call chain
- !10 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 100, unit: !0)
- !11 = distinct !DISubprogram(name: "inline1", scope: !1, file: !1, line: 200, unit: !0)
- !12 = distinct !DISubprogram(name: "inline2", scope: !1, file: !1, line: 300, unit: !0)
- !13 = distinct !DISubprogram(name: "inline3", scope: !1, file: !1, line: 400, unit: !0)
-
- ; Location in inline3 (line 401), inlined at location !21
- !20 = !DILocation(line: 401, column: 5, scope: !13, inlinedAt: !21)
-
- ; Location in inline2 (line 301) where inline3 was called, inlined at !22
- !21 = !DILocation(line: 301, column: 10, scope: !12, inlinedAt: !22)
-
- ; Location in inline1 (line 201) where inline2 was called, inlined at !23
- !22 = !DILocation(line: 201, column: 15, scope: !11, inlinedAt: !23)
-
- ; Location in main (line 101) where inline1 was called (no more inlinedAt)
- !23 = !DILocation(line: 101, column: 3, scope: !10)
- )");
-
- ASSERT_TRUE(M);
-
- Function *MainFunc = M->getFunction("main");
- ASSERT_TRUE(MainFunc);
- Instruction &RetInst = MainFunc->getEntryBlock().front();
-
- // Use getDebugLoc() to get the location from the ret instruction.
- const DILocation *InnermostLoc = RetInst.getDebugLoc().get();
- ASSERT_TRUE(InnermostLoc);
-
- // Test getScope() - should return the immediate scope (inline3).
- DILocalScope *ImmediateScope = InnermostLoc->getScope();
- ASSERT_TRUE(ImmediateScope);
- EXPECT_TRUE(isa<DISubprogram>(ImmediateScope));
- EXPECT_EQ(cast<DISubprogram>(ImmediateScope)->getName(), "inline3");
-
- // Test getInlinedAt() - should return the next level in the inlining chain.
- const DILocation *NextLevel = InnermostLoc->getInlinedAt();
- ASSERT_TRUE(NextLevel);
- EXPECT_EQ(NextLevel->getLine(), 301u);
- EXPECT_EQ(cast<DISubprogram>(NextLevel->getScope())->getName(), "inline2");
-
- // Test getInlinedAtLocation() - should return the outermost location.
- const DILocation *OutermostLoc = InnermostLoc->getInlinedAtLocation();
- ASSERT_TRUE(OutermostLoc);
- EXPECT_EQ(OutermostLoc->getLine(), 101u);
- EXPECT_EQ(OutermostLoc->getColumn(), 3u);
- EXPECT_EQ(OutermostLoc->getInlinedAt(), nullptr);
- EXPECT_EQ(cast<DISubprogram>(OutermostLoc->getScope())->getName(), "main");
-
- // Test getInlinedAtScope() - should return the scope of the outermost
- // location.
- DILocalScope *InlinedAtScope = InnermostLoc->getInlinedAtScope();
- ASSERT_TRUE(InlinedAtScope);
- EXPECT_TRUE(isa<DISubprogram>(InlinedAtScope));
- EXPECT_EQ(cast<DISubprogram>(InlinedAtScope)->getName(), "main");
- EXPECT_EQ(InlinedAtScope, OutermostLoc->getScope());
-}
-
} // end namespace
More information about the llvm-commits
mailing list