Skip to content

Commit 320c1c6

Browse files
committed
Handle review comments.
Move the variable update to a separate helper function.
1 parent 1bf7277 commit 320c1c6

File tree

2 files changed

+70
-62
lines changed

2 files changed

+70
-62
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6808,6 +6808,73 @@ FunctionCallee OpenMPIRBuilder::createDispatchDeinitFunction() {
68086808
return getOrCreateRuntimeFunction(M, omp::OMPRTL___kmpc_dispatch_deinit);
68096809
}
68106810

6811+
static void FixupDebugInfoForOutlinedFunction(
6812+
OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, Function *Func,
6813+
DenseMap<Value *, std::tuple<Value *, unsigned>> &ValueReplacementMap) {
6814+
DenseMap<const MDNode *, MDNode *> Cache;
6815+
SmallDenseMap<DILocalVariable *, DILocalVariable *> RemappedVariables;
6816+
6817+
auto GetUpdatedDIVariable = [&](DILocalVariable *OldVar, unsigned arg) {
6818+
auto NewSP = Func->getSubprogram();
6819+
DILocalVariable *&NewVar = RemappedVariables[OldVar];
6820+
if (!NewVar) {
6821+
DILocalScope *NewScope = DILocalScope::cloneScopeForSubprogram(
6822+
*OldVar->getScope(), *NewSP, Builder.getContext(), Cache);
6823+
NewVar = llvm::DILocalVariable::get(
6824+
Builder.getContext(), NewScope, OldVar->getName(), OldVar->getFile(),
6825+
OldVar->getLine(), OldVar->getType(), arg, OldVar->getFlags(),
6826+
OldVar->getAlignInBits(), OldVar->getAnnotations());
6827+
}
6828+
return NewVar;
6829+
};
6830+
6831+
DISubprogram *NewSP = Func->getSubprogram();
6832+
if (NewSP) {
6833+
// The location and scope of variable intrinsics and records still point to
6834+
// the parent function of the target region. Update them.
6835+
for (Instruction &I : instructions(Func)) {
6836+
if (auto *DDI = dyn_cast<llvm::DbgVariableIntrinsic>(&I)) {
6837+
DILocalVariable *OldVar = DDI->getVariable();
6838+
unsigned ArgNo = OldVar->getArg();
6839+
for (auto Loc : DDI->location_ops()) {
6840+
auto Iter = ValueReplacementMap.find(Loc);
6841+
if (Iter != ValueReplacementMap.end()) {
6842+
DDI->replaceVariableLocationOp(Loc, std::get<0>(Iter->second));
6843+
ArgNo = std::get<1>(Iter->second) + 1;
6844+
}
6845+
}
6846+
DDI->setVariable(GetUpdatedDIVariable(OldVar, ArgNo));
6847+
}
6848+
for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
6849+
DILocalVariable *OldVar = DVR.getVariable();
6850+
unsigned ArgNo = OldVar->getArg();
6851+
for (auto Loc : DVR.location_ops()) {
6852+
auto Iter = ValueReplacementMap.find(Loc);
6853+
if (Iter != ValueReplacementMap.end()) {
6854+
DVR.replaceVariableLocationOp(Loc, std::get<0>(Iter->second));
6855+
ArgNo = std::get<1>(Iter->second) + 1;
6856+
}
6857+
}
6858+
DVR.setVariable(GetUpdatedDIVariable(OldVar, ArgNo));
6859+
}
6860+
}
6861+
// An extra argument is passed to the device. Create the debug data for it.
6862+
if (OMPBuilder.Config.isTargetDevice()) {
6863+
DICompileUnit *CU = NewSP->getUnit();
6864+
auto M = Builder.GetInsertBlock()->getModule();
6865+
DIBuilder DB(*M, true, CU);
6866+
DIType *VoidPtrTy =
6867+
DB.createQualifiedType(dwarf::DW_TAG_pointer_type, nullptr);
6868+
DILocalVariable *Var = DB.createParameterVariable(
6869+
NewSP, "dyn_ptr", /*ArgNo*/ 1, NewSP->getFile(), /*LineNo=*/0,
6870+
VoidPtrTy, /*AlwaysPreserve=*/false, DINode::DIFlags::FlagArtificial);
6871+
auto Loc = DILocation::get(Func->getContext(), 0, 0, NewSP, 0);
6872+
DB.insertDeclare(&(*Func->arg_begin()), Var, DB.createExpression(), Loc,
6873+
&(*Func->begin()));
6874+
}
6875+
}
6876+
}
6877+
68116878
static Expected<Function *> createOutlinedFunction(
68126879
OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
68136880
const OpenMPIRBuilder::TargetKernelDefaultAttrs &DefaultAttrs,
@@ -7008,67 +7075,8 @@ static Expected<Function *> createOutlinedFunction(
70087075
for (auto Deferred : DeferredReplacement)
70097076
ReplaceValue(std::get<0>(Deferred), std::get<1>(Deferred), Func);
70107077

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-
}
7078+
FixupDebugInfoForOutlinedFunction(OMPBuilder, Builder, Func,
7079+
ValueReplacementMap);
70727080
return Func;
70737081
}
70747082

mlir/test/Target/LLVMIR/omptarget-debug-var-1.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#var_x = #llvm.di_local_variable<scope = #sp,
2727
name = "x", file = #file, line = 12, type = #real_ty>
2828

29-
module attributes {omp.is_target_device = true} {
29+
module attributes {llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_target_device = true} {
3030
llvm.func @test() {
3131
%0 = llvm.mlir.constant(1 : i64) : i64
3232
%1 = llvm.alloca %0 x f32 : (i64) -> !llvm.ptr

0 commit comments

Comments
 (0)