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 "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"
23 bool LoadAssemblyInto(Module *M, const char *assembly) {
26 NULL != ParseAssemblyString(assembly, M, Error, M->getContext());
28 raw_string_ostream os(errMsg);
30 EXPECT_TRUE(success) << os.str();
34 void createModule1(LLVMContext &Context1, Module *&M1, Function *&FooF1) {
35 M1 = new Module("test1", Context1);
37 "define i32 @add1(i32 %ArgX1) { "
39 " %addresult = add i32 1, %ArgX1 "
40 " ret i32 %addresult "
43 "define i32 @foo1() { "
45 " %add1 = call i32 @add1(i32 10) "
48 FooF1 = M1->getFunction("foo1");
51 void createModule2(LLVMContext &Context2, Module *&M2, Function *&FooF2) {
52 M2 = new Module("test2", Context2);
54 "define i32 @add2(i32 %ArgX2) { "
56 " %addresult = add i32 2, %ArgX2 "
57 " ret i32 %addresult "
60 "define i32 @foo2() { "
62 " %add2 = call i32 @add2(i32 10) "
65 FooF2 = M2->getFunction("foo2");
68 // ARM tests disabled pending fix for PR10783.
71 TEST(MultiJitTest, EagerMode) {
75 createModule1(Context1, M1, FooF1);
80 createModule2(Context2, M2, FooF2);
82 // Now we create the JIT in eager mode
83 OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
84 EE1->DisableLazyCompilation(true);
85 OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
86 EE2->DisableLazyCompilation(true);
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);
93 // Import result of execution:
94 EXPECT_EQ(gv1.IntVal, 11);
95 EXPECT_EQ(gv2.IntVal, 12);
97 EE1->freeMachineCodeForFunction(FooF1);
98 EE2->freeMachineCodeForFunction(FooF2);
101 TEST(MultiJitTest, LazyMode) {
102 LLVMContext Context1;
105 createModule1(Context1, M1, FooF1);
107 LLVMContext Context2;
110 createModule2(Context2, M2, FooF2);
112 // Now we create the JIT in lazy mode
113 OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
114 EE1->DisableLazyCompilation(false);
115 OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
116 EE2->DisableLazyCompilation(false);
118 // Call the `foo' function with no arguments:
119 std::vector<GenericValue> noargs;
120 GenericValue gv1 = EE1->runFunction(FooF1, noargs);
121 GenericValue gv2 = EE2->runFunction(FooF2, noargs);
123 // Import result of execution:
124 EXPECT_EQ(gv1.IntVal, 11);
125 EXPECT_EQ(gv2.IntVal, 12);
127 EE1->freeMachineCodeForFunction(FooF1);
128 EE2->freeMachineCodeForFunction(FooF2);
132 extern void *getPointerToNamedFunction(const char *Name);
135 TEST(MultiJitTest, JitPool) {
136 LLVMContext Context1;
139 createModule1(Context1, M1, FooF1);
141 LLVMContext Context2;
144 createModule2(Context2, M2, FooF2);
146 // Now we create two JITs
147 OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
148 OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
150 Function *F1 = EE1->FindFunctionNamed("foo1");
151 void *foo1 = EE1->getPointerToFunction(F1);
153 Function *F2 = EE2->FindFunctionNamed("foo2");
154 void *foo2 = EE2->getPointerToFunction(F2);
157 EXPECT_EQ(getPointerToNamedFunction("foo1"), foo1);
160 EXPECT_EQ(getPointerToNamedFunction("foo2"), foo2);
164 sa = (intptr_t)getPointerToNamedFunction("getPointerToNamedFunction");
165 EXPECT_TRUE(sa != 0);
166 intptr_t fa = (intptr_t)&getPointerToNamedFunction;
167 EXPECT_TRUE(fa != 0);
169 // getPointerToNamedFunction might be indirect jump on Win32 --enable-shared.
170 // FF 25 <disp32>: jmp *(pointer to IAT)
171 if (sa != fa && memcmp((char *)fa, "\xFF\x25", 2) == 0) {
172 fa = *(intptr_t *)(fa + 2); // Address to IAT
173 EXPECT_TRUE(fa != 0);
174 fa = *(intptr_t *)fa; // Bound value of IAT
177 EXPECT_TRUE(sa == fa);
179 #endif // !defined(__arm__)
181 } // anonymous namespace