Skip to content

Commit

Permalink
[MLIR][LLVM] Fix debug intrinsic import (#82637)
Browse files Browse the repository at this point in the history
This revision handles the case that the translation of a scope fails due
to cyclic metadata. This mainly affects the import of debug intrinsics
that indirectly take such a scope as metadata argument (e.g. via local
variable or label metadata). This commit ensures we drop intrinsics with
such a dependency on cyclic metadata.
  • Loading branch information
gysit authored Feb 23, 2024
1 parent bbdc62e commit 335d34d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 17 deletions.
6 changes: 5 additions & 1 deletion mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,11 @@ def LLVM_DbgLabelOp : LLVM_IntrOp<"dbg.label", [], [], [], 0> {
});
}];
let mlirBuilder = [{
$_op = $_builder.create<$_qualCppClassName>($_location, $_label_attr($label));
DILabelAttr labelAttr = $_label_attr($label);
// Drop the intrinsic if the label translation fails due to cylic metadata.
if (!labelAttr)
return success();
$_op = $_builder.create<$_qualCppClassName>($_location, labelAttr);
}];
let assemblyFormat = "$label attr-dict";
}
Expand Down
35 changes: 24 additions & 11 deletions mlir/lib/Target/LLVMIR/DebugImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,31 @@ DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
}

DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
return DILabelAttr::get(context, translate(node->getScope()),
// Return nullptr if the scope or type is a cyclic dependency.
DIScopeAttr scope = translate(node->getScope());
if (node->getScope() && !scope)
return nullptr;
return DILabelAttr::get(context, scope,
getStringAttrOrNull(node->getRawName()),
translate(node->getFile()), node->getLine());
}

DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
return DILexicalBlockAttr::get(context, translate(node->getScope()),
translate(node->getFile()), node->getLine(),
node->getColumn());
// Return nullptr if the scope or type is a cyclic dependency.
DIScopeAttr scope = translate(node->getScope());
if (node->getScope() && !scope)
return nullptr;
return DILexicalBlockAttr::get(context, scope, translate(node->getFile()),
node->getLine(), node->getColumn());
}

DILexicalBlockFileAttr
DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
return DILexicalBlockFileAttr::get(context, translate(node->getScope()),
translate(node->getFile()),
// Return nullptr if the scope or type is a cyclic dependency.
DIScopeAttr scope = translate(node->getScope());
if (node->getScope() && !scope)
return nullptr;
return DILexicalBlockFileAttr::get(context, scope, translate(node->getFile()),
node->getDiscriminator());
}

Expand All @@ -135,11 +145,14 @@ DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
}

DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
return DILocalVariableAttr::get(context, translate(node->getScope()),
getStringAttrOrNull(node->getRawName()),
translate(node->getFile()), node->getLine(),
node->getArg(), node->getAlignInBits(),
translate(node->getType()));
// Return nullptr if the scope or type is a cyclic dependency.
DIScopeAttr scope = translate(node->getScope());
if (node->getScope() && !scope)
return nullptr;
return DILocalVariableAttr::get(
context, scope, getStringAttrOrNull(node->getRawName()),
translate(node->getFile()), node->getLine(), node->getArg(),
node->getAlignInBits(), translate(node->getType()));
}

DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
Expand Down
9 changes: 7 additions & 2 deletions mlir/lib/Target/LLVMIR/ModuleImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,13 @@ ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
// TODO: find a way to support this case.
if (isMetadataKillLocation(dbgIntr))
return emitUnsupportedWarning();
// Drop debug intrinsics if the associated variable information cannot be
// translated due to cyclic debug metadata.
// TODO: Support cyclic debug metadata.
DILocalVariableAttr localVariableAttr =
matchLocalVariableAttr(dbgIntr->getArgOperand(1));
if (!localVariableAttr)
return emitUnsupportedWarning();
FailureOr<Value> argOperand = convertMetadataValue(dbgIntr->getArgOperand(0));
if (failed(argOperand))
return emitError(loc) << "failed to convert a debug intrinsic operand: "
Expand All @@ -1991,8 +1998,6 @@ ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
} else {
builder.setInsertionPointAfterValue(*argOperand);
}
DILocalVariableAttr localVariableAttr =
matchLocalVariableAttr(dbgIntr->getArgOperand(1));
auto locationExprAttr =
debugImporter->translateExpression(dbgIntr->getExpression());
Operation *op =
Expand Down
40 changes: 37 additions & 3 deletions mlir/test/Target/LLVMIR/Import/import-failure.ll
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ define void @unhandled_intrinsic() gc "example" {

; // -----

; Check that debug intrinsics with an unsupported argument are dropped.

declare void @llvm.dbg.value(metadata, metadata, metadata)

; CHECK: import-failure.ll
; 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
; CHECK-SAME: warning: dropped intrinsic: call void @llvm.dbg.value(metadata !DIArgList(i64 %{{.*}}, 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))
; CHECK: import-failure.ll
; CHECK-SAME: warning: dropped intrinsic: call void @llvm.dbg.value(metadata !6, metadata !3, metadata !DIExpression()), !dbg !5
define void @dropped_instruction(i64 %arg1) {
; CHECK-SAME: warning: dropped intrinsic: call void @llvm.dbg.value(metadata !6, metadata !3, metadata !DIExpression())
define void @unsupported_argument(i64 %arg1) {
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
call void @llvm.dbg.value(metadata !6, metadata !3, metadata !DIExpression()), !dbg !5
ret void
Expand All @@ -83,6 +85,38 @@ define void @dropped_instruction(i64 %arg1) {

; // -----

; Check that debug intrinsics that depend on cyclic metadata are dropped.

declare void @llvm.dbg.value(metadata, metadata, metadata)

; CHECK: import-failure.ll
; CHECK-SAME: warning: dropped instruction: call void @llvm.dbg.label(metadata !{{.*}})
; CHECK: import-failure.ll
; CHECK-SAME: warning: dropped intrinsic: call void @llvm.dbg.value(metadata i64 %{{.*}}, metadata !3, metadata !DIExpression())
define void @cylic_metadata(i64 %arg1) {
call void @llvm.dbg.value(metadata i64 %arg1, metadata !10, metadata !DIExpression()), !dbg !14
call void @llvm.dbg.label(metadata !13), !dbg !14
ret void
}

!llvm.dbg.cu = !{!1}
!llvm.module.flags = !{!0}
!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = distinct !DICompileUnit(language: DW_LANG_C, file: !2)
!2 = !DIFile(filename: "import-failure.ll", directory: "/")
!3 = !DICompositeType(tag: DW_TAG_array_type, size: 42, baseType: !4)
!4 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !3)
!5 = distinct !DISubprogram(name: "class_method", scope: !2, file: !2, type: !6, spFlags: DISPFlagDefinition, unit: !1)
!6 = !DISubroutineType(types: !7)
!7 = !{!3}
!10 = !DILocalVariable(scope: !5, name: "arg1", file: !2, line: 1, arg: 1, align: 64);
!11 = !DILexicalBlock(scope: !5)
!12 = !DILexicalBlockFile(scope: !11, discriminator: 0)
!13 = !DILabel(scope: !12, name: "label", file: !2, line: 42)
!14 = !DILocation(line: 1, column: 2, scope: !5)

; // -----

; global_dtors with non-null data fields cannot be represented in MLIR.
; CHECK: <unknown>
; CHECK-SAME: error: unhandled global variable: @llvm.global_dtors
Expand Down

0 comments on commit 335d34d

Please sign in to comment.