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 "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"
17 #include <vector>
18
19 using namespace llvm;
20
21 namespace {
22
LoadAssemblyInto(Module * M,const char * assembly)23 bool LoadAssemblyInto(Module *M, const char *assembly) {
24 SMDiagnostic Error;
25 bool success =
26 NULL != ParseAssemblyString(assembly, M, Error, M->getContext());
27 std::string errMsg;
28 raw_string_ostream os(errMsg);
29 Error.Print("", os);
30 EXPECT_TRUE(success) << os.str();
31 return success;
32 }
33
createModule1(LLVMContext & Context1,Module * & M1,Function * & FooF1)34 void createModule1(LLVMContext &Context1, Module *&M1, Function *&FooF1) {
35 M1 = new Module("test1", Context1);
36 LoadAssemblyInto(M1,
37 "define i32 @add1(i32 %ArgX1) { "
38 "entry: "
39 " %addresult = add i32 1, %ArgX1 "
40 " ret i32 %addresult "
41 "} "
42 " "
43 "define i32 @foo1() { "
44 "entry: "
45 " %add1 = call i32 @add1(i32 10) "
46 " ret i32 %add1 "
47 "} ");
48 FooF1 = M1->getFunction("foo1");
49 }
50
createModule2(LLVMContext & Context2,Module * & M2,Function * & FooF2)51 void createModule2(LLVMContext &Context2, Module *&M2, Function *&FooF2) {
52 M2 = new Module("test2", Context2);
53 LoadAssemblyInto(M2,
54 "define i32 @add2(i32 %ArgX2) { "
55 "entry: "
56 " %addresult = add i32 2, %ArgX2 "
57 " ret i32 %addresult "
58 "} "
59 " "
60 "define i32 @foo2() { "
61 "entry: "
62 " %add2 = call i32 @add2(i32 10) "
63 " ret i32 %add2 "
64 "} ");
65 FooF2 = M2->getFunction("foo2");
66 }
67
68 // ARM tests disabled pending fix for PR10783.
69 #if !defined(__arm__)
70
TEST(MultiJitTest,EagerMode)71 TEST(MultiJitTest, EagerMode) {
72 LLVMContext Context1;
73 Module *M1 = 0;
74 Function *FooF1 = 0;
75 createModule1(Context1, M1, FooF1);
76
77 LLVMContext Context2;
78 Module *M2 = 0;
79 Function *FooF2 = 0;
80 createModule2(Context2, M2, FooF2);
81
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);
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
TEST(MultiJitTest,LazyMode)101 TEST(MultiJitTest, LazyMode) {
102 LLVMContext Context1;
103 Module *M1 = 0;
104 Function *FooF1 = 0;
105 createModule1(Context1, M1, FooF1);
106
107 LLVMContext Context2;
108 Module *M2 = 0;
109 Function *FooF2 = 0;
110 createModule2(Context2, M2, FooF2);
111
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);
117
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);
122
123 // Import result of execution:
124 EXPECT_EQ(gv1.IntVal, 11);
125 EXPECT_EQ(gv2.IntVal, 12);
126
127 EE1->freeMachineCodeForFunction(FooF1);
128 EE2->freeMachineCodeForFunction(FooF2);
129 }
130
131 extern "C" {
132 extern void *getPointerToNamedFunction(const char *Name);
133 }
134
TEST(MultiJitTest,JitPool)135 TEST(MultiJitTest, JitPool) {
136 LLVMContext Context1;
137 Module *M1 = 0;
138 Function *FooF1 = 0;
139 createModule1(Context1, M1, FooF1);
140
141 LLVMContext Context2;
142 Module *M2 = 0;
143 Function *FooF2 = 0;
144 createModule2(Context2, M2, FooF2);
145
146 // Now we create two JITs
147 OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
148 OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
149
150 Function *F1 = EE1->FindFunctionNamed("foo1");
151 void *foo1 = EE1->getPointerToFunction(F1);
152
153 Function *F2 = EE2->FindFunctionNamed("foo2");
154 void *foo2 = EE2->getPointerToFunction(F2);
155
156 // Function in M1
157 EXPECT_EQ(getPointerToNamedFunction("foo1"), foo1);
158
159 // Function in M2
160 EXPECT_EQ(getPointerToNamedFunction("foo2"), foo2);
161
162 // Symbol search
163 EXPECT_EQ((intptr_t)getPointerToNamedFunction("getPointerToNamedFunction"),
164 (intptr_t)&getPointerToNamedFunction);
165 }
166 #endif // !defined(__arm__)
167
168 } // anonymous namespace
169