Skip to content

Commit 3e96070

Browse files
authored
[MLIR][LLVM] Avoid exporting broken debug intrinsics without a location (#70643)
LLVM IR does not allow debug intrinsics without a debug attachment. The location export can fail the export of a location due to multiple reasons. To deal with this, this commit adds a check to the debug intrinsic's LLVM builders, that skips them, if the location is `nullptr`. Fixes #60222
1 parent 7b2e009 commit 3e96070

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ def LLVM_CoroResumeOp : LLVM_IntrOp<"coro.resume", [], [], [], 0> {
526526
class LLVM_DbgIntrOp<string name, string argName, list<Trait> traits = []>
527527
: LLVM_IntrOp<name, [], [], traits, 0> {
528528
let llvmBuilder = [{
529+
// Debug intrinsics without debug locations are invalid.
530+
if(!builder.getCurrentDebugLocation())
531+
return success();
529532
llvm::Module *module = builder.GetInsertBlock()->getModule();
530533
llvm::LLVMContext &ctx = module->getContext();
531534
llvm::Function *fn =
@@ -566,6 +569,9 @@ def LLVM_DbgLabelOp : LLVM_IntrOp<"dbg.label", [], [], [], 0> {
566569
let summary = "Relates the program to a debug information label.";
567570
let arguments = (ins LLVM_DILabelAttr:$label);
568571
let llvmBuilder = [{
572+
// Debug intrinsics without debug locations are invalid.
573+
if(!builder.getCurrentDebugLocation())
574+
return success();
569575
llvm::Module *module = builder.GetInsertBlock()->getModule();
570576
llvm::LLVMContext &ctx = module->getContext();
571577
llvm::Function *fn =

mlir/test/Target/LLVMIR/llvmir-debug.mlir

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,34 @@ llvm.func @func_without_subprogram(%0 : i32) {
232232
// CHECK: ![[FILE:.*]] = !DIFile(filename: "foo.mlir", directory: "/test/")
233233
// CHECK-DAG: ![[FUNC:.*]] = distinct !DISubprogram(name: "func", scope: ![[FILE]]
234234
// CHECK-DAG: ![[VAR_LOC]] = !DILocalVariable(name: "a", scope: ![[FUNC]], file: ![[FILE]]
235+
236+
// -----
237+
238+
// Ensures that debug intrinsics without a valid location are not exported to
239+
// avoid broken LLVM IR.
240+
241+
#di_file = #llvm.di_file<"foo.mlir" in "/test/">
242+
#di_compile_unit = #llvm.di_compile_unit<
243+
sourceLanguage = DW_LANG_C, file = #di_file, producer = "MLIR",
244+
isOptimized = true, emissionKind = Full
245+
>
246+
#di_subprogram = #llvm.di_subprogram<
247+
compileUnit = #di_compile_unit, scope = #di_file, name = "outer_func",
248+
file = #di_file, subprogramFlags = "Definition|Optimized"
249+
>
250+
#di_local_variable = #llvm.di_local_variable<scope = #di_subprogram, name = "a">
251+
#declared_var = #llvm.di_local_variable<scope = #di_subprogram, name = "alloc">
252+
#di_label = #llvm.di_label<scope = #di_subprogram, name = "label", file = #di_file, line = 42>
253+
254+
// CHECK-LABEL: define i32 @dbg_intrinsics_with_no_location(
255+
llvm.func @dbg_intrinsics_with_no_location(%arg0: i32) -> (i32) {
256+
%allocCount = llvm.mlir.constant(1 : i32) : i32
257+
%alloc = llvm.alloca %allocCount x i64 : (i32) -> !llvm.ptr
258+
// CHECK-NOT: @llvm.dbg.value
259+
llvm.intr.dbg.value #di_local_variable = %arg0 : i32
260+
// CHECK-NOT: @llvm.dbg.declare
261+
llvm.intr.dbg.declare #declared_var = %alloc : !llvm.ptr
262+
// CHECK-NOT: @llvm.dbg.label
263+
llvm.intr.dbg.label #di_label
264+
llvm.return %arg0 : i32
265+
}

0 commit comments

Comments
 (0)