@@ -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+
68116878static 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
0 commit comments