Debian Codegraph @

llvm-project/llvm/unittests/AsmParser/AsmParserTest.cpp

raw
1 //===- llvm/unittest/AsmParser/AsmParserTest.cpp - asm parser unittests ---===//
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/ADT/StringRef.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/AsmParser/SlotMapping.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/Error.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "gtest/gtest.h"
19
20 using namespace llvm;
21
22 namespace {
23
24 TEST(AsmParserTest, NullTerminatedInput) {
25 LLVMContext Ctx;
26 StringRef Source = "; Empty module \n";
27 SMDiagnostic Error;
28 auto Mod = parseAssemblyString(Source, Error, Ctx);
29
30 EXPECT_TRUE(Mod != nullptr);
31 EXPECT_TRUE(Error.getMessage().empty());
32 }
33
34 #ifdef GTEST_HAS_DEATH_TEST
35 #ifndef NDEBUG
36
37 TEST(AsmParserTest, NonNullTerminatedInput) {
38 LLVMContext Ctx;
39 StringRef Source = "; Empty module \n\1\2";
40 SMDiagnostic Error;
41 std::unique_ptr<Module> Mod;
42 EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2),
43 Error, Ctx),
44 "Buffer is not null terminated!");
45 }
46
47 #endif
48 #endif
49
50 TEST(AsmParserTest, SlotMappingTest) {
51 LLVMContext Ctx;
52 StringRef Source = "@0 = global i32 0\n !0 = !{}\n !42 = !{i32 42}";
53 SMDiagnostic Error;
54 SlotMapping Mapping;
55 auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
56
57 EXPECT_TRUE(Mod != nullptr);
58 EXPECT_TRUE(Error.getMessage().empty());
59
60 ASSERT_EQ(Mapping.GlobalValues.size(), 1u);
61 EXPECT_TRUE(isa<GlobalVariable>(Mapping.GlobalValues[0]));
62
63 EXPECT_EQ(Mapping.MetadataNodes.size(), 2u);
64 EXPECT_EQ(Mapping.MetadataNodes.count(0), 1u);
65 EXPECT_EQ(Mapping.MetadataNodes.count(42), 1u);
66 EXPECT_EQ(Mapping.MetadataNodes.count(1), 0u);
67 }
68
69 TEST(AsmParserTest, TypeAndConstantValueParsing) {
70 LLVMContext Ctx;
71 SMDiagnostic Error;
72 StringRef Source = "define void @test() {\n entry:\n ret void\n}";
73 auto Mod = parseAssemblyString(Source, Error, Ctx);
74 ASSERT_TRUE(Mod != nullptr);
75 auto &M = *Mod;
76
77 const Value *V;
78 V = parseConstantValue("double 3.5", Error, M);
79 ASSERT_TRUE(V);
80 EXPECT_TRUE(V->getType()->isDoubleTy());
81 ASSERT_TRUE(isa<ConstantFP>(V));
82 EXPECT_TRUE(cast<ConstantFP>(V)->isExactlyValue(3.5));
83
84 V = parseConstantValue("i32 42", Error, M);
85 ASSERT_TRUE(V);
86 EXPECT_TRUE(V->getType()->isIntegerTy());
87 ASSERT_TRUE(isa<ConstantInt>(V));
88 EXPECT_TRUE(cast<ConstantInt>(V)->equalsInt(42));
89
90 V = parseConstantValue("<4 x i32> <i32 0, i32 1, i32 2, i32 3>", Error, M);
91 ASSERT_TRUE(V);
92 EXPECT_TRUE(V->getType()->isVectorTy());
93 ASSERT_TRUE(isa<ConstantDataVector>(V));
94
95 V = parseConstantValue("i32 add (i32 1, i32 2)", Error, M);
96 ASSERT_TRUE(V);
97 ASSERT_TRUE(isa<ConstantInt>(V));
98
99 V = parseConstantValue("ptr blockaddress(@test, %entry)", Error, M);
100 ASSERT_TRUE(V);
101 ASSERT_TRUE(isa<BlockAddress>(V));
102
103 V = parseConstantValue("ptr undef", Error, M);
104 ASSERT_TRUE(V);
105 ASSERT_TRUE(isa<UndefValue>(V));
106
107 EXPECT_FALSE(parseConstantValue("duble 3.25", Error, M));
108 EXPECT_EQ(Error.getMessage(), "expected type");
109
110 EXPECT_FALSE(parseConstantValue("i32 3.25", Error, M));
111 EXPECT_EQ(Error.getMessage(), "floating point constant invalid for type");
112
113 EXPECT_FALSE(parseConstantValue("ptr @foo", Error, M));
114 EXPECT_EQ(Error.getMessage(), "expected a constant value");
115
116 EXPECT_FALSE(parseConstantValue("i32 3, ", Error, M));
117 EXPECT_EQ(Error.getMessage(), "expected end of string");
118 }
119
120 TEST(AsmParserTest, TypeAndConstantValueWithSlotMappingParsing) {
121 LLVMContext Ctx;
122 SMDiagnostic Error;
123 StringRef Source =
124 "%st = type { i32, i32 }\n"
125 "@v = common global [50 x %st] zeroinitializer, align 16\n"
126 "%0 = type { i32, i32, i32, i32 }\n"
127 "@g = common global [50 x %0] zeroinitializer, align 16\n"
128 "define void @marker4(i64 %d) {\n"
129 "entry:\n"
130 " %conv = trunc i64 %d to i32\n"
131 " store i32 %conv, ptr getelementptr inbounds "
132 " ([50 x %st], ptr @v, i64 0, i64 1, i32 0), align 16\n"
133 " store i32 %conv, ptr getelementptr inbounds "
134 " ([50 x %0], ptr @g, i64 0, i64 1, i32 0), align 16\n"
135 " ret void\n"
136 "}";
137 SlotMapping Mapping;
138 auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
139 ASSERT_TRUE(Mod != nullptr);
140 auto &M = *Mod;
141
142 const Value *V;
143 V = parseConstantValue("ptr getelementptr inbounds ([50 x %st], ptr "
144 "@v, i64 0, i64 1, i32 0)",
145 Error, M, &Mapping);
146 ASSERT_TRUE(V);
147 ASSERT_TRUE(isa<ConstantExpr>(V));
148
149 V = parseConstantValue("ptr getelementptr inbounds ([50 x %0], ptr "
150 "@g, i64 0, i64 1, i32 0)",
151 Error, M, &Mapping);
152 ASSERT_TRUE(V);
153 ASSERT_TRUE(isa<ConstantExpr>(V));
154 }
155
156 TEST(AsmParserTest, TypeWithSlotMappingParsing) {
157 LLVMContext Ctx;
158 SMDiagnostic Error;
159 StringRef Source =
160 "%st = type { i32, i32 }\n"
161 "@v = common global [50 x %st] zeroinitializer, align 16\n"
162 "%0 = type { i32, i32, i32, i32 }\n"
163 "@g = common global [50 x %0] zeroinitializer, align 16\n"
164 "define void @marker4(i64 %d) {\n"
165 "entry:\n"
166 " %conv = trunc i64 %d to i32\n"
167 " store i32 %conv, ptr getelementptr inbounds "
168 " ([50 x %st], ptr @v, i64 0, i64 0, i32 0), align 16\n"
169 " store i32 %conv, ptr getelementptr inbounds "
170 " ([50 x %0], ptr @g, i64 0, i64 0, i32 0), align 16\n"
171 " ret void\n"
172 "}";
173 SlotMapping Mapping;
174 auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
175 ASSERT_TRUE(Mod != nullptr);
176 auto &M = *Mod;
177
178 // Check we properly parse integer types.
179 Type *Ty;
180 Ty = parseType("i32", Error, M, &Mapping);
181 ASSERT_TRUE(Ty);
182 ASSERT_TRUE(Ty->isIntegerTy());
183 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
184
185 // Check we properly parse integer types with exotic size.
186 Ty = parseType("i13", Error, M, &Mapping);
187 ASSERT_TRUE(Ty);
188 ASSERT_TRUE(Ty->isIntegerTy());
189 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 13);
190
191 // Check we properly parse floating point types.
192 Ty = parseType("float", Error, M, &Mapping);
193 ASSERT_TRUE(Ty);
194 ASSERT_TRUE(Ty->isFloatTy());
195
196 Ty = parseType("double", Error, M, &Mapping);
197 ASSERT_TRUE(Ty);
198 ASSERT_TRUE(Ty->isDoubleTy());
199
200 // Check we properly parse struct types.
201 // Named struct.
202 Ty = parseType("%st", Error, M, &Mapping);
203 ASSERT_TRUE(Ty);
204 ASSERT_TRUE(Ty->isStructTy());
205
206 // Check the details of the struct.
207 StructType *ST = cast<StructType>(Ty);
208 ASSERT_TRUE(ST->getNumElements() == 2);
209 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
210 Ty = ST->getElementType(i);
211 ASSERT_TRUE(Ty->isIntegerTy());
212 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
213 }
214
215 // Anonymous struct.
216 Ty = parseType("%0", Error, M, &Mapping);
217 ASSERT_TRUE(Ty);
218 ASSERT_TRUE(Ty->isStructTy());
219
220 // Check the details of the struct.
221 ST = cast<StructType>(Ty);
222 ASSERT_TRUE(ST->getNumElements() == 4);
223 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
224 Ty = ST->getElementType(i);
225 ASSERT_TRUE(Ty->isIntegerTy());
226 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
227 }
228
229 // Check we properly parse vector types.
230 Ty = parseType("<5 x i32>", Error, M, &Mapping);
231 ASSERT_TRUE(Ty);
232 ASSERT_TRUE(Ty->isVectorTy());
233
234 // Check the details of the vector.
235 auto *VT = cast<FixedVectorType>(Ty);
236 ASSERT_TRUE(VT->getNumElements() == 5);
237 ASSERT_TRUE(VT->getPrimitiveSizeInBits().getFixedValue() == 160);
238 Ty = VT->getElementType();
239 ASSERT_TRUE(Ty->isIntegerTy());
240 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
241
242 // Opaque struct.
243 Ty = parseType("%opaque", Error, M, &Mapping);
244 ASSERT_TRUE(Ty);
245 ASSERT_TRUE(Ty->isStructTy());
246
247 ST = cast<StructType>(Ty);
248 ASSERT_TRUE(ST->isOpaque());
249
250 // Check we properly parse pointer types.
251 Ty = parseType("ptr", Error, M, &Mapping);
252 ASSERT_TRUE(Ty);
253 ASSERT_TRUE(Ty->isPointerTy());
254
255 PointerType *PT = cast<PointerType>(Ty);
256 ASSERT_TRUE(PT->isOpaque());
257
258 // Check that we reject types with garbage.
259 Ty = parseType("i32 garbage", Error, M, &Mapping);
260 ASSERT_TRUE(!Ty);
261 }
262
263 TEST(AsmParserTest, TypeAtBeginningWithSlotMappingParsing) {
264 LLVMContext Ctx;
265 SMDiagnostic Error;
266 StringRef Source =
267 "%st = type { i32, i32 }\n"
268 "@v = common global [50 x %st] zeroinitializer, align 16\n"
269 "%0 = type { i32, i32, i32, i32 }\n"
270 "@g = common global [50 x %0] zeroinitializer, align 16\n"
271 "define void @marker4(i64 %d) {\n"
272 "entry:\n"
273 " %conv = trunc i64 %d to i32\n"
274 " store i32 %conv, ptr getelementptr inbounds "
275 " ([50 x %st], ptr @v, i64 0, i64 0, i32 0), align 16\n"
276 " store i32 %conv, ptr getelementptr inbounds "
277 " ([50 x %0], ptr @g, i64 0, i64 0, i32 0), align 16\n"
278 " ret void\n"
279 "}";
280 SlotMapping Mapping;
281 auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
282 ASSERT_TRUE(Mod != nullptr);
283 auto &M = *Mod;
284 unsigned Read;
285
286 // Check we properly parse integer types.
287 Type *Ty;
288 Ty = parseTypeAtBeginning("i32", Read, Error, M, &Mapping);
289 ASSERT_TRUE(Ty);
290 ASSERT_TRUE(Ty->isIntegerTy());
291 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
292 ASSERT_TRUE(Read == 3);
293
294 // Check we properly parse integer types with exotic size.
295 Ty = parseTypeAtBeginning("i13", Read, Error, M, &Mapping);
296 ASSERT_TRUE(Ty);
297 ASSERT_TRUE(Ty->isIntegerTy());
298 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 13);
299 ASSERT_TRUE(Read == 3);
300
301 // Check we properly parse floating point types.
302 Ty = parseTypeAtBeginning("float", Read, Error, M, &Mapping);
303 ASSERT_TRUE(Ty);
304 ASSERT_TRUE(Ty->isFloatTy());
305 ASSERT_TRUE(Read == 5);
306
307 Ty = parseTypeAtBeginning("double", Read, Error, M, &Mapping);
308 ASSERT_TRUE(Ty);
309 ASSERT_TRUE(Ty->isDoubleTy());
310 ASSERT_TRUE(Read == 6);
311
312 // Check we properly parse struct types.
313 // Named struct.
314 Ty = parseTypeAtBeginning("%st", Read, Error, M, &Mapping);
315 ASSERT_TRUE(Ty);
316 ASSERT_TRUE(Ty->isStructTy());
317 ASSERT_TRUE(Read == 3);
318
319 // Check the details of the struct.
320 StructType *ST = cast<StructType>(Ty);
321 ASSERT_TRUE(ST->getNumElements() == 2);
322 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
323 Ty = ST->getElementType(i);
324 ASSERT_TRUE(Ty->isIntegerTy());
325 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
326 }
327
328 // Anonymous struct.
329 Ty = parseTypeAtBeginning("%0", Read, Error, M, &Mapping);
330 ASSERT_TRUE(Ty);
331 ASSERT_TRUE(Ty->isStructTy());
332 ASSERT_TRUE(Read == 2);
333
334 // Check the details of the struct.
335 ST = cast<StructType>(Ty);
336 ASSERT_TRUE(ST->getNumElements() == 4);
337 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
338 Ty = ST->getElementType(i);
339 ASSERT_TRUE(Ty->isIntegerTy());
340 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
341 }
342
343 // Check we properly parse vector types.
344 Ty = parseTypeAtBeginning("<5 x i32>", Read, Error, M, &Mapping);
345 ASSERT_TRUE(Ty);
346 ASSERT_TRUE(Ty->isVectorTy());
347 ASSERT_TRUE(Read == 9);
348
349 // Check the details of the vector.
350 auto *VT = cast<FixedVectorType>(Ty);
351 ASSERT_TRUE(VT->getNumElements() == 5);
352 ASSERT_TRUE(VT->getPrimitiveSizeInBits().getFixedValue() == 160);
353 Ty = VT->getElementType();
354 ASSERT_TRUE(Ty->isIntegerTy());
355 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
356
357 // Opaque struct.
358 Ty = parseTypeAtBeginning("%opaque", Read, Error, M, &Mapping);
359 ASSERT_TRUE(Ty);
360 ASSERT_TRUE(Ty->isStructTy());
361 ASSERT_TRUE(Read == 7);
362
363 ST = cast<StructType>(Ty);
364 ASSERT_TRUE(ST->isOpaque());
365
366 // Check we properly parse pointer types.
367 // One indirection.
368 Ty = parseTypeAtBeginning("ptr", Read, Error, M, &Mapping);
369 ASSERT_TRUE(Ty);
370 ASSERT_TRUE(Ty->isPointerTy());
371 ASSERT_TRUE(Read == 3);
372
373 PointerType *PT = cast<PointerType>(Ty);
374 ASSERT_TRUE(PT->isOpaque());
375
376 // Check that we reject types with garbage.
377 Ty = parseTypeAtBeginning("i32 garbage", Read, Error, M, &Mapping);
378 ASSERT_TRUE(Ty);
379 ASSERT_TRUE(Ty->isIntegerTy());
380 ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
381 // We go to the next token, i.e., we read "i32" + ' '.
382 ASSERT_TRUE(Read == 4);
383 }
384
385 TEST(AsmParserTest, InvalidDataLayoutStringCallback) {
386 LLVMContext Ctx;
387 SMDiagnostic Error;
388 // Note the invalid i8:7 part
389 // Overalign i32 as marker so we can check that indeed this DL was used,
390 // and not some default.
391 StringRef InvalidDLStr =
392 "e-m:e-p:64:64-i8:7-i16:16-i32:64-i64:64-f80:128-n8:16:32:64";
393 StringRef FixedDLStr =
394 "e-m:e-p:64:64-i8:8-i16:16-i32:64-i64:64-f80:128-n8:16:32:64";
395 Expected<DataLayout> ExpectedFixedDL = DataLayout::parse(FixedDLStr);
396 ASSERT_TRUE(!ExpectedFixedDL.takeError());
397 DataLayout FixedDL = ExpectedFixedDL.get();
398 std::string Source = ("target datalayout = \"" + InvalidDLStr + "\"\n").str();
399 MemoryBufferRef SourceBuffer(Source, "<string>");
400
401 // Check that we reject the source without a DL override.
402 SlotMapping Mapping1;
403 auto Mod1 = parseAssembly(SourceBuffer, Error, Ctx, &Mapping1);
404 EXPECT_TRUE(Mod1 == nullptr);
405
406 // Check that we pass the correct DL str to the callback,
407 // that fixing the DL str from the callback works,
408 // and that the resulting module has the correct DL.
409 SlotMapping Mapping2;
410 auto Mod2 = parseAssembly(
411 SourceBuffer, Error, Ctx, &Mapping2,
412 [&](StringRef Triple, StringRef DLStr) -> std::optional<std::string> {
413 EXPECT_EQ(DLStr, InvalidDLStr);
414 return std::string{FixedDLStr};
415 });
416 ASSERT_TRUE(Mod2 != nullptr);
417 EXPECT_EQ(Mod2->getDataLayout(), FixedDL);
418 }
419
420 } // end anonymous namespace