1 //===- llvm/unittest/IR/AttributesTest.cpp - Attributes unit tests --------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/Attributes.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/AttributeMask.h"
12 #include "llvm/IR/DerivedTypes.h"
13 #include "llvm/IR/InstrTypes.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "gtest/gtest.h"
22 TEST(Attributes
, Uniquing
) {
25 Attribute AttrA
= Attribute::get(C
, Attribute::AlwaysInline
);
26 Attribute AttrB
= Attribute::get(C
, Attribute::AlwaysInline
);
27 EXPECT_EQ(AttrA
, AttrB
);
29 AttributeList ASs
[] = {AttributeList::get(C
, 1, Attribute::ZExt
),
30 AttributeList::get(C
, 2, Attribute::SExt
)};
32 AttributeList SetA
= AttributeList::get(C
, ASs
);
33 AttributeList SetB
= AttributeList::get(C
, ASs
);
34 EXPECT_EQ(SetA
, SetB
);
37 TEST(Attributes
, Ordering
) {
40 Attribute Align4
= Attribute::get(C
, Attribute::Alignment
, 4);
41 Attribute Align5
= Attribute::get(C
, Attribute::Alignment
, 5);
42 Attribute Deref4
= Attribute::get(C
, Attribute::Dereferenceable
, 4);
43 Attribute Deref5
= Attribute::get(C
, Attribute::Dereferenceable
, 5);
44 EXPECT_TRUE(Align4
< Align5
);
45 EXPECT_TRUE(Align4
< Deref4
);
46 EXPECT_TRUE(Align4
< Deref5
);
47 EXPECT_TRUE(Align5
< Deref4
);
49 Attribute ByVal
= Attribute::get(C
, Attribute::ByVal
, Type::getInt32Ty(C
));
50 EXPECT_FALSE(ByVal
< Attribute::get(C
, Attribute::ZExt
));
51 EXPECT_TRUE(ByVal
< Align4
);
52 EXPECT_FALSE(ByVal
< ByVal
);
54 AttributeList ASs
[] = {AttributeList::get(C
, 2, Attribute::ZExt
),
55 AttributeList::get(C
, 1, Attribute::SExt
)};
57 AttributeList SetA
= AttributeList::get(C
, ASs
);
59 SetA
.removeParamAttributes(C
, 0, ASs
[1].getParamAttrs(0));
60 EXPECT_NE(SetA
, SetB
);
63 TEST(Attributes
, AddAttributes
) {
67 B
.addAttribute(Attribute::NoReturn
);
68 AL
= AL
.addFnAttributes(C
, AttrBuilder(C
, AttributeSet::get(C
, B
)));
69 EXPECT_TRUE(AL
.hasFnAttr(Attribute::NoReturn
));
71 B
.addAttribute(Attribute::SExt
);
72 AL
= AL
.addRetAttributes(C
, B
);
73 EXPECT_TRUE(AL
.hasRetAttr(Attribute::SExt
));
74 EXPECT_TRUE(AL
.hasFnAttr(Attribute::NoReturn
));
77 TEST(Attributes
, RemoveAlign
) {
80 Attribute AlignAttr
= Attribute::getWithAlignment(C
, Align(8));
81 Attribute StackAlignAttr
= Attribute::getWithStackAlignment(C
, Align(32));
82 AttrBuilder
B_align_readonly(C
);
83 B_align_readonly
.addAttribute(AlignAttr
);
84 B_align_readonly
.addAttribute(Attribute::ReadOnly
);
85 AttributeMask B_align
;
86 B_align
.addAttribute(AlignAttr
);
87 AttrBuilder
B_stackalign_optnone(C
);
88 B_stackalign_optnone
.addAttribute(StackAlignAttr
);
89 B_stackalign_optnone
.addAttribute(Attribute::OptimizeNone
);
90 AttributeMask B_stackalign
;
91 B_stackalign
.addAttribute(StackAlignAttr
);
93 AttributeSet AS
= AttributeSet::get(C
, B_align_readonly
);
94 EXPECT_TRUE(AS
.getAlignment() == MaybeAlign(8));
95 EXPECT_TRUE(AS
.hasAttribute(Attribute::ReadOnly
));
96 AS
= AS
.removeAttribute(C
, Attribute::Alignment
);
97 EXPECT_FALSE(AS
.hasAttribute(Attribute::Alignment
));
98 EXPECT_TRUE(AS
.hasAttribute(Attribute::ReadOnly
));
99 AS
= AttributeSet::get(C
, B_align_readonly
);
100 AS
= AS
.removeAttributes(C
, B_align
);
101 EXPECT_TRUE(AS
.getAlignment() == std::nullopt
);
102 EXPECT_TRUE(AS
.hasAttribute(Attribute::ReadOnly
));
105 AL
= AL
.addParamAttributes(C
, 0, B_align_readonly
);
106 AL
= AL
.addRetAttributes(C
, B_stackalign_optnone
);
107 EXPECT_TRUE(AL
.hasRetAttrs());
108 EXPECT_TRUE(AL
.hasRetAttr(Attribute::StackAlignment
));
109 EXPECT_TRUE(AL
.hasRetAttr(Attribute::OptimizeNone
));
110 EXPECT_TRUE(AL
.getRetStackAlignment() == MaybeAlign(32));
111 EXPECT_TRUE(AL
.hasParamAttrs(0));
112 EXPECT_TRUE(AL
.hasParamAttr(0, Attribute::Alignment
));
113 EXPECT_TRUE(AL
.hasParamAttr(0, Attribute::ReadOnly
));
114 EXPECT_TRUE(AL
.getParamAlignment(0) == MaybeAlign(8));
116 AL
= AL
.removeParamAttribute(C
, 0, Attribute::Alignment
);
117 EXPECT_FALSE(AL
.hasParamAttr(0, Attribute::Alignment
));
118 EXPECT_TRUE(AL
.hasParamAttr(0, Attribute::ReadOnly
));
119 EXPECT_TRUE(AL
.hasRetAttr(Attribute::StackAlignment
));
120 EXPECT_TRUE(AL
.hasRetAttr(Attribute::OptimizeNone
));
121 EXPECT_TRUE(AL
.getRetStackAlignment() == MaybeAlign(32));
123 AL
= AL
.removeRetAttribute(C
, Attribute::StackAlignment
);
124 EXPECT_FALSE(AL
.hasParamAttr(0, Attribute::Alignment
));
125 EXPECT_TRUE(AL
.hasParamAttr(0, Attribute::ReadOnly
));
126 EXPECT_FALSE(AL
.hasRetAttr(Attribute::StackAlignment
));
127 EXPECT_TRUE(AL
.hasRetAttr(Attribute::OptimizeNone
));
130 AL2
= AL2
.addParamAttributes(C
, 0, B_align_readonly
);
131 AL2
= AL2
.addRetAttributes(C
, B_stackalign_optnone
);
133 AL2
= AL2
.removeParamAttributes(C
, 0, B_align
);
134 EXPECT_FALSE(AL2
.hasParamAttr(0, Attribute::Alignment
));
135 EXPECT_TRUE(AL2
.hasParamAttr(0, Attribute::ReadOnly
));
136 EXPECT_TRUE(AL2
.hasRetAttr(Attribute::StackAlignment
));
137 EXPECT_TRUE(AL2
.hasRetAttr(Attribute::OptimizeNone
));
138 EXPECT_TRUE(AL2
.getRetStackAlignment() == MaybeAlign(32));
140 AL2
= AL2
.removeRetAttributes(C
, B_stackalign
);
141 EXPECT_FALSE(AL2
.hasParamAttr(0, Attribute::Alignment
));
142 EXPECT_TRUE(AL2
.hasParamAttr(0, Attribute::ReadOnly
));
143 EXPECT_FALSE(AL2
.hasRetAttr(Attribute::StackAlignment
));
144 EXPECT_TRUE(AL2
.hasRetAttr(Attribute::OptimizeNone
));
147 TEST(Attributes
, AddMatchingAlignAttr
) {
150 AL
= AL
.addParamAttribute(C
, 0, Attribute::getWithAlignment(C
, Align(8)));
151 AL
= AL
.addParamAttribute(C
, 1, Attribute::getWithAlignment(C
, Align(32)));
152 EXPECT_EQ(Align(8), AL
.getParamAlignment(0));
153 EXPECT_EQ(Align(32), AL
.getParamAlignment(1));
156 B
.addAttribute(Attribute::NonNull
);
157 B
.addAlignmentAttr(8);
158 AL
= AL
.addParamAttributes(C
, 0, B
);
159 EXPECT_EQ(Align(8), AL
.getParamAlignment(0));
160 EXPECT_EQ(Align(32), AL
.getParamAlignment(1));
161 EXPECT_TRUE(AL
.hasParamAttr(0, Attribute::NonNull
));
164 TEST(Attributes
, EmptyGet
) {
166 AttributeList EmptyLists
[] = {AttributeList(), AttributeList()};
167 AttributeList AL
= AttributeList::get(C
, EmptyLists
);
168 EXPECT_TRUE(AL
.isEmpty());
171 TEST(Attributes
, OverflowGet
) {
173 std::pair
<unsigned, Attribute
> Attrs
[] = { { AttributeList::ReturnIndex
, Attribute::get(C
, Attribute::SExt
) },
174 { AttributeList::FunctionIndex
, Attribute::get(C
, Attribute::ReadOnly
) } };
175 AttributeList AL
= AttributeList::get(C
, Attrs
);
176 EXPECT_EQ(2U, AL
.getNumAttrSets());
179 TEST(Attributes
, StringRepresentation
) {
181 StructType
*Ty
= StructType::create(Type::getInt32Ty(C
), "mystruct");
183 // Insufficiently careful printing can result in byval(%mystruct = { i32 })
184 Attribute A
= Attribute::getWithByValType(C
, Ty
);
185 EXPECT_EQ(A
.getAsString(), "byval(%mystruct)");
187 A
= Attribute::getWithByValType(C
, Type::getInt32Ty(C
));
188 EXPECT_EQ(A
.getAsString(), "byval(i32)");
191 TEST(Attributes
, HasParentContext
) {
195 Attribute Attr1
= Attribute::get(C1
, Attribute::AlwaysInline
);
196 Attribute Attr2
= Attribute::get(C2
, Attribute::AlwaysInline
);
197 EXPECT_TRUE(Attr1
.hasParentContext(C1
));
198 EXPECT_FALSE(Attr1
.hasParentContext(C2
));
199 EXPECT_FALSE(Attr2
.hasParentContext(C1
));
200 EXPECT_TRUE(Attr2
.hasParentContext(C2
));
204 AttributeSet AS1
= AttributeSet::get(
205 C1
, ArrayRef(Attribute::get(C1
, Attribute::NoReturn
)));
206 AttributeSet AS2
= AttributeSet::get(
207 C2
, ArrayRef(Attribute::get(C2
, Attribute::NoReturn
)));
208 EXPECT_TRUE(AS1
.hasParentContext(C1
));
209 EXPECT_FALSE(AS1
.hasParentContext(C2
));
210 EXPECT_FALSE(AS2
.hasParentContext(C1
));
211 EXPECT_TRUE(AS2
.hasParentContext(C2
));
215 AttributeList AL1
= AttributeList::get(C1
, 1, Attribute::ZExt
);
216 AttributeList AL2
= AttributeList::get(C2
, 1, Attribute::ZExt
);
217 EXPECT_TRUE(AL1
.hasParentContext(C1
));
218 EXPECT_FALSE(AL1
.hasParentContext(C2
));
219 EXPECT_FALSE(AL2
.hasParentContext(C1
));
220 EXPECT_TRUE(AL2
.hasParentContext(C2
));
224 TEST(Attributes
, AttributeListPrinting
) {
229 raw_string_ostream
OS(S
);
231 AL
.addFnAttribute(C
, Attribute::AlwaysInline
).print(OS
);
232 EXPECT_EQ(S
, "AttributeList[\n"
233 " { function => alwaysinline }\n"
239 raw_string_ostream
OS(S
);
241 AL
.addRetAttribute(C
, Attribute::SExt
).print(OS
);
242 EXPECT_EQ(S
, "AttributeList[\n"
243 " { return => signext }\n"
249 raw_string_ostream
OS(S
);
251 AL
.addParamAttribute(C
, 5, Attribute::ZExt
).print(OS
);
252 EXPECT_EQ(S
, "AttributeList[\n"
253 " { arg(5) => zeroext }\n"
258 TEST(Attributes
, MismatchedABIAttrs
) {
259 const char *IRString
= R
"IR(
260 declare void @f1(i32* byval(i32))
262 call void @f1(i32* null)
265 declare void @f2(i32* preallocated(i32))
267 call void @f2(i32* null)
270 declare void @f3(i32* inalloca(i32))
272 call void @f3(i32* null)
279 std::unique_ptr
<Module
> M
= parseAssemblyString(IRString
, Err
, Context
);
283 auto *I
= cast
<CallBase
>(&M
->getFunction("g")->getEntryBlock().front());
284 ASSERT_TRUE(I
->isByValArgument(0));
285 ASSERT_TRUE(I
->getParamByValType(0));
288 auto *I
= cast
<CallBase
>(&M
->getFunction("h")->getEntryBlock().front());
289 ASSERT_TRUE(I
->getParamPreallocatedType(0));
292 auto *I
= cast
<CallBase
>(&M
->getFunction("i")->getEntryBlock().front());
293 ASSERT_TRUE(I
->isInAllocaArgument(0));
294 ASSERT_TRUE(I
->getParamInAllocaType(0));
298 TEST(Attributes
, RemoveParamAttributes
) {
301 AL
= AL
.addParamAttribute(C
, 1, Attribute::NoUndef
);
302 EXPECT_EQ(AL
.getNumAttrSets(), 4U);
303 AL
= AL
.addParamAttribute(C
, 3, Attribute::NonNull
);
304 EXPECT_EQ(AL
.getNumAttrSets(), 6U);
305 AL
= AL
.removeParamAttributes(C
, 3);
306 EXPECT_EQ(AL
.getNumAttrSets(), 4U);
307 AL
= AL
.removeParamAttribute(C
, 1, Attribute::NoUndef
);
308 EXPECT_EQ(AL
.getNumAttrSets(), 0U);
311 } // end anonymous namespace