Skip to content

Commit e7e242e

Browse files
[MLIR][LLVM] Fix debug value/declare import in face of landing pads (#132871)
Debug value/declare operations imported before landing pad operations at the bb start break invoke op verification: ``` error: first operation in unwind destination should be a llvm.landingpad operation ``` This this issue by making the placement slightly more smart.
1 parent 9aecbdf commit e7e242e

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2512,7 +2512,19 @@ ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
25122512
Block *dominatedBlock = (*dominatedBlocks.begin())->getBlock();
25132513
builder.setInsertionPoint(dominatedBlock->getTerminator());
25142514
} else {
2515-
builder.setInsertionPointAfterValue(*argOperand);
2515+
Value insertPt = *argOperand;
2516+
if (auto blockArg = dyn_cast<BlockArgument>(*argOperand)) {
2517+
// The value might be coming from a phi node and is now a block argument,
2518+
// which means the insertion point is set to the start of the block. If
2519+
// this block is a target destination of an invoke, the insertion point
2520+
// must happen after the landing pad operation.
2521+
Block *insertionBlock = argOperand->getParentBlock();
2522+
if (!insertionBlock->empty() &&
2523+
isa<LandingpadOp>(insertionBlock->front()))
2524+
insertPt = cast<LandingpadOp>(insertionBlock->front()).getRes();
2525+
}
2526+
2527+
builder.setInsertionPointAfterValue(insertPt);
25162528
}
25172529
auto locationExprAttr =
25182530
debugImporter->translateExpression(dbgIntr->getExpression());

mlir/test/Target/LLVMIR/Import/exception.ll

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: mlir-translate -import-llvm %s | FileCheck %s
1+
; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
22

33
@_ZTIi = external dso_local constant ptr
44
@_ZTIii= external dso_local constant ptr
@@ -148,3 +148,45 @@ exit:
148148
; CHECK: llvm.return
149149
ret void
150150
}
151+
152+
; // -----
153+
154+
declare i32 @__gxx_personality_v0(...)
155+
declare void @foo(ptr)
156+
157+
; CHECK-LABEL: @invokeLandingpad
158+
define i32 @invokeLandingpad(ptr %addr) personality ptr @__gxx_personality_v0 {
159+
entry:
160+
%1 = alloca i8
161+
%zzz = load i32, ptr %addr, align 4
162+
invoke void @foo(ptr %1) to label %bb1 unwind label %bb2
163+
164+
bb1:
165+
%yyy = load i32, ptr %addr, align 4
166+
invoke void @foo(ptr %1) to label %bb3 unwind label %bb2
167+
168+
; CHECK: ^bb{{.*}}(%[[ARG:.*]]: i32):
169+
bb2:
170+
%phi_var = phi i32 [ %zzz, %entry ], [ %yyy, %bb1 ], !dbg !5
171+
; CHECK: llvm.landingpad cleanup : !llvm.struct<(ptr, i32)>
172+
; CHECK-NEXT: llvm.intr.dbg.value #di_local_variable #llvm.di_expression<[DW_OP_LLVM_fragment(64, 64)]> = %[[ARG]] : i32
173+
%3 = landingpad { ptr, i32 } cleanup, !dbg !5
174+
#dbg_value(i32 %phi_var, !8, !DIExpression(DW_OP_LLVM_fragment, 64, 64), !7)
175+
br label %bb3
176+
177+
bb3:
178+
; CHECK: llvm.return %{{[0-9]+}} : i32
179+
ret i32 1
180+
}
181+
182+
!llvm.dbg.cu = !{!1}
183+
!llvm.module.flags = !{!0}
184+
!0 = !{i32 2, !"Debug Info Version", i32 3}
185+
!1 = distinct !DICompileUnit(language: DW_LANG_C, file: !2)
186+
!2 = !DIFile(filename: "landingpad.ll", directory: "/")
187+
!3 = distinct !DISubprogram(name: "instruction_loc", scope: !2, file: !2, spFlags: DISPFlagDefinition, unit: !1)
188+
!4 = distinct !DISubprogram(name: "callee", scope: !2, file: !2, spFlags: DISPFlagDefinition, unit: !1)
189+
!5 = !DILocation(line: 1, column: 2, scope: !3)
190+
!6 = !DILocation(line: 2, column: 2, scope: !3)
191+
!7 = !DILocation(line: 7, column: 4, scope: !4, inlinedAt: !6)
192+
!8 = !DILocalVariable(scope: !4, name: "size")

0 commit comments

Comments
 (0)