Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / unittests / Bitcode / BitReaderTest.cpp
blobc4d7c2fadbeb4d4c28a0addb9238235f4887235f
1 //===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===//
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 "BitReaderTestCode.h"
10 #include "llvm/ADT/MapVector.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/AsmParser/Parser.h"
14 #include "llvm/Bitcode/BitcodeReader.h"
15 #include "llvm/Bitcode/BitcodeWriter.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/InstrTypes.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "gtest/gtest.h"
27 using namespace llvm;
29 namespace {
31 std::unique_ptr<Module> parseAssembly(LLVMContext &Context,
32 const char *Assembly) {
33 SMDiagnostic Error;
34 std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
36 std::string ErrMsg;
37 raw_string_ostream OS(ErrMsg);
38 Error.print("", OS);
40 // A failure here means that the test itself is buggy.
41 if (!M)
42 report_fatal_error(OS.str().c_str());
44 return M;
47 static void writeModuleToBuffer(std::unique_ptr<Module> Mod,
48 SmallVectorImpl<char> &Buffer) {
49 raw_svector_ostream OS(Buffer);
50 WriteBitcodeToFile(*Mod, OS);
53 static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
54 SmallString<1024> &Mem,
55 const char *Assembly) {
56 writeModuleToBuffer(parseAssembly(Context, Assembly), Mem);
57 Expected<std::unique_ptr<Module>> ModuleOrErr =
58 getLazyBitcodeModule(MemoryBufferRef(Mem.str(), "test"), Context);
59 if (!ModuleOrErr)
60 report_fatal_error("Could not parse bitcode module");
61 return std::move(ModuleOrErr.get());
64 // Tests that lazy evaluation can parse functions out of order.
65 TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) {
66 SmallString<1024> Mem;
67 LLVMContext Context;
68 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
69 Context, Mem, "define void @f() {\n"
70 " unreachable\n"
71 "}\n"
72 "define void @g() {\n"
73 " unreachable\n"
74 "}\n"
75 "define void @h() {\n"
76 " unreachable\n"
77 "}\n"
78 "define void @j() {\n"
79 " unreachable\n"
80 "}\n");
81 EXPECT_FALSE(verifyModule(*M, &dbgs()));
83 Function *F = M->getFunction("f");
84 Function *G = M->getFunction("g");
85 Function *H = M->getFunction("h");
86 Function *J = M->getFunction("j");
88 // Initially all functions are not materialized (no basic blocks).
89 EXPECT_TRUE(F->empty());
90 EXPECT_TRUE(G->empty());
91 EXPECT_TRUE(H->empty());
92 EXPECT_TRUE(J->empty());
93 EXPECT_FALSE(verifyModule(*M, &dbgs()));
95 // Materialize h.
96 ASSERT_FALSE(H->materialize());
97 EXPECT_TRUE(F->empty());
98 EXPECT_TRUE(G->empty());
99 EXPECT_FALSE(H->empty());
100 EXPECT_TRUE(J->empty());
101 EXPECT_FALSE(verifyModule(*M, &dbgs()));
103 // Materialize g.
104 ASSERT_FALSE(G->materialize());
105 EXPECT_TRUE(F->empty());
106 EXPECT_FALSE(G->empty());
107 EXPECT_FALSE(H->empty());
108 EXPECT_TRUE(J->empty());
109 EXPECT_FALSE(verifyModule(*M, &dbgs()));
111 // Materialize j.
112 ASSERT_FALSE(J->materialize());
113 EXPECT_TRUE(F->empty());
114 EXPECT_FALSE(G->empty());
115 EXPECT_FALSE(H->empty());
116 EXPECT_FALSE(J->empty());
117 EXPECT_FALSE(verifyModule(*M, &dbgs()));
119 // Materialize f.
120 ASSERT_FALSE(F->materialize());
121 EXPECT_FALSE(F->empty());
122 EXPECT_FALSE(G->empty());
123 EXPECT_FALSE(H->empty());
124 EXPECT_FALSE(J->empty());
125 EXPECT_FALSE(verifyModule(*M, &dbgs()));
128 TEST(BitReaderTest, MaterializeFunctionsStrictFP) {
129 SmallString<1024> Mem;
131 LLVMContext Context;
132 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
133 Context, Mem, "define double @foo(double %a) {\n"
134 " %result = call double @bar(double %a) strictfp\n"
135 " ret double %result\n"
136 "}\n"
137 "declare double @bar(double)\n");
138 Function *Foo = M->getFunction("foo");
139 ASSERT_FALSE(Foo->materialize());
140 EXPECT_FALSE(Foo->empty());
142 for (auto &BB : *Foo) {
143 auto It = BB.begin();
144 while (It != BB.end()) {
145 Instruction &I = *It;
146 ++It;
148 if (auto *Call = dyn_cast<CallBase>(&I)) {
149 EXPECT_FALSE(Call->isStrictFP());
150 EXPECT_TRUE(Call->isNoBuiltin());
155 EXPECT_FALSE(verifyModule(*M, &dbgs()));
158 TEST(BitReaderTest, MaterializeConstrainedFPStrictFP) {
159 SmallString<1024> Mem;
161 LLVMContext Context;
162 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
163 Context, Mem,
164 "define double @foo(double %a) {\n"
165 " %result = call double @llvm.experimental.constrained.sqrt.f64(double "
166 "%a, metadata !\"round.tonearest\", metadata !\"fpexcept.strict\") "
167 "strictfp\n"
168 " ret double %result\n"
169 "}\n"
170 "declare double @llvm.experimental.constrained.sqrt.f64(double, "
171 "metadata, metadata)\n");
172 Function *Foo = M->getFunction("foo");
173 ASSERT_FALSE(Foo->materialize());
174 EXPECT_FALSE(Foo->empty());
176 for (auto &BB : *Foo) {
177 auto It = BB.begin();
178 while (It != BB.end()) {
179 Instruction &I = *It;
180 ++It;
182 if (auto *Call = dyn_cast<CallBase>(&I)) {
183 EXPECT_TRUE(Call->isStrictFP());
184 EXPECT_FALSE(Call->isNoBuiltin());
189 EXPECT_FALSE(verifyModule(*M, &dbgs()));
192 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
193 SmallString<1024> Mem;
195 LLVMContext Context;
196 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
197 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
198 "define void @func() {\n"
199 " unreachable\n"
200 "bb:\n"
201 " unreachable\n"
202 "}\n");
203 EXPECT_FALSE(verifyModule(*M, &dbgs()));
204 EXPECT_FALSE(M->getFunction("func")->empty());
207 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) {
208 SmallString<1024> Mem;
210 LLVMContext Context;
211 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
212 Context, Mem, "define i8* @before() {\n"
213 " ret i8* blockaddress(@func, %bb)\n"
214 "}\n"
215 "define void @other() {\n"
216 " unreachable\n"
217 "}\n"
218 "define void @func() {\n"
219 " unreachable\n"
220 "bb:\n"
221 " unreachable\n"
222 "}\n");
223 EXPECT_TRUE(M->getFunction("before")->empty());
224 EXPECT_TRUE(M->getFunction("func")->empty());
225 EXPECT_FALSE(verifyModule(*M, &dbgs()));
227 // Materialize @before, pulling in @func.
228 EXPECT_FALSE(M->getFunction("before")->materialize());
229 EXPECT_FALSE(M->getFunction("func")->empty());
230 EXPECT_TRUE(M->getFunction("other")->empty());
231 EXPECT_FALSE(verifyModule(*M, &dbgs()));
234 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) {
235 SmallString<1024> Mem;
237 LLVMContext Context;
238 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
239 Context, Mem, "define void @func() {\n"
240 " unreachable\n"
241 "bb:\n"
242 " unreachable\n"
243 "}\n"
244 "define void @other() {\n"
245 " unreachable\n"
246 "}\n"
247 "define i8* @after() {\n"
248 " ret i8* blockaddress(@func, %bb)\n"
249 "}\n");
250 EXPECT_TRUE(M->getFunction("after")->empty());
251 EXPECT_TRUE(M->getFunction("func")->empty());
252 EXPECT_FALSE(verifyModule(*M, &dbgs()));
254 // Materialize @after, pulling in @func.
255 EXPECT_FALSE(M->getFunction("after")->materialize());
256 EXPECT_FALSE(M->getFunction("func")->empty());
257 EXPECT_TRUE(M->getFunction("other")->empty());
258 EXPECT_FALSE(verifyModule(*M, &dbgs()));
261 // Helper function to convert type metadata to a string for testing
262 static std::string mdToString(Metadata *MD) {
263 std::string S;
264 if (auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
265 if (VMD->getType()->isPointerTy()) {
266 S += "ptr";
267 return S;
271 if (auto *TMD = dyn_cast<MDTuple>(MD)) {
272 S += "!{";
273 for (unsigned I = 0; I < TMD->getNumOperands(); I++) {
274 if (I != 0)
275 S += ", ";
276 S += mdToString(TMD->getOperand(I).get());
278 S += "}";
279 } else if (auto *SMD = dyn_cast<MDString>(MD)) {
280 S += "!'";
281 S += SMD->getString();
282 S += "'";
283 } else if (auto *I = mdconst::dyn_extract<ConstantInt>(MD)) {
284 S += std::to_string(I->getZExtValue());
285 } else if (auto *P = mdconst::dyn_extract<PoisonValue>(MD)) {
286 auto *Ty = P->getType();
287 if (Ty->isIntegerTy()) {
288 S += "i";
289 S += std::to_string(Ty->getIntegerBitWidth());
290 } else if (Ty->isStructTy()) {
291 S += "%";
292 S += Ty->getStructName();
293 } else {
294 llvm_unreachable("unhandled poison metadata");
296 } else {
297 llvm_unreachable("unhandled metadata");
299 return S;
302 // Recursively look into a (pointer) type and the the type.
303 // For primitive types it's a poison value of the type, for a pointer it's a
304 // metadata tuple with the addrspace and the referenced type. For a function,
305 // it's a tuple where the first element is the string "function", the second
306 // element is the return type or the string "void" and the following elements
307 // are the argument types.
308 static Metadata *getTypeMetadataEntry(unsigned TypeID, LLVMContext &Context,
309 GetTypeByIDTy GetTypeByID,
310 GetContainedTypeIDTy GetContainedTypeID) {
311 Type *Ty = GetTypeByID(TypeID);
312 if (auto *FTy = dyn_cast<FunctionType>(Ty)) {
313 // Save the function signature as metadata
314 SmallVector<Metadata *> SignatureMD;
315 SignatureMD.push_back(MDString::get(Context, "function"));
316 // Return type
317 if (FTy->getReturnType()->isVoidTy())
318 SignatureMD.push_back(MDString::get(Context, "void"));
319 else
320 SignatureMD.push_back(getTypeMetadataEntry(GetContainedTypeID(TypeID, 0),
321 Context, GetTypeByID,
322 GetContainedTypeID));
323 // Arguments
324 for (unsigned I = 0; I != FTy->getNumParams(); ++I)
325 SignatureMD.push_back(
326 getTypeMetadataEntry(GetContainedTypeID(TypeID, I + 1), Context,
327 GetTypeByID, GetContainedTypeID));
329 return MDTuple::get(Context, SignatureMD);
332 if (!Ty->isPointerTy())
333 return ConstantAsMetadata::get(PoisonValue::get(Ty));
335 // Return !{<addrspace>, <inner>} for pointer
336 SmallVector<Metadata *, 2> MD;
337 MD.push_back(ConstantAsMetadata::get(ConstantInt::get(
338 Type::getInt32Ty(Context), Ty->getPointerAddressSpace())));
339 MD.push_back(getTypeMetadataEntry(GetContainedTypeID(TypeID, 0), Context,
340 GetTypeByID, GetContainedTypeID));
341 return MDTuple::get(Context, MD);
344 // Test that when reading bitcode with typed pointers and upgrading them to
345 // opaque pointers, the type information of function signatures can be extracted
346 // and stored in metadata.
347 TEST(BitReaderTest, AccessFunctionTypeInfo) {
348 StringRef Bitcode(reinterpret_cast<const char *>(AccessFunctionTypeInfoBc),
349 sizeof(AccessFunctionTypeInfoBc));
351 LLVMContext Context;
352 ParserCallbacks Callbacks;
353 // Supply a callback that stores the signature of a function into metadata,
354 // so that the types behind pointers can be accessed.
355 // Each function gets a !types metadata, which is a tuple with one element
356 // for a non-void return type and every argument. For primitive types it's
357 // a poison value of the type, for a pointer it's a metadata tuple with
358 // the addrspace and the referenced type.
359 Callbacks.ValueType = [&](Value *V, unsigned TypeID,
360 GetTypeByIDTy GetTypeByID,
361 GetContainedTypeIDTy GetContainedTypeID) {
362 if (auto *F = dyn_cast<Function>(V)) {
363 auto *MD = getTypeMetadataEntry(TypeID, F->getContext(), GetTypeByID,
364 GetContainedTypeID);
365 F->setMetadata("types", cast<MDNode>(MD));
369 Expected<std::unique_ptr<Module>> ModuleOrErr =
370 parseBitcodeFile(MemoryBufferRef(Bitcode, "test"), Context, Callbacks);
372 if (!ModuleOrErr)
373 report_fatal_error("Could not parse bitcode module");
374 std::unique_ptr<Module> M = std::move(ModuleOrErr.get());
376 EXPECT_EQ(mdToString(M->getFunction("func")->getMetadata("types")),
377 "!{!'function', !'void'}");
378 EXPECT_EQ(mdToString(M->getFunction("func_header")->getMetadata("types")),
379 "!{!'function', i32}");
380 EXPECT_EQ(mdToString(M->getFunction("ret_ptr")->getMetadata("types")),
381 "!{!'function', !{0, i8}}");
382 EXPECT_EQ(mdToString(M->getFunction("ret_and_arg_ptr")->getMetadata("types")),
383 "!{!'function', !{0, i8}, !{8, i32}}");
384 EXPECT_EQ(mdToString(M->getFunction("double_ptr")->getMetadata("types")),
385 "!{!'function', !{1, i8}, !{2, !{0, i32}}, !{0, !{0, !{0, i32}}}}");
388 // Test that when reading bitcode with typed pointers and upgrading them to
389 // opaque pointers, the type information of pointers in metadata can be
390 // extracted and stored in metadata.
391 TEST(BitReaderTest, AccessMetadataTypeInfo) {
392 StringRef Bitcode(reinterpret_cast<const char *>(AccessMetadataTypeInfoBc),
393 sizeof(AccessFunctionTypeInfoBc));
395 LLVMContext Context;
396 ParserCallbacks Callbacks;
397 // Supply a callback that stores types from metadata,
398 // so that the types behind pointers can be accessed.
399 // Non-pointer entries are ignored. Values with a pointer type are
400 // replaced by a metadata tuple with {original value, type md}. We cannot
401 // save the metadata outside because after conversion to opaque pointers,
402 // entries are not distinguishable anymore (e.g. i32* and i8* are both
403 // upgraded to ptr).
404 Callbacks.MDType = [&](Metadata **Val, unsigned TypeID,
405 GetTypeByIDTy GetTypeByID,
406 GetContainedTypeIDTy GetContainedTypeID) {
407 auto *OrigVal = cast<ValueAsMetadata>(*Val);
408 if (OrigVal->getType()->isPointerTy()) {
409 // Ignore function references, their signature can be saved like
410 // in the test above
411 if (!isa<Function>(OrigVal->getValue())) {
412 SmallVector<Metadata *> Tuple;
413 Tuple.push_back(OrigVal);
414 Tuple.push_back(getTypeMetadataEntry(GetContainedTypeID(TypeID, 0),
415 OrigVal->getContext(), GetTypeByID,
416 GetContainedTypeID));
417 *Val = MDTuple::get(OrigVal->getContext(), Tuple);
422 Expected<std::unique_ptr<Module>> ModuleOrErr =
423 parseBitcodeFile(MemoryBufferRef(Bitcode, "test"), Context, Callbacks);
425 if (!ModuleOrErr)
426 report_fatal_error("Could not parse bitcode module");
427 std::unique_ptr<Module> M = std::move(ModuleOrErr.get());
429 EXPECT_EQ(
430 mdToString(M->getNamedMetadata("md")->getOperand(0)),
431 "!{2, !{ptr, %dx.types.f32}, ptr, !{ptr, !{!'function', !'void'}}}");
432 EXPECT_EQ(mdToString(M->getNamedMetadata("md2")->getOperand(0)),
433 "!{!{ptr, !{!'function', !{0, i8}, !{2, !{0, i32}}}}, !{ptr, !{0, "
434 "!{0, i32}}}}");
437 } // end namespace