Make it explicit that ExecutionEngine takes ownership of the modules.
[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 "llvm/ExecutionEngine/JIT.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/ExecutionEngine/GenericValue.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "gtest/gtest.h"
17 #include <vector>
18
19 using namespace llvm;
20
21 namespace {
22
23 // ARM, PowerPC and SystemZ tests disabled pending fix for PR10783.
24 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__) \
25                       && !defined(__aarch64__)
26
27 std::unique_ptr<Module> loadAssembly(LLVMContext &Context,
28                                      const char *Assembly) {
29   SMDiagnostic Error;
30   std::unique_ptr<Module> Ret(
31       ParseAssemblyString(Assembly, nullptr, Error, Context));
32   std::string errMsg;
33   raw_string_ostream os(errMsg);
34   Error.print("", os);
35   EXPECT_TRUE((bool)Ret) << os.str();
36   return std::move(Ret);
37 }
38
39 std::unique_ptr<Module> createModule1(LLVMContext &Context1, Function *&FooF1) {
40   std::unique_ptr<Module> Ret =  loadAssembly(Context1,
41                    "define i32 @add1(i32 %ArgX1) { "
42                    "entry: "
43                    "  %addresult = add i32 1, %ArgX1 "
44                    "  ret i32 %addresult "
45                    "} "
46                    " "
47                    "define i32 @foo1() { "
48                    "entry: "
49                    "  %add1 = call i32 @add1(i32 10) "
50                    "  ret i32 %add1 "
51                    "} ");
52   FooF1 = Ret->getFunction("foo1");
53   return std::move(Ret);
54 }
55
56 std::unique_ptr<Module> createModule2(LLVMContext &Context2, Function *&FooF2) {
57   std::unique_ptr<Module> Ret = loadAssembly(Context2,
58                    "define i32 @add2(i32 %ArgX2) { "
59                    "entry: "
60                    "  %addresult = add i32 2, %ArgX2 "
61                    "  ret i32 %addresult "
62                    "} "
63                    " "
64                    "define i32 @foo2() { "
65                    "entry: "
66                    "  %add2 = call i32 @add2(i32 10) "
67                    "  ret i32 %add2 "
68                    "} ");
69   FooF2 = Ret->getFunction("foo2");
70   return std::move(Ret);
71 }
72
73 TEST(MultiJitTest, EagerMode) {
74   LLVMContext Context1;
75   Function *FooF1 = nullptr;
76   std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
77
78   LLVMContext Context2;
79   Function *FooF2 = nullptr;
80   std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
81
82   // Now we create the JIT in eager mode
83   std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
84   EE1->DisableLazyCompilation(true);
85   std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
86   EE2->DisableLazyCompilation(true);
87
88   // Call the `foo' function with no arguments:
89   std::vector<GenericValue> noargs;
90   GenericValue gv1 = EE1->runFunction(FooF1, noargs);
91   GenericValue gv2 = EE2->runFunction(FooF2, noargs);
92
93   // Import result of execution:
94   EXPECT_EQ(gv1.IntVal, 11);
95   EXPECT_EQ(gv2.IntVal, 12);
96
97   EE1->freeMachineCodeForFunction(FooF1);
98   EE2->freeMachineCodeForFunction(FooF2);
99 }
100
101 TEST(MultiJitTest, LazyMode) {
102   LLVMContext Context1;
103   Function *FooF1 = nullptr;
104   std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
105
106   LLVMContext Context2;
107   Function *FooF2 = nullptr;
108   std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
109
110   // Now we create the JIT in lazy mode
111   std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
112   EE1->DisableLazyCompilation(false);
113   std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
114   EE2->DisableLazyCompilation(false);
115
116   // Call the `foo' function with no arguments:
117   std::vector<GenericValue> noargs;
118   GenericValue gv1 = EE1->runFunction(FooF1, noargs);
119   GenericValue gv2 = EE2->runFunction(FooF2, noargs);
120
121   // Import result of execution:
122   EXPECT_EQ(gv1.IntVal, 11);
123   EXPECT_EQ(gv2.IntVal, 12);
124
125   EE1->freeMachineCodeForFunction(FooF1);
126   EE2->freeMachineCodeForFunction(FooF2);
127 }
128
129 extern "C" {
130   extern void *getPointerToNamedFunction(const char *Name);
131 }
132
133 TEST(MultiJitTest, JitPool) {
134   LLVMContext Context1;
135   Function *FooF1 = nullptr;
136   std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
137
138   LLVMContext Context2;
139   Function *FooF2 = nullptr;
140   std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
141
142   // Now we create two JITs
143   std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
144   std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
145
146   Function *F1 = EE1->FindFunctionNamed("foo1");
147   void *foo1 = EE1->getPointerToFunction(F1);
148
149   Function *F2 = EE2->FindFunctionNamed("foo2");
150   void *foo2 = EE2->getPointerToFunction(F2);
151
152   // Function in M1
153   EXPECT_EQ(getPointerToNamedFunction("foo1"), foo1);
154
155   // Function in M2
156   EXPECT_EQ(getPointerToNamedFunction("foo2"), foo2);
157
158   // Symbol search
159   intptr_t
160     sa = (intptr_t)getPointerToNamedFunction("getPointerToNamedFunction");
161   EXPECT_TRUE(sa != 0);
162   intptr_t fa = (intptr_t)&getPointerToNamedFunction;
163   EXPECT_TRUE(fa != 0);
164 #ifdef __i386__
165   // getPointerToNamedFunction might be indirect jump on Win32 --enable-shared.
166   // FF 25 <disp32>: jmp *(pointer to IAT)
167   if (sa != fa && memcmp((char *)fa, "\xFF\x25", 2) == 0) {
168     fa = *(intptr_t *)(fa + 2); // Address to IAT
169     EXPECT_TRUE(fa != 0);
170     fa = *(intptr_t *)fa;       // Bound value of IAT
171   }
172 #elif defined(__x86_64__)
173   // getPointerToNamedFunction might be indirect jump
174   // on Win32 x64 --enable-shared.
175   // FF 25 <pcrel32>: jmp *(RIP + pointer to IAT)
176   if (sa != fa && memcmp((char *)fa, "\xFF\x25", 2) == 0) {
177     fa += *(int32_t *)(fa + 2) + 6;     // Address to IAT(RIP)
178     fa = *(intptr_t *)fa;               // Bound value of IAT
179   }
180 #endif
181   EXPECT_TRUE(sa == fa);
182 }
183 #endif  // !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__)
184
185 }  // anonymous namespace