BitReaderTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/Bitcode/BitstreamWriter.h"
  11. #include "llvm/Bitcode/ReaderWriter.h"
  12. #include "llvm/AsmParser/Parser.h"
  13. #include "llvm/IR/Constants.h"
  14. #include "llvm/IR/Instructions.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/IR/Verifier.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include "llvm/Support/SourceMgr.h"
  21. #include "gtest/gtest.h"
  22. using namespace llvm;
  23. namespace {
  24. std::unique_ptr<Module> parseAssembly(const char *Assembly) {
  25. SMDiagnostic Error;
  26. std::unique_ptr<Module> M =
  27. parseAssemblyString(Assembly, Error, getGlobalContext());
  28. std::string ErrMsg;
  29. raw_string_ostream OS(ErrMsg);
  30. Error.print("", OS);
  31. // A failure here means that the test itself is buggy.
  32. if (!M)
  33. report_fatal_error(OS.str().c_str());
  34. return M;
  35. }
  36. static void writeModuleToBuffer(std::unique_ptr<Module> Mod,
  37. SmallVectorImpl<char> &Buffer) {
  38. raw_svector_ostream OS(Buffer);
  39. WriteBitcodeToFile(Mod.get(), OS);
  40. }
  41. static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
  42. SmallString<1024> &Mem,
  43. const char *Assembly) {
  44. writeModuleToBuffer(parseAssembly(Assembly), Mem);
  45. std::unique_ptr<MemoryBuffer> Buffer =
  46. MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
  47. ErrorOr<Module *> ModuleOrErr =
  48. getLazyBitcodeModule(std::move(Buffer), Context);
  49. return std::unique_ptr<Module>(ModuleOrErr.get());
  50. }
  51. TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) {
  52. SmallString<1024> Mem;
  53. LLVMContext Context;
  54. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  55. Context, Mem, "define internal i32 @func() {\n"
  56. "ret i32 0\n"
  57. "}\n");
  58. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  59. M->getFunction("func")->materialize();
  60. EXPECT_FALSE(M->getFunction("func")->empty());
  61. EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
  62. GlobalValue::InternalLinkage);
  63. // Check that the linkage type is preserved after dematerialization.
  64. M->getFunction("func")->Dematerialize();
  65. EXPECT_TRUE(M->getFunction("func")->empty());
  66. EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
  67. GlobalValue::InternalLinkage);
  68. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  69. }
  70. TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
  71. SmallString<1024> Mem;
  72. LLVMContext Context;
  73. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  74. Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
  75. "define void @func() {\n"
  76. " unreachable\n"
  77. "bb:\n"
  78. " unreachable\n"
  79. "}\n");
  80. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  81. // Try (and fail) to dematerialize @func.
  82. M->getFunction("func")->Dematerialize();
  83. EXPECT_FALSE(M->getFunction("func")->empty());
  84. }
  85. TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) {
  86. SmallString<1024> Mem;
  87. LLVMContext Context;
  88. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  89. Context, Mem, "define i8* @before() {\n"
  90. " ret i8* blockaddress(@func, %bb)\n"
  91. "}\n"
  92. "define void @other() {\n"
  93. " unreachable\n"
  94. "}\n"
  95. "define void @func() {\n"
  96. " unreachable\n"
  97. "bb:\n"
  98. " unreachable\n"
  99. "}\n");
  100. EXPECT_TRUE(M->getFunction("before")->empty());
  101. EXPECT_TRUE(M->getFunction("func")->empty());
  102. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  103. // Materialize @before, pulling in @func.
  104. EXPECT_FALSE(M->getFunction("before")->materialize());
  105. EXPECT_FALSE(M->getFunction("func")->empty());
  106. EXPECT_TRUE(M->getFunction("other")->empty());
  107. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  108. // Try (and fail) to dematerialize @func.
  109. M->getFunction("func")->Dematerialize();
  110. EXPECT_FALSE(M->getFunction("func")->empty());
  111. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  112. }
  113. TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) {
  114. SmallString<1024> Mem;
  115. LLVMContext Context;
  116. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  117. Context, Mem, "define void @func() {\n"
  118. " unreachable\n"
  119. "bb:\n"
  120. " unreachable\n"
  121. "}\n"
  122. "define void @other() {\n"
  123. " unreachable\n"
  124. "}\n"
  125. "define i8* @after() {\n"
  126. " ret i8* blockaddress(@func, %bb)\n"
  127. "}\n");
  128. EXPECT_TRUE(M->getFunction("after")->empty());
  129. EXPECT_TRUE(M->getFunction("func")->empty());
  130. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  131. // Materialize @after, pulling in @func.
  132. EXPECT_FALSE(M->getFunction("after")->materialize());
  133. EXPECT_FALSE(M->getFunction("func")->empty());
  134. EXPECT_TRUE(M->getFunction("other")->empty());
  135. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  136. // Try (and fail) to dematerialize @func.
  137. M->getFunction("func")->Dematerialize();
  138. EXPECT_FALSE(M->getFunction("func")->empty());
  139. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  140. }
  141. } // end namespace