[llvm] 3bc4157 - Add a default address space for globals to DataLayout

Alex Richardson via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 20 07:47:06 PST 2020


Author: Alex Richardson
Date: 2020-11-20T15:46:52Z
New Revision: 3bc4157556b01e10288df120dd068e093ae20901

URL: https://github.com/llvm/llvm-project/commit/3bc4157556b01e10288df120dd068e093ae20901
DIFF: https://github.com/llvm/llvm-project/commit/3bc4157556b01e10288df120dd068e093ae20901.diff

LOG: 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

Added: 
    llvm/test/Assembler/invalid-datalayout-globals-addrspace.ll

Modified: 
    llvm/docs/LangRef.rst
    llvm/include/llvm/IR/DataLayout.h
    llvm/include/llvm/IR/GlobalVariable.h
    llvm/lib/IR/DataLayout.cpp
    llvm/lib/IR/Globals.cpp
    llvm/unittests/IR/DataLayoutTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index e62d4d428997..47f24e9aa9f7 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/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<address space>``
+    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<address space>``
     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
index a58f38130016..bf273038c51e 100644
--- a/llvm/include/llvm/IR/DataLayout.h
+++ b/llvm/include/llvm/IR/DataLayout.h
@@ -123,6 +123,7 @@ class DataLayout {
   unsigned AllocaAddrSpace;
   MaybeAlign StackNaturalAlign;
   unsigned ProgramAddrSpace;
+  unsigned DefaultGlobalsAddrSpace;
 
   MaybeAlign FunctionPtrAlign;
   FunctionPtrAlignType TheFunctionPtrAlignType;
@@ -219,6 +220,7 @@ class DataLayout {
     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 @@ class DataLayout {
   }
 
   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
index 12093e337d6e..674d49eb9de6 100644
--- a/llvm/include/llvm/IR/GlobalVariable.h
+++ b/llvm/include/llvm/IR/GlobalVariable.h
@@ -56,10 +56,11 @@ class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
                  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<unsigned> AddressSpace = None,
                  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
index ffb3adcdbf8a..66ae982e5120 100644
--- a/llvm/lib/IR/DataLayout.cpp
+++ b/llvm/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/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index ed946ef3fd12..c2cbe7ddddf8 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/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<unsigned> AddressSpace,
                                bool isExternallyInitialized)
     : GlobalObject(Ty, Value::GlobalVariableVal,
                    OperandTraits<GlobalVariable>::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/llvm/test/Assembler/invalid-datalayout-globals-addrspace.ll b/llvm/test/Assembler/invalid-datalayout-globals-addrspace.ll
new file mode 100644
index 000000000000..2c01b6c0019e
--- /dev/null
+++ b/llvm/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/llvm/unittests/IR/DataLayoutTest.cpp b/llvm/unittests/IR/DataLayoutTest.cpp
index de8ac253c45c..3f860c8eaad1 100644
--- a/llvm/unittests/IR/DataLayoutTest.cpp
+++ b/llvm/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<Module> 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


        


More information about the llvm-commits mailing list