Skip to content

Commit 667a195

Browse files
committed
[mlir][LLVM] Fix dbg intrinsic import of killed locations
This commit ensures that debug intrinsics of killed variables do not cause a crash of the importer. Killed locations are usually undef constants, but in infrequent cases can also be metadata nodes, which caused problems. Reviewed By: zero9178 Differential Revision: https://reviews.llvm.org/D157724
1 parent ad648c9 commit 667a195

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,18 @@ LogicalResult ModuleImport::processFunction(llvm::Function *func) {
17501750
return success();
17511751
}
17521752

1753+
/// Checks if `dbgIntr` is a kill location that holds metadata instead of an SSA
1754+
/// value.
1755+
static bool isMetadataKillLocation(llvm::DbgVariableIntrinsic *dbgIntr) {
1756+
if (!dbgIntr->isKillLocation())
1757+
return false;
1758+
llvm::Value *value = dbgIntr->getArgOperand(0);
1759+
auto *nodeAsVal = dyn_cast<llvm::MetadataAsValue>(value);
1760+
if (!nodeAsVal)
1761+
return false;
1762+
return !isa<llvm::ValueAsMetadata>(nodeAsVal->getMetadata());
1763+
}
1764+
17531765
LogicalResult
17541766
ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
17551767
DominanceInfo &domInfo) {
@@ -1763,9 +1775,15 @@ ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
17631775
// TODO: Support debug intrinsics that evaluate a debug expression.
17641776
if (dbgIntr->hasArgList() || dbgIntr->getExpression()->getNumElements() != 0)
17651777
return emitUnsupportedWarning();
1778+
// Kill locations can have metadata nodes as location operand. This
1779+
// cannot be converted to poison as the type cannot be reconstructed.
1780+
// TODO: find a way to support this case.
1781+
if (isMetadataKillLocation(dbgIntr))
1782+
return emitUnsupportedWarning();
17661783
FailureOr<Value> argOperand = convertMetadataValue(dbgIntr->getArgOperand(0));
17671784
if (failed(argOperand))
1768-
return failure();
1785+
return emitError(loc) << "failed to convert a debug intrinsic operand: "
1786+
<< diag(*dbgIntr);
17691787

17701788
// Ensure that the debug instrinsic is inserted right after its operand is
17711789
// defined. Otherwise, the operand might not necessarily dominate the

mlir/test/Target/LLVMIR/Import/import-failure.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ declare void @llvm.dbg.value(metadata, metadata, metadata)
6565
; CHECK-SAME: warning: dropped intrinsic: call void @llvm.dbg.value(metadata i64 %arg1, metadata !3, metadata !DIExpression(DW_OP_plus_uconst, 42, DW_OP_stack_value)), !dbg !5
6666
; CHECK: import-failure.ll
6767
; CHECK-SAME: warning: dropped intrinsic: call void @llvm.dbg.value(metadata !DIArgList(i64 %arg1, i64 undef), metadata !3, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_constu, 1, DW_OP_mul, DW_OP_plus, DW_OP_stack_value)), !dbg !5
68+
; CHECK: import-failure.ll
69+
; CHECK-SAME: warning: dropped intrinsic: call void @llvm.dbg.value(metadata !6, metadata !3, metadata !DIExpression()), !dbg !5
6870
define void @dropped_instruction(i64 %arg1) {
6971
call void @llvm.dbg.value(metadata i64 %arg1, metadata !3, metadata !DIExpression(DW_OP_plus_uconst, 42, DW_OP_stack_value)), !dbg !5
7072
call void @llvm.dbg.value(metadata !DIArgList(i64 %arg1, i64 undef), metadata !3, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_constu, 1, DW_OP_mul, DW_OP_plus, DW_OP_stack_value)), !dbg !5
73+
call void @llvm.dbg.value(metadata !6, metadata !3, metadata !DIExpression()), !dbg !5
7174
ret void
7275
}
7376

@@ -79,6 +82,7 @@ define void @dropped_instruction(i64 %arg1) {
7982
!3 = !DILocalVariable(scope: !4, name: "arg1", file: !2, line: 1, arg: 1, align: 64);
8083
!4 = distinct !DISubprogram(name: "intrinsic", scope: !2, file: !2, spFlags: DISPFlagDefinition, unit: !1)
8184
!5 = !DILocation(line: 1, column: 2, scope: !4)
85+
!6 = !{}
8286

8387
; // -----
8488

0 commit comments

Comments
 (0)