1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00
llvm-mirror/unittests/IR/DataLayoutTest.cpp
Alex Richardson 9c96f39f77 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
2020-11-20 15:46:52 +00:00

93 lines
3.8 KiB
C++

//===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#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"
using namespace llvm;
namespace {
TEST(DataLayoutTest, FunctionPtrAlign) {
EXPECT_EQ(MaybeAlign(0), DataLayout("").getFunctionPtrAlign());
EXPECT_EQ(MaybeAlign(1), DataLayout("Fi8").getFunctionPtrAlign());
EXPECT_EQ(MaybeAlign(2), DataLayout("Fi16").getFunctionPtrAlign());
EXPECT_EQ(MaybeAlign(4), DataLayout("Fi32").getFunctionPtrAlign());
EXPECT_EQ(MaybeAlign(8), DataLayout("Fi64").getFunctionPtrAlign());
EXPECT_EQ(MaybeAlign(1), DataLayout("Fn8").getFunctionPtrAlign());
EXPECT_EQ(MaybeAlign(2), DataLayout("Fn16").getFunctionPtrAlign());
EXPECT_EQ(MaybeAlign(4), DataLayout("Fn32").getFunctionPtrAlign());
EXPECT_EQ(MaybeAlign(8), DataLayout("Fn64").getFunctionPtrAlign());
EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent, \
DataLayout("").getFunctionPtrAlignType());
EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent, \
DataLayout("Fi8").getFunctionPtrAlignType());
EXPECT_EQ(DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign, \
DataLayout("Fn8").getFunctionPtrAlignType());
EXPECT_EQ(DataLayout("Fi8"), DataLayout("Fi8"));
EXPECT_NE(DataLayout("Fi8"), DataLayout("Fi16"));
EXPECT_NE(DataLayout("Fi8"), DataLayout("Fn8"));
DataLayout a(""), b("Fi8"), c("Fn8");
EXPECT_NE(a, b);
EXPECT_NE(a, c);
EXPECT_NE(b, c);
a = b;
EXPECT_EQ(a, b);
a = c;
EXPECT_EQ(a, c);
}
TEST(DataLayoutTest, ValueOrABITypeAlignment) {
const DataLayout DL("Fi8");
LLVMContext Context;
Type *const FourByteAlignType = Type::getInt32Ty(Context);
EXPECT_EQ(Align(16),
DL.getValueOrABITypeAlignment(MaybeAlign(16), FourByteAlignType));
EXPECT_EQ(Align(4),
DL.getValueOrABITypeAlignment(MaybeAlign(), FourByteAlignType));
}
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