Skip to content

Commit 87f652d

Browse files
committed
Migrate getOrCreateInternalVariable from Clang to OMPIRBuilder.
This patch removes getOrCreateInternalVariable from Clang OMP CodeGen and replaces it's uses with OMPBuilder::getOrCreateInternalVariable. Also refactors OMPBuilder::getOrCreateInternalVariable to change type of name from Twine to StringRef Differential Revision: https://reviews.llvm.org/D137720
1 parent c780184 commit 87f652d

File tree

4 files changed

+33
-71
lines changed

4 files changed

+33
-71
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,7 @@ Address CGOpenMPRuntime::getAddrOfDeclareTargetVar(const VarDecl *VD) {
16441644
QualType PtrTy = CGM.getContext().getPointerType(VD->getType());
16451645
llvm::Type *LlvmPtrTy = CGM.getTypes().ConvertTypeForMem(PtrTy);
16461646
if (!Ptr) {
1647-
Ptr = getOrCreateInternalVariable(LlvmPtrTy, PtrName);
1647+
Ptr = OMPBuilder.getOrCreateInternalVariable(LlvmPtrTy, PtrName);
16481648

16491649
auto *GV = cast<llvm::GlobalVariable>(Ptr);
16501650
GV->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
@@ -1664,8 +1664,8 @@ CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) {
16641664
!CGM.getContext().getTargetInfo().isTLSSupported());
16651665
// Lookup the entry, lazily creating it if necessary.
16661666
std::string Suffix = getName({"cache", ""});
1667-
return getOrCreateInternalVariable(
1668-
CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix));
1667+
return OMPBuilder.getOrCreateInternalVariable(
1668+
CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix).str());
16691669
}
16701670

16711671
Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF,
@@ -1969,8 +1969,8 @@ Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
19691969
StringRef Name) {
19701970
std::string Suffix = getName({"artificial", ""});
19711971
llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType);
1972-
llvm::GlobalVariable *GAddr =
1973-
getOrCreateInternalVariable(VarLVType, Twine(Name).concat(Suffix));
1972+
llvm::GlobalVariable *GAddr = OMPBuilder.getOrCreateInternalVariable(
1973+
VarLVType, Twine(Name).concat(Suffix).str());
19741974
if (CGM.getLangOpts().OpenMP && CGM.getLangOpts().OpenMPUseTLS &&
19751975
CGM.getTarget().isTLSSupported()) {
19761976
GAddr->setThreadLocal(/*Val=*/true);
@@ -1984,8 +1984,9 @@ Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
19841984
CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy),
19851985
CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy,
19861986
/*isSigned=*/false),
1987-
getOrCreateInternalVariable(
1988-
CGM.VoidPtrPtrTy, Twine(Name).concat(Suffix).concat(CacheSuffix))};
1987+
OMPBuilder.getOrCreateInternalVariable(
1988+
CGM.VoidPtrPtrTy,
1989+
Twine(Name).concat(Suffix).concat(CacheSuffix).str())};
19891990
return Address(
19901991
CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
19911992
CGF.EmitRuntimeCall(
@@ -2130,30 +2131,10 @@ Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF,
21302131
return ThreadIDTemp;
21312132
}
21322133

2133-
llvm::GlobalVariable *CGOpenMPRuntime::getOrCreateInternalVariable(
2134-
llvm::Type *Ty, const llvm::Twine &Name, unsigned AddressSpace) {
2135-
SmallString<256> Buffer;
2136-
llvm::raw_svector_ostream Out(Buffer);
2137-
Out << Name;
2138-
StringRef RuntimeName = Out.str();
2139-
auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first;
2140-
if (Elem.second) {
2141-
assert(Elem.second->getType()->isOpaqueOrPointeeTypeMatches(Ty) &&
2142-
"OMP internal variable has different type than requested");
2143-
return &*Elem.second;
2144-
}
2145-
2146-
return Elem.second = new llvm::GlobalVariable(
2147-
CGM.getModule(), Ty, /*IsConstant*/ false,
2148-
llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty),
2149-
Elem.first(), /*InsertBefore=*/nullptr,
2150-
llvm::GlobalValue::NotThreadLocal, AddressSpace);
2151-
}
2152-
21532134
llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) {
21542135
std::string Prefix = Twine("gomp_critical_user_", CriticalName).str();
21552136
std::string Name = getName({Prefix, "var"});
2156-
return getOrCreateInternalVariable(KmpCriticalNameTy, Name);
2137+
return OMPBuilder.getOrCreateInternalVariable(KmpCriticalNameTy, Name);
21572138
}
21582139

