Skip to content

Commit

Permalink
Add a default address space for globals to DataLayout
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arichardson committed Nov 20, 2020
1 parent 4766a86 commit 3bc4157
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 7 deletions.
8 changes: 8 additions & 0 deletions llvm/docs/LangRef.rst
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/IR/DataLayout.h
Expand Up @@ -123,6 +123,7 @@ class DataLayout {
unsigned AllocaAddrSpace;
MaybeAlign StackNaturalAlign;
unsigned ProgramAddrSpace;
unsigned DefaultGlobalsAddrSpace;

MaybeAlign FunctionPtrAlign;
FunctionPtrAlignType TheFunctionPtrAlignType;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -295,6 +297,9 @@ class DataLayout {
}

unsigned getProgramAddressSpace() const { return ProgramAddrSpace; }
unsigned getDefaultGlobalsAddressSpace() const {
return DefaultGlobalsAddrSpace;
}

bool hasMicrosoftFastStdCallMangling() const {
return ManglingMode == MM_WinCOFFX86;
Expand Down
9 changes: 5 additions & 4 deletions llvm/include/llvm/IR/GlobalVariable.h
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/IR/DataLayout.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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 &&
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/IR/Globals.cpp
Expand Up @@ -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) &&
Expand Down
4 changes: 4 additions & 0 deletions 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"
35 changes: 34 additions & 1 deletion llvm/unittests/IR/DataLayoutTest.cpp
Expand Up @@ -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"

Expand Down Expand Up @@ -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

0 comments on commit 3bc4157

Please sign in to comment.