Skip to content

Commit 2c90ebf

Browse files
authored
[OMPIRBuilder][debug] Don't drop debug info for loop constructs. (#144393)
In OMPIRBuilder, we have many cases where we don't handle the debug location correctly while changing the location or insertion point. This is one of those cases. Please see the following test program. ``` program main implicit none integer i, j integer array(16384) !$omp target teams distribute DO i=1,16384 !$omp parallel do DO j=1,16384 array(j) = i ENDDO !$omp end parallel do ENDDO !$omp end target teams distribute print *, array end program main ``` When tried to compile with the follownig command `flang -g -O2 -fopenmp test.f90 -o test --offload-arch=gfx90a` will fail in the verification with the following errors: `!dbg attachment points at wrong subprogram for function` This happens because we were dropping the debug location in the createCanonicalLoop and the call to the functions like `__kmpc_distribute_static_4u` get generated without a debug location. When it gets inlined, the locations inside it are not adjusted as the call instruction does not have the debug locations (`llvm/lib/Transforms/Utils/InlineFunction.cpp:fixupLineNumbers`). Later Verifier finds that the caller have instructions with debug locations that point to another function and fails. The fix is simple to not drop the debug location.
1 parent 308b97a commit 2c90ebf

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4184,7 +4184,11 @@ Expected<CanonicalLoopInfo *> OpenMPIRBuilder::createCanonicalLoop(
41844184
Value *IndVar = Builder.CreateAdd(Span, Start);
41854185
return BodyGenCB(Builder.saveIP(), IndVar);
41864186
};
4187-
LocationDescription LoopLoc = ComputeIP.isSet() ? Loc.IP : Builder.saveIP();
4187+
LocationDescription LoopLoc =
4188+
ComputeIP.isSet()
4189+
? Loc
4190+
: LocationDescription(Builder.saveIP(),
4191+
Builder.getCurrentDebugLocation());
41884192
return createCanonicalLoop(LoopLoc, BodyGen, TripCount, Name);
41894193
}
41904194

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<"dlti.alloca_memory_space", 5 : ui32>>, llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_target_device = true} {
4+
omp.private {type = private} @_QFEj_private_i32 : i32 loc(#loc1)
5+
omp.private {type = private} @_QFEi_private_i32 : i32 loc(#loc1)
6+
llvm.func @test() {
7+
%3 = llvm.mlir.constant(1 : i64) : i64
8+
%4 = llvm.alloca %3 x i32 {bindc_name = "j"} : (i64) -> !llvm.ptr<5> loc(#loc4)
9+
%5 = llvm.addrspacecast %4 : !llvm.ptr<5> to !llvm.ptr loc(#loc4)
10+
%6 = llvm.mlir.constant(1 : i64) : i64
11+
%7 = llvm.alloca %6 x i32 {bindc_name = "i"} : (i64) -> !llvm.ptr<5> loc(#loc4)
12+
%8 = llvm.addrspacecast %7 : !llvm.ptr<5> to !llvm.ptr
13+
%9 = llvm.mlir.constant(16383 : index) : i64
14+
%10 = llvm.mlir.constant(0 : index) : i64
15+
%11 = llvm.mlir.constant(1 : index) : i64
16+
%12 = llvm.mlir.constant(16384 : i32) : i32
17+
%14 = llvm.mlir.addressof @_QFEarray : !llvm.ptr
18+
%18 = omp.map.info var_ptr(%8 : !llvm.ptr, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr {name = "i"} loc(#loc3)
19+
%20 = omp.map.info var_ptr(%5 : !llvm.ptr, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr {name = "j"} loc(#loc3)
20+
%22 = omp.map.bounds lower_bound(%10 : i64) upper_bound(%9 : i64) extent(%9 : i64) stride(%11 : i64) start_idx(%11 : i64) loc(#loc3)
21+
%23 = omp.map.info var_ptr(%14 : !llvm.ptr, !llvm.array<16384 x i32>) map_clauses(implicit, tofrom) capture(ByRef) bounds(%22) -> !llvm.ptr {name = "array"} loc(#loc3)
22+
%24 = omp.map.info var_ptr(%8 : !llvm.ptr, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr {name = "i"} loc(#loc3)
23+
omp.target map_entries(%18 -> %arg0, %20 -> %arg2, %23 -> %arg4, %24 -> %arg5 : !llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr) {
24+
%25 = llvm.mlir.constant(1 : i32) : i32
25+
%27 = llvm.mlir.constant(16384 : i32) : i32
26+
omp.teams {
27+
omp.distribute private(@_QFEi_private_i32 %arg5 -> %arg6 : !llvm.ptr) {
28+
omp.loop_nest (%arg7) : i32 = (%25) to (%27) inclusive step (%25) {
29+
omp.parallel {
30+
omp.wsloop private(@_QFEj_private_i32 %arg2 -> %arg8 : !llvm.ptr) {
31+
omp.loop_nest (%arg9) : i32 = (%25) to (%27) inclusive step (%25) {
32+
llvm.store %arg9, %arg8 : i32, !llvm.ptr loc(#loc9)
33+
omp.yield
34+
} loc(#loc9)
35+
} loc(#loc9)
36+
omp.terminator loc(#loc9)
37+
} loc(#loc9)
38+
omp.yield loc(#loc9)
39+
} loc(#loc9)
40+
} loc(#loc9)
41+
omp.terminator loc(#loc9)
42+
} loc(#loc9)
43+
omp.terminator loc(#loc9)
44+
} loc(#loc9)
45+
llvm.return loc(#loc9)
46+
} loc(#loc14)
47+
llvm.mlir.global internal @_QFEarray() {addr_space = 0 : i32} : !llvm.array<16384 x i32> {
48+
%0 = llvm.mlir.zero : !llvm.array<16384 x i32>
49+
llvm.return %0 : !llvm.array<16384 x i32>
50+
} loc(#loc2)
51+
}
52+
#di_file = #llvm.di_file<"test.f90" in "">
53+
#di_null_type = #llvm.di_null_type
54+
#loc1 = loc("test.f90":4:23)
55+
#loc2 = loc("test.f90":4:15)
56+
#loc3 = loc("test.f90":1:7)
57+
#loc4 = loc("test.f90":4:18)
58+
#loc9 = loc("test.f90":13:11)
59+
#di_compile_unit = #llvm.di_compile_unit<id = distinct[0]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "flang", isOptimized = true, emissionKind = LineTablesOnly>
60+
#di_subroutine_type = #llvm.di_subroutine_type<callingConvention = DW_CC_program, types = #di_null_type>
61+
#di_subprogram = #llvm.di_subprogram<id = distinct[1]<>, compileUnit = #di_compile_unit, scope = #di_file, name = "main", file = #di_file, subprogramFlags = "Definition|Optimized|MainSubprogram", type = #di_subroutine_type>
62+
#loc14 = loc(fused<#di_subprogram>[#loc3])
63+
64+
65+
// CHECK: call void @__kmpc_distribute_static{{.*}}!dbg
66+

0 commit comments

Comments
 (0)