Skip to content

Commit 11fee8e

Browse files
committed
[mlir][llvmir][debug] Correctly generate location for phi nodes.
In convertBlockImpl, the debug location is set on the builder before the op is processed. This results in correct location being given to corresponding llvm instructions. But same is not done when phi nodes are created a few lines above. This result is phi nodes getting whatever the current debug location of the builder is. It can be nothing or in worst case a stale location. Fixed by calling SetCurrentDebugLocation before generating phi nodes.
1 parent d12250c commit 11fee8e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,8 @@ LogicalResult ModuleTranslation::convertBlockImpl(Block &bb,
947947
if (!isCompatibleType(wrappedType))
948948
return emitError(bb.front().getLoc(),
949949
"block argument does not have an LLVM type");
950+
builder.SetCurrentDebugLocation(
951+
debugTranslation->translateLoc(arg.getLoc(), subprogram));
950952
llvm::Type *type = convertType(wrappedType);
951953
llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors);
952954
mapValue(arg, phi);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
4+
module attributes {} {
5+
llvm.func @test(%arg0: !llvm.ptr) {
6+
%0 = llvm.mlir.constant(1 : i64) : i64 loc(#loc2)
7+
%1 = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr loc(#loc2)
8+
%3 = llvm.mlir.constant(100 : index) : i64 loc(#loc2)
9+
%7 = llvm.trunc %0 : i64 to i32 loc(#loc2)
10+
llvm.br ^bb1(%7, %3 : i32, i64) loc(#loc2)
11+
^bb1(%8: i32 loc(#loc4), %9: i64 loc(#loc5)): // 2 preds: ^bb0, ^bb2
12+
%10 = llvm.icmp "sgt" %9, %0 : i64 loc(#loc3)
13+
llvm.cond_br %10, ^bb2, ^bb3 loc(#loc3)
14+
^bb2: // pred: ^bb1
15+
%13 = llvm.load %1 : !llvm.ptr -> i32 loc(#loc3)
16+
%14 = llvm.add %13, %7 : i32 loc(#loc3)
17+
%15 = llvm.sub %9, %0 : i64 loc(#loc3)
18+
llvm.br ^bb1(%14, %15 : i32, i64) loc(#loc3)
19+
^bb3: // pred: ^bb1
20+
llvm.return loc(#loc3)
21+
} loc(#loc6)
22+
}
23+
24+
#int_ty = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "integer",
25+
sizeInBits = 32, encoding = DW_ATE_signed>
26+
#file = #llvm.di_file<"test.f90" in "">
27+
#cu = #llvm.di_compile_unit<id = distinct[0]<>,
28+
sourceLanguage = DW_LANG_Fortran95, file = #file, isOptimized = false,
29+
emissionKind = Full>
30+
#sp_ty = #llvm.di_subroutine_type<callingConvention = DW_CC_normal,
31+
types = #int_ty>
32+
#sp = #llvm.di_subprogram<id = distinct[1]<>, compileUnit = #cu, scope = #file,
33+
name = "test", file = #file, line = 1, scopeLine = 1,
34+
subprogramFlags = Definition, type = #sp_ty>
35+
36+
#loc1 = loc("test.f90":1:1)
37+
#loc2 = loc("test.f90":15:22)
38+
#loc3 = loc("test.f90":26:3)
39+
#loc4 = loc("test.f90":8:2)
40+
#loc5 = loc("test.f90":9:5)
41+
#loc6 = loc(fused<#sp>[#loc1])
42+
43+
// CHECK-LABEl: define void @test
44+
// CHECK: phi i32{{.*}}!dbg ![[LOC1:[0-9]+]]
45+
// CHECK: phi i64{{.*}}!dbg ![[LOC2:[0-9]+]]
46+
// CHECK: ![[LOC1]] = !DILocation(line: 8, column: 2{{.*}})
47+
// CHECK: ![[LOC2]] = !DILocation(line: 9, column: 5{{.*}})

0 commit comments

Comments
 (0)