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 @@ -9375,17 +9375,12 @@ // The map types are always constant so we don't need to generate code to // fill arrays. Instead, we create an array constant. - SmallVector Mapping(CombinedInfo.Types.size(), 0); + SmallVector Mapping(CombinedInfo.Types.size(), 0); llvm::copy(CombinedInfo.Types, Mapping.begin()); - llvm::Constant *MapTypesArrayInit = - llvm::ConstantDataArray::get(CGF.Builder.getContext(), Mapping); std::string MaptypesName = CGM.getOpenMPRuntime().getName({"offload_maptypes"}); - auto *MapTypesArrayGbl = new llvm::GlobalVariable( - CGM.getModule(), MapTypesArrayInit->getType(), - /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, - MapTypesArrayInit, MaptypesName); - MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); + auto *MapTypesArrayGbl = + OMPBuilder.createOffloadMaptypes(Mapping, MaptypesName); Info.MapTypesArray = MapTypesArrayGbl; // The information types are only built if there is debug information @@ -9397,19 +9392,12 @@ auto fillInfoMap = [&](MappableExprsHandler::MappingExprInfo &MapExpr) { return emitMappingInformation(CGF, OMPBuilder, MapExpr); }; - SmallVector InfoMap(CombinedInfo.Exprs.size()); + SmallVector InfoMap(CombinedInfo.Exprs.size()); llvm::transform(CombinedInfo.Exprs, InfoMap.begin(), fillInfoMap); - - llvm::Constant *MapNamesArrayInit = llvm::ConstantArray::get( - llvm::ArrayType::get( - llvm::Type::getInt8Ty(CGF.Builder.getContext())->getPointerTo(), - CombinedInfo.Exprs.size()), - InfoMap); - auto *MapNamesArrayGbl = new llvm::GlobalVariable( - CGM.getModule(), MapNamesArrayInit->getType(), - /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, - MapNamesArrayInit, - CGM.getOpenMPRuntime().getName({"offload_mapnames"})); + std::string MapnamesName = + CGM.getOpenMPRuntime().getName({"offload_mapnames"}); + auto *MapNamesArrayGbl = + OMPBuilder.createOffloadMapnames(InfoMap, MapnamesName); Info.MapNamesArray = MapNamesArrayGbl; } @@ -9424,15 +9412,8 @@ } } if (EndMapTypesDiffer) { - MapTypesArrayInit = - llvm::ConstantDataArray::get(CGF.Builder.getContext(), Mapping); - MaptypesName = CGM.getOpenMPRuntime().getName({"offload_maptypes"}); - MapTypesArrayGbl = new llvm::GlobalVariable( - CGM.getModule(), MapTypesArrayInit->getType(), - /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, - MapTypesArrayInit, MaptypesName); - MapTypesArrayGbl->setUnnamedAddr( - llvm::GlobalValue::UnnamedAddr::Global); + MapTypesArrayGbl = + OMPBuilder.createOffloadMaptypes(Mapping, MaptypesName); Info.MapTypesArrayEnd = MapTypesArrayGbl; } } 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 @@ -615,6 +615,14 @@ /// variables. StringMap, BumpPtrAllocator> InternalVars; + /// Create the global variable holding the offload mappings information. + GlobalVariable *createOffloadMaptypes(SmallVector &Mappings, + std::string VarName); + + /// Create the global variable holding the offload names information. + GlobalVariable *createOffloadMapnames(SmallVector &Names, + std::string VarName); + public: /// Generator for __kmpc_copyprivate /// 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 @@ -2095,6 +2095,35 @@ return getOrCreateOMPInternalVariable(KmpCriticalNameTy, Name); } +// Create the global variable holding the offload mappings information. +GlobalVariable * +OpenMPIRBuilder::createOffloadMaptypes(SmallVector &Mappings, + std::string VarName) { + llvm::Constant *MaptypesArrayInit = + llvm::ConstantDataArray::get(M.getContext(), Mappings); + auto *MaptypesArrayGlobal = new llvm::GlobalVariable( + M, MaptypesArrayInit->getType(), + /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, MaptypesArrayInit, + VarName); + MaptypesArrayGlobal->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); + return MaptypesArrayGlobal; +} + +// Create the global variables holding the offload names information. +GlobalVariable * +OpenMPIRBuilder::createOffloadMapnames(SmallVector &Names, + std::string VarName) { + llvm::Constant *MapNamesArrayInit = llvm::ConstantArray::get( + llvm::ArrayType::get( + llvm::Type::getInt8Ty(M.getContext())->getPointerTo(), Names.size()), + Names); + auto *MapNamesArrayGlobal = new llvm::GlobalVariable( + M, MapNamesArrayInit->getType(), + /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, MapNamesArrayInit, + VarName); + return MapNamesArrayGlobal; +} + // Create all simple and struct types exposed by the runtime and remember // the llvm::PointerTypes of them for easy access later. void OpenMPIRBuilder::initializeTypes(Module &M) {