diff --git a/clang/lib/CodeGen/CGBlocks.cpp b/clang/lib/CodeGen/CGBlocks.cpp --- a/clang/lib/CodeGen/CGBlocks.cpp +++ b/clang/lib/CodeGen/CGBlocks.cpp @@ -2939,9 +2939,8 @@ if (NSConcreteGlobalBlock) return NSConcreteGlobalBlock; - NSConcreteGlobalBlock = GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock", - Int8PtrTy->getPointerTo(), - nullptr); + NSConcreteGlobalBlock = + GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock", Int8PtrTy, 0, nullptr); configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock); return NSConcreteGlobalBlock; } @@ -2950,9 +2949,8 @@ if (NSConcreteStackBlock) return NSConcreteStackBlock; - NSConcreteStackBlock = GetOrCreateLLVMGlobal("_NSConcreteStackBlock", - Int8PtrTy->getPointerTo(), - nullptr); + NSConcreteStackBlock = + GetOrCreateLLVMGlobal("_NSConcreteStackBlock", Int8PtrTy, 0, nullptr); configureBlocksRuntimeObject(*this, NSConcreteStackBlock); return NSConcreteStackBlock; } diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -1477,11 +1477,10 @@ const FunctionDecl *FD); void UpdateMultiVersionNames(GlobalDecl GD, const FunctionDecl *FD); - llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName, - llvm::PointerType *PTy, - const VarDecl *D, - ForDefinition_t IsForDefinition - = NotForDefinition); + llvm::Constant * + GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, + unsigned AddrSpace, const VarDecl *D, + ForDefinition_t IsForDefinition = NotForDefinition); bool GetCPUAndFeaturesAttributes(GlobalDecl GD, llvm::AttrBuilder &AttrBuilder); diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2808,9 +2808,7 @@ GlobalDecl(cast(VD)), /*ForVTable=*/false); else - Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), - llvm::PointerType::getUnqual(DeclTy), - nullptr); + Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, 0, nullptr); auto *F = cast(Aliasee); F->setLinkage(llvm::Function::ExternalWeakLinkage); @@ -3772,9 +3770,9 @@ } /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, -/// create and return an llvm GlobalVariable with the specified type. If there -/// is something in the module with the specified name, return it potentially -/// bitcasted to the right type. +/// create and return an llvm GlobalVariable with the specified type and address +/// space. If there is something in the module with the specified name, return +/// it potentially bitcasted to the right type. /// /// If D is non-null, it specifies a decl that correspond to this. This is used /// to set the attributes on the global when it is first created. @@ -3783,9 +3781,8 @@ /// type Ty will be returned, not conversion of a variable with the same /// mangled name but some other type. llvm::Constant * -CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, - llvm::PointerType *Ty, - const VarDecl *D, +CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, + unsigned AddrSpace, const VarDecl *D, ForDefinition_t IsForDefinition) { // Lookup the entry, lazily creating it if necessary. llvm::GlobalValue *Entry = GetGlobalValue(MangledName); @@ -3802,7 +3799,7 @@ if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D) getOpenMPRuntime().registerTargetGlobalVariable(D, Entry); - if (Entry->getType() == Ty) + if (Entry->getValueType() == Ty && Entry->getAddressSpace() == AddrSpace) return Entry; // If there are two attempts to define the same mangled name, issue an @@ -3826,22 +3823,24 @@ } // Make sure the result is of the correct type. - if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace()) - return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty); + if (Entry->getType()->getAddressSpace() != AddrSpace) { + return llvm::ConstantExpr::getAddrSpaceCast(Entry, + Ty->getPointerTo(AddrSpace)); + } // (If global is requested for a definition, we always need to create a new // global, not just return a bitcast.) if (!IsForDefinition) - return llvm::ConstantExpr::getBitCast(Entry, Ty); + return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo(AddrSpace)); } - auto AddrSpace = GetGlobalVarAddressSpace(D); - auto TargetAddrSpace = getContext().getTargetAddressSpace(AddrSpace); + auto DAddrSpace = GetGlobalVarAddressSpace(D); + auto TargetAddrSpace = getContext().getTargetAddressSpace(DAddrSpace); auto *GV = new llvm::GlobalVariable( - getModule(), Ty->getElementType(), false, - llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr, - llvm::GlobalVariable::NotThreadLocal, TargetAddrSpace); + getModule(), Ty, false, llvm::GlobalValue::ExternalLinkage, nullptr, + MangledName, nullptr, llvm::GlobalVariable::NotThreadLocal, + TargetAddrSpace); // If we already created a global with the same mangled name (but different // type) before, take its name and remove it from its parent. @@ -3964,11 +3963,11 @@ LangAS ExpectedAS = D ? D->getType().getAddressSpace() : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default); - assert(getContext().getTargetAddressSpace(ExpectedAS) == - Ty->getPointerAddressSpace()); - if (AddrSpace != ExpectedAS) - return getTargetCodeGenInfo().performAddrSpaceCast(*this, GV, AddrSpace, - ExpectedAS, Ty); + assert(getContext().getTargetAddressSpace(ExpectedAS) == AddrSpace); + if (DAddrSpace != ExpectedAS) { + return getTargetCodeGenInfo().performAddrSpaceCast( + *this, GV, DAddrSpace, ExpectedAS, Ty->getPointerTo(AddrSpace)); + } return GV; } @@ -4056,11 +4055,10 @@ if (!Ty) Ty = getTypes().ConvertTypeForMem(ASTTy); - llvm::PointerType *PTy = - llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy)); - StringRef MangledName = getMangledName(D); - return GetOrCreateLLVMGlobal(MangledName, PTy, D, IsForDefinition); + return GetOrCreateLLVMGlobal(MangledName, Ty, + getContext().getTargetAddressSpace(ASTTy), D, + IsForDefinition); } /// CreateRuntimeVariable - Create a new runtime global variable with the @@ -4068,12 +4066,11 @@ llvm::Constant * CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty, StringRef Name) { - auto PtrTy = + auto AddrSpace = getContext().getLangOpts().OpenCL - ? llvm::PointerType::get( - Ty, getContext().getTargetAddressSpace(LangAS::opencl_global)) - : llvm::PointerType::getUnqual(Ty); - auto *Ret = GetOrCreateLLVMGlobal(Name, PtrTy, nullptr); + ? getContext().getTargetAddressSpace(LangAS::opencl_global) + : 0; + auto *Ret = GetOrCreateLLVMGlobal(Name, Ty, AddrSpace, nullptr); setDSOLocal(cast(Ret->stripPointerCasts())); return Ret; } @@ -4487,9 +4484,8 @@ if (getCodeGenOpts().hasReducedDebugInfo()) { QualType ASTTy = D->getType(); llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType()); - llvm::PointerType *PTy = - llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy)); - llvm::Constant *GV = GetOrCreateLLVMGlobal(D->getName(), PTy, D); + llvm::Constant *GV = GetOrCreateLLVMGlobal( + D->getName(), Ty, getContext().getTargetAddressSpace(ASTTy), D); DI->EmitExternalVariable( cast(GV->stripPointerCasts()), D); } @@ -4861,8 +4857,7 @@ /*ForVTable=*/false); LT = getFunctionLinkage(GD); } else { - Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), - llvm::PointerType::getUnqual(DeclTy), + Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, 0, /*D=*/nullptr); if (const auto *VD = dyn_cast(GD.getDecl())) LT = getLLVMLinkageVarDefinition(VD, D->getType().isConstQualified());