diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -2228,6 +2228,14 @@ program memory space defaults to the default address space of 0, which corresponds to a Von Neumann architecture that has code and data in the same space. +``G
`` + Specifies the address space to be used by default when creating global + variables. If omitted, the globals address space defaults to the default + address space 0. + Note: variable declarations without an address space are always created in + address space 0, this property only affects the default value to be used + when creating globals without additional contextual information (e.g. in + LLVM passes). ``A`` Specifies the address space of objects created by '``alloca``'. Defaults to the default address space of 0. diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h --- a/llvm/include/llvm/IR/DataLayout.h +++ b/llvm/include/llvm/IR/DataLayout.h @@ -123,6 +123,7 @@ unsigned AllocaAddrSpace; MaybeAlign StackNaturalAlign; unsigned ProgramAddrSpace; + unsigned DefaultGlobalsAddrSpace; MaybeAlign FunctionPtrAlign; FunctionPtrAlignType TheFunctionPtrAlignType; @@ -212,6 +213,7 @@ FunctionPtrAlign = DL.FunctionPtrAlign; TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType; ProgramAddrSpace = DL.ProgramAddrSpace; + DefaultGlobalsAddrSpace = DL.DefaultGlobalsAddrSpace; ManglingMode = DL.ManglingMode; LegalIntWidths = DL.LegalIntWidths; Alignments = DL.Alignments; @@ -284,6 +286,9 @@ } unsigned getProgramAddressSpace() const { return ProgramAddrSpace; } + unsigned getDefaultGlobalsAddressSpace() const { + return DefaultGlobalsAddrSpace; + } bool hasMicrosoftFastStdCallMangling() const { return ManglingMode == MM_WinCOFFX86; diff --git a/llvm/include/llvm/IR/GlobalVariable.h b/llvm/include/llvm/IR/GlobalVariable.h --- a/llvm/include/llvm/IR/GlobalVariable.h +++ b/llvm/include/llvm/IR/GlobalVariable.h @@ -57,10 +57,11 @@ bool isExternallyInitialized = false); /// GlobalVariable ctor - This creates a global and inserts it before the /// specified other global. - GlobalVariable(Module &M, Type *Ty, bool isConstant, - LinkageTypes Linkage, Constant *Initializer, - const Twine &Name = "", GlobalVariable *InsertBefore = nullptr, - ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0, + GlobalVariable(Module &M, Type *Ty, bool isConstant, LinkageTypes Linkage, + Constant *Initializer, const Twine &Name = "", + GlobalVariable *InsertBefore = nullptr, + ThreadLocalMode = NotThreadLocal, + unsigned AddressSpace = UINT_MAX, bool isExternallyInitialized = false); GlobalVariable(const GlobalVariable &) = delete; GlobalVariable &operator=(const GlobalVariable &) = delete; diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp --- a/llvm/lib/IR/DataLayout.cpp +++ b/llvm/lib/IR/DataLayout.cpp @@ -179,6 +179,7 @@ AllocaAddrSpace = 0; StackNaturalAlign.reset(); ProgramAddrSpace = 0; + DefaultGlobalsAddrSpace = 0; FunctionPtrAlign.reset(); TheFunctionPtrAlignType = FunctionPtrAlignType::Independent; ManglingMode = MM_None; @@ -419,6 +420,10 @@ AllocaAddrSpace = getAddrSpace(Tok); break; } + case 'G': { // Default address space for global variables. + DefaultGlobalsAddrSpace = getAddrSpace(Tok); + break; + } case 'm': if (!Tok.empty()) report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string"); @@ -464,6 +469,7 @@ AllocaAddrSpace == Other.AllocaAddrSpace && StackNaturalAlign == Other.StackNaturalAlign && ProgramAddrSpace == Other.ProgramAddrSpace && + DefaultGlobalsAddrSpace == Other.DefaultGlobalsAddrSpace && FunctionPtrAlign == Other.FunctionPtrAlign && TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType && ManglingMode == Other.ManglingMode && diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp --- a/llvm/lib/IR/Globals.cpp +++ b/llvm/lib/IR/Globals.cpp @@ -362,7 +362,10 @@ bool isExternallyInitialized) : GlobalObject(Ty, Value::GlobalVariableVal, OperandTraits