From 9c96f39f77400168f0fb59759b996122376056a6 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Fri, 20 Nov 2020 15:17:52 +0000 Subject: [PATCH] Add a default address space for globals to DataLayout This is similar to the existing alloca and program address spaces (D37052) and should be used when creating/accessing global variables. We need this in our CHERI fork of LLVM to place all globals in address space 200. This ensures that values are accessed using CHERI load/store instructions instead of the normal MIPS/RISC-V ones. The problem this is trying to fix is that most of the time the type of globals is created using a simple PointerType::getUnqual() (or ::get() with the default address-space value of 0). This does not work for us and we get assertion/compilation/instruction selection failures whenever a new call is added that uses the default value of zero. In our fork we have removed the default parameter value of zero for most address space arguments and use DL.getProgramAddressSpace() or DL.getGlobalsAddressSpace() whenever possible. If this change is accepted, I will upstream follow-up patches to use DL.getGlobalsAddressSpace() instead of relying on the default value of 0 for PointerType::get(), etc. This patch and the follow-up changes will not have any functional changes for existing backends with the default globals address space of zero. A follow-up commit will change the default globals address space for AMDGPU to 1. Reviewed By: dylanmckay Differential Revision: https://reviews.llvm.org/D70947 --- docs/LangRef.rst | 8 +++++ include/llvm/IR/DataLayout.h | 5 +++ include/llvm/IR/GlobalVariable.h | 9 ++--- lib/IR/DataLayout.cpp | 7 ++++ lib/IR/Globals.cpp | 8 +++-- .../invalid-datalayout-globals-addrspace.ll | 4 +++ unittests/IR/DataLayoutTest.cpp | 35 ++++++++++++++++++- 7 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 test/Assembler/invalid-datalayout-globals-addrspace.ll diff --git a/docs/LangRef.rst b/docs/LangRef.rst index e62d4d42899..47f24e9aa9f 100644 --- a/docs/LangRef.rst +++ b/docs/LangRef.rst @@ -2370,6 +2370,14 @@ as follows: 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/include/llvm/IR/DataLayout.h b/include/llvm/IR/DataLayout.h index a58f3813001..bf273038c51 100644 --- a/include/llvm/IR/DataLayout.h +++ b/include/llvm/IR/DataLayout.h @@ -123,6 +123,7 @@ private: unsigned AllocaAddrSpace; MaybeAlign StackNaturalAlign; unsigned ProgramAddrSpace; + unsigned DefaultGlobalsAddrSpace; MaybeAlign FunctionPtrAlign; FunctionPtrAlignType TheFunctionPtrAlignType; @@ -219,6 +220,7 @@ public: FunctionPtrAlign = DL.FunctionPtrAlign; TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType; ProgramAddrSpace = DL.ProgramAddrSpace; + DefaultGlobalsAddrSpace = DL.DefaultGlobalsAddrSpace; ManglingMode = DL.ManglingMode; LegalIntWidths = DL.LegalIntWidths; Alignments = DL.Alignments; @@ -295,6 +297,9 @@ public: } unsigned getProgramAddressSpace() const { return ProgramAddrSpace; } + unsigned getDefaultGlobalsAddressSpace() const { + return DefaultGlobalsAddrSpace; + } bool hasMicrosoftFastStdCallMangling() const { return ManglingMode == MM_WinCOFFX86; diff --git a/include/llvm/IR/GlobalVariable.h b/include/llvm/IR/GlobalVariable.h index 12093e337d6..674d49eb9de 100644 --- a/include/llvm/IR/GlobalVariable.h +++ b/include/llvm/IR/GlobalVariable.h @@ -56,10 +56,11 @@ public: 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, + Optional AddressSpace = None, bool isExternallyInitialized = false); GlobalVariable(const GlobalVariable &) = delete; GlobalVariable &operator=(const GlobalVariable &) = delete; diff --git a/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index ffb3adcdbf8..66ae982e512 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -182,6 +182,7 @@ void DataLayout::reset(StringRef Desc) { AllocaAddrSpace = 0; StackNaturalAlign.reset(); ProgramAddrSpace = 0; + DefaultGlobalsAddrSpace = 0; FunctionPtrAlign.reset(); TheFunctionPtrAlignType = FunctionPtrAlignType::Independent; ManglingMode = MM_None; @@ -479,6 +480,11 @@ Error DataLayout::parseSpecifier(StringRef Desc) { return Err; break; } + case 'G': { // Default address space for global variables. + if (Error Err = getAddrSpace(Tok, DefaultGlobalsAddrSpace)) + return Err; + break; + } case 'm': if (!Tok.empty()) return reportError("Unexpected trailing characters after mangling " @@ -530,6 +536,7 @@ bool DataLayout::operator==(const DataLayout &Other) const { AllocaAddrSpace == Other.AllocaAddrSpace && StackNaturalAlign == Other.StackNaturalAlign && ProgramAddrSpace == Other.ProgramAddrSpace && + DefaultGlobalsAddrSpace == Other.DefaultGlobalsAddrSpace && FunctionPtrAlign == Other.FunctionPtrAlign && TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType && ManglingMode == Other.ManglingMode && diff --git a/lib/IR/Globals.cpp b/lib/IR/Globals.cpp index ed946ef3fd1..c2cbe7ddddf 100644 --- a/lib/IR/Globals.cpp +++ b/lib/IR/Globals.cpp @@ -352,11 +352,15 @@ GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, LinkageTypes Link, Constant *InitVal, const Twine &Name, GlobalVariable *Before, - ThreadLocalMode TLMode, unsigned AddressSpace, + ThreadLocalMode TLMode, + Optional AddressSpace, bool isExternallyInitialized) : GlobalObject(Ty, Value::GlobalVariableVal, OperandTraits::op_begin(this), - InitVal != nullptr, Link, Name, AddressSpace), + InitVal != nullptr, Link, Name, + AddressSpace + ? *AddressSpace + : M.getDataLayout().getDefaultGlobalsAddressSpace()), isConstantGlobal(constant), isExternallyInitializedConstant(isExternallyInitialized) { assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) && diff --git a/test/Assembler/invalid-datalayout-globals-addrspace.ll b/test/Assembler/invalid-datalayout-globals-addrspace.ll new file mode 100644 index 00000000000..2c01b6c0019 --- /dev/null +++ b/test/Assembler/invalid-datalayout-globals-addrspace.ll @@ -0,0 +1,4 @@ +; RUN: not --crash llvm-as < %s 2>&1 | FileCheck %s + +; CHECK: Invalid address space, must be a 24-bit integer +target datalayout = "G16777216" diff --git a/unittests/IR/DataLayoutTest.cpp b/unittests/IR/DataLayoutTest.cpp index de8ac253c45..3f860c8eaad 100644 --- a/unittests/IR/DataLayoutTest.cpp +++ b/unittests/IR/DataLayoutTest.cpp @@ -7,7 +7,9 @@ //===----------------------------------------------------------------------===// #include "llvm/IR/DataLayout.h" +#include "llvm/IR/GlobalVariable.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "gtest/gtest.h" @@ -56,4 +58,35 @@ TEST(DataLayoutTest, ValueOrABITypeAlignment) { DL.getValueOrABITypeAlignment(MaybeAlign(), FourByteAlignType)); } -} // anonymous namespace +TEST(DataLayoutTest, GlobalsAddressSpace) { + // When not explicitly defined the globals address space should be zero: + EXPECT_EQ(DataLayout("").getDefaultGlobalsAddressSpace(), 0u); + EXPECT_EQ(DataLayout("P1-A2").getDefaultGlobalsAddressSpace(), 0u); + EXPECT_EQ(DataLayout("G2").getDefaultGlobalsAddressSpace(), 2u); + // Check that creating a GlobalVariable without an explicit address space + // in a module with a default globals address space respects that default: + LLVMContext Context; + std::unique_ptr M(new Module("MyModule", Context)); + // Default is globals in address space zero: + auto *Int32 = Type::getInt32Ty(Context); + auto *DefaultGlobal1 = new GlobalVariable( + *M, Int32, false, GlobalValue::ExternalLinkage, nullptr); + EXPECT_EQ(DefaultGlobal1->getAddressSpace(), 0u); + auto *ExplicitGlobal1 = new GlobalVariable( + *M, Int32, false, GlobalValue::ExternalLinkage, nullptr, "", nullptr, + GlobalValue::NotThreadLocal, 123); + EXPECT_EQ(ExplicitGlobal1->getAddressSpace(), 123u); + + // When using a datalayout with the global address space set to 200, global + // variables should default to 200 + M->setDataLayout("G200"); + auto *DefaultGlobal2 = new GlobalVariable( + *M, Int32, false, GlobalValue::ExternalLinkage, nullptr); + EXPECT_EQ(DefaultGlobal2->getAddressSpace(), 200u); + auto *ExplicitGlobal2 = new GlobalVariable( + *M, Int32, false, GlobalValue::ExternalLinkage, nullptr, "", nullptr, + GlobalValue::NotThreadLocal, 123); + EXPECT_EQ(ExplicitGlobal2->getAddressSpace(), 123u); +} + +} // anonymous namespace