diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.h b/clang/lib/CodeGen/CGOpenMPRuntime.h --- a/clang/lib/CodeGen/CGOpenMPRuntime.h +++ b/clang/lib/CodeGen/CGOpenMPRuntime.h @@ -601,16 +601,6 @@ /// \return Cache variable for the specified threadprivate. llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD); - /// Gets (if variable with the given name already exist) or creates - /// internal global variable with the specified Name. The created variable has - /// linkage CommonLinkage by default and is initialized by null value. - /// \param Ty Type of the global variable. If it is exist already the type - /// must be the same. - /// \param Name Name of the variable. - llvm::GlobalVariable *getOrCreateInternalVariable(llvm::Type *Ty, - const llvm::Twine &Name, - unsigned AddressSpace = 0); - /// Set of threadprivate variables with the generated initializer. llvm::StringSet<> ThreadPrivateWithDefinition; 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 @@ -1644,7 +1644,7 @@ QualType PtrTy = CGM.getContext().getPointerType(VD->getType()); llvm::Type *LlvmPtrTy = CGM.getTypes().ConvertTypeForMem(PtrTy); if (!Ptr) { - Ptr = getOrCreateInternalVariable(LlvmPtrTy, PtrName); + Ptr = OMPBuilder.getOrCreateInternalVariable(LlvmPtrTy, PtrName); auto *GV = cast(Ptr); GV->setLinkage(llvm::GlobalValue::WeakAnyLinkage); @@ -1664,8 +1664,8 @@ !CGM.getContext().getTargetInfo().isTLSSupported()); // Lookup the entry, lazily creating it if necessary. std::string Suffix = getName({"cache", ""}); - return getOrCreateInternalVariable( - CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix)); + return OMPBuilder.getOrCreateInternalVariable( + CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix).str()); } Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, @@ -1969,8 +1969,8 @@ StringRef Name) { std::string Suffix = getName({"artificial", ""}); llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType); - llvm::GlobalVariable *GAddr = - getOrCreateInternalVariable(VarLVType, Twine(Name).concat(Suffix)); + llvm::GlobalVariable *GAddr = OMPBuilder.getOrCreateInternalVariable( + VarLVType, Twine(Name).concat(Suffix).str()); if (CGM.getLangOpts().OpenMP && CGM.getLangOpts().OpenMPUseTLS && CGM.getTarget().isTLSSupported()) { GAddr->setThreadLocal(/*Val=*/true); @@ -1984,8 +1984,9 @@ CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy), CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy, /*isSigned=*/false), - getOrCreateInternalVariable( - CGM.VoidPtrPtrTy, Twine(Name).concat(Suffix).concat(CacheSuffix))}; + OMPBuilder.getOrCreateInternalVariable( + CGM.VoidPtrPtrTy, + Twine(Name).concat(Suffix).concat(CacheSuffix).str())}; return Address( CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( CGF.EmitRuntimeCall( @@ -2130,30 +2131,10 @@ return ThreadIDTemp; } -llvm::GlobalVariable *CGOpenMPRuntime::getOrCreateInternalVariable( - llvm::Type *Ty, const llvm::Twine &Name, unsigned AddressSpace) { - SmallString<256> Buffer; - llvm::raw_svector_ostream Out(Buffer); - Out << Name; - StringRef RuntimeName = Out.str(); - auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first; - if (Elem.second) { - assert(Elem.second->getType()->isOpaqueOrPointeeTypeMatches(Ty) && - "OMP internal variable has different type than requested"); - return &*Elem.second; - } - - return Elem.second = new llvm::GlobalVariable( - CGM.getModule(), Ty, /*IsConstant*/ false, - llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty), - Elem.first(), /*InsertBefore=*/nullptr, - llvm::GlobalValue::NotThreadLocal, AddressSpace); -} - llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); std::string Name = getName({Prefix, "var"}); - return getOrCreateInternalVariable(KmpCriticalNameTy, Name); + return OMPBuilder.getOrCreateInternalVariable(KmpCriticalNameTy, Name); } namespace { @@ -10402,7 +10383,7 @@ std::string RefName = getName({VarName, "ref"}); if (!CGM.GetGlobalValue(RefName)) { llvm::Constant *AddrRef = - getOrCreateInternalVariable(Addr->getType(), RefName); + OMPBuilder.getOrCreateInternalVariable(Addr->getType(), RefName); auto *GVAddrRef = cast(AddrRef); GVAddrRef->setConstant(/*Val=*/true); GVAddrRef->setLinkage(llvm::GlobalValue::InternalLinkage); @@ -12195,15 +12176,15 @@ // Last updated loop counter for the lastprivate conditional var. // int last_iv = 0; llvm::Type *LLIVTy = CGF.ConvertTypeForMem(IVLVal.getType()); - llvm::Constant *LastIV = - getOrCreateInternalVariable(LLIVTy, getName({UniqueDeclName, "iv"})); + llvm::Constant *LastIV = OMPBuilder.getOrCreateInternalVariable( + LLIVTy, getName({UniqueDeclName, "iv"})); cast(LastIV)->setAlignment( IVLVal.getAlignment().getAsAlign()); LValue LastIVLVal = CGF.MakeNaturalAlignAddrLValue(LastIV, IVLVal.getType()); // Last value of the lastprivate conditional. // decltype(priv_a) last_a; - llvm::GlobalVariable *Last = getOrCreateInternalVariable( + llvm::GlobalVariable *Last = OMPBuilder.getOrCreateInternalVariable( CGF.ConvertTypeForMem(LVal.getType()), UniqueDeclName); Last->setAlignment(LVal.getAlignment().getAsAlign()); LValue LastLVal = CGF.MakeAddrLValue( 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 @@ -1462,15 +1462,6 @@ StringRef FirstSeparator, StringRef Separator); - /// Gets (if variable with the given name already exist) or creates - /// internal global variable with the specified Name. The created variable has - /// linkage CommonLinkage by default and is initialized by null value. - /// \param Ty Type of the global variable. If it is exist already the type - /// must be the same. - /// \param Name Name of the variable. - Constant *getOrCreateOMPInternalVariable(Type *Ty, const Twine &Name, - unsigned AddressSpace = 0); - /// Returns corresponding lock object for the specified critical region /// name. If the lock object does not exist it is created, otherwise the /// reference to the existing copy is returned. @@ -1726,6 +1717,16 @@ void loadOffloadInfoMetadata(Module &M, OffloadEntriesInfoManager &OffloadEntriesInfoManager); + + /// Gets (if variable with the given name already exist) or creates + /// internal global variable with the specified Name. The created variable has + /// linkage CommonLinkage by default and is initialized by null value. + /// \param Ty Type of the global variable. If it is exist already the type + /// must be the same. + /// \param Name Name of the variable. + GlobalVariable *getOrCreateInternalVariable(llvm::Type *Ty, + const llvm::StringRef &Name, + unsigned AddressSpace = 0); }; /// Data structure to contain the information needed to uniquely identify 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 @@ -3866,7 +3866,7 @@ Value *Ident = getOrCreateIdent(SrcLocStr, SrcLocStrSize); Value *ThreadId = getOrCreateThreadID(Ident); Constant *ThreadPrivateCache = - getOrCreateOMPInternalVariable(Int8PtrPtr, Name); + getOrCreateInternalVariable(Int8PtrPtr, Name.str()); llvm::Value *Args[] = {Ident, ThreadId, Pointer, Size, ThreadPrivateCache}; Function *Fn = @@ -3963,18 +3963,10 @@ return OS.str().str(); } -Constant *OpenMPIRBuilder::getOrCreateOMPInternalVariable( - llvm::Type *Ty, const llvm::Twine &Name, unsigned AddressSpace) { - // TODO: Replace the twine arg with stringref to get rid of the conversion - // logic. However This is taken from current implementation in clang as is. - // Since this method is used in many places exclusively for OMP internal use - // we will keep it as is for temporarily until we move all users to the - // builder and then, if possible, fix it everywhere in one go. - SmallString<256> Buffer; - llvm::raw_svector_ostream Out(Buffer); - Out << Name; - StringRef RuntimeName = Out.str(); - auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first; +GlobalVariable * +OpenMPIRBuilder::getOrCreateInternalVariable(Type *Ty, const StringRef &Name, + unsigned AddressSpace) { + auto &Elem = *InternalVars.try_emplace(Name, nullptr).first; if (Elem.second) { assert(cast(Elem.second->getType()) ->isOpaqueOrPointeeTypeMatches(Ty) && @@ -3984,20 +3976,19 @@ // variable for possibly changing that to internal or private, or maybe // create different versions of the function for different OMP internal // variables. - Elem.second = new llvm::GlobalVariable( - M, Ty, /*IsConstant*/ false, llvm::GlobalValue::CommonLinkage, - llvm::Constant::getNullValue(Ty), Elem.first(), - /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal, - AddressSpace); + Elem.second = new GlobalVariable( + M, Ty, /*IsConstant*/ false, GlobalValue::CommonLinkage, + Constant::getNullValue(Ty), Elem.first(), + /*InsertBefore=*/nullptr, GlobalValue::NotThreadLocal, AddressSpace); } - return Elem.second; + return cast(&*Elem.second); } Value *OpenMPIRBuilder::getOMPCriticalRegionLock(StringRef CriticalName) { std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); std::string Name = getNameWithSeparators({Prefix, "var"}, ".", "."); - return getOrCreateOMPInternalVariable(KmpCriticalNameTy, Name); + return getOrCreateInternalVariable(KmpCriticalNameTy, Name); } GlobalVariable *