workflows: Fix typo in pr-subscriber
[llvm-project.git] / llvm / unittests / IR / DataLayoutTest.cpp
blobb9fc2172b7bf2ebf27d9992b7fa7962219207ef0
1 //===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/DataLayout.h"
10 #include "llvm/IR/GlobalVariable.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/IR/Type.h"
14 #include "llvm/Testing/Support/Error.h"
15 #include "gtest/gtest.h"
17 using namespace llvm;
19 namespace {
21 TEST(DataLayoutTest, FunctionPtrAlign) {
22 EXPECT_EQ(MaybeAlign(0), DataLayout("").getFunctionPtrAlign());
23 EXPECT_EQ(MaybeAlign(1), DataLayout("Fi8").getFunctionPtrAlign());
24 EXPECT_EQ(MaybeAlign(2), DataLayout("Fi16").getFunctionPtrAlign());
25 EXPECT_EQ(MaybeAlign(4), DataLayout("Fi32").getFunctionPtrAlign());
26 EXPECT_EQ(MaybeAlign(8), DataLayout("Fi64").getFunctionPtrAlign());
27 EXPECT_EQ(MaybeAlign(1), DataLayout("Fn8").getFunctionPtrAlign());
28 EXPECT_EQ(MaybeAlign(2), DataLayout("Fn16").getFunctionPtrAlign());
29 EXPECT_EQ(MaybeAlign(4), DataLayout("Fn32").getFunctionPtrAlign());
30 EXPECT_EQ(MaybeAlign(8), DataLayout("Fn64").getFunctionPtrAlign());
31 EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent, \
32 DataLayout("").getFunctionPtrAlignType());
33 EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent, \
34 DataLayout("Fi8").getFunctionPtrAlignType());
35 EXPECT_EQ(DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign, \
36 DataLayout("Fn8").getFunctionPtrAlignType());
37 EXPECT_EQ(DataLayout("Fi8"), DataLayout("Fi8"));
38 EXPECT_NE(DataLayout("Fi8"), DataLayout("Fi16"));
39 EXPECT_NE(DataLayout("Fi8"), DataLayout("Fn8"));
41 DataLayout a(""), b("Fi8"), c("Fn8");
42 EXPECT_NE(a, b);
43 EXPECT_NE(a, c);
44 EXPECT_NE(b, c);
46 a = b;
47 EXPECT_EQ(a, b);
48 a = c;
49 EXPECT_EQ(a, c);
52 TEST(DataLayoutTest, ValueOrABITypeAlignment) {
53 const DataLayout DL("Fi8");
54 LLVMContext Context;
55 Type *const FourByteAlignType = Type::getInt32Ty(Context);
56 EXPECT_EQ(Align(16),
57 DL.getValueOrABITypeAlignment(MaybeAlign(16), FourByteAlignType));
58 EXPECT_EQ(Align(4),
59 DL.getValueOrABITypeAlignment(MaybeAlign(), FourByteAlignType));
62 TEST(DataLayoutTest, GlobalsAddressSpace) {
63 // When not explicitly defined the globals address space should be zero:
64 EXPECT_EQ(DataLayout("").getDefaultGlobalsAddressSpace(), 0u);
65 EXPECT_EQ(DataLayout("P1-A2").getDefaultGlobalsAddressSpace(), 0u);
66 EXPECT_EQ(DataLayout("G2").getDefaultGlobalsAddressSpace(), 2u);
67 // Check that creating a GlobalVariable without an explicit address space
68 // in a module with a default globals address space respects that default:
69 LLVMContext Context;
70 std::unique_ptr<Module> M(new Module("MyModule", Context));
71 // Default is globals in address space zero:
72 auto *Int32 = Type::getInt32Ty(Context);
73 auto *DefaultGlobal1 = new GlobalVariable(
74 *M, Int32, false, GlobalValue::ExternalLinkage, nullptr);
75 EXPECT_EQ(DefaultGlobal1->getAddressSpace(), 0u);
76 auto *ExplicitGlobal1 = new GlobalVariable(
77 *M, Int32, false, GlobalValue::ExternalLinkage, nullptr, "", nullptr,
78 GlobalValue::NotThreadLocal, 123);
79 EXPECT_EQ(ExplicitGlobal1->getAddressSpace(), 123u);
81 // When using a datalayout with the global address space set to 200, global
82 // variables should default to 200
83 M->setDataLayout("G200");
84 auto *DefaultGlobal2 = new GlobalVariable(
85 *M, Int32, false, GlobalValue::ExternalLinkage, nullptr);
86 EXPECT_EQ(DefaultGlobal2->getAddressSpace(), 200u);
87 auto *ExplicitGlobal2 = new GlobalVariable(
88 *M, Int32, false, GlobalValue::ExternalLinkage, nullptr, "", nullptr,
89 GlobalValue::NotThreadLocal, 123);
90 EXPECT_EQ(ExplicitGlobal2->getAddressSpace(), 123u);
93 TEST(DataLayoutTest, VectorAlign) {
94 Expected<DataLayout> DL = DataLayout::parse("v64:64");
95 EXPECT_THAT_EXPECTED(DL, Succeeded());
97 LLVMContext Context;
98 Type *const FloatTy = Type::getFloatTy(Context);
99 Type *const V8F32Ty = FixedVectorType::get(FloatTy, 8);
101 // The alignment for a vector type larger than any specified vector type uses
102 // the natural alignment as a fallback.
103 EXPECT_EQ(Align(4 * 8), DL->getABITypeAlign(V8F32Ty));
104 EXPECT_EQ(Align(4 * 8), DL->getPrefTypeAlign(V8F32Ty));
107 } // anonymous namespace