libbcc

Change-Id: Ieaa3ebd5a38f370752495549f8870b534eeedfc5
diff --git a/unittests/ExecutionEngine/JIT/JITTest.cpp b/unittests/ExecutionEngine/JIT/JITTest.cpp
index 84ee0e3..b85f724 100644
--- a/unittests/ExecutionEngine/JIT/JITTest.cpp
+++ b/unittests/ExecutionEngine/JIT/JITTest.cpp
@@ -65,6 +65,8 @@
     stubsAllocated = 0;
   }
 
+  void setSizeRequired(bool Required) { SizeRequired = Required; }
+
   virtual void setMemoryWritable() { Base->setMemoryWritable(); }
   virtual void setMemoryExecutable() { Base->setMemoryExecutable(); }
   virtual void setPoisonMemory(bool poison) { Base->setPoisonMemory(poison); }
@@ -628,6 +630,54 @@
                         << " not 7 from the IR version.";
 }
 
+TEST_F(JITTest, NeedsExactSizeWithManyGlobals) {
+  // PR5291: When the JMM needed the exact size of function bodies before
+  // starting to emit them, the JITEmitter would modify a set while iterating
+  // over it.
+  TheJIT->DisableLazyCompilation(true);
+  RJMM->setSizeRequired(true);
+
+  LoadAssembly("@A = global i32 42 "
+               "@B = global i32* @A "
+               "@C = global i32** @B "
+               "@D = global i32*** @C "
+               "@E = global i32**** @D "
+               "@F = global i32***** @E "
+               "@G = global i32****** @F "
+               "@H = global i32******* @G "
+               "@I = global i32******** @H "
+               "define i32********* @test() { "
+               "  ret i32********* @I "
+               "}");
+  Function *testIR = M->getFunction("test");
+  int32_t********* (*test)() = reinterpret_cast<int32_t*********(*)()>(
+    (intptr_t)TheJIT->getPointerToFunction(testIR));
+  EXPECT_EQ(42, *********test());
+}
+
+TEST_F(JITTest, EscapedLazyStubStillCallable) {
+  TheJIT->DisableLazyCompilation(false);
+  LoadAssembly("define internal i32 @stubbed() { "
+               "  ret i32 42 "
+               "} "
+               " "
+               "define i32()* @get_stub() { "
+               "  ret i32()* @stubbed "
+               "} ");
+  typedef int32_t(*StubTy)();
+
+  // Call get_stub() to get the address of @stubbed without actually JITting it.
+  Function *get_stubIR = M->getFunction("get_stub");
+  StubTy (*get_stub)() = reinterpret_cast<StubTy(*)()>(
+    (intptr_t)TheJIT->getPointerToFunction(get_stubIR));
+  StubTy stubbed = get_stub();
+  // Now get_stubIR is the only reference to stubbed's stub.
+  get_stubIR->eraseFromParent();
+  // Now there are no references inside the JIT, but we've got a pointer outside
+  // it.  The stub should be callable and return the right value.
+  EXPECT_EQ(42, stubbed());
+}
+
 // Converts the LLVM assembly to bitcode and returns it in a std::string.  An
 // empty string indicates an error.
 std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) {
diff --git a/unittests/ExecutionEngine/JIT/MultiJITTest.cpp b/unittests/ExecutionEngine/JIT/MultiJITTest.cpp
new file mode 100644
index 0000000..8997d39
--- /dev/null
+++ b/unittests/ExecutionEngine/JIT/MultiJITTest.cpp
@@ -0,0 +1,164 @@
+//===- MultiJITTest.cpp - Unit tests for instantiating multiple JITs ------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "llvm/LLVMContext.h"
+#include "llvm/Module.h"
+#include "llvm/Assembly/Parser.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/Support/SourceMgr.h"
+#include <vector>
+
+using namespace llvm;
+
+namespace {
+
+bool LoadAssemblyInto(Module *M, const char *assembly) {
+  SMDiagnostic Error;
+  bool success =
+    NULL != ParseAssemblyString(assembly, M, Error, M->getContext());
+  std::string errMsg;
+  raw_string_ostream os(errMsg);
+  Error.Print("", os);
+  EXPECT_TRUE(success) << os.str();
+  return success;
+}
+
+void createModule1(LLVMContext &Context1, Module *&M1, Function *&FooF1) {
+  M1 = new Module("test1", Context1);
+  LoadAssemblyInto(M1,
+                   "define i32 @add1(i32 %ArgX1) { "
+                   "entry: "
+                   "  %addresult = add i32 1, %ArgX1 "
+                   "  ret i32 %addresult "
+                   "} "
+                   " "
+                   "define i32 @foo1() { "
+                   "entry: "
+                   "  %add1 = call i32 @add1(i32 10) "
+                   "  ret i32 %add1 "
+                   "} ");
+  FooF1 = M1->getFunction("foo1");
+}
+
+void createModule2(LLVMContext &Context2, Module *&M2, Function *&FooF2) {
+  M2 = new Module("test2", Context2);
+  LoadAssemblyInto(M2,
+                   "define i32 @add2(i32 %ArgX2) { "
+                   "entry: "
+                   "  %addresult = add i32 2, %ArgX2 "
+                   "  ret i32 %addresult "
+                   "} "
+                   " "
+                   "define i32 @foo2() { "
+                   "entry: "
+                   "  %add2 = call i32 @add2(i32 10) "
+                   "  ret i32 %add2 "
+                   "} ");
+  FooF2 = M2->getFunction("foo2");
+}
+
+TEST(MultiJitTest, EagerMode) {
+  LLVMContext Context1;
+  Module *M1 = 0;
+  Function *FooF1 = 0;
+  createModule1(Context1, M1, FooF1);
+
+  LLVMContext Context2;
+  Module *M2 = 0;
+  Function *FooF2 = 0;
+  createModule2(Context2, M2, FooF2);
+
+  // Now we create the JIT in eager mode
+  OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
+  EE1->DisableLazyCompilation(true);
+  OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  EE2->DisableLazyCompilation(true);
+
+  // Call the `foo' function with no arguments:
+  std::vector<GenericValue> noargs;
+  GenericValue gv1 = EE1->runFunction(FooF1, noargs);
+  GenericValue gv2 = EE2->runFunction(FooF2, noargs);
+
+  // Import result of execution:
+  EXPECT_EQ(gv1.IntVal, 11);
+  EXPECT_EQ(gv2.IntVal, 12);
+
+  EE1->freeMachineCodeForFunction(FooF1);
+  EE2->freeMachineCodeForFunction(FooF2);
+}
+
+TEST(MultiJitTest, LazyMode) {
+  LLVMContext Context1;
+  Module *M1 = 0;
+  Function *FooF1 = 0;
+  createModule1(Context1, M1, FooF1);
+
+  LLVMContext Context2;
+  Module *M2 = 0;
+  Function *FooF2 = 0;
+  createModule2(Context2, M2, FooF2);
+
+  // Now we create the JIT in lazy mode
+  OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
+  EE1->DisableLazyCompilation(false);
+  OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  EE2->DisableLazyCompilation(false);
+
+  // Call the `foo' function with no arguments:
+  std::vector<GenericValue> noargs;
+  GenericValue gv1 = EE1->runFunction(FooF1, noargs);
+  GenericValue gv2 = EE2->runFunction(FooF2, noargs);
+
+  // Import result of execution:
+  EXPECT_EQ(gv1.IntVal, 11);
+  EXPECT_EQ(gv2.IntVal, 12);
+
+  EE1->freeMachineCodeForFunction(FooF1);
+  EE2->freeMachineCodeForFunction(FooF2);
+}
+
+extern "C" {
+  extern void *getPointerToNamedFunction(const char *Name);
+}
+
+TEST(MultiJitTest, JitPool) {
+  LLVMContext Context1;
+  Module *M1 = 0;
+  Function *FooF1 = 0;
+  createModule1(Context1, M1, FooF1);
+
+  LLVMContext Context2;
+  Module *M2 = 0;
+  Function *FooF2 = 0;
+  createModule2(Context2, M2, FooF2);
+
+  // Now we create two JITs
+  OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
+  OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+
+  Function *F1 = EE1->FindFunctionNamed("foo1");
+  void *foo1 = EE1->getPointerToFunction(F1);
+
+  Function *F2 = EE2->FindFunctionNamed("foo2");
+  void *foo2 = EE2->getPointerToFunction(F2);
+
+  // Function in M1
+  EXPECT_EQ(getPointerToNamedFunction("foo1"), foo1);
+
+  // Function in M2
+  EXPECT_EQ(getPointerToNamedFunction("foo2"), foo2);
+
+  // Symbol search
+  EXPECT_EQ((intptr_t)getPointerToNamedFunction("getPointerToNamedFunction"),
+            (intptr_t)&getPointerToNamedFunction);
+}
+
+}  // anonymous namespace