Skip to content

Commit 1bf7277

Browse files
committed
[OMPIRBuilder][debug] Fix debug info for variables in target region.
When a new function is created to handle OpenMP target region, the variables used in it are passed as arguments. The scope and the location of these variable contains still point to te parent function of the target region. Such variables will fail in Verifier as the scope of the variables will be different from the containing functions. Currently, flang is the only user of createOutlinedFunction and it does not generate any debug data for the the variables in the target region to avoid this error. When this PR is in, we should be able to remove this limit in the flang (and anyother client) and have the better debug experience for the target region. This PR changes the location and scope of the variables in the target region to point to correct entities. It is similar to what fixupDebugInfoPostExtraction does for CodeExtractor. I initially tried to re-use that function but found quickly that it would require quite a bit of re-factoring and additions before it could be used. It was much simpler to make the changes locally.
1 parent d0f472c commit 1bf7277

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include "llvm/IR/Function.h"
3939
#include "llvm/IR/GlobalVariable.h"
4040
#include "llvm/IR/IRBuilder.h"
41+
#include "llvm/IR/InstIterator.h"
42+
#include "llvm/IR/IntrinsicInst.h"
4143
#include "llvm/IR/LLVMContext.h"
4244
#include "llvm/IR/MDBuilder.h"
4345
#include "llvm/IR/Metadata.h"
@@ -6931,6 +6933,8 @@ static Expected<Function *> createOutlinedFunction(
69316933
? make_range(Func->arg_begin() + 1, Func->arg_end())
69326934
: Func->args();
69336935

6936+
DenseMap<Value *, std::tuple<Value *, unsigned>> ValueReplacementMap;
6937+
69346938
auto ReplaceValue = [](Value *Input, Value *InputCopy, Function *Func) {
69356939
// Things like GEP's can come in the form of Constants. Constants and
69366940
// ConstantExpr's do not have access to the knowledge of what they're
@@ -6972,6 +6976,7 @@ static Expected<Function *> createOutlinedFunction(
69726976
if (!AfterIP)
69736977
return AfterIP.takeError();
69746978
Builder.restoreIP(*AfterIP);
6979+
ValueReplacementMap[Input] = std::make_tuple(InputCopy, Arg.getArgNo());
69756980

69766981
// In certain cases a Global may be set up for replacement, however, this
69776982
// Global may be used in multiple arguments to the kernel, just segmented
@@ -7003,6 +7008,67 @@ static Expected<Function *> createOutlinedFunction(
70037008
for (auto Deferred : DeferredReplacement)
70047009
ReplaceValue(std::get<0>(Deferred), std::get<1>(Deferred), Func);
70057010

7011+
DenseMap<const MDNode *, MDNode *> Cache;
7012+
SmallDenseMap<DILocalVariable *, DILocalVariable *> RemappedVariables;
7013+
7014+
auto GetUpdatedDIVariable = [&](DILocalVariable *OldVar, unsigned arg) {
7015+
auto NewSP = Func->getSubprogram();
7016+
DILocalVariable *&NewVar = RemappedVariables[OldVar];
7017+
if (!NewVar) {
7018+
DILocalScope *NewScope = DILocalScope::cloneScopeForSubprogram(
7019+
*OldVar->getScope(), *NewSP, Builder.getContext(), Cache);
7020+
NewVar = llvm::DILocalVariable::get(
7021+
Builder.getContext(), NewScope, OldVar->getName(), OldVar->getFile(),
7022+
OldVar->getLine(), OldVar->getType(), arg, OldVar->getFlags(),
7023+
OldVar->getAlignInBits(), OldVar->getAnnotations());
7024+
}
7025+
return NewVar;
7026+
};
7027+
7028+
DISubprogram *NewSP = Func->getSubprogram();
7029+
if (NewSP) {
7030+
// The location and scope of variable intrinsics and records still point to
7031+
// the parent function of the target region. Update them.
7032+
for (Instruction &I : instructions(Func)) {
7033+
if (auto *DDI = dyn_cast<llvm::DbgVariableIntrinsic>(&I)) {
7034+
DILocalVariable *OldVar = DDI->getVariable();
7035+
unsigned ArgNo = OldVar->getArg();
7036+
for (auto Loc : DDI->location_ops()) {
7037+
auto Iter = ValueReplacementMap.find(Loc);
7038+
if (Iter != ValueReplacementMap.end()) {
7039+
DDI->replaceVariableLocationOp(Loc, std::get<0>(Iter->second));
7040+
ArgNo = std::get<1>(Iter->second) + 1;
7041+
}
7042+
}
7043+
DDI->setVariable(GetUpdatedDIVariable(OldVar, ArgNo));
7044+
}
7045+
for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
7046+
DILocalVariable *OldVar = DVR.getVariable();
7047+
unsigned ArgNo = OldVar->getArg();
7048+
for (auto Loc : DVR.location_ops()) {
7049+
auto Iter = ValueReplacementMap.find(Loc);
7050+
if (Iter != ValueReplacementMap.end()) {
7051+
DVR.replaceVariableLocationOp(Loc, std::get<0>(Iter->second));
7052+
ArgNo = std::get<1>(Iter->second) + 1;
7053+
}
7054+
}
7055+
DVR.setVariable(GetUpdatedDIVariable(OldVar, ArgNo));
7056+
}
7057+
}
7058+
// An extra argument is passed to the device. Create the debug data for it.
7059+
if (OMPBuilder.Config.isTargetDevice()) {
7060+
DICompileUnit *CU = NewSP->getUnit();
7061+
DIBuilder DB(*M, true, CU);
7062+
DIType *VoidPtrTy =
7063+
DB.createQualifiedType(dwarf::DW_TAG_pointer_type, nullptr);
7064+
DILocalVariable *Var = DB.createParameterVariable(
7065+
NewSP, "dyn_ptr", /*ArgNo*/ 1, NewSP->getFile(), /*LineNo=*/0,
7066+
VoidPtrTy, /*AlwaysPreserve=*/false, DINode::DIFlags::FlagArtificial);
7067+
auto Loc = DILocation::get(Func->getContext(), 0, 0, NewSP, 0);
7068+
DB.insertDeclare(&(*Func->arg_begin()), Var, DB.createExpression(), Loc,
7069+
&(*Func->begin()));
7070+
}
7071+
}
70067072
return Func;
70077073
}
70087074

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
#int_ty = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "integer",
4+
sizeInBits = 32, encoding = DW_ATE_signed>
5+
#real_ty = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "real",
6+
sizeInBits = 32, encoding = DW_ATE_float>
7+
#file = #llvm.di_file<"target.f90" in "">
8+
#di_null_type = #llvm.di_null_type
9+
#cu = #llvm.di_compile_unit<id = distinct[0]<>,
10+
sourceLanguage = DW_LANG_Fortran95, file = #file, isOptimized = false,
11+
emissionKind = Full>
12+
#array_ty = #llvm.di_composite_type<tag = DW_TAG_array_type,
13+
baseType = #int_ty, elements = #llvm.di_subrange<count = 10 : i64>>
14+
#sp_ty = #llvm.di_subroutine_type<callingConvention = DW_CC_program,
15+
types = #di_null_type>
16+
#g_var = #llvm.di_global_variable<scope = #cu, name = "arr",
17+
linkageName = "_QFEarr", file = #file, line = 4,
18+
type = #array_ty, isDefined = true>
19+
#g_var_expr = #llvm.di_global_variable_expression<var = #g_var>
20+
#sp = #llvm.di_subprogram<id = distinct[2]<>, compileUnit = #cu, scope = #file,
21+
name = "test", file = #file, subprogramFlags = "Definition", type = #sp_ty>
22+
#var_arr = #llvm.di_local_variable<scope = #sp,
23+
name = "arr", file = #file, line = 4, type = #array_ty>
24+
#var_i = #llvm.di_local_variable<scope = #sp,
25+
name = "i", file = #file, line = 13, type = #int_ty>
26+
#var_x = #llvm.di_local_variable<scope = #sp,
27+
name = "x", file = #file, line = 12, type = #real_ty>
28+
29+
module attributes {omp.is_target_device = true} {
30+
llvm.func @test() {
31+
%0 = llvm.mlir.constant(1 : i64) : i64
32+
%1 = llvm.alloca %0 x f32 : (i64) -> !llvm.ptr
33+
%4 = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr
34+
%6 = llvm.mlir.constant(9 : index) : i64
35+
%7 = llvm.mlir.constant(0 : index) : i64
36+
%8 = llvm.mlir.constant(1 : index) : i64
37+
%10 = llvm.mlir.constant(10 : index) : i64
38+
%11 = llvm.mlir.addressof @_QFEarr : !llvm.ptr
39+
%14 = omp.map.info var_ptr(%1 : !llvm.ptr, f32) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr
40+
%15 = omp.map.bounds lower_bound(%7 : i64) upper_bound(%6 : i64) extent(%10 : i64) stride(%8 : i64) start_idx(%8 : i64)
41+
%16 = omp.map.info var_ptr(%11 : !llvm.ptr, !llvm.array<10 x i32>) map_clauses(tofrom) capture(ByRef) bounds(%15) -> !llvm.ptr
42+
%17 = omp.map.info var_ptr(%4 : !llvm.ptr, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr
43+
omp.target map_entries(%14 -> %arg0, %16 -> %arg1, %17 -> %arg2 : !llvm.ptr, !llvm.ptr, !llvm.ptr) {
44+
llvm.intr.dbg.declare #var_x = %arg0 : !llvm.ptr
45+
llvm.intr.dbg.declare #var_arr = %arg1 : !llvm.ptr
46+
llvm.intr.dbg.declare #var_i = %arg2 : !llvm.ptr
47+
omp.terminator
48+
}
49+
llvm.return
50+
} loc(#loc3)
51+
llvm.mlir.global internal @_QFEarr() {addr_space = 0 : i32, dbg_exprs = [#g_var_expr]} : !llvm.array<10 x i32> {
52+
} loc(#loc4)
53+
}
54+
#loc1 = loc("target.f90":4:7)
55+
#loc2 = loc("target.f90":11:7)
56+
#loc3 = loc(fused<#sp>[#loc2])
57+
#loc4 = loc(fused<#g_var>[#loc1])
58+
59+
// CHECK: ![[SP:[0-9]+]] = distinct !DISubprogram(name: "__omp_offloading{{.*}}test{{.*}})
60+
// CHECK: !DILocalVariable(name: "dyn_ptr", arg: 1, scope: ![[SP]]{{.*}}flags: DIFlagArtificial)
61+
// CHECK: !DILocalVariable(name: "x", arg: 2, scope: ![[SP]]{{.*}})
62+
// CHECK: !DILocalVariable(name: "arr", arg: 3, scope: ![[SP]]{{.*}})
63+
// CHECK: !DILocalVariable(name: "i", arg: 4, scope: ![[SP]]{{.*}})
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
#int_ty = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "integer",
4+
sizeInBits = 32, encoding = DW_ATE_signed>
5+
#real_ty = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "real",
6+
sizeInBits = 32, encoding = DW_ATE_float>
7+
#file = #llvm.di_file<"target.f90" in "">
8+
#di_null_type = #llvm.di_null_type
9+
#cu = #llvm.di_compile_unit<id = distinct[0]<>,
10+
sourceLanguage = DW_LANG_Fortran95, file = #file, isOptimized = false,
11+
emissionKind = Full>
12+
#array_ty = #llvm.di_composite_type<tag = DW_TAG_array_type,
13+
baseType = #int_ty, elements = #llvm.di_subrange<count = 10 : i64>>
14+
#sp_ty = #llvm.di_subroutine_type<callingConvention = DW_CC_program,
15+
types = #di_null_type>
16+
#g_var = #llvm.di_global_variable<scope = #cu, name = "arr",
17+
linkageName = "_QFEarr", file = #file, line = 4,
18+
type = #array_ty, isDefined = true>
19+
#g_var_expr = #llvm.di_global_variable_expression<var = #g_var>
20+
#sp = #llvm.di_subprogram<id = distinct[2]<>, compileUnit = #cu, scope = #file,
21+
name = "test", file = #file, subprogramFlags = "Definition", type = #sp_ty>
22+
#var_arr = #llvm.di_local_variable<scope = #sp,
23+
name = "arr", file = #file, line = 4, type = #array_ty>
24+
#var_i = #llvm.di_local_variable<scope = #sp,
25+
name = "i", file = #file, line = 13, type = #int_ty>
26+
#var_x = #llvm.di_local_variable<scope = #sp,
27+
name = "x", file = #file, line = 12, type = #real_ty>
28+
29+
module attributes {omp.is_target_device = false} {
30+
llvm.func @test() {
31+
%0 = llvm.mlir.constant(1 : i64) : i64
32+
%1 = llvm.alloca %0 x f32 : (i64) -> !llvm.ptr
33+
%4 = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr
34+
%6 = llvm.mlir.constant(9 : index) : i64
35+
%7 = llvm.mlir.constant(0 : index) : i64
36+
%8 = llvm.mlir.constant(1 : index) : i64
37+
%10 = llvm.mlir.constant(10 : index) : i64
38+
%11 = llvm.mlir.addressof @_QFEarr : !llvm.ptr
39+
%14 = omp.map.info var_ptr(%1 : !llvm.ptr, f32) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr
40+
%15 = omp.map.bounds lower_bound(%7 : i64) upper_bound(%6 : i64) extent(%10 : i64) stride(%8 : i64) start_idx(%8 : i64)
41+
%16 = omp.map.info var_ptr(%11 : !llvm.ptr, !llvm.array<10 x i32>) map_clauses(tofrom) capture(ByRef) bounds(%15) -> !llvm.ptr
42+
%17 = omp.map.info var_ptr(%4 : !llvm.ptr, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr
43+
omp.target map_entries(%14 -> %arg0, %16 -> %arg1, %17 -> %arg2 : !llvm.ptr, !llvm.ptr, !llvm.ptr) {
44+
llvm.intr.dbg.declare #var_x = %arg0 : !llvm.ptr
45+
llvm.intr.dbg.declare #var_arr = %arg1 : !llvm.ptr
46+
llvm.intr.dbg.declare #var_i = %arg2 : !llvm.ptr
47+
omp.terminator
48+
}
49+
llvm.return
50+
} loc(#loc3)
51+
llvm.mlir.global internal @_QFEarr() {addr_space = 0 : i32, dbg_exprs = [#g_var_expr]} : !llvm.array<10 x i32> {
52+
} loc(#loc4)
53+
}
54+
#loc1 = loc("target.f90":4:7)
55+
#loc2 = loc("target.f90":11:7)
56+
#loc3 = loc(fused<#sp>[#loc2])
57+
#loc4 = loc(fused<#g_var>[#loc1])
58+
59+
// CHECK: ![[SP:[0-9]+]] = distinct !DISubprogram(name: "__omp_offloading{{.*}}test{{.*}})
60+
// CHECK: !DILocalVariable(name: "x", arg: 1, scope: ![[SP]]{{.*}})
61+
// CHECK: !DILocalVariable(name: "arr", arg: 2, scope: ![[SP]]{{.*}})
62+
// CHECK: !DILocalVariable(name: "i", arg: 3, scope: ![[SP]]{{.*}})

0 commit comments

Comments
 (0)