21592140
namespace {
@@ -10402,7 +10383,7 @@ void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD,
1040210383
std::string RefName = getName({VarName, "ref"});
1040310384
if (!CGM.GetGlobalValue(RefName)) {
1040410385
llvm::Constant *AddrRef =
10405-
getOrCreateInternalVariable(Addr->getType(), RefName);
10386+
OMPBuilder.getOrCreateInternalVariable(Addr->getType(), RefName);
1040610387
auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef);
1040710388
GVAddrRef->setConstant(/*Val=*/true);
1040810389
GVAddrRef->setLinkage(llvm::GlobalValue::InternalLinkage);
@@ -12195,15 +12176,15 @@ void CGOpenMPRuntime::emitLastprivateConditionalUpdate(CodeGenFunction &CGF,
1219512176
// Last updated loop counter for the lastprivate conditional var.
1219612177
// int<xx> last_iv = 0;
1219712178
llvm::Type *LLIVTy = CGF.ConvertTypeForMem(IVLVal.getType());
12198-
llvm::Constant *LastIV =
12199-
getOrCreateInternalVariable(LLIVTy, getName({UniqueDeclName, "iv"}));
12179+
llvm::Constant *LastIV = OMPBuilder.getOrCreateInternalVariable(
12180+
LLIVTy, getName({UniqueDeclName, "iv"}));
1220012181
cast<llvm::GlobalVariable>(LastIV)->setAlignment(
1220112182
IVLVal.getAlignment().getAsAlign());
1220212183
LValue LastIVLVal = CGF.MakeNaturalAlignAddrLValue(LastIV, IVLVal.getType());
1220312184

1220412185
// Last value of the lastprivate conditional.
1220512186
// decltype(priv_a) last_a;
12206-
llvm::GlobalVariable *Last = getOrCreateInternalVariable(
12187+
llvm::GlobalVariable *Last = OMPBuilder.getOrCreateInternalVariable(
1220712188
CGF.ConvertTypeForMem(LVal.getType()), UniqueDeclName);
1220812189
Last->setAlignment(LVal.getAlignment().getAsAlign());
1220912190
LValue LastLVal = CGF.MakeAddrLValue(

clang/lib/CodeGen/CGOpenMPRuntime.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -601,16 +601,6 @@ class CGOpenMPRuntime {
601601
/// \return Cache variable for the specified threadprivate.
602602
llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
603603

604-
/// Gets (if variable with the given name already exist) or creates
605-
/// internal global variable with the specified Name. The created variable has
606-
/// linkage CommonLinkage by default and is initialized by null value.
607-
/// \param Ty Type of the global variable. If it is exist already the type
608-
/// must be the same.
609-
/// \param Name Name of the variable.
610-
llvm::GlobalVariable *getOrCreateInternalVariable(llvm::Type *Ty,
611-
const llvm::Twine &Name,
612-
unsigned AddressSpace = 0);
613-
614604
/// Set of threadprivate variables with the generated initializer.
615605
llvm::StringSet<> ThreadPrivateWithDefinition;
616606

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,15 +1462,6 @@ class OpenMPIRBuilder {
14621462
StringRef FirstSeparator,
14631463
StringRef Separator);
14641464

1465-
/// Gets (if variable with the given name already exist) or creates
1466-
/// internal global variable with the specified Name. The created variable has
1467-
/// linkage CommonLinkage by default and is initialized by null value.
1468-
/// \param Ty Type of the global variable. If it is exist already the type
1469-
/// must be the same.
1470-
/// \param Name Name of the variable.
1471-
Constant *getOrCreateOMPInternalVariable(Type *Ty, const Twine &Name,
1472-
unsigned AddressSpace = 0);
1473-
14741465
/// Returns corresponding lock object for the specified critical region
14751466
/// name. If the lock object does not exist it is created, otherwise the
14761467
/// reference to the existing copy is returned.
@@ -1726,6 +1717,15 @@ class OpenMPIRBuilder {
17261717
void
17271718
loadOffloadInfoMetadata(Module &M,
17281719
OffloadEntriesInfoManager &OffloadEntriesInfoManager);
1720+
1721+
/// Gets (if variable with the given name already exist) or creates
1722+
/// internal global variable with the specified Name. The created variable has
1723+
/// linkage CommonLinkage by default and is initialized by null value.
1724+
/// \param Ty Type of the global variable. If it is exist already the type
1725+
/// must be the same.
1726+
/// \param Name Name of the variable.
1727+
GlobalVariable *getOrCreateInternalVariable(Type *Ty, const StringRef &Name,
1728+
unsigned AddressSpace = 0);
17291729
};
17301730

17311731
/// Data structure to contain the information needed to uniquely identify

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3866,7 +3866,7 @@ CallInst *OpenMPIRBuilder::createCachedThreadPrivate(
38663866
Value *Ident = getOrCreateIdent(SrcLocStr, SrcLocStrSize);
38673867
Value *ThreadId = getOrCreateThreadID(Ident);
38683868
Constant *ThreadPrivateCache =
3869-
getOrCreateOMPInternalVariable(Int8PtrPtr, Name);
3869+
getOrCreateInternalVariable(Int8PtrPtr, Name.str());
38703870
llvm::Value *Args[] = {Ident, ThreadId, Pointer, Size, ThreadPrivateCache};
38713871

38723872
Function *Fn =
@@ -3963,18 +3963,10 @@ std::string OpenMPIRBuilder::getNameWithSeparators(ArrayRef<StringRef> Parts,
39633963
return OS.str().str();
39643964
}
39653965

3966-
Constant *OpenMPIRBuilder::getOrCreateOMPInternalVariable(
3967-
llvm::Type *Ty, const llvm::Twine &Name, unsigned AddressSpace) {
3968-
// TODO: Replace the twine arg with stringref to get rid of the conversion
3969-
// logic. However This is taken from current implementation in clang as is.
3970-
// Since this method is used in many places exclusively for OMP internal use
3971-
// we will keep it as is for temporarily until we move all users to the
3972-
// builder and then, if possible, fix it everywhere in one go.
3973-
SmallString<256> Buffer;
3974-
llvm::raw_svector_ostream Out(Buffer);
3975-
Out << Name;
3976-
StringRef RuntimeName = Out.str();
3977-
auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first;
3966+
GlobalVariable *
3967+
OpenMPIRBuilder::getOrCreateInternalVariable(Type *Ty, const StringRef &Name,
3968+
unsigned AddressSpace) {
3969+
auto &Elem = *InternalVars.try_emplace(Name, nullptr).first;
39783970
if (Elem.second) {
39793971
assert(cast<PointerType>(Elem.second->getType())
39803972
->isOpaqueOrPointeeTypeMatches(Ty) &&
@@ -3984,20 +3976,19 @@ Constant *OpenMPIRBuilder::getOrCreateOMPInternalVariable(
39843976
// variable for possibly changing that to internal or private, or maybe
39853977
// create different versions of the function for different OMP internal
39863978
// variables.
3987-
Elem.second = new llvm::GlobalVariable(
3988-
M, Ty, /*IsConstant*/ false, llvm::GlobalValue::CommonLinkage,
3989-
llvm::Constant::getNullValue(Ty), Elem.first(),
3990-
/*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal,
3991-
AddressSpace);
3979+
Elem.second = new GlobalVariable(
3980+
M, Ty, /*IsConstant=*/false, GlobalValue::CommonLinkage,
3981+
Constant::getNullValue(Ty), Elem.first(),
3982+
/*InsertBefore=*/nullptr, GlobalValue::NotThreadLocal, AddressSpace);
39923983
}
39933984

3994-
return Elem.second;
3985+
return cast<GlobalVariable>(&*Elem.second);
39953986
}
39963987

39973988
Value *OpenMPIRBuilder::getOMPCriticalRegionLock(StringRef CriticalName) {
39983989
std::string Prefix = Twine("gomp_critical_user_", CriticalName).str();
39993990
std::string Name = getNameWithSeparators({Prefix, "var"}, ".", ".");
4000-
return getOrCreateOMPInternalVariable(KmpCriticalNameTy, Name);
3991+
return getOrCreateInternalVariable(KmpCriticalNameTy, Name);
40013992
}
40023993

40033994
GlobalVariable *

0 commit comments

Comments
 (0)