Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / unittests / IR / AttributesTest.cpp
blobda72fa14510cbeabe751ecd644241d5449be9525
1 //===- llvm/unittest/IR/AttributesTest.cpp - Attributes unit 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/Attributes.h"
10 #include "llvm-c/Core.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/AttributeMask.h"
13 #include "llvm/IR/ConstantRange.h"
14 #include "llvm/IR/DerivedTypes.h"
15 #include "llvm/IR/InstrTypes.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "gtest/gtest.h"
20 using namespace llvm;
22 namespace {
24 TEST(Attributes, Uniquing) {
25 LLVMContext C;
27 Attribute AttrA = Attribute::get(C, Attribute::AlwaysInline);
28 Attribute AttrB = Attribute::get(C, Attribute::AlwaysInline);
29 EXPECT_EQ(AttrA, AttrB);
31 AttributeList ASs[] = {AttributeList::get(C, 1, Attribute::ZExt),
32 AttributeList::get(C, 2, Attribute::SExt)};
34 AttributeList SetA = AttributeList::get(C, ASs);
35 AttributeList SetB = AttributeList::get(C, ASs);
36 EXPECT_EQ(SetA, SetB);
39 TEST(Attributes, Ordering) {
40 LLVMContext C;
42 Attribute Align4 = Attribute::get(C, Attribute::Alignment, 4);
43 Attribute Align5 = Attribute::get(C, Attribute::Alignment, 5);
44 Attribute Deref4 = Attribute::get(C, Attribute::Dereferenceable, 4);
45 Attribute Deref5 = Attribute::get(C, Attribute::Dereferenceable, 5);
46 EXPECT_TRUE(Align4 < Align5);
47 EXPECT_TRUE(Align4 < Deref4);
48 EXPECT_TRUE(Align4 < Deref5);
49 EXPECT_TRUE(Align5 < Deref4);
51 Attribute ByVal = Attribute::get(C, Attribute::ByVal, Type::getInt32Ty(C));
52 EXPECT_FALSE(ByVal < Attribute::get(C, Attribute::ZExt));
53 EXPECT_TRUE(ByVal < Align4);
54 EXPECT_FALSE(ByVal < ByVal);
56 AttributeList ASs[] = {AttributeList::get(C, 2, Attribute::ZExt),
57 AttributeList::get(C, 1, Attribute::SExt)};
59 AttributeList SetA = AttributeList::get(C, ASs);
60 AttributeList SetB =
61 SetA.removeParamAttributes(C, 0, ASs[1].getParamAttrs(0));
62 EXPECT_NE(SetA, SetB);
65 TEST(Attributes, AddAttributes) {
66 LLVMContext C;
67 AttributeList AL;
68 AttrBuilder B(C);
69 B.addAttribute(Attribute::NoReturn);
70 AL = AL.addFnAttributes(C, AttrBuilder(C, AttributeSet::get(C, B)));
71 EXPECT_TRUE(AL.hasFnAttr(Attribute::NoReturn));
72 B.clear();
73 B.addAttribute(Attribute::SExt);
74 AL = AL.addRetAttributes(C, B);
75 EXPECT_TRUE(AL.hasRetAttr(Attribute::SExt));
76 EXPECT_TRUE(AL.hasFnAttr(Attribute::NoReturn));
79 TEST(Attributes, RemoveAlign) {
80 LLVMContext C;
82 Attribute AlignAttr = Attribute::getWithAlignment(C, Align(8));
83 Attribute StackAlignAttr = Attribute::getWithStackAlignment(C, Align(32));
84 AttrBuilder B_align_readonly(C);
85 B_align_readonly.addAttribute(AlignAttr);
86 B_align_readonly.addAttribute(Attribute::ReadOnly);
87 AttributeMask B_align;
88 B_align.addAttribute(AlignAttr);
89 AttrBuilder B_stackalign_optnone(C);
90 B_stackalign_optnone.addAttribute(StackAlignAttr);
91 B_stackalign_optnone.addAttribute(Attribute::OptimizeNone);
92 AttributeMask B_stackalign;
93 B_stackalign.addAttribute(StackAlignAttr);
95 AttributeSet AS = AttributeSet::get(C, B_align_readonly);
96 EXPECT_TRUE(AS.getAlignment() == MaybeAlign(8));
97 EXPECT_TRUE(AS.hasAttribute(Attribute::ReadOnly));
98 AS = AS.removeAttribute(C, Attribute::Alignment);
99 EXPECT_FALSE(AS.hasAttribute(Attribute::Alignment));
100 EXPECT_TRUE(AS.hasAttribute(Attribute::ReadOnly));
101 AS = AttributeSet::get(C, B_align_readonly);
102 AS = AS.removeAttributes(C, B_align);
103 EXPECT_TRUE(AS.getAlignment() == std::nullopt);
104 EXPECT_TRUE(AS.hasAttribute(Attribute::ReadOnly));
106 AttributeList AL;
107 AL = AL.addParamAttributes(C, 0, B_align_readonly);
108 AL = AL.addRetAttributes(C, B_stackalign_optnone);
109 EXPECT_TRUE(AL.hasRetAttrs());
110 EXPECT_TRUE(AL.hasRetAttr(Attribute::StackAlignment));
111 EXPECT_TRUE(AL.hasRetAttr(Attribute::OptimizeNone));
112 EXPECT_TRUE(AL.getRetStackAlignment() == MaybeAlign(32));
113 EXPECT_TRUE(AL.hasParamAttrs(0));
114 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::Alignment));
115 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::ReadOnly));
116 EXPECT_TRUE(AL.getParamAlignment(0) == MaybeAlign(8));
118 AL = AL.removeParamAttribute(C, 0, Attribute::Alignment);
119 EXPECT_FALSE(AL.hasParamAttr(0, Attribute::Alignment));
120 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::ReadOnly));
121 EXPECT_TRUE(AL.hasRetAttr(Attribute::StackAlignment));
122 EXPECT_TRUE(AL.hasRetAttr(Attribute::OptimizeNone));
123 EXPECT_TRUE(AL.getRetStackAlignment() == MaybeAlign(32));
125 AL = AL.removeRetAttribute(C, Attribute::StackAlignment);
126 EXPECT_FALSE(AL.hasParamAttr(0, Attribute::Alignment));
127 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::ReadOnly));
128 EXPECT_FALSE(AL.hasRetAttr(Attribute::StackAlignment));
129 EXPECT_TRUE(AL.hasRetAttr(Attribute::OptimizeNone));
131 AttributeList AL2;
132 AL2 = AL2.addParamAttributes(C, 0, B_align_readonly);
133 AL2 = AL2.addRetAttributes(C, B_stackalign_optnone);
135 AL2 = AL2.removeParamAttributes(C, 0, B_align);
136 EXPECT_FALSE(AL2.hasParamAttr(0, Attribute::Alignment));
137 EXPECT_TRUE(AL2.hasParamAttr(0, Attribute::ReadOnly));
138 EXPECT_TRUE(AL2.hasRetAttr(Attribute::StackAlignment));
139 EXPECT_TRUE(AL2.hasRetAttr(Attribute::OptimizeNone));
140 EXPECT_TRUE(AL2.getRetStackAlignment() == MaybeAlign(32));
142 AL2 = AL2.removeRetAttributes(C, B_stackalign);
143 EXPECT_FALSE(AL2.hasParamAttr(0, Attribute::Alignment));
144 EXPECT_TRUE(AL2.hasParamAttr(0, Attribute::ReadOnly));
145 EXPECT_FALSE(AL2.hasRetAttr(Attribute::StackAlignment));
146 EXPECT_TRUE(AL2.hasRetAttr(Attribute::OptimizeNone));
149 TEST(Attributes, AddMatchingAlignAttr) {
150 LLVMContext C;
151 AttributeList AL;
152 AL = AL.addParamAttribute(C, 0, Attribute::getWithAlignment(C, Align(8)));
153 AL = AL.addParamAttribute(C, 1, Attribute::getWithAlignment(C, Align(32)));
154 EXPECT_EQ(Align(8), AL.getParamAlignment(0));
155 EXPECT_EQ(Align(32), AL.getParamAlignment(1));
157 AttrBuilder B(C);
158 B.addAttribute(Attribute::NonNull);
159 B.addAlignmentAttr(8);
160 AL = AL.addParamAttributes(C, 0, B);
161 EXPECT_EQ(Align(8), AL.getParamAlignment(0));
162 EXPECT_EQ(Align(32), AL.getParamAlignment(1));
163 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::NonNull));
166 TEST(Attributes, EmptyGet) {
167 LLVMContext C;
168 AttributeList EmptyLists[] = {AttributeList(), AttributeList()};
169 AttributeList AL = AttributeList::get(C, EmptyLists);
170 EXPECT_TRUE(AL.isEmpty());
173 TEST(Attributes, OverflowGet) {
174 LLVMContext C;
175 std::pair<unsigned, Attribute> Attrs[] = { { AttributeList::ReturnIndex, Attribute::get(C, Attribute::SExt) },
176 { AttributeList::FunctionIndex, Attribute::get(C, Attribute::ReadOnly) } };
177 AttributeList AL = AttributeList::get(C, Attrs);
178 EXPECT_EQ(2U, AL.getNumAttrSets());
181 TEST(Attributes, StringRepresentation) {
182 LLVMContext C;
183 StructType *Ty = StructType::create(Type::getInt32Ty(C), "mystruct");
185 // Insufficiently careful printing can result in byval(%mystruct = { i32 })
186 Attribute A = Attribute::getWithByValType(C, Ty);
187 EXPECT_EQ(A.getAsString(), "byval(%mystruct)");
189 A = Attribute::getWithByValType(C, Type::getInt32Ty(C));
190 EXPECT_EQ(A.getAsString(), "byval(i32)");
193 TEST(Attributes, HasParentContext) {
194 LLVMContext C1, C2;
197 Attribute Attr1 = Attribute::get(C1, Attribute::AlwaysInline);
198 Attribute Attr2 = Attribute::get(C2, Attribute::AlwaysInline);
199 EXPECT_TRUE(Attr1.hasParentContext(C1));
200 EXPECT_FALSE(Attr1.hasParentContext(C2));
201 EXPECT_FALSE(Attr2.hasParentContext(C1));
202 EXPECT_TRUE(Attr2.hasParentContext(C2));
206 AttributeSet AS1 = AttributeSet::get(
207 C1, ArrayRef(Attribute::get(C1, Attribute::NoReturn)));
208 AttributeSet AS2 = AttributeSet::get(
209 C2, ArrayRef(Attribute::get(C2, Attribute::NoReturn)));
210 EXPECT_TRUE(AS1.hasParentContext(C1));
211 EXPECT_FALSE(AS1.hasParentContext(C2));
212 EXPECT_FALSE(AS2.hasParentContext(C1));
213 EXPECT_TRUE(AS2.hasParentContext(C2));
217 AttributeList AL1 = AttributeList::get(C1, 1, Attribute::ZExt);
218 AttributeList AL2 = AttributeList::get(C2, 1, Attribute::ZExt);
219 EXPECT_TRUE(AL1.hasParentContext(C1));
220 EXPECT_FALSE(AL1.hasParentContext(C2));
221 EXPECT_FALSE(AL2.hasParentContext(C1));
222 EXPECT_TRUE(AL2.hasParentContext(C2));
226 TEST(Attributes, AttributeListPrinting) {
227 LLVMContext C;
230 std::string S;
231 raw_string_ostream OS(S);
232 AttributeList AL;
233 AL.addFnAttribute(C, Attribute::AlwaysInline).print(OS);
234 EXPECT_EQ(S, "AttributeList[\n"
235 " { function => alwaysinline }\n"
236 "]\n");
240 std::string S;
241 raw_string_ostream OS(S);
242 AttributeList AL;
243 AL.addRetAttribute(C, Attribute::SExt).print(OS);
244 EXPECT_EQ(S, "AttributeList[\n"
245 " { return => signext }\n"
246 "]\n");
250 std::string S;
251 raw_string_ostream OS(S);
252 AttributeList AL;
253 AL.addParamAttribute(C, 5, Attribute::ZExt).print(OS);
254 EXPECT_EQ(S, "AttributeList[\n"
255 " { arg(5) => zeroext }\n"
256 "]\n");
260 TEST(Attributes, MismatchedABIAttrs) {
261 const char *IRString = R"IR(
262 declare void @f1(i32* byval(i32))
263 define void @g() {
264 call void @f1(i32* null)
265 ret void
267 declare void @f2(i32* preallocated(i32))
268 define void @h() {
269 call void @f2(i32* null)
270 ret void
272 declare void @f3(i32* inalloca(i32))
273 define void @i() {
274 call void @f3(i32* null)
275 ret void
277 )IR";
279 SMDiagnostic Err;
280 LLVMContext Context;
281 std::unique_ptr<Module> M = parseAssemblyString(IRString, Err, Context);
282 ASSERT_TRUE(M);
285 auto *I = cast<CallBase>(&M->getFunction("g")->getEntryBlock().front());
286 ASSERT_TRUE(I->isByValArgument(0));
287 ASSERT_TRUE(I->getParamByValType(0));
290 auto *I = cast<CallBase>(&M->getFunction("h")->getEntryBlock().front());
291 ASSERT_TRUE(I->getParamPreallocatedType(0));
294 auto *I = cast<CallBase>(&M->getFunction("i")->getEntryBlock().front());
295 ASSERT_TRUE(I->isInAllocaArgument(0));
296 ASSERT_TRUE(I->getParamInAllocaType(0));
300 TEST(Attributes, RemoveParamAttributes) {
301 LLVMContext C;
302 AttributeList AL;
303 AL = AL.addParamAttribute(C, 1, Attribute::NoUndef);
304 EXPECT_EQ(AL.getNumAttrSets(), 4U);
305 AL = AL.addParamAttribute(C, 3, Attribute::NonNull);
306 EXPECT_EQ(AL.getNumAttrSets(), 6U);
307 AL = AL.removeParamAttributes(C, 3);
308 EXPECT_EQ(AL.getNumAttrSets(), 4U);
309 AL = AL.removeParamAttribute(C, 1, Attribute::NoUndef);
310 EXPECT_EQ(AL.getNumAttrSets(), 0U);
313 TEST(Attributes, ConstantRangeAttributeCAPI) {
314 LLVMContext C;
316 const unsigned NumBits = 8;
317 const uint64_t LowerWords[] = {0};
318 const uint64_t UpperWords[] = {42};
320 ConstantRange Range(APInt(NumBits, ArrayRef(LowerWords)),
321 APInt(NumBits, ArrayRef(UpperWords)));
323 Attribute RangeAttr = Attribute::get(C, Attribute::Range, Range);
324 auto OutAttr = unwrap(LLVMCreateConstantRangeAttribute(
325 wrap(&C), Attribute::Range, NumBits, LowerWords, UpperWords));
326 EXPECT_EQ(OutAttr, RangeAttr);
329 const unsigned NumBits = 128;
330 const uint64_t LowerWords[] = {1, 1};
331 const uint64_t UpperWords[] = {42, 42};
333 ConstantRange Range(APInt(NumBits, ArrayRef(LowerWords)),
334 APInt(NumBits, ArrayRef(UpperWords)));
336 Attribute RangeAttr = Attribute::get(C, Attribute::Range, Range);
337 auto OutAttr = unwrap(LLVMCreateConstantRangeAttribute(
338 wrap(&C), Attribute::Range, NumBits, LowerWords, UpperWords));
339 EXPECT_EQ(OutAttr, RangeAttr);
343 TEST(Attributes, CalleeAttributes) {
344 const char *IRString = R"IR(
345 declare void @f1(i32 %i)
346 declare void @f2(i32 range(i32 1, 2) %i)
348 define void @g1(i32 %i) {
349 call void @f1(i32 %i)
350 ret void
352 define void @g2(i32 %i) {
353 call void @f2(i32 %i)
354 ret void
356 define void @g3(i32 %i) {
357 call void @f1(i32 range(i32 3, 4) %i)
358 ret void
360 define void @g4(i32 %i) {
361 call void @f2(i32 range(i32 3, 4) %i)
362 ret void
364 )IR";
366 SMDiagnostic Err;
367 LLVMContext Context;
368 std::unique_ptr<Module> M = parseAssemblyString(IRString, Err, Context);
369 ASSERT_TRUE(M);
372 auto *I = cast<CallBase>(&M->getFunction("g1")->getEntryBlock().front());
373 ASSERT_FALSE(I->getParamAttr(0, Attribute::Range).isValid());
376 auto *I = cast<CallBase>(&M->getFunction("g2")->getEntryBlock().front());
377 ASSERT_TRUE(I->getParamAttr(0, Attribute::Range).isValid());
380 auto *I = cast<CallBase>(&M->getFunction("g3")->getEntryBlock().front());
381 ASSERT_TRUE(I->getParamAttr(0, Attribute::Range).isValid());
384 auto *I = cast<CallBase>(&M->getFunction("g4")->getEntryBlock().front());
385 ASSERT_TRUE(I->getParamAttr(0, Attribute::Range).isValid());
389 } // end anonymous namespace