Skip to content

Commit d3d4d98

Browse files
committed
[clang][NFC] GetOrCreateLLVMGlobal takes LangAS
Pass a LangAS instead of a target address space to GetOrCreateLLVMGlobal, to remove a place where the frontend assumes that target address space 0 is special. Differential Revision: https://reviews.llvm.org/D108445
1 parent bc194a5 commit d3d4d98

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

clang/lib/CodeGen/CGBlocks.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,8 +2910,8 @@ llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
29102910
if (NSConcreteGlobalBlock)
29112911
return NSConcreteGlobalBlock;
29122912

2913-
NSConcreteGlobalBlock =
2914-
GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock", Int8PtrTy, 0, nullptr);
2913+
NSConcreteGlobalBlock = GetOrCreateLLVMGlobal(
2914+
"_NSConcreteGlobalBlock", Int8PtrTy, LangAS::Default, nullptr);
29152915
configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock);
29162916
return NSConcreteGlobalBlock;
29172917
}
@@ -2920,8 +2920,8 @@ llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
29202920
if (NSConcreteStackBlock)
29212921
return NSConcreteStackBlock;
29222922

2923-
NSConcreteStackBlock =
2924-
GetOrCreateLLVMGlobal("_NSConcreteStackBlock", Int8PtrTy, 0, nullptr);
2923+
NSConcreteStackBlock = GetOrCreateLLVMGlobal(
2924+
"_NSConcreteStackBlock", Int8PtrTy, LangAS::Default, nullptr);
29252925
configureBlocksRuntimeObject(*this, NSConcreteStackBlock);
29262926
return NSConcreteStackBlock;
29272927
}

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,7 +2851,8 @@ ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
28512851
GlobalDecl(cast<FunctionDecl>(VD)),
28522852
/*ForVTable=*/false);
28532853
else
2854-
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, 0, nullptr);
2854+
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
2855+
nullptr);
28552856

28562857
auto *F = cast<llvm::GlobalValue>(Aliasee);
28572858
F->setLinkage(llvm::Function::ExternalWeakLinkage);
@@ -3824,10 +3825,11 @@ bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
38243825
/// mangled name but some other type.
38253826
llvm::Constant *
38263827
CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
3827-
unsigned AddrSpace, const VarDecl *D,
3828+
LangAS AddrSpace, const VarDecl *D,
38283829
ForDefinition_t IsForDefinition) {
38293830
// Lookup the entry, lazily creating it if necessary.
38303831
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
3832+
unsigned TargetAS = getContext().getTargetAddressSpace(AddrSpace);
38313833
if (Entry) {
38323834
if (WeakRefReferences.erase(Entry)) {
38333835
if (D && !D->hasAttr<WeakAttr>())
@@ -3841,7 +3843,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
38413843
if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
38423844
getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);
38433845

3844-
if (Entry->getValueType() == Ty && Entry->getAddressSpace() == AddrSpace)
3846+
if (Entry->getValueType() == Ty && Entry->getAddressSpace() == TargetAS)
38453847
return Entry;
38463848

38473849
// If there are two attempts to define the same mangled name, issue an
@@ -3865,24 +3867,23 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
38653867
}
38663868

38673869
// Make sure the result is of the correct type.
3868-
if (Entry->getType()->getAddressSpace() != AddrSpace) {
3870+
if (Entry->getType()->getAddressSpace() != TargetAS) {
38693871
return llvm::ConstantExpr::getAddrSpaceCast(Entry,
3870-
Ty->getPointerTo(AddrSpace));
3872+
Ty->getPointerTo(TargetAS));
38713873
}
38723874

38733875
// (If global is requested for a definition, we always need to create a new
38743876
// global, not just return a bitcast.)
38753877
if (!IsForDefinition)
3876-
return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo(AddrSpace));
3878+
return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo(TargetAS));
38773879
}
38783880

38793881
auto DAddrSpace = GetGlobalVarAddressSpace(D);
3880-
auto TargetAddrSpace = getContext().getTargetAddressSpace(DAddrSpace);
38813882

38823883
auto *GV = new llvm::GlobalVariable(
38833884
getModule(), Ty, false, llvm::GlobalValue::ExternalLinkage, nullptr,
38843885
MangledName, nullptr, llvm::GlobalVariable::NotThreadLocal,
3885-
TargetAddrSpace);
3886+
getContext().getTargetAddressSpace(DAddrSpace));
38863887

38873888
// If we already created a global with the same mangled name (but different
38883889
// type) before, take its name and remove it from its parent.
@@ -4005,10 +4006,10 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
40054006
LangAS ExpectedAS =
40064007
D ? D->getType().getAddressSpace()
40074008
: (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
4008-
assert(getContext().getTargetAddressSpace(ExpectedAS) == AddrSpace);
4009+
assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS);
40094010
if (DAddrSpace != ExpectedAS) {
40104011
return getTargetCodeGenInfo().performAddrSpaceCast(
4011-
*this, GV, DAddrSpace, ExpectedAS, Ty->getPointerTo(AddrSpace));
4012+
*this, GV, DAddrSpace, ExpectedAS, Ty->getPointerTo(TargetAS));
40124013
}
40134014

40144015
return GV;
@@ -4098,8 +4099,7 @@ llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
40984099
Ty = getTypes().ConvertTypeForMem(ASTTy);
40994100

41004101
StringRef MangledName = getMangledName(D);
4101-
return GetOrCreateLLVMGlobal(MangledName, Ty,
4102-
getContext().getTargetAddressSpace(ASTTy), D,
4102+
return GetOrCreateLLVMGlobal(MangledName, Ty, ASTTy.getAddressSpace(), D,
41034103
IsForDefinition);
41044104
}
41054105

@@ -4108,10 +4108,8 @@ llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
41084108
llvm::Constant *
41094109
CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
41104110
StringRef Name) {
4111-
auto AddrSpace =
4112-
getContext().getLangOpts().OpenCL
4113-
? getContext().getTargetAddressSpace(LangAS::opencl_global)
4114-
: 0;
4111+
LangAS AddrSpace = getContext().getLangOpts().OpenCL ? LangAS::opencl_global
4112+
: LangAS::Default;
41154113
auto *Ret = GetOrCreateLLVMGlobal(Name, Ty, AddrSpace, nullptr);
41164114
setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
41174115
return Ret;
@@ -4528,8 +4526,8 @@ void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
45284526
if (getCodeGenOpts().hasReducedDebugInfo()) {
45294527
QualType ASTTy = D->getType();
45304528
llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
4531-
llvm::Constant *GV = GetOrCreateLLVMGlobal(
4532-
D->getName(), Ty, getContext().getTargetAddressSpace(ASTTy), D);
4529+
llvm::Constant *GV =
4530+
GetOrCreateLLVMGlobal(D->getName(), Ty, ASTTy.getAddressSpace(), D);
45334531
DI->EmitExternalVariable(
45344532
cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
45354533
}
@@ -4901,7 +4899,7 @@ void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
49014899
/*ForVTable=*/false);
49024900
LT = getFunctionLinkage(GD);
49034901
} else {
4904-
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, 0,
4902+
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
49054903
/*D=*/nullptr);
49064904
if (const auto *VD = dyn_cast<VarDecl>(GD.getDecl()))
49074905
LT = getLLVMLinkageVarDefinition(VD, D->getType().isConstQualified());

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,8 +1478,8 @@ class CodeGenModule : public CodeGenTypeCache {
14781478
void UpdateMultiVersionNames(GlobalDecl GD, const FunctionDecl *FD);
14791479

14801480
llvm::Constant *
1481-
GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
1482-
unsigned AddrSpace, const VarDecl *D,
1481+
GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, LangAS AddrSpace,
1482+
const VarDecl *D,
14831483
ForDefinition_t IsForDefinition = NotForDefinition);
14841484

14851485
bool GetCPUAndFeaturesAttributes(GlobalDecl GD,

0 commit comments

Comments
 (0)