Skip to content

Commit 82f512a

Browse files
abidhcjdb
authored andcommitted
[mlir][llvmir][debug] Correctly generate location for phi nodes. (llvm#105534)
In [convertBlockImpl](https://github.com/llvm/llvm-project/blob/87eeed1f0ebe57abffde560c25dd9829dc6038f3/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp#L959), 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 da02ef3 commit 82f512a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
llvm.func @test_phi_locations(%arg0: !llvm.ptr) {
4+
%0 = llvm.mlir.constant(1 : i64) : i64 loc(#loc1)
5+
%1 = llvm.mlir.constant(100 : i32) : i32 loc(#loc1)
6+
llvm.br ^bb1(%1, %0 : i32, i64) loc(#loc1)
7+
^bb1(%2: i32 loc(#loc2), %3: i64 loc(#loc3)):
8+
%4 = llvm.icmp "sgt" %3, %0 : i64 loc(#loc1)
9+
llvm.cond_br %4, ^bb2, ^bb1(%2, %3 : i32, i64) loc(#loc1)
10+
^bb2:
11+
llvm.return loc(#loc1)
12+
} loc(#loc4)
13+
14+
#file = #llvm.di_file<"test.f90" in "">
15+
#cu = #llvm.di_compile_unit<id = distinct[0]<>,
16+
sourceLanguage = DW_LANG_Fortran95, file = #file, isOptimized = false,
17+
emissionKind = Full>
18+
#sp_ty = #llvm.di_subroutine_type<callingConvention = DW_CC_normal>
19+
#sp = #llvm.di_subprogram<id = distinct[1]<>, compileUnit = #cu, scope = #file,
20+
name = "test_phi_locations", file = #file, subprogramFlags = Definition,
21+
type = #sp_ty>
22+
23+
#loc1 = loc("test.f90":15:22)
24+
#loc2 = loc("test.f90":8:2)
25+
#loc3 = loc("test.f90":9:5)
26+
#loc4 = loc(fused<#sp>[#loc1])
27+
28+
// CHECK-LABEL: define void @test_phi_locations
29+
// CHECK: phi i32{{.*}}!dbg ![[LOC1:[0-9]+]]
30+
// CHECK: phi i64{{.*}}!dbg ![[LOC2:[0-9]+]]
31+
// CHECK: ![[LOC1]] = !DILocation(line: 8, column: 2{{.*}})
32+
// CHECK: ![[LOC2]] = !DILocation(line: 9, column: 5{{.*}})

0 commit comments

Comments
 (0)