Skip to content

Commit c54c894

Browse files
committed
Handle review comments.
Move the variable update to a separate helper function.
1 parent 29b0d60 commit c54c894

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
@@ -6806,6 +6806,73 @@ FunctionCallee OpenMPIRBuilder::createDispatchDeinitFunction() {
68066806
return getOrCreateRuntimeFunction(M, omp::OMPRTL___kmpc_dispatch_deinit);
68076807
}
68086808

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

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

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)