mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-24 19:52:54 +01:00
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
This commit is contained in:
parent
82c83de8d2
commit
9c96f39f77
@ -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.
|
||||
|
@ -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;
|
||||
|
@ -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<unsigned> AddressSpace = None,
|
||||
bool isExternallyInitialized = false);
|
||||
GlobalVariable(const GlobalVariable &) = delete;
|
||||
GlobalVariable &operator=(const GlobalVariable &) = delete;
|
||||
|
@ -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 &&
|
||||
|
@ -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) &&
|
||||
|
4
test/Assembler/invalid-datalayout-globals-addrspace.ll
Normal file
4
test/Assembler/invalid-datalayout-globals-addrspace.ll
Normal file
@ -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"
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user