Skip to content

Commit e6174eb

Browse files
[DebugInfo] Helper method for finding the deepest inlining location (#161696)
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.
1 parent 6beb860 commit e6174eb

File tree

2 files changed

+88
-7
lines changed

2 files changed

+88
-7
lines changed

llvm/include/llvm/IR/DebugInfoMetadata.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,14 +2600,19 @@ class DILocation : public MDNode {
26002600
StringRef getDirectory() const { return getScope()->getDirectory(); }
26012601
std::optional<StringRef> getSource() const { return getScope()->getSource(); }
26022602

2603-
/// Get the scope where this is inlined.
2604-
///
2605-
/// Walk through \a getInlinedAt() and return \a getScope() from the deepest
2606-
/// location.
2603+
/// Walk through \a getInlinedAt() and return the \a DILocation of the
2604+
/// outermost call site in the inlining chain.
2605+
const DILocation *getInlinedAtLocation() const {
2606+
const DILocation *Current = this;
2607+
while (const DILocation *Next = Current->getInlinedAt())
2608+
Current = Next;
2609+
return Current;
2610+
}
2611+
2612+
// Return the \a DILocalScope of the outermost call site in the inlining
2613+
// chain.
26072614
DILocalScope *getInlinedAtScope() const {
2608-
if (auto *IA = getInlinedAt())
2609-
return IA->getInlinedAtScope();
2610-
return getScope();
2615+
return getInlinedAtLocation()->getScope();
26112616
}
26122617

26132618
/// Get the DWARF discriminator.

llvm/unittests/IR/DebugInfoTest.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,82 @@ TEST(MetadataTest, DbgVariableRecordConversionRoutines) {
12501250
EXPECT_EQ(DVI2->getExpression(), Expr2);
12511251
}
12521252

1253+
TEST(MetadataTest, InlinedAtMethodsWithMultipleLevels) {
1254+
LLVMContext C;
1255+
1256+
// Create IR with 3 levels of inlining:
1257+
// main() calls inline1() which calls inline2() which calls inline3()
1258+
// We'll test from the perspective of code in inline3()
1259+
std::unique_ptr<Module> M = parseIR(C, R"(
1260+
define void @main() !dbg !10 {
1261+
ret void, !dbg !20
1262+
}
1263+
1264+
!llvm.dbg.cu = !{!0}
1265+
!llvm.module.flags = !{!2}
1266+
1267+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1)
1268+
!1 = !DIFile(filename: "test.c", directory: "/test")
1269+
!2 = !{i32 2, !"Debug Info Version", i32 3}
1270+
1271+
; Subprograms for each function in the call chain
1272+
!10 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 100, unit: !0)
1273+
!11 = distinct !DISubprogram(name: "inline1", scope: !1, file: !1, line: 200, unit: !0)
1274+
!12 = distinct !DISubprogram(name: "inline2", scope: !1, file: !1, line: 300, unit: !0)
1275+
!13 = distinct !DISubprogram(name: "inline3", scope: !1, file: !1, line: 400, unit: !0)
1276+
1277+
; Location in inline3 (line 401), inlined at location !21
1278+
!20 = !DILocation(line: 401, column: 5, scope: !13, inlinedAt: !21)
1279+
1280+
; Location in inline2 (line 301) where inline3 was called, inlined at !22
1281+
!21 = !DILocation(line: 301, column: 10, scope: !12, inlinedAt: !22)
1282+
1283+
; Location in inline1 (line 201) where inline2 was called, inlined at !23
1284+
!22 = !DILocation(line: 201, column: 15, scope: !11, inlinedAt: !23)
1285+
1286+
; Location in main (line 101) where inline1 was called (no more inlinedAt)
1287+
!23 = !DILocation(line: 101, column: 3, scope: !10)
1288+
)");
1289+
1290+
ASSERT_TRUE(M);
1291+
1292+
Function *MainFunc = M->getFunction("main");
1293+
ASSERT_TRUE(MainFunc);
1294+
Instruction &RetInst = MainFunc->getEntryBlock().front();
1295+
1296+
// Use getDebugLoc() to get the location from the ret instruction.
1297+
const DILocation *InnermostLoc = RetInst.getDebugLoc().get();
1298+
ASSERT_TRUE(InnermostLoc);
1299+
1300+
// Test getScope() - should return the immediate scope (inline3).
1301+
DILocalScope *ImmediateScope = InnermostLoc->getScope();
1302+
ASSERT_TRUE(ImmediateScope);
1303+
EXPECT_TRUE(isa<DISubprogram>(ImmediateScope));
1304+
EXPECT_EQ(cast<DISubprogram>(ImmediateScope)->getName(), "inline3");
1305+
1306+
// Test getInlinedAt() - should return the next level in the inlining chain.
1307+
const DILocation *NextLevel = InnermostLoc->getInlinedAt();
1308+
ASSERT_TRUE(NextLevel);
1309+
EXPECT_EQ(NextLevel->getLine(), 301u);
1310+
EXPECT_EQ(cast<DISubprogram>(NextLevel->getScope())->getName(), "inline2");
1311+
1312+
// Test getInlinedAtLocation() - should return the outermost location.
1313+
const DILocation *OutermostLoc = InnermostLoc->getInlinedAtLocation();
1314+
ASSERT_TRUE(OutermostLoc);
1315+
EXPECT_EQ(OutermostLoc->getLine(), 101u);
1316+
EXPECT_EQ(OutermostLoc->getColumn(), 3u);
1317+
EXPECT_EQ(OutermostLoc->getInlinedAt(), nullptr);
1318+
EXPECT_EQ(cast<DISubprogram>(OutermostLoc->getScope())->getName(), "main");
1319+
1320+
// Test getInlinedAtScope() - should return the scope of the outermost
1321+
// location.
1322+
DILocalScope *InlinedAtScope = InnermostLoc->getInlinedAtScope();
1323+
ASSERT_TRUE(InlinedAtScope);
1324+
EXPECT_TRUE(isa<DISubprogram>(InlinedAtScope));
1325+
EXPECT_EQ(cast<DISubprogram>(InlinedAtScope)->getName(), "main");
1326+
EXPECT_EQ(InlinedAtScope, OutermostLoc->getScope());
1327+
}
1328+
12531329
// Test that the hashing function for DISubprograms representing methods produce
12541330
// the same result after replacing their scope (the type containing the
12551331
// subprogram) from a temporary DIType with the permanent one.

0 commit comments

Comments
 (0)