Skip to content

Commit d2fe889

Browse files
authored
[KeyInstr] Don't propagate source atoms to new uncond br in splitBasicBlock (#139070)
splitBasicBlock inserts an unconditional branch in the "before" block to the "after" block. It copies the DebugLoc from the split point. Prevent it copying the source location atom. Add unittest. RFC: https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668
1 parent e9df48e commit d2fe889

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

llvm/lib/IR/BasicBlock.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName,
612612
this->getNextNode());
613613

614614
// Save DebugLoc of split point before invalidating iterator.
615-
DebugLoc Loc = I->getStableDebugLoc();
615+
DebugLoc Loc = I->getStableDebugLoc()->getWithoutAtom();
616616
// Move all of the specified instructions from the original basic block into
617617
// the new basic block.
618618
New->splice(New->end(), this, I, end());
@@ -641,7 +641,7 @@ BasicBlock *BasicBlock::splitBasicBlockBefore(iterator I, const Twine &BBName) {
641641

642642
BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(), this);
643643
// Save DebugLoc of split point before invalidating iterator.
644-
DebugLoc Loc = I->getDebugLoc();
644+
DebugLoc Loc = I->getDebugLoc()->getWithoutAtom();
645645
// Move all of the specified instructions from the original basic block into
646646
// the new basic block.
647647
New->splice(New->end(), this, begin(), I);

llvm/unittests/IR/BasicBlockDbgInfoTest.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,75 @@ TEST(BasicBlockDbgInfoTest, SplitBasicBlockBefore) {
149149
ASSERT_TRUE(I2->hasDbgRecords());
150150
}
151151

152+
TEST(BasicBlockDbgInfoTest, DropSourceAtomOnSplit) {
153+
LLVMContext C;
154+
std::unique_ptr<Module> M = parseIR(C, R"---(
155+
define dso_local void @func() !dbg !10 {
156+
%1 = alloca i32, align 4
157+
ret void, !dbg !DILocation(line: 3, column: 2, scope: !10, atomGroup: 1, atomRank: 1)
158+
}
159+
160+
!llvm.dbg.cu = !{!0}
161+
!llvm.module.flags = !{!2, !3}
162+
163+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "dummy", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
164+
!1 = !DIFile(filename: "dummy", directory: "dummy")
165+
!2 = !{i32 7, !"Dwarf Version", i32 5}
166+
!3 = !{i32 2, !"Debug Info Version", i32 3}
167+
!10 = distinct !DISubprogram(name: "func", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !13)
168+
!11 = !DISubroutineType(types: !12)
169+
!12 = !{null}
170+
!13 = !{}
171+
!14 = !DILocalVariable(name: "a", scope: !10, file: !1, line: 2, type: !15)
172+
!15 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
173+
)---");
174+
ASSERT_TRUE(M);
175+
176+
Function *F = M->getFunction("func");
177+
178+
// Test splitBasicBlockBefore.
179+
{
180+
BasicBlock &BB = F->back();
181+
// Split at `ret void`.
182+
BasicBlock *Before =
183+
BB.splitBasicBlockBefore(std::prev(BB.end(), 1), "before");
184+
const DebugLoc &BrToAfterDL = Before->getTerminator()->getDebugLoc();
185+
ASSERT_TRUE(BrToAfterDL);
186+
EXPECT_EQ(BrToAfterDL->getAtomGroup(), 0u);
187+
188+
BasicBlock *After = Before->getSingleSuccessor();
189+
ASSERT_TRUE(After);
190+
const DebugLoc &OrigTerminatorDL = After->getTerminator()->getDebugLoc();
191+
ASSERT_TRUE(OrigTerminatorDL);
192+
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
193+
EXPECT_EQ(OrigTerminatorDL->getAtomGroup(), 1u);
194+
#else
195+
EXPECT_EQ(OrigTerminatorDL->getAtomGroup(), 0u);
196+
#endif
197+
}
198+
199+
// Test splitBasicBlock.
200+
{
201+
BasicBlock &BB = F->back();
202+
// Split at `ret void`.
203+
BasicBlock *After = BB.splitBasicBlock(std::prev(BB.end(), 1), "before");
204+
205+
const DebugLoc &OrigTerminatorDL = After->getTerminator()->getDebugLoc();
206+
ASSERT_TRUE(OrigTerminatorDL);
207+
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
208+
EXPECT_EQ(OrigTerminatorDL->getAtomGroup(), 1u);
209+
#else
210+
EXPECT_EQ(OrigTerminatorDL->getAtomGroup(), 0u);
211+
#endif
212+
213+
BasicBlock *Before = After->getSinglePredecessor();
214+
ASSERT_TRUE(Before);
215+
const DebugLoc &BrToAfterDL = Before->getTerminator()->getDebugLoc();
216+
ASSERT_TRUE(BrToAfterDL);
217+
EXPECT_EQ(BrToAfterDL->getAtomGroup(), 0u);
218+
}
219+
}
220+
152221
TEST(BasicBlockDbgInfoTest, MarkerOperations) {
153222
LLVMContext C;
154223
std::unique_ptr<Module> M = parseIR(C, R"(

0 commit comments

Comments
 (0)