1 //===- MultiJITTest.cpp - Unit tests for instantiating multiple JITs ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
23 // ARM, PowerPC and SystemZ tests disabled pending fix for PR10783.
24 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__) \
25 && !defined(__aarch64__)
27 std::unique_ptr<Module> loadAssembly(LLVMContext &Context,
28 const char *Assembly) {
30 std::unique_ptr<Module> Ret = parseAssemblyString(Assembly, Error, Context);
32 raw_string_ostream os(errMsg);
34 EXPECT_TRUE((bool)Ret) << os.str();
35 return std::move(Ret);
38 std::unique_ptr<Module> createModule1(LLVMContext &Context1, Function *&FooF1) {
39 std::unique_ptr<Module> Ret = loadAssembly(Context1,
40 "define i32 @add1(i32 %ArgX1) { "
42 " %addresult = add i32 1, %ArgX1 "
43 " ret i32 %addresult "
46 "define i32 @foo1() { "
48 " %add1 = call i32 @add1(i32 10) "
51 FooF1 = Ret->getFunction("foo1");
52 return std::move(Ret);
55 std::unique_ptr<Module> createModule2(LLVMContext &Context2, Function *&FooF2) {
56 std::unique_ptr<Module> Ret = loadAssembly(Context2,
57 "define i32 @add2(i32 %ArgX2) { "
59 " %addresult = add i32 2, %ArgX2 "
60 " ret i32 %addresult "
63 "define i32 @foo2() { "
65 " %add2 = call i32 @add2(i32 10) "
68 FooF2 = Ret->getFunction("foo2");
69 return std::move(Ret);
72 TEST(MultiJitTest, EagerMode) {
74 Function *FooF1 = nullptr;
75 std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
78 Function *FooF2 = nullptr;
79 std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
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);
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);
92 // Import result of execution:
93 EXPECT_EQ(gv1.IntVal, 11);
94 EXPECT_EQ(gv2.IntVal, 12);
96 EE1->freeMachineCodeForFunction(FooF1);
97 EE2->freeMachineCodeForFunction(FooF2);
100 TEST(MultiJitTest, LazyMode) {
101 LLVMContext Context1;
102 Function *FooF1 = nullptr;
103 std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
105 LLVMContext Context2;
106 Function *FooF2 = nullptr;
107 std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
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);
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);
120 // Import result of execution:
121 EXPECT_EQ(gv1.IntVal, 11);
122 EXPECT_EQ(gv2.IntVal, 12);
124 EE1->freeMachineCodeForFunction(FooF1);
125 EE2->freeMachineCodeForFunction(FooF2);
129 extern void *getPointerToNamedFunction(const char *Name);
132 TEST(MultiJitTest, JitPool) {
133 LLVMContext Context1;
134 Function *FooF1 = nullptr;
135 std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
137 LLVMContext Context2;
138 Function *FooF2 = nullptr;
139 std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
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());
145 Function *F1 = EE1->FindFunctionNamed("foo1");
146 void *foo1 = EE1->getPointerToFunction(F1);
148 Function *F2 = EE2->FindFunctionNamed("foo2");
149 void *foo2 = EE2->getPointerToFunction(F2);
152 EXPECT_EQ(getPointerToNamedFunction("foo1"), foo1);
155 EXPECT_EQ(getPointerToNamedFunction("foo2"), foo2);
159 sa = (intptr_t)getPointerToNamedFunction("getPointerToNamedFunction");
160 EXPECT_TRUE(sa != 0);
161 intptr_t fa = (intptr_t)&getPointerToNamedFunction;
162 EXPECT_TRUE(fa != 0);
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
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
180 EXPECT_TRUE(sa == fa);
182 #endif // !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__)
184 } // anonymous namespace