diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td --- a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td +++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td @@ -546,8 +546,16 @@ // Drop the intrinsic when its operand could not be converted. This can // happen for use before definition cases that are allowed for debug // intrinsics. + // TODO: Implement a two pass solution that translates the debug intrinsics + // after the entire function as been translated. if (failed(argOperand)) return success(); + + // Ensure that the debug instrinsic is inserted right after its operand is + // defined. Otherwise, the operand might not necessarily dominate the + // intrinsic. + OpBuilder::InsertionGuard guard($_builder); + $_builder.setInsertionPointAfterValue(*argOperand); $_op = $_builder.create<$_qualCppClassName>($_location, *argOperand, $_var_attr($varInfo)); }]; diff --git a/mlir/test/Target/LLVMIR/Import/debug-info.ll b/mlir/test/Target/LLVMIR/Import/debug-info.ll --- a/mlir/test/Target/LLVMIR/Import/debug-info.ll +++ b/mlir/test/Target/LLVMIR/Import/debug-info.ll @@ -233,27 +233,30 @@ ; // ----- +; NOTE: The debug intrinsics are reordered as a side-effect of the dominance- +; preserving measures needed to import LLVM IR. + ; CHECK: #[[FILE:.+]] = #llvm.di_file< ; CHECK: #[[$SP:.+]] = #llvm.di_subprogram< ; CHECK: #[[$LABEL:.+]] = #llvm.di_label -; CHECK: #[[$VAR0:.+]] = #llvm.di_local_variable ; CHECK: #[[$VAR1:.+]] = #llvm.di_local_variable +; CHECK: #[[$VAR0:.+]] = #llvm.di_local_variable ; CHECK-LABEL: @intrinsic ; CHECK-SAME: %[[ARG0:[a-zA-Z0-9]+]] ; CHECK-SAME: %[[ARG1:[a-zA-Z0-9]+]] define void @intrinsic(i64 %0, ptr %1) { + ; CHECK: llvm.intr.dbg.declare #[[$VAR1]] = %[[ARG1]] : !llvm.ptr loc(#[[LOC1:.+]]) ; CHECK: llvm.intr.dbg.value #[[$VAR0]] = %[[ARG0]] : i64 loc(#[[LOC0:.+]]) call void @llvm.dbg.value(metadata i64 %0, metadata !5, metadata !DIExpression()), !dbg !7 - ; CHECK: llvm.intr.dbg.declare #[[$VAR1]] = %[[ARG1]] : !llvm.ptr loc(#[[LOC1:.+]]) call void @llvm.dbg.declare(metadata ptr %1, metadata !6, metadata !DIExpression()), !dbg !9 ; CHECK: llvm.intr.dbg.label #[[$LABEL]] loc(#[[LOC1:.+]]) call void @llvm.dbg.label(metadata !10), !dbg !9 ret void } -; CHECK: #[[LOC0]] = loc(fused<#[[$SP]]>[{{.*}}]) ; CHECK: #[[LOC1]] = loc(fused<#[[$SP]]>[{{.*}}]) +; CHECK: #[[LOC0]] = loc(fused<#[[$SP]]>[{{.*}}]) declare void @llvm.dbg.value(metadata, metadata, metadata) declare void @llvm.dbg.declare(metadata, metadata, metadata) @@ -372,6 +375,40 @@ ; // ----- +; This test checks that broken dominance doesn't break the metadata import. + +; CHECK-LABEL: @dbg_broken_dominance +define void @dbg_broken_dominance(ptr %arg, i1 %cond) { + br i1 %cond, label %b1, label %b2 +b1: + br label %b3 +b2: + %dbg_arg = getelementptr double, ptr %arg, i64 16 + ; CHECK: llvm.getelementptr + ; CHECK-NEXT: llvm.intr.dbg.value + br label %b3 +b3: + call void @llvm.dbg.value(metadata ptr %dbg_arg, metadata !7, metadata !DIExpression()), !dbg !9 + ret void +} + +declare void @llvm.dbg.value(metadata, metadata, metadata) + +!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: "debug-info.ll", directory: "/") +!3 = !DICompositeType(tag: DW_TAG_class_type, name: "class_field", file: !2, line: 42, flags: DIFlagTypePassByReference | DIFlagNonTrivial, elements: !4) +!4 = !{!6} +!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !3, flags: DIFlagArtificial | DIFlagObjectPointer) +!6 = !DIDerivedType(tag: DW_TAG_member, name: "call_field", file: !2, baseType: !5) +!7 = !DILocalVariable(scope: !8, name: "var", file: !2, type: !5); +!8 = distinct !DISubprogram(name: "dbg_use_before_def", scope: !2, file: !2, spFlags: DISPFlagDefinition, unit: !1) +!9 = !DILocation(line: 1, column: 2, scope: !8) + +; // ----- + ; CHECK-DAG: #[[NAMESPACE:.+]] = #llvm.di_namespace ; CHECK-DAG: #[[SUBPROGRAM:.+]] = #llvm.di_subprogram