|
38 | 38 | #include "llvm/IR/Function.h"
|
39 | 39 | #include "llvm/IR/GlobalVariable.h"
|
40 | 40 | #include "llvm/IR/IRBuilder.h"
|
| 41 | +#include "llvm/IR/InstIterator.h" |
| 42 | +#include "llvm/IR/IntrinsicInst.h" |
41 | 43 | #include "llvm/IR/LLVMContext.h"
|
42 | 44 | #include "llvm/IR/MDBuilder.h"
|
43 | 45 | #include "llvm/IR/Metadata.h"
|
@@ -6810,6 +6812,72 @@ FunctionCallee OpenMPIRBuilder::createDispatchDeinitFunction() {
|
6810 | 6812 | return getOrCreateRuntimeFunction(M, omp::OMPRTL___kmpc_dispatch_deinit);
|
6811 | 6813 | }
|
6812 | 6814 |
|
| 6815 | +static void FixupDebugInfoForOutlinedFunction( |
| 6816 | + OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, Function *Func, |
| 6817 | + DenseMap<Value *, std::tuple<Value *, unsigned>> &ValueReplacementMap) { |
| 6818 | + |
| 6819 | + DISubprogram *NewSP = Func->getSubprogram(); |
| 6820 | + if (!NewSP) |
| 6821 | + return; |
| 6822 | + |
| 6823 | + DenseMap<const MDNode *, MDNode *> Cache; |
| 6824 | + SmallDenseMap<DILocalVariable *, DILocalVariable *> RemappedVariables; |
| 6825 | + |
| 6826 | + auto GetUpdatedDIVariable = [&](DILocalVariable *OldVar, unsigned arg) { |
| 6827 | + auto NewSP = Func->getSubprogram(); |
| 6828 | + DILocalVariable *&NewVar = RemappedVariables[OldVar]; |
| 6829 | + // Only use cached variable if the arg number matches. This is important |
| 6830 | + // so that DIVariable created for privatized variables are not discarded. |
| 6831 | + if (NewVar && (arg == NewVar->getArg())) |
| 6832 | + return NewVar; |
| 6833 | + |
| 6834 | + DILocalScope *NewScope = DILocalScope::cloneScopeForSubprogram( |
| 6835 | + *OldVar->getScope(), *NewSP, Builder.getContext(), Cache); |
| 6836 | + NewVar = llvm::DILocalVariable::get( |
| 6837 | + Builder.getContext(), NewScope, OldVar->getName(), OldVar->getFile(), |
| 6838 | + OldVar->getLine(), OldVar->getType(), arg, OldVar->getFlags(), |
| 6839 | + OldVar->getAlignInBits(), OldVar->getAnnotations()); |
| 6840 | + return NewVar; |
| 6841 | + }; |
| 6842 | + |
| 6843 | + auto UpdateDebugRecord = [&](auto *DR) { |
| 6844 | + DILocalVariable *OldVar = DR->getVariable(); |
| 6845 | + unsigned ArgNo = 0; |
| 6846 | + for (auto Loc : DR->location_ops()) { |
| 6847 | + auto Iter = ValueReplacementMap.find(Loc); |
| 6848 | + if (Iter != ValueReplacementMap.end()) { |
| 6849 | + DR->replaceVariableLocationOp(Loc, std::get<0>(Iter->second)); |
| 6850 | + ArgNo = std::get<1>(Iter->second) + 1; |
| 6851 | + } |
| 6852 | + } |
| 6853 | + DR->setVariable(GetUpdatedDIVariable(OldVar, ArgNo)); |
| 6854 | + }; |
| 6855 | + |
| 6856 | + // The location and scope of variable intrinsics and records still point to |
| 6857 | + // the parent function of the target region. Update them. |
| 6858 | + for (Instruction &I : instructions(Func)) { |
| 6859 | + if (auto *DDI = dyn_cast<llvm::DbgVariableIntrinsic>(&I)) |
| 6860 | + UpdateDebugRecord(DDI); |
| 6861 | + |
| 6862 | + for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) |
| 6863 | + UpdateDebugRecord(&DVR); |
| 6864 | + } |
| 6865 | + // An extra argument is passed to the device. Create the debug data for it. |
| 6866 | + if (OMPBuilder.Config.isTargetDevice()) { |
| 6867 | + DICompileUnit *CU = NewSP->getUnit(); |
| 6868 | + Module *M = Func->getParent(); |
| 6869 | + DIBuilder DB(*M, true, CU); |
| 6870 | + DIType *VoidPtrTy = |
| 6871 | + DB.createQualifiedType(dwarf::DW_TAG_pointer_type, nullptr); |
| 6872 | + DILocalVariable *Var = DB.createParameterVariable( |
| 6873 | + NewSP, "dyn_ptr", /*ArgNo*/ 1, NewSP->getFile(), /*LineNo=*/0, |
| 6874 | + VoidPtrTy, /*AlwaysPreserve=*/false, DINode::DIFlags::FlagArtificial); |
| 6875 | + auto Loc = DILocation::get(Func->getContext(), 0, 0, NewSP, 0); |
| 6876 | + DB.insertDeclare(&(*Func->arg_begin()), Var, DB.createExpression(), Loc, |
| 6877 | + &(*Func->begin())); |
| 6878 | + } |
| 6879 | +} |
| 6880 | + |
6813 | 6881 | static Expected<Function *> createOutlinedFunction(
|
6814 | 6882 | OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
|
6815 | 6883 | const OpenMPIRBuilder::TargetKernelDefaultAttrs &DefaultAttrs,
|
@@ -6935,6 +7003,8 @@ static Expected<Function *> createOutlinedFunction(
|
6935 | 7003 | ? make_range(Func->arg_begin() + 1, Func->arg_end())
|
6936 | 7004 | : Func->args();
|
6937 | 7005 |
|
| 7006 | + DenseMap<Value *, std::tuple<Value *, unsigned>> ValueReplacementMap; |
| 7007 | + |
6938 | 7008 | auto ReplaceValue = [](Value *Input, Value *InputCopy, Function *Func) {
|
6939 | 7009 | // Things like GEP's can come in the form of Constants. Constants and
|
6940 | 7010 | // ConstantExpr's do not have access to the knowledge of what they're
|
@@ -6976,6 +7046,7 @@ static Expected<Function *> createOutlinedFunction(
|
6976 | 7046 | if (!AfterIP)
|
6977 | 7047 | return AfterIP.takeError();
|
6978 | 7048 | Builder.restoreIP(*AfterIP);
|
| 7049 | + ValueReplacementMap[Input] = std::make_tuple(InputCopy, Arg.getArgNo()); |
6979 | 7050 |
|
6980 | 7051 | // In certain cases a Global may be set up for replacement, however, this
|
6981 | 7052 | // Global may be used in multiple arguments to the kernel, just segmented
|
@@ -7007,6 +7078,8 @@ static Expected<Function *> createOutlinedFunction(
|
7007 | 7078 | for (auto Deferred : DeferredReplacement)
|
7008 | 7079 | ReplaceValue(std::get<0>(Deferred), std::get<1>(Deferred), Func);
|
7009 | 7080 |
|
| 7081 | + FixupDebugInfoForOutlinedFunction(OMPBuilder, Builder, Func, |
| 7082 | + ValueReplacementMap); |
7010 | 7083 | return Func;
|
7011 | 7084 | }
|
7012 | 7085 |
|
|
0 commit comments