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 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/Attributes.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/DerivedTypes.h"
12 #include "llvm/IR/InstrTypes.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "gtest/gtest.h"
17 using namespace llvm;
18
19 namespace {
20
TEST(Attributes,Uniquing)21 TEST(Attributes, Uniquing) {
22 LLVMContext C;
23
24 Attribute AttrA = Attribute::get(C, Attribute::AlwaysInline);
25 Attribute AttrB = Attribute::get(C, Attribute::AlwaysInline);
26 EXPECT_EQ(AttrA, AttrB);
27
28 AttributeList ASs[] = {AttributeList::get(C, 1, Attribute::ZExt),
29 AttributeList::get(C, 2, Attribute::SExt)};
30
31 AttributeList SetA = AttributeList::get(C, ASs);
32 AttributeList SetB = AttributeList::get(C, ASs);
33 EXPECT_EQ(SetA, SetB);
34 }
35
TEST(Attributes,Ordering)36 TEST(Attributes, Ordering) {
37 LLVMContext C;
38
39 Attribute Align4 = Attribute::get(C, Attribute::Alignment, 4);
40 Attribute Align5 = Attribute::get(C, Attribute::Alignment, 5);
41 Attribute Deref4 = Attribute::get(C, Attribute::Dereferenceable, 4);
42 Attribute Deref5 = Attribute::get(C, Attribute::Dereferenceable, 5);
43 EXPECT_TRUE(Align4 < Align5);
44 EXPECT_TRUE(Align4 < Deref4);
45 EXPECT_TRUE(Align4 < Deref5);
46 EXPECT_TRUE(Align5 < Deref4);
47
48 Attribute ByVal = Attribute::get(C, Attribute::ByVal, Type::getInt32Ty(C));
49 EXPECT_FALSE(ByVal < Attribute::get(C, Attribute::ZExt));
50 EXPECT_TRUE(ByVal < Align4);
51 EXPECT_FALSE(ByVal < ByVal);
52
53 AttributeList ASs[] = {AttributeList::get(C, 2, Attribute::ZExt),
54 AttributeList::get(C, 1, Attribute::SExt)};
55
56 AttributeList SetA = AttributeList::get(C, ASs);
57 AttributeList SetB =
58 SetA.removeParamAttributes(C, 0, ASs[1].getParamAttrs(0));
59 EXPECT_NE(SetA, SetB);
60 }
61
TEST(Attributes,AddAttributes)62 TEST(Attributes, AddAttributes) {
63 LLVMContext C;
64 AttributeList AL;
65 AttrBuilder B(C);
66 B.addAttribute(Attribute::NoReturn);
67 AL = AL.addFnAttributes(C, AttrBuilder(C, AttributeSet::get(C, B)));
68 EXPECT_TRUE(AL.hasFnAttr(Attribute::NoReturn));
69 B.clear();
70 B.addAttribute(Attribute::SExt);
71 AL = AL.addRetAttributes(C, B);
72 EXPECT_TRUE(AL.hasRetAttr(Attribute::SExt));
73 EXPECT_TRUE(AL.hasFnAttr(Attribute::NoReturn));
74 }
75
TEST(Attributes,RemoveAlign)76 TEST(Attributes, RemoveAlign) {
77 LLVMContext C;
78
79 Attribute AlignAttr = Attribute::getWithAlignment(C, Align(8));
80 Attribute StackAlignAttr = Attribute::getWithStackAlignment(C, Align(32));
81 AttrBuilder B_align_readonly(C);
82 B_align_readonly.addAttribute(AlignAttr);
83 B_align_readonly.addAttribute(Attribute::ReadOnly);
84 AttributeMask B_align;
85 B_align.addAttribute(AlignAttr);
86 AttrBuilder B_stackalign_optnone(C);
87 B_stackalign_optnone.addAttribute(StackAlignAttr);
88 B_stackalign_optnone.addAttribute(Attribute::OptimizeNone);
89 AttributeMask B_stackalign;
90 B_stackalign.addAttribute(StackAlignAttr);
91
92 AttributeSet AS = AttributeSet::get(C, B_align_readonly);
93 EXPECT_TRUE(AS.getAlignment() == MaybeAlign(8));
94 EXPECT_TRUE(AS.hasAttribute(Attribute::ReadOnly));
95 AS = AS.removeAttribute(C, Attribute::Alignment);
96 EXPECT_FALSE(AS.hasAttribute(Attribute::Alignment));
97 EXPECT_TRUE(AS.hasAttribute(Attribute::ReadOnly));
98 AS = AttributeSet::get(C, B_align_readonly);
99 AS = AS.removeAttributes(C, B_align);
100 EXPECT_TRUE(AS.getAlignment() == None);
101 EXPECT_TRUE(AS.hasAttribute(Attribute::ReadOnly));
102
103 AttributeList AL;
104 AL = AL.addParamAttributes(C, 0, B_align_readonly);
105 AL = AL.addRetAttributes(C, B_stackalign_optnone);
106 EXPECT_TRUE(AL.hasRetAttrs());
107 EXPECT_TRUE(AL.hasRetAttr(Attribute::StackAlignment));
108 EXPECT_TRUE(AL.hasRetAttr(Attribute::OptimizeNone));
109 EXPECT_TRUE(AL.getRetStackAlignment() == MaybeAlign(32));
110 EXPECT_TRUE(AL.hasParamAttrs(0));
111 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::Alignment));
112 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::ReadOnly));
113 EXPECT_TRUE(AL.getParamAlignment(0) == MaybeAlign(8));
114
115 AL = AL.removeParamAttribute(C, 0, Attribute::Alignment);
116 EXPECT_FALSE(AL.hasParamAttr(0, Attribute::Alignment));
117 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::ReadOnly));
118 EXPECT_TRUE(AL.hasRetAttr(Attribute::StackAlignment));
119 EXPECT_TRUE(AL.hasRetAttr(Attribute::OptimizeNone));
120 EXPECT_TRUE(AL.getRetStackAlignment() == MaybeAlign(32));
121
122 AL = AL.removeRetAttribute(C, Attribute::StackAlignment);
123 EXPECT_FALSE(AL.hasParamAttr(0, Attribute::Alignment));
124 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::ReadOnly));
125 EXPECT_FALSE(AL.hasRetAttr(Attribute::StackAlignment));
126 EXPECT_TRUE(AL.hasRetAttr(Attribute::OptimizeNone));
127
128 AttributeList AL2;
129 AL2 = AL2.addParamAttributes(C, 0, B_align_readonly);
130 AL2 = AL2.addRetAttributes(C, B_stackalign_optnone);
131
132 AL2 = AL2.removeParamAttributes(C, 0, B_align);
133 EXPECT_FALSE(AL2.hasParamAttr(0, Attribute::Alignment));
134 EXPECT_TRUE(AL2.hasParamAttr(0, Attribute::ReadOnly));
135 EXPECT_TRUE(AL2.hasRetAttr(Attribute::StackAlignment));
136 EXPECT_TRUE(AL2.hasRetAttr(Attribute::OptimizeNone));
137 EXPECT_TRUE(AL2.getRetStackAlignment() == MaybeAlign(32));
138
139 AL2 = AL2.removeRetAttributes(C, B_stackalign);
140 EXPECT_FALSE(AL2.hasParamAttr(0, Attribute::Alignment));
141 EXPECT_TRUE(AL2.hasParamAttr(0, Attribute::ReadOnly));
142 EXPECT_FALSE(AL2.hasRetAttr(Attribute::StackAlignment));
143 EXPECT_TRUE(AL2.hasRetAttr(Attribute::OptimizeNone));
144 }
145
TEST(Attributes,AddMatchingAlignAttr)146 TEST(Attributes, AddMatchingAlignAttr) {
147 LLVMContext C;
148 AttributeList AL;
149 AL = AL.addParamAttribute(C, 0, Attribute::getWithAlignment(C, Align(8)));
150 AL = AL.addParamAttribute(C, 1, Attribute::getWithAlignment(C, Align(32)));
151 EXPECT_EQ(Align(8), AL.getParamAlignment(0));
152 EXPECT_EQ(Align(32), AL.getParamAlignment(1));
153
154 AttrBuilder B(C);
155 B.addAttribute(Attribute::NonNull);
156 B.addAlignmentAttr(8);
157 AL = AL.addParamAttributes(C, 0, B);
158 EXPECT_EQ(Align(8), AL.getParamAlignment(0));
159 EXPECT_EQ(Align(32), AL.getParamAlignment(1));
160 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::NonNull));
161 }
162
TEST(Attributes,EmptyGet)163 TEST(Attributes, EmptyGet) {
164 LLVMContext C;
165 AttributeList EmptyLists[] = {AttributeList(), AttributeList()};
166 AttributeList AL = AttributeList::get(C, EmptyLists);
167 EXPECT_TRUE(AL.isEmpty());
168 }
169
TEST(Attributes,OverflowGet)170 TEST(Attributes, OverflowGet) {
171 LLVMContext C;
172 std::pair<unsigned, Attribute> Attrs[] = { { AttributeList::ReturnIndex, Attribute::get(C, Attribute::SExt) },
173 { AttributeList::FunctionIndex, Attribute::get(C, Attribute::ReadOnly) } };
174 AttributeList AL = AttributeList::get(C, Attrs);
175 EXPECT_EQ(2U, AL.getNumAttrSets());
176 }
177
TEST(Attributes,StringRepresentation)178 TEST(Attributes, StringRepresentation) {
179 LLVMContext C;
180 StructType *Ty = StructType::create(Type::getInt32Ty(C), "mystruct");
181
182 // Insufficiently careful printing can result in byval(%mystruct = { i32 })
183 Attribute A = Attribute::getWithByValType(C, Ty);
184 EXPECT_EQ(A.getAsString(), "byval(%mystruct)");
185
186 A = Attribute::getWithByValType(C, Type::getInt32Ty(C));
187 EXPECT_EQ(A.getAsString(), "byval(i32)");
188 }
189
TEST(Attributes,HasParentContext)190 TEST(Attributes, HasParentContext) {
191 LLVMContext C1, C2;
192
193 {
194 Attribute Attr1 = Attribute::get(C1, Attribute::AlwaysInline);
195 Attribute Attr2 = Attribute::get(C2, Attribute::AlwaysInline);
196 EXPECT_TRUE(Attr1.hasParentContext(C1));
197 EXPECT_FALSE(Attr1.hasParentContext(C2));
198 EXPECT_FALSE(Attr2.hasParentContext(C1));
199 EXPECT_TRUE(Attr2.hasParentContext(C2));
200 }
201
202 {
203 AttributeSet AS1 = AttributeSet::get(
204 C1, makeArrayRef(Attribute::get(C1, Attribute::NoReturn)));
205 AttributeSet AS2 = AttributeSet::get(
206 C2, makeArrayRef(Attribute::get(C2, Attribute::NoReturn)));
207 EXPECT_TRUE(AS1.hasParentContext(C1));
208 EXPECT_FALSE(AS1.hasParentContext(C2));
209 EXPECT_FALSE(AS2.hasParentContext(C1));
210 EXPECT_TRUE(AS2.hasParentContext(C2));
211 }
212
213 {
214 AttributeList AL1 = AttributeList::get(C1, 1, Attribute::ZExt);
215 AttributeList AL2 = AttributeList::get(C2, 1, Attribute::ZExt);
216 EXPECT_TRUE(AL1.hasParentContext(C1));
217 EXPECT_FALSE(AL1.hasParentContext(C2));
218 EXPECT_FALSE(AL2.hasParentContext(C1));
219 EXPECT_TRUE(AL2.hasParentContext(C2));
220 }
221 }
222
TEST(Attributes,AttributeListPrinting)223 TEST(Attributes, AttributeListPrinting) {
224 LLVMContext C;
225
226 {
227 std::string S;
228 raw_string_ostream OS(S);
229 AttributeList AL;
230 AL.addFnAttribute(C, Attribute::AlwaysInline).print(OS);
231 EXPECT_EQ(S, "AttributeList[\n"
232 " { function => alwaysinline }\n"
233 "]\n");
234 }
235
236 {
237 std::string S;
238 raw_string_ostream OS(S);
239 AttributeList AL;
240 AL.addRetAttribute(C, Attribute::SExt).print(OS);
241 EXPECT_EQ(S, "AttributeList[\n"
242 " { return => signext }\n"
243 "]\n");
244 }
245
246 {
247 std::string S;
248 raw_string_ostream OS(S);
249 AttributeList AL;
250 AL.addParamAttribute(C, 5, Attribute::ZExt).print(OS);
251 EXPECT_EQ(S, "AttributeList[\n"
252 " { arg(5) => zeroext }\n"
253 "]\n");
254 }
255 }
256
TEST(Attributes,MismatchedABIAttrs)257 TEST(Attributes, MismatchedABIAttrs) {
258 const char *IRString = R"IR(
259 declare void @f1(i32* byval(i32))
260 define void @g() {
261 call void @f1(i32* null)
262 ret void
263 }
264 declare void @f2(i32* preallocated(i32))
265 define void @h() {
266 call void @f2(i32* null)
267 ret void
268 }
269 declare void @f3(i32* inalloca(i32))
270 define void @i() {
271 call void @f3(i32* null)
272 ret void
273 }
274 )IR";
275
276 SMDiagnostic Err;
277 LLVMContext Context;
278 std::unique_ptr<Module> M = parseAssemblyString(IRString, Err, Context);
279 ASSERT_TRUE(M);
280
281 {
282 auto *I = cast<CallBase>(&M->getFunction("g")->getEntryBlock().front());
283 ASSERT_TRUE(I->isByValArgument(0));
284 ASSERT_TRUE(I->getParamByValType(0));
285 }
286 {
287 auto *I = cast<CallBase>(&M->getFunction("h")->getEntryBlock().front());
288 ASSERT_TRUE(I->getParamPreallocatedType(0));
289 }
290 {
291 auto *I = cast<CallBase>(&M->getFunction("i")->getEntryBlock().front());
292 ASSERT_TRUE(I->isInAllocaArgument(0));
293 ASSERT_TRUE(I->getParamInAllocaType(0));
294 }
295 }
296
297 } // end anonymous namespace
298