BitReaderTest.cpp 6.3 KB

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