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 TEST(MultiJitTest, EagerMode) {
72 createModule1(Context1, M1, FooF1);
77 createModule2(Context2, M2, FooF2);
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);
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);
90 // Import result of execution:
91 EXPECT_EQ(gv1.IntVal, 11);
92 EXPECT_EQ(gv2.IntVal, 12);
94 EE1->freeMachineCodeForFunction(FooF1);
95 EE2->freeMachineCodeForFunction(FooF2);
98 TEST(MultiJitTest, LazyMode) {
102 createModule1(Context1, M1, FooF1);
104 LLVMContext Context2;
107 createModule2(Context2, M2, FooF2);
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);
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;
136 createModule1(Context1, M1, FooF1);
138 LLVMContext Context2;
141 createModule2(Context2, M2, FooF2);
143 // Now we create two JITs
144 OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
145 OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
147 Function *F1 = EE1->FindFunctionNamed("foo1");
148 void *foo1 = EE1->getPointerToFunction(F1);
150 Function *F2 = EE2->FindFunctionNamed("foo2");
151 void *foo2 = EE2->getPointerToFunction(F2);
154 EXPECT_EQ(getPointerToNamedFunction("foo1"), foo1);
157 EXPECT_EQ(getPointerToNamedFunction("foo2"), foo2);
160 EXPECT_EQ((intptr_t)getPointerToNamedFunction("getPointerToNamedFunction"),
161 (intptr_t)&getPointerToNamedFunction);
164 } // anonymous namespace