Make it possible to create multiple JIT instances at the same time, by removing
[oota-llvm.git] / unittests / ExecutionEngine / JIT / MultiJITTest.cpp
1 //===- MultiJITTest.cpp - Unit tests for instantiating multiple JITs ------===//
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
10 #include "gtest/gtest.h"
11 #include "llvm/LLVMContext.h"
12 #include "llvm/Module.h"
13 #include "llvm/Assembly/Parser.h"
14 #include "llvm/ExecutionEngine/GenericValue.h"
15 #include "llvm/ExecutionEngine/JIT.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include <vector>
18
19 using namespace llvm;
20
21 namespace {
22
23 bool LoadAssemblyInto(Module *M, const char *assembly) {
24   SMDiagnostic Error;
25   bool success =
26     NULL != ParseAssemblyString(assembly, M, Error, M->getContext());
27   std::string errMsg;
28   raw_string_ostream os(errMsg);
29   Error.Print("", os);
30   EXPECT_TRUE(success) << os.str();
31   return success;
32 }
33
34 void createModule1(LLVMContext &Context1, Module *&M1, Function *&FooF1) {
35   M1 = new Module("test1", Context1);
36   LoadAssemblyInto(M1,
37                    "define i32 @add1(i32 %ArgX1) { "
38                    "entry: "
39                    "  %addresult = add i32 1, %ArgX1 "
40                    "  ret i32 %addresult "
41                    "} "
42                    " "
43                    "define i32 @foo1() { "
44                    "entry: "
45                    "  %add1 = call i32 @add1(i32 10) "
46                    "  ret i32 %add1 "
47                    "} ");
48   FooF1 = M1->getFunction("foo1");
49 }
50
51 void createModule2(LLVMContext &Context2, Module *&M2, Function *&FooF2) {
52   M2 = new Module("test2", Context2);
53   LoadAssemblyInto(M2,
54                    "define i32 @add2(i32 %ArgX2) { "
55                    "entry: "
56                    "  %addresult = add i32 2, %ArgX2 "
57                    "  ret i32 %addresult "
58                    "} "
59                    " "
60                    "define i32 @foo2() { "
61                    "entry: "
62                    "  %add2 = call i32 @add2(i32 10) "
63                    "  ret i32 %add2 "
64                    "} ");
65   FooF2 = M2->getFunction("foo2");
66 }
67
68 TEST(MultiJitTest, EagerMode) {
69   LLVMContext Context1;
70   Module *M1 = 0;
71   Function *FooF1 = 0;
72   createModule1(Context1, M1, FooF1);
73
74   LLVMContext Context2;
75   Module *M2 = 0;
76   Function *FooF2 = 0;
77   createModule2(Context2, M2, FooF2);
78
79   // Now we create the JIT in eager mode
80   OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
81   EE1->DisableLazyCompilation(true);
82   OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
83   EE2->DisableLazyCompilation(true);
84
85   // Call the `foo' function with no arguments:
86   std::vector<GenericValue> noargs;
87   GenericValue gv1 = EE1->runFunction(FooF1, noargs);
88   GenericValue gv2 = EE2->runFunction(FooF2, noargs);
89
90   // Import result of execution:
91   EXPECT_EQ(gv1.IntVal, 11);
92   EXPECT_EQ(gv2.IntVal, 12);
93
94   EE1->freeMachineCodeForFunction(FooF1);
95   EE2->freeMachineCodeForFunction(FooF2);
96 }
97
98 TEST(MultiJitTest, LazyMode) {
99   LLVMContext Context1;
100   Module *M1 = 0;
101   Function *FooF1 = 0;
102   createModule1(Context1, M1, FooF1);
103
104   LLVMContext Context2;
105   Module *M2 = 0;
106   Function *FooF2 = 0;
107   createModule2(Context2, M2, FooF2);
108
109   // Now we create the JIT in lazy mode
110   OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
111   EE1->DisableLazyCompilation(false);
112   OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
113   EE2->DisableLazyCompilation(false);
114
115   // Call the `foo' function with no arguments:
116   std::vector<GenericValue> noargs;
117   GenericValue gv1 = EE1->runFunction(FooF1, noargs);
118   GenericValue gv2 = EE2->runFunction(FooF2, noargs);
119
120   // Import result of execution:
121   EXPECT_EQ(gv1.IntVal, 11);
122   EXPECT_EQ(gv2.IntVal, 12);
123
124   EE1->freeMachineCodeForFunction(FooF1);
125   EE2->freeMachineCodeForFunction(FooF2);
126 }
127
128 extern "C" {
129   extern void *getPointerToNamedFunction(const char *Name);
130 }
131
132 TEST(MultiJitTest, JitPool) {
133   LLVMContext Context1;
134   Module *M1 = 0;
135   Function *FooF1 = 0;
136   createModule1(Context1, M1, FooF1);
137
138   LLVMContext Context2;
139   Module *M2 = 0;
140   Function *FooF2 = 0;
141   createModule2(Context2, M2, FooF2);
142
143   // Now we create two JITs
144   OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
145   OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
146
147   Function *F1 = EE1->FindFunctionNamed("foo1");
148   void *foo1 = EE1->getPointerToFunction(F1);
149
150   Function *F2 = EE2->FindFunctionNamed("foo2");
151   void *foo2 = EE2->getPointerToFunction(F2);
152
153   // Function in M1
154   EXPECT_EQ(getPointerToNamedFunction("foo1"), foo1);
155
156   // Function in M2
157   EXPECT_EQ(getPointerToNamedFunction("foo2"), foo2);
158
159   // Symbol search
160   EXPECT_EQ((intptr_t)getPointerToNamedFunction("getPointerToNamedFunction"),
161             (intptr_t)&getPointerToNamedFunction);
162 }
163
164 }  // anonymous namespace