[CFLAA] LLVM_CONSTEXPR -> const
[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 = parseAssemblyString(Assembly, Error, Context);
31   std::string errMsg;
32   raw_string_ostream os(errMsg);
33   Error.print("", os);
34   EXPECT_TRUE((bool)Ret) << os.str();
35   return std::move(Ret);
36 }
37
38 std::unique_ptr<Module> createModule1(LLVMContext &Context1, Function *&FooF1) {
39   std::unique_ptr<Module> Ret =  loadAssembly(Context1,
40                    "define i32 @add1(i32 %ArgX1) { "
41                    "entry: "
42                    "  %addresult = add i32 1, %ArgX1 "
43                    "  ret i32 %addresult "
44                    "} "
45                    " "
46                    "define i32 @foo1() { "
47                    "entry: "
48                    "  %add1 = call i32 @add1(i32 10) "
49                    "  ret i32 %add1 "
50                    "} ");
51   FooF1 = Ret->getFunction("foo1");
52   return std::move(Ret);
53 }
54
55 std::unique_ptr<Module> createModule2(LLVMContext &Context2, Function *&FooF2) {
56   std::unique_ptr<Module> Ret = loadAssembly(Context2,
57                    "define i32 @add2(i32 %ArgX2) { "
58                    "entry: "
59                    "  %addresult = add i32 2, %ArgX2 "
60                    "  ret i32 %addresult "
61                    "} "
62                    " "
63                    "define i32 @foo2() { "
64                    "entry: "
65                    "  %add2 = call i32 @add2(i32 10) "
66                    "  ret i32 %add2 "
67                    "} ");
68   FooF2 = Ret->getFunction("foo2");
69   return std::move(Ret);
70 }
71
72 TEST(MultiJitTest, EagerMode) {
73   LLVMContext Context1;
74   Function *FooF1 = nullptr;
75   std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
76
77   LLVMContext Context2;
78   Function *FooF2 = nullptr;
79   std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
80
81   // Now we create the JIT in eager mode
82   std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
83   EE1->DisableLazyCompilation(true);
84   std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
85   EE2->DisableLazyCompilation(true);
86
87   // Call the `foo' function with no arguments:
88   std::vector<GenericValue> noargs;
89   GenericValue gv1 = EE1->runFunction(FooF1, noargs);
90   GenericValue gv2 = EE2->runFunction(FooF2, noargs);
91
92   // Import result of execution:
93   EXPECT_EQ(gv1.IntVal, 11);
94   EXPECT_EQ(gv2.IntVal, 12);
95
96   EE1->freeMachineCodeForFunction(FooF1);
97   EE2->freeMachineCodeForFunction(FooF2);
98 }
99
100 TEST(MultiJitTest, LazyMode) {
101   LLVMContext Context1;
102   Function *FooF1 = nullptr;
103   std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
104
105   LLVMContext Context2;
106   Function *FooF2 = nullptr;
107   std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
108
109   // Now we create the JIT in lazy mode
110   std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
111   EE1->DisableLazyCompilation(false);
112   std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(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   Function *FooF1 = nullptr;
135   std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
136
137   LLVMContext Context2;
138   Function *FooF2 = nullptr;
139   std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
140
141   // Now we create two JITs
142   std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
143   std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
144
145   Function *F1 = EE1->FindFunctionNamed("foo1");
146   void *foo1 = EE1->getPointerToFunction(F1);
147
148   Function *F2 = EE2->FindFunctionNamed("foo2");
149   void *foo2 = EE2->getPointerToFunction(F2);
150
151   // Function in M1
152   EXPECT_EQ(getPointerToNamedFunction("foo1"), foo1);
153
154   // Function in M2
155   EXPECT_EQ(getPointerToNamedFunction("foo2"), foo2);
156
157   // Symbol search
158   intptr_t
159     sa = (intptr_t)getPointerToNamedFunction("getPointerToNamedFunction");
160   EXPECT_TRUE(sa != 0);
161   intptr_t fa = (intptr_t)&getPointerToNamedFunction;
162   EXPECT_TRUE(fa != 0);
163 #ifdef __i386__
164   // getPointerToNamedFunction might be indirect jump on Win32 --enable-shared.
165   // FF 25 <disp32>: jmp *(pointer to IAT)
166   if (sa != fa && memcmp((char *)fa, "\xFF\x25", 2) == 0) {
167     fa = *(intptr_t *)(fa + 2); // Address to IAT
168     EXPECT_TRUE(fa != 0);
169     fa = *(intptr_t *)fa;       // Bound value of IAT
170   }
171 #elif defined(__x86_64__)
172   // getPointerToNamedFunction might be indirect jump
173   // on Win32 x64 --enable-shared.
174   // FF 25 <pcrel32>: jmp *(RIP + pointer to IAT)
175   if (sa != fa && memcmp((char *)fa, "\xFF\x25", 2) == 0) {
176     fa += *(int32_t *)(fa + 2) + 6;     // Address to IAT(RIP)
177     fa = *(intptr_t *)fa;               // Bound value of IAT
178   }
179 #endif
180   EXPECT_TRUE(sa == fa);
181 }
182 #endif  // !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__)
183
184 }  // anonymous namespace