diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -3172,52 +3172,7 @@ return; } - llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info"); - if (!MD) - return; - - for (llvm::MDNode *MN : MD->operands()) { - auto &&GetMDInt = [MN](unsigned Idx) { - auto *V = cast(MN->getOperand(Idx)); - return cast(V->getValue())->getZExtValue(); - }; - - auto &&GetMDString = [MN](unsigned Idx) { - auto *V = cast(MN->getOperand(Idx)); - return V->getString(); - }; - - switch (GetMDInt(0)) { - default: - llvm_unreachable("Unexpected metadata!"); - break; - case llvm::OffloadEntriesInfoManager::OffloadEntryInfo:: - OffloadingEntryInfoTargetRegion: { - assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " - "only required for the " - "device code generation."); - llvm::TargetRegionEntryInfo EntryInfo(/*ParentName=*/GetMDString(3), - /*DeviceID=*/GetMDInt(1), - /*FileID=*/GetMDInt(2), - /*Line=*/GetMDInt(4)); - OffloadEntriesInfoManager.initializeTargetRegionEntryInfo( - EntryInfo, /*Order=*/GetMDInt(5)); - break; - } - case llvm::OffloadEntriesInfoManager::OffloadEntryInfo:: - OffloadingEntryInfoDeviceGlobalVar: - assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " - "only required for the " - "device code generation."); - OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo( - /*MangledName=*/GetMDString(1), - static_cast< - llvm::OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind>( - /*Flags=*/GetMDInt(2)), - /*Order=*/GetMDInt(3)); - break; - } - } + llvm::loadOffloadInfoMetadata(*ME.get(), OffloadEntriesInfoManager, CGM.getLangOpts().OpenMPIsDevice); } void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) { diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h --- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h +++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h @@ -2160,6 +2160,12 @@ void invalidate(); }; +/// Loads all the offload entries information from the host IR +/// metadata. +void loadOffloadInfoMetadata( + Module &M, llvm::OffloadEntriesInfoManager &OffloadEntriesInfoManager, + bool isDevice); + } // end namespace llvm #endif // LLVM_FRONTEND_OPENMP_OMPIRBUILDER_H diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -4705,6 +4705,65 @@ getTargetRegionEntryFnName(Name, ParentName, DeviceID, FileID, Line); } +/// Loads all the offload entries information from the host IR +/// metadata. +void llvm::loadOffloadInfoMetadata( + Module &M, OffloadEntriesInfoManager &OffloadEntriesInfoManager, + bool isDevice) { + // If we are in target mode, load the metadata from the host IR. This code has + // to match the metadaata creation in createOffloadEntriesAndInfoMetadata(). + + if (!isDevice) + return; + + llvm::NamedMDNode *MD = M.getNamedMetadata("omp_offload.info"); + if (!MD) + return; + + for (llvm::MDNode *MN : MD->operands()) { + auto &&GetMDInt = [MN](unsigned Idx) { + auto *V = cast(MN->getOperand(Idx)); + return cast(V->getValue())->getZExtValue(); + }; + + auto &&GetMDString = [MN](unsigned Idx) { + auto *V = cast(MN->getOperand(Idx)); + return V->getString(); + }; + + switch (GetMDInt(0)) { + default: + llvm_unreachable("Unexpected metadata!"); + break; + case llvm::OffloadEntriesInfoManager::OffloadEntryInfo:: + OffloadingEntryInfoTargetRegion: { + assert(isDevice && "Initialization of entries is " + "only required for the " + "device code generation."); + llvm::TargetRegionEntryInfo EntryInfo(/*ParentName=*/GetMDString(3), + /*DeviceID=*/GetMDInt(1), + /*FileID=*/GetMDInt(2), + /*Line=*/GetMDInt(4)); + OffloadEntriesInfoManager.initializeTargetRegionEntryInfo( + EntryInfo, /*Order=*/GetMDInt(5)); + break; + } + case llvm::OffloadEntriesInfoManager::OffloadEntryInfo:: + OffloadingEntryInfoDeviceGlobalVar: + assert(isDevice && "Initialization of entries is " + "only required for the " + "device code generation."); + OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo( + /*MangledName=*/GetMDString(1), + static_cast< + llvm::OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind>( + /*Flags=*/GetMDInt(2)), + /*Order=*/GetMDInt(3)); + break; + } + } +} + bool OffloadEntriesInfoManager::empty() const { return OffloadEntriesTargetRegion.empty() && OffloadEntriesDeviceGlobalVar.empty();