Skip to content

Commit 0fb5037

Browse files
authored
[RemoveDIs] Fix SIGSEGV caused by splitBasicBlock (#90312)
See `llvm/unittests/IR/BasicBlockDbgInfoTest.cpp` for a test case.
1 parent bafc5f4 commit 0fb5037

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

llvm/lib/IR/BasicBlock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,9 +1009,9 @@ void BasicBlock::spliceDebugInfoImpl(BasicBlock::iterator Dest, BasicBlock *Src,
10091009
// generate the iterator with begin() / getFirstInsertionPt(), it means
10101010
// any trailing debug-info at the end of the block would "normally" have
10111011
// been pushed in front of "First". Move it there now.
1012-
DbgMarker *FirstMarker = getMarker(First);
10131012
DbgMarker *TrailingDbgRecords = getTrailingDbgRecords();
10141013
if (TrailingDbgRecords) {
1014+
DbgMarker *FirstMarker = createMarker(First);
10151015
FirstMarker->absorbDebugValues(*TrailingDbgRecords, true);
10161016
TrailingDbgRecords->eraseFromParent();
10171017
deleteTrailingDbgRecords();

llvm/unittests/IR/BasicBlockDbgInfoTest.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,62 @@ TEST(BasicBlockDbgInfoTest, InsertAfterSelf) {
109109
UseNewDbgInfoFormat = false;
110110
}
111111

112+
TEST(BasicBlockDbgInfoTest, SplitBasicBlockBefore) {
113+
LLVMContext C;
114+
UseNewDbgInfoFormat = true;
115+
116+
std::unique_ptr<Module> M = parseIR(C, R"---(
117+
define dso_local void @func() #0 !dbg !10 {
118+
%1 = alloca i32, align 4
119+
tail call void @llvm.dbg.declare(metadata ptr %1, metadata !14, metadata !DIExpression()), !dbg !16
120+
store i32 2, ptr %1, align 4, !dbg !16
121+
ret void, !dbg !17
122+
}
123+
124+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #0
125+
126+
attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
127+
128+
!llvm.dbg.cu = !{!0}
129+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
130+
!llvm.ident = !{!9}
131+
132+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "dummy", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
133+
!1 = !DIFile(filename: "dummy", directory: "dummy")
134+
!2 = !{i32 7, !"Dwarf Version", i32 5}
135+
!3 = !{i32 2, !"Debug Info Version", i32 3}
136+
!4 = !{i32 1, !"wchar_size", i32 4}
137+
!5 = !{i32 8, !"PIC Level", i32 2}
138+
!6 = !{i32 7, !"PIE Level", i32 2}
139+
!7 = !{i32 7, !"uwtable", i32 2}
140+
!8 = !{i32 7, !"frame-pointer", i32 2}
141+
!9 = !{!"dummy"}
142+
!10 = distinct !DISubprogram(name: "func", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !13)
143+
!11 = !DISubroutineType(types: !12)
144+
!12 = !{null}
145+
!13 = !{}
146+
!14 = !DILocalVariable(name: "a", scope: !10, file: !1, line: 2, type: !15)
147+
!15 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
148+
!16 = !DILocation(line: 2, column: 6, scope: !10)
149+
!17 = !DILocation(line: 3, column: 2, scope: !10)
150+
)---");
151+
ASSERT_TRUE(M);
152+
153+
M->convertToNewDbgValues();
154+
155+
Function *F = M->getFunction("func");
156+
157+
BasicBlock &BB = F->getEntryBlock();
158+
auto I = std::prev(BB.end(), 2);
159+
BB.splitBasicBlockBefore(I, "before");
160+
161+
BasicBlock &BBBefore = F->getEntryBlock();
162+
auto I2 = std::prev(BBBefore.end(), 2);
163+
ASSERT_TRUE(I2->hasDbgRecords());
164+
165+
UseNewDbgInfoFormat = false;
166+
}
167+
112168
TEST(BasicBlockDbgInfoTest, MarkerOperations) {
113169
LLVMContext C;
114170
UseNewDbgInfoFormat = true;

0 commit comments

Comments
 (0)