Skip to content

release/19.x: [RemoveDIs] Fix spliceDebugInfo splice-to-end edge case (#105671, #106723) #106952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions llvm/lib/IR/BasicBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,16 @@ void BasicBlock::spliceDebugInfoImpl(BasicBlock::iterator Dest, BasicBlock *Src,
if (ReadFromTail && Src->getMarker(Last)) {
DbgMarker *FromLast = Src->getMarker(Last);
if (LastIsEnd) {
Dest->adoptDbgRecords(Src, Last, true);
// adoptDbgRecords will release any trailers.
if (Dest == end()) {
// Abosrb the trailing markers from Src.
assert(FromLast == Src->getTrailingDbgRecords());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FromLast can't be null because of the outer Src->getMarker(Last) check.

createMarker(Dest)->absorbDebugValues(*FromLast, true);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

createMarker gets an existing marker if it exists or creates a new one otherwise, the return value is never null. end() is a valid and expected input to this function.

FromLast->eraseFromParent();
Src->deleteTrailingDbgRecords();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Src is not null (it is dereferenced throughout function without null checks).

Here the trailing marker is deleted (eraseFromParent) and unlinked from its parent (deleteTrailingDbgRecords) - the order doesn't matter because deleteTrailingDbgRecords just removes the ptr from a map.

} else {
// adoptDbgRecords will release any trailers.
Dest->adoptDbgRecords(Src, Last, true);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same behaviour as before the patch in the else branch.

}
assert(!Src->getTrailingDbgRecords());
} else {
// FIXME: can we use adoptDbgRecords here to reduce allocations?
Expand Down
52 changes: 52 additions & 0 deletions llvm/unittests/IR/BasicBlockDbgInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1525,4 +1525,56 @@ TEST(BasicBlockDbgInfoTest, DbgMoveToEnd) {
EXPECT_FALSE(Ret->hasDbgRecords());
}

TEST(BasicBlockDbgInfoTest, CloneTrailingRecordsToEmptyBlock) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(C, R"(
define i16 @foo(i16 %a) !dbg !6 {
entry:
%b = add i16 %a, 0
#dbg_value(i16 %b, !9, !DIExpression(), !11)
ret i16 0, !dbg !11
}
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!5}
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
!1 = !DIFile(filename: "t.ll", directory: "/")
!2 = !{}
!5 = !{i32 2, !"Debug Info Version", i32 3}
!6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
!7 = !DISubroutineType(types: !2)
!8 = !{!9}
!9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
!11 = !DILocation(line: 1, column: 1, scope: !6)
)");
ASSERT_TRUE(M);

Function *F = M->getFunction("foo");
BasicBlock &BB = F->getEntryBlock();
// Start with no trailing records.
ASSERT_FALSE(BB.getTrailingDbgRecords());

BasicBlock::iterator Ret = std::prev(BB.end());
BasicBlock::iterator B = std::prev(Ret);

// Delete terminator which has debug records: we now get trailing records.
Ret->eraseFromParent();
EXPECT_TRUE(BB.getTrailingDbgRecords());

BasicBlock *NewBB = BasicBlock::Create(C, "NewBB", F);
NewBB->splice(NewBB->end(), &BB, B, BB.end());

// The trailing records should've been absorbed into NewBB.
EXPECT_FALSE(BB.getTrailingDbgRecords());
EXPECT_TRUE(NewBB->getTrailingDbgRecords());
if (DbgMarker *Trailing = NewBB->getTrailingDbgRecords()) {
EXPECT_EQ(llvm::range_size(Trailing->getDbgRecordRange()), 1u);
// Drop the trailing records now, to prevent a cleanup assertion.
Trailing->eraseFromParent();
NewBB->deleteTrailingDbgRecords();
}
}

} // End anonymous namespace.
Loading