1 //===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/AsmParser/Parser.h"
13 #include "llvm/Bitcode/BitstreamWriter.h"
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/Verifier.h"
20 #include "llvm/Support/DataStream.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "gtest/gtest.h"
30 std::unique_ptr<Module> parseAssembly(const char *Assembly) {
32 std::unique_ptr<Module> M =
33 parseAssemblyString(Assembly, Error, getGlobalContext());
36 raw_string_ostream OS(ErrMsg);
39 // A failure here means that the test itself is buggy.
41 report_fatal_error(OS.str().c_str());
46 static void writeModuleToBuffer(std::unique_ptr<Module> Mod,
47 SmallVectorImpl<char> &Buffer) {
48 raw_svector_ostream OS(Buffer);
49 WriteBitcodeToFile(Mod.get(), OS);
52 static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
53 SmallString<1024> &Mem,
54 const char *Assembly) {
55 writeModuleToBuffer(parseAssembly(Assembly), Mem);
56 std::unique_ptr<MemoryBuffer> Buffer =
57 MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
58 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
59 getLazyBitcodeModule(std::move(Buffer), Context);
60 return std::move(ModuleOrErr.get());
63 class BufferDataStreamer : public DataStreamer {
64 std::unique_ptr<MemoryBuffer> Buffer;
66 size_t GetBytes(unsigned char *Out, size_t Len) override {
67 StringRef Buf = Buffer->getBuffer();
68 size_t Left = Buf.size() - Pos;
69 Len = std::min(Left, Len);
70 memcpy(Out, Buffer->getBuffer().substr(Pos).data(), Len);
76 BufferDataStreamer(std::unique_ptr<MemoryBuffer> Buffer)
77 : Buffer(std::move(Buffer)) {}
80 static std::unique_ptr<Module>
81 getStreamedModuleFromAssembly(LLVMContext &Context, SmallString<1024> &Mem,
82 const char *Assembly) {
83 writeModuleToBuffer(parseAssembly(Assembly), Mem);
84 std::unique_ptr<MemoryBuffer> Buffer =
85 MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
86 auto Streamer = make_unique<BufferDataStreamer>(std::move(Buffer));
87 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
88 getStreamedBitcodeModule("test", std::move(Streamer), Context);
89 return std::move(ModuleOrErr.get());
92 TEST(BitReaderTest, MateralizeForwardRefWithStream) {
93 SmallString<1024> Mem;
96 std::unique_ptr<Module> M = getStreamedModuleFromAssembly(
97 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
98 "define void @func() {\n"
103 EXPECT_FALSE(M->getFunction("func")->empty());
106 TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) {
107 SmallString<1024> Mem;
110 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
111 Context, Mem, "define internal i32 @func() {\n"
115 EXPECT_FALSE(verifyModule(*M, &dbgs()));
117 M->getFunction("func")->materialize();
118 EXPECT_FALSE(M->getFunction("func")->empty());
119 EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
120 GlobalValue::InternalLinkage);
122 // Check that the linkage type is preserved after dematerialization.
123 M->getFunction("func")->dematerialize();
124 EXPECT_TRUE(M->getFunction("func")->empty());
125 EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
126 GlobalValue::InternalLinkage);
127 EXPECT_FALSE(verifyModule(*M, &dbgs()));
130 // Tests that lazy evaluation can parse functions out of order.
131 TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) {
132 SmallString<1024> Mem;
134 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
135 Context, Mem, "define void @f() {\n"
138 "define void @g() {\n"
141 "define void @h() {\n"
144 "define void @j() {\n"
147 EXPECT_FALSE(verifyModule(*M, &dbgs()));
149 Function *F = M->getFunction("f");
150 Function *G = M->getFunction("g");
151 Function *H = M->getFunction("h");
152 Function *J = M->getFunction("j");
154 // Initially all functions are not materialized (no basic blocks).
155 EXPECT_TRUE(F->empty());
156 EXPECT_TRUE(G->empty());
157 EXPECT_TRUE(H->empty());
158 EXPECT_TRUE(J->empty());
159 EXPECT_FALSE(verifyModule(*M, &dbgs()));
163 EXPECT_TRUE(F->empty());
164 EXPECT_TRUE(G->empty());
165 EXPECT_FALSE(H->empty());
166 EXPECT_TRUE(J->empty());
167 EXPECT_FALSE(verifyModule(*M, &dbgs()));
171 EXPECT_TRUE(F->empty());
172 EXPECT_FALSE(G->empty());
173 EXPECT_FALSE(H->empty());
174 EXPECT_TRUE(J->empty());
175 EXPECT_FALSE(verifyModule(*M, &dbgs()));
179 EXPECT_TRUE(F->empty());
180 EXPECT_FALSE(G->empty());
181 EXPECT_FALSE(H->empty());
182 EXPECT_FALSE(J->empty());
183 EXPECT_FALSE(verifyModule(*M, &dbgs()));
187 EXPECT_FALSE(F->empty());
188 EXPECT_FALSE(G->empty());
189 EXPECT_FALSE(H->empty());
190 EXPECT_FALSE(J->empty());
191 EXPECT_FALSE(verifyModule(*M, &dbgs()));
194 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
195 SmallString<1024> Mem;
198 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
199 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
200 "define void @func() {\n"
205 EXPECT_FALSE(verifyModule(*M, &dbgs()));
207 // Try (and fail) to dematerialize @func.
208 M->getFunction("func")->dematerialize();
209 EXPECT_FALSE(M->getFunction("func")->empty());
212 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) {
213 SmallString<1024> Mem;
216 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
217 Context, Mem, "define i8* @before() {\n"
218 " ret i8* blockaddress(@func, %bb)\n"
220 "define void @other() {\n"
223 "define void @func() {\n"
228 EXPECT_TRUE(M->getFunction("before")->empty());
229 EXPECT_TRUE(M->getFunction("func")->empty());
230 EXPECT_FALSE(verifyModule(*M, &dbgs()));
232 // Materialize @before, pulling in @func.
233 EXPECT_FALSE(M->getFunction("before")->materialize());
234 EXPECT_FALSE(M->getFunction("func")->empty());
235 EXPECT_TRUE(M->getFunction("other")->empty());
236 EXPECT_FALSE(verifyModule(*M, &dbgs()));
238 // Try (and fail) to dematerialize @func.
239 M->getFunction("func")->dematerialize();
240 EXPECT_FALSE(M->getFunction("func")->empty());
241 EXPECT_FALSE(verifyModule(*M, &dbgs()));
244 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) {
245 SmallString<1024> Mem;
248 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
249 Context, Mem, "define void @func() {\n"
254 "define void @other() {\n"
257 "define i8* @after() {\n"
258 " ret i8* blockaddress(@func, %bb)\n"
260 EXPECT_TRUE(M->getFunction("after")->empty());
261 EXPECT_TRUE(M->getFunction("func")->empty());
262 EXPECT_FALSE(verifyModule(*M, &dbgs()));
264 // Materialize @after, pulling in @func.
265 EXPECT_FALSE(M->getFunction("after")->materialize());
266 EXPECT_FALSE(M->getFunction("func")->empty());
267 EXPECT_TRUE(M->getFunction("other")->empty());
268 EXPECT_FALSE(verifyModule(*M, &dbgs()));
270 // Try (and fail) to dematerialize @func.
271 M->getFunction("func")->dematerialize();
272 EXPECT_FALSE(M->getFunction("func")->empty());
273 EXPECT_FALSE(verifyModule(*M, &dbgs()));