BitReaderTest.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/ADT/SmallString.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/AsmParser/Parser.h"
  12. #include "llvm/Bitcode/BitstreamReader.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/Debug.h"
  21. #include "llvm/Support/Error.h"
  22. #include "llvm/Support/MemoryBuffer.h"
  23. #include "llvm/Support/SourceMgr.h"
  24. #include "gtest/gtest.h"
  25. using namespace llvm;
  26. namespace {
  27. std::unique_ptr<Module> parseAssembly(LLVMContext &Context,
  28. const char *Assembly) {
  29. SMDiagnostic Error;
  30. std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
  31. std::string ErrMsg;
  32. raw_string_ostream OS(ErrMsg);
  33. Error.print("", OS);
  34. // A failure here means that the test itself is buggy.
  35. if (!M)
  36. report_fatal_error(OS.str().c_str());
  37. return M;
  38. }
  39. static void writeModuleToBuffer(std::unique_ptr<Module> Mod,
  40. SmallVectorImpl<char> &Buffer) {
  41. raw_svector_ostream OS(Buffer);
  42. WriteBitcodeToFile(Mod.get(), OS);
  43. }
  44. static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
  45. SmallString<1024> &Mem,
  46. const char *Assembly) {
  47. writeModuleToBuffer(parseAssembly(Context, Assembly), Mem);
  48. ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
  49. getLazyBitcodeModule(MemoryBufferRef(Mem.str(), "test"), Context);
  50. return std::move(ModuleOrErr.get());
  51. }
  52. // Tests that lazy evaluation can parse functions out of order.
  53. TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) {
  54. SmallString<1024> Mem;
  55. LLVMContext Context;
  56. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  57. Context, Mem, "define void @f() {\n"
  58. " unreachable\n"
  59. "}\n"
  60. "define void @g() {\n"
  61. " unreachable\n"
  62. "}\n"
  63. "define void @h() {\n"
  64. " unreachable\n"
  65. "}\n"
  66. "define void @j() {\n"
  67. " unreachable\n"
  68. "}\n");
  69. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  70. Function *F = M->getFunction("f");
  71. Function *G = M->getFunction("g");
  72. Function *H = M->getFunction("h");
  73. Function *J = M->getFunction("j");
  74. // Initially all functions are not materialized (no basic blocks).
  75. EXPECT_TRUE(F->empty());
  76. EXPECT_TRUE(G->empty());
  77. EXPECT_TRUE(H->empty());
  78. EXPECT_TRUE(J->empty());
  79. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  80. // Materialize h.
  81. ASSERT_FALSE(H->materialize());
  82. EXPECT_TRUE(F->empty());
  83. EXPECT_TRUE(G->empty());
  84. EXPECT_FALSE(H->empty());
  85. EXPECT_TRUE(J->empty());
  86. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  87. // Materialize g.
  88. ASSERT_FALSE(G->materialize());
  89. EXPECT_TRUE(F->empty());
  90. EXPECT_FALSE(G->empty());
  91. EXPECT_FALSE(H->empty());
  92. EXPECT_TRUE(J->empty());
  93. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  94. // Materialize j.
  95. ASSERT_FALSE(J->materialize());
  96. EXPECT_TRUE(F->empty());
  97. EXPECT_FALSE(G->empty());
  98. EXPECT_FALSE(H->empty());
  99. EXPECT_FALSE(J->empty());
  100. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  101. // Materialize f.
  102. ASSERT_FALSE(F->materialize());
  103. EXPECT_FALSE(F->empty());
  104. EXPECT_FALSE(G->empty());
  105. EXPECT_FALSE(H->empty());
  106. EXPECT_FALSE(J->empty());
  107. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  108. }
  109. TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
  110. SmallString<1024> Mem;
  111. LLVMContext Context;
  112. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  113. Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
  114. "define void @func() {\n"
  115. " unreachable\n"
  116. "bb:\n"
  117. " unreachable\n"
  118. "}\n");
  119. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  120. EXPECT_FALSE(M->getFunction("func")->empty());
  121. }
  122. TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) {
  123. SmallString<1024> Mem;
  124. LLVMContext Context;
  125. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  126. Context, Mem, "define i8* @before() {\n"
  127. " ret i8* blockaddress(@func, %bb)\n"
  128. "}\n"
  129. "define void @other() {\n"
  130. " unreachable\n"
  131. "}\n"
  132. "define void @func() {\n"
  133. " unreachable\n"
  134. "bb:\n"
  135. " unreachable\n"
  136. "}\n");
  137. EXPECT_TRUE(M->getFunction("before")->empty());
  138. EXPECT_TRUE(M->getFunction("func")->empty());
  139. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  140. // Materialize @before, pulling in @func.
  141. EXPECT_FALSE(M->getFunction("before")->materialize());
  142. EXPECT_FALSE(M->getFunction("func")->empty());
  143. EXPECT_TRUE(M->getFunction("other")->empty());
  144. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  145. }
  146. TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) {
  147. SmallString<1024> Mem;
  148. LLVMContext Context;
  149. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  150. Context, Mem, "define void @func() {\n"
  151. " unreachable\n"
  152. "bb:\n"
  153. " unreachable\n"
  154. "}\n"
  155. "define void @other() {\n"
  156. " unreachable\n"
  157. "}\n"
  158. "define i8* @after() {\n"
  159. " ret i8* blockaddress(@func, %bb)\n"
  160. "}\n");
  161. EXPECT_TRUE(M->getFunction("after")->empty());
  162. EXPECT_TRUE(M->getFunction("func")->empty());
  163. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  164. // Materialize @after, pulling in @func.
  165. EXPECT_FALSE(M->getFunction("after")->materialize());
  166. EXPECT_FALSE(M->getFunction("func")->empty());
  167. EXPECT_TRUE(M->getFunction("other")->empty());
  168. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  169. }
  170. } // end namespace