[llvm-commits] [llvm] r147425 - in /llvm/trunk: lib/Bitcode/Reader/BitcodeReader.cpp lib/Bitcode/Reader/BitcodeReader.h unittests/CMakeLists.txt unittests/VMCore/pr11677.cpp

Rafael Espindola rafael.espindola at gmail.com
Sun Jan 1 23:49:53 PST 2012


Author: rafael
Date: Mon Jan  2 01:49:53 2012
New Revision: 147425

URL: http://llvm.org/viewvc/llvm-project?rev=147425&view=rev
Log:
Materialize functions whose basic blocks are used by global variables. Fixes
PR11677.

Added:
    llvm/trunk/unittests/VMCore/pr11677.cpp
Modified:
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.h
    llvm/trunk/unittests/CMakeLists.txt

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=147425&r1=147424&r2=147425&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Mon Jan  2 01:49:53 2012
@@ -27,6 +27,13 @@
 #include "llvm/OperandTraits.h"
 using namespace llvm;
 
+void BitcodeReader::materializeForwardReferencedFunctions() {
+  while (!BlockAddrFwdRefs.empty()) {
+    Function *F = BlockAddrFwdRefs.begin()->first;
+    F->Materialize();
+  }
+}
+
 void BitcodeReader::FreeState() {
   if (BufferOwned)
     delete Buffer;
@@ -2779,6 +2786,9 @@
   }
   // Have the BitcodeReader dtor delete 'Buffer'.
   R->setBufferOwned(true);
+
+  R->materializeForwardReferencedFunctions();
+
   return M;
 }
 

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.h?rev=147425&r1=147424&r2=147425&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.h (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.h Mon Jan  2 01:49:53 2012
@@ -184,7 +184,9 @@
   ~BitcodeReader() {
     FreeState();
   }
-  
+
+  void materializeForwardReferencedFunctions();
+
   void FreeState();
   
   /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer

Modified: llvm/trunk/unittests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/CMakeLists.txt?rev=147425&r1=147424&r2=147425&view=diff
==============================================================================
--- llvm/trunk/unittests/CMakeLists.txt (original)
+++ llvm/trunk/unittests/CMakeLists.txt Mon Jan  2 01:49:53 2012
@@ -112,6 +112,7 @@
   VMCore/PassManagerTest.cpp
   VMCore/ValueMapTest.cpp
   VMCore/VerifierTest.cpp
+  VMCore/pr11677.cpp
   )
 
 # MSVC9 and 8 cannot compile ValueMapTest.cpp due to their bug.

Added: llvm/trunk/unittests/VMCore/pr11677.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/VMCore/pr11677.cpp?rev=147425&view=auto
==============================================================================
--- llvm/trunk/unittests/VMCore/pr11677.cpp (added)
+++ llvm/trunk/unittests/VMCore/pr11677.cpp Mon Jan  2 01:49:53 2012
@@ -0,0 +1,64 @@
+//===- llvm/unittest/VMCore/pr11677.cpp - Test for blockaddr --------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/Verifier.h"
+#include "llvm/Bitcode/BitstreamWriter.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/Constants.h"
+#include "llvm/Instructions.h"
+#include "llvm/LLVMContext.h"
+#include "llvm/Module.h"
+#include "llvm/PassManager.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "gtest/gtest.h"
+
+namespace llvm {
+namespace {
+
+static Module *makeLLVMModule() {
+  Module* Mod = new Module("test-mem", getGlobalContext());
+
+  FunctionType* FuncTy =
+    FunctionType::get(Type::getVoidTy(Mod->getContext()), false);
+  Function* Func = Function::Create(FuncTy,GlobalValue::ExternalLinkage,
+                                    "func", Mod);
+
+  BasicBlock* Entry = BasicBlock::Create(Mod->getContext(), "entry", Func);
+  new UnreachableInst(Mod->getContext(), Entry);
+
+  BasicBlock* BB = BasicBlock::Create(Mod->getContext(), "bb", Func);
+  new UnreachableInst(Mod->getContext(), BB);
+
+  PointerType* Int8Ptr = Type::getInt8PtrTy(Mod->getContext());
+  new GlobalVariable(*Mod, Int8Ptr, /*isConstant=*/true,
+                     GlobalValue::ExternalLinkage,
+                     BlockAddress::get(BB), "table");
+
+  return Mod;
+}
+
+static void writeModuleToBuffer(std::vector<unsigned char> &Buffer) {
+  Module *Mod = makeLLVMModule();
+  BitstreamWriter Stream(Buffer);
+  WriteBitcodeToStream(Mod, Stream);
+}
+
+TEST(PR11677, BlockAddr) {
+  std::vector<unsigned char> Mem;
+  writeModuleToBuffer(Mem);
+  StringRef Data((const char*)&Mem[0], Mem.size());
+  MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Data, "test", false);
+  std::string errMsg;
+  Module *m = getLazyBitcodeModule(Buffer, getGlobalContext(), &errMsg);
+  PassManager passes;
+  passes.add(createVerifierPass());
+  passes.run(*m);
+}
+}
+}





More information about the llvm-commits mailing list