https://github.com/OCHyams created https://github.com/llvm/llvm-project/pull/106952
Please can we backport 43661a1214353ea1773a711f403f8d1118e9ca0f (and 7ffe67c17c524c2d3056c0721a33c7012dce3061) into the next dot release. Replaces #106691 - this one includes a follow-up fix in 7ffe67c17c524c2d3056c0721a33c7012dce3061. I couldn't create two separate requests because it creates conflicts. >From 70d00400165ee76199a1e4565fdebb6d84d2eec7 Mon Sep 17 00:00:00 2001 From: Orlando Cazalet-Hyams <orlando.hy...@sony.com> Date: Thu, 29 Aug 2024 14:12:02 +0100 Subject: [PATCH 1/2] [RemoveDIs] Fix spliceDebugInfo splice-to-end edge case (#105671) Fix #105571 which demonstrates an end() iterator dereference when performing a non-empty splice to end() from a region that ends at Src::end(). Rather than calling Instruction::adoptDbgRecords from Dest, create a marker (which takes an iterator) and absorbDebugValues onto that. The "absorb" variant doesn't clean up the source marker, which in this case we know is a trailing marker, so we have to do that manually. (cherry picked from commit 43661a1214353ea1773a711f403f8d1118e9ca0f) --- llvm/lib/IR/BasicBlock.cpp | 12 ++++- llvm/unittests/IR/BasicBlockDbgInfoTest.cpp | 54 +++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp index 0a9498f051cb59..46896d3cdf7d50 100644 --- a/llvm/lib/IR/BasicBlock.cpp +++ b/llvm/lib/IR/BasicBlock.cpp @@ -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()); + createMarker(Dest)->absorbDebugValues(*FromLast, true); + FromLast->eraseFromParent(); + Src->deleteTrailingDbgRecords(); + } else { + // adoptDbgRecords will release any trailers. + Dest->adoptDbgRecords(Src, Last, true); + } assert(!Src->getTrailingDbgRecords()); } else { // FIXME: can we use adoptDbgRecords here to reduce allocations? diff --git a/llvm/unittests/IR/BasicBlockDbgInfoTest.cpp b/llvm/unittests/IR/BasicBlockDbgInfoTest.cpp index 835780e63aaf4f..5615a4493d20a1 100644 --- a/llvm/unittests/IR/BasicBlockDbgInfoTest.cpp +++ b/llvm/unittests/IR/BasicBlockDbgInfoTest.cpp @@ -1525,4 +1525,58 @@ 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 (NewBB->getTrailingDbgRecords()) { + EXPECT_EQ( + llvm::range_size(NewBB->getTrailingDbgRecords()->getDbgRecordRange()), + 1u); + } + + // Drop the trailing records now, to prevent a cleanup assertion. + NewBB->deleteTrailingDbgRecords(); +} + } // End anonymous namespace. >From a0dadeb3a4bd7332046b9af811646b730eaec9d1 Mon Sep 17 00:00:00 2001 From: Orlando Cazalet-Hyams <orlando.hy...@sony.com> Date: Fri, 30 Aug 2024 13:44:42 +0100 Subject: [PATCH 2/2] [RemoveDIs] Fix asan-identified leak in unittest https://github.com/llvm/llvm-project/pull/106691#issuecomment-2320960847 (cherry picked from commit a80307d0a271223136e0845571606400fb41b2ee) --- llvm/unittests/IR/BasicBlockDbgInfoTest.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/llvm/unittests/IR/BasicBlockDbgInfoTest.cpp b/llvm/unittests/IR/BasicBlockDbgInfoTest.cpp index 5615a4493d20a1..5ce14d3f6b9cef 100644 --- a/llvm/unittests/IR/BasicBlockDbgInfoTest.cpp +++ b/llvm/unittests/IR/BasicBlockDbgInfoTest.cpp @@ -1569,14 +1569,12 @@ TEST(BasicBlockDbgInfoTest, CloneTrailingRecordsToEmptyBlock) { // The trailing records should've been absorbed into NewBB. EXPECT_FALSE(BB.getTrailingDbgRecords()); EXPECT_TRUE(NewBB->getTrailingDbgRecords()); - if (NewBB->getTrailingDbgRecords()) { - EXPECT_EQ( - llvm::range_size(NewBB->getTrailingDbgRecords()->getDbgRecordRange()), - 1u); + 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(); } - - // Drop the trailing records now, to prevent a cleanup assertion. - NewBB->deleteTrailingDbgRecords(); } } // End anonymous namespace. _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits