blob: c5ca36e417cfb98f319466fb8d4d4890fabe5fda [file] [log] [blame]
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +00001//===- MCJITMultipeModuleTest.cpp - Unit tests for the MCJIT---------------===//
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// This test suite verifies MCJIT for handling multiple modules in a single
11// ExecutionEngine by building multiple modules, making function calls across
12// modules, accessing global variables, etc.
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ExecutionEngine/MCJIT.h"
16#include "MCJITTestBase.h"
17#include "gtest/gtest.h"
18
19using namespace llvm;
20
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +000021namespace {
22
Juergen Ributzka05c5a932013-11-19 03:08:35 +000023class MCJITMultipleModuleTest : public testing::Test, public MCJITTestBase {};
24
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +000025// FIXME: ExecutionEngine has no support empty modules
26/*
27TEST_F(MCJITMultipleModuleTest, multiple_empty_modules) {
28 SKIP_UNSUPPORTED_PLATFORM;
29
30 createJIT(M.take());
31 // JIT-compile
32 EXPECT_NE(0, TheJIT->getObjectImage())
33 << "Unable to generate executable loaded object image";
34
35 TheJIT->addModule(createEmptyModule("<other module>"));
36 TheJIT->addModule(createEmptyModule("<other other module>"));
37
38 // JIT again
39 EXPECT_NE(0, TheJIT->getObjectImage())
40 << "Unable to generate executable loaded object image";
41}
42*/
43
44// Helper Function to test add operation
45void checkAdd(uint64_t ptr) {
46 ASSERT_TRUE(ptr != 0) << "Unable to get pointer to function.";
47 int (*AddPtr)(int, int) = (int (*)(int, int))ptr;
48 EXPECT_EQ(0, AddPtr(0, 0));
49 EXPECT_EQ(1, AddPtr(1, 0));
50 EXPECT_EQ(3, AddPtr(1, 2));
51 EXPECT_EQ(-5, AddPtr(-2, -3));
52 EXPECT_EQ(30, AddPtr(10, 20));
53 EXPECT_EQ(-30, AddPtr(-10, -20));
54 EXPECT_EQ(-40, AddPtr(-10, -30));
55}
56
57void checkAccumulate(uint64_t ptr) {
58 ASSERT_TRUE(ptr != 0) << "Unable to get pointer to function.";
59 int32_t (*FPtr)(int32_t) = (int32_t (*)(int32_t))(intptr_t)ptr;
60 EXPECT_EQ(0, FPtr(0));
61 EXPECT_EQ(1, FPtr(1));
62 EXPECT_EQ(3, FPtr(2));
63 EXPECT_EQ(6, FPtr(3));
64 EXPECT_EQ(10, FPtr(4));
65 EXPECT_EQ(15, FPtr(5));
66}
67
68// FIXME: ExecutionEngine has no support empty modules
69/*
70TEST_F(MCJITMultipleModuleTest, multiple_empty_modules) {
71 SKIP_UNSUPPORTED_PLATFORM;
72
73 createJIT(M.take());
74 // JIT-compile
75 EXPECT_NE(0, TheJIT->getObjectImage())
76 << "Unable to generate executable loaded object image";
77
78 TheJIT->addModule(createEmptyModule("<other module>"));
79 TheJIT->addModule(createEmptyModule("<other other module>"));
80
81 // JIT again
82 EXPECT_NE(0, TheJIT->getObjectImage())
83 << "Unable to generate executable loaded object image";
84}
85*/
86
87// Module A { Function FA },
88// Module B { Function FB },
89// execute FA then FB
90TEST_F(MCJITMultipleModuleTest, two_module_case) {
91 SKIP_UNSUPPORTED_PLATFORM;
92
Ahmed Charles56440fd2014-03-06 05:51:42 +000093 std::unique_ptr<Module> A, B;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +000094 Function *FA, *FB;
95 createTwoModuleCase(A, FA, B, FB);
96
Ahmed Charles96c9d952014-03-05 10:19:29 +000097 createJIT(A.release());
98 TheJIT->addModule(B.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +000099
100 uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
101 checkAdd(ptr);
102
103 ptr = TheJIT->getFunctionAddress(FB->getName().str());
104 checkAdd(ptr);
105}
106
107// Module A { Function FA },
108// Module B { Function FB },
109// execute FB then FA
110TEST_F(MCJITMultipleModuleTest, two_module_reverse_case) {
111 SKIP_UNSUPPORTED_PLATFORM;
112
Ahmed Charles56440fd2014-03-06 05:51:42 +0000113 std::unique_ptr<Module> A, B;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000114 Function *FA, *FB;
115 createTwoModuleCase(A, FA, B, FB);
116
Ahmed Charles96c9d952014-03-05 10:19:29 +0000117 createJIT(A.release());
118 TheJIT->addModule(B.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000119
120 uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
121 TheJIT->finalizeObject();
122 checkAdd(ptr);
123
124 ptr = TheJIT->getFunctionAddress(FA->getName().str());
125 checkAdd(ptr);
126}
127
128// Module A { Function FA },
129// Module B { Extern FA, Function FB which calls FA },
130// execute FB then FA
131TEST_F(MCJITMultipleModuleTest, two_module_extern_reverse_case) {
132 SKIP_UNSUPPORTED_PLATFORM;
133
Ahmed Charles56440fd2014-03-06 05:51:42 +0000134 std::unique_ptr<Module> A, B;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000135 Function *FA, *FB;
136 createTwoModuleExternCase(A, FA, B, FB);
137
Ahmed Charles96c9d952014-03-05 10:19:29 +0000138 createJIT(A.release());
139 TheJIT->addModule(B.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000140
141 uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
142 TheJIT->finalizeObject();
143 checkAdd(ptr);
144
145 ptr = TheJIT->getFunctionAddress(FA->getName().str());
146 checkAdd(ptr);
147}
148
149// Module A { Function FA },
150// Module B { Extern FA, Function FB which calls FA },
151// execute FA then FB
152TEST_F(MCJITMultipleModuleTest, two_module_extern_case) {
153 SKIP_UNSUPPORTED_PLATFORM;
154
Ahmed Charles56440fd2014-03-06 05:51:42 +0000155 std::unique_ptr<Module> A, B;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000156 Function *FA, *FB;
157 createTwoModuleExternCase(A, FA, B, FB);
158
Ahmed Charles96c9d952014-03-05 10:19:29 +0000159 createJIT(A.release());
160 TheJIT->addModule(B.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000161
162 uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
163 checkAdd(ptr);
164
165 ptr = TheJIT->getFunctionAddress(FB->getName().str());
166 checkAdd(ptr);
167}
168
169// Module A { Function FA1, Function FA2 which calls FA1 },
170// Module B { Extern FA1, Function FB which calls FA1 },
171// execute FB then FA2
172TEST_F(MCJITMultipleModuleTest, two_module_consecutive_call_case) {
173 SKIP_UNSUPPORTED_PLATFORM;
174
Ahmed Charles56440fd2014-03-06 05:51:42 +0000175 std::unique_ptr<Module> A, B;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000176 Function *FA1, *FA2, *FB;
177 createTwoModuleExternCase(A, FA1, B, FB);
178 FA2 = insertSimpleCallFunction<int32_t(int32_t, int32_t)>(A.get(), FA1);
179
Ahmed Charles96c9d952014-03-05 10:19:29 +0000180 createJIT(A.release());
181 TheJIT->addModule(B.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000182
183 uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
184 TheJIT->finalizeObject();
185 checkAdd(ptr);
186
187 ptr = TheJIT->getFunctionAddress(FA2->getName().str());
188 checkAdd(ptr);
189}
190
191// TODO:
192// Module A { Extern Global GVB, Global Variable GVA, Function FA loads GVB },
193// Module B { Extern Global GVA, Global Variable GVB, Function FB loads GVA },
194
195
196// Module A { Global Variable GVA, Function FA loads GVA },
197// Module B { Global Variable GVB, Function FB loads GVB },
198// execute FB then FA
199TEST_F(MCJITMultipleModuleTest, two_module_global_variables_case) {
200 SKIP_UNSUPPORTED_PLATFORM;
201
Ahmed Charles56440fd2014-03-06 05:51:42 +0000202 std::unique_ptr<Module> A, B;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000203 Function *FA, *FB;
204 GlobalVariable *GVA, *GVB;
205 A.reset(createEmptyModule("A"));
206 B.reset(createEmptyModule("B"));
207
208 int32_t initialNum = 7;
209 GVA = insertGlobalInt32(A.get(), "GVA", initialNum);
210 GVB = insertGlobalInt32(B.get(), "GVB", initialNum);
211 FA = startFunction<int32_t(void)>(A.get(), "FA");
212 endFunctionWithRet(FA, Builder.CreateLoad(GVA));
213 FB = startFunction<int32_t(void)>(B.get(), "FB");
214 endFunctionWithRet(FB, Builder.CreateLoad(GVB));
215
Ahmed Charles96c9d952014-03-05 10:19:29 +0000216 createJIT(A.release());
217 TheJIT->addModule(B.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000218
219 uint64_t FBPtr = TheJIT->getFunctionAddress(FB->getName().str());
220 TheJIT->finalizeObject();
221 EXPECT_TRUE(0 != FBPtr);
222 int32_t(*FuncPtr)(void) = (int32_t(*)(void))FBPtr;
223 EXPECT_EQ(initialNum, FuncPtr())
224 << "Invalid value for global returned from JITted function in module B";
225
226 uint64_t FAPtr = TheJIT->getFunctionAddress(FA->getName().str());
227 EXPECT_TRUE(0 != FAPtr);
228 FuncPtr = (int32_t(*)(void))FAPtr;
229 EXPECT_EQ(initialNum, FuncPtr())
230 << "Invalid value for global returned from JITted function in module A";
231}
232
233// Module A { Function FA },
234// Module B { Extern FA, Function FB which calls FA },
235// Module C { Extern FA, Function FC which calls FA },
236// execute FC, FB, FA
237TEST_F(MCJITMultipleModuleTest, three_module_case) {
Tim Northover39ddb3f2013-10-02 16:11:07 +0000238 SKIP_UNSUPPORTED_PLATFORM;
239
Ahmed Charles56440fd2014-03-06 05:51:42 +0000240 std::unique_ptr<Module> A, B, C;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000241 Function *FA, *FB, *FC;
242 createThreeModuleCase(A, FA, B, FB, C, FC);
243
Ahmed Charles96c9d952014-03-05 10:19:29 +0000244 createJIT(A.release());
245 TheJIT->addModule(B.release());
246 TheJIT->addModule(C.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000247
248 uint64_t ptr = TheJIT->getFunctionAddress(FC->getName().str());
249 checkAdd(ptr);
250
251 ptr = TheJIT->getFunctionAddress(FB->getName().str());
252 checkAdd(ptr);
253
254 ptr = TheJIT->getFunctionAddress(FA->getName().str());
255 checkAdd(ptr);
256}
257
258// Module A { Function FA },
259// Module B { Extern FA, Function FB which calls FA },
260// Module C { Extern FA, Function FC which calls FA },
261// execute FA, FB, FC
262TEST_F(MCJITMultipleModuleTest, three_module_case_reverse_order) {
Tim Northover39ddb3f2013-10-02 16:11:07 +0000263 SKIP_UNSUPPORTED_PLATFORM;
264
Ahmed Charles56440fd2014-03-06 05:51:42 +0000265 std::unique_ptr<Module> A, B, C;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000266 Function *FA, *FB, *FC;
267 createThreeModuleCase(A, FA, B, FB, C, FC);
268
Ahmed Charles96c9d952014-03-05 10:19:29 +0000269 createJIT(A.release());
270 TheJIT->addModule(B.release());
271 TheJIT->addModule(C.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000272
273 uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
274 checkAdd(ptr);
275
276 ptr = TheJIT->getFunctionAddress(FB->getName().str());
277 checkAdd(ptr);
278
279 ptr = TheJIT->getFunctionAddress(FC->getName().str());
280 checkAdd(ptr);
281}
282
283// Module A { Function FA },
284// Module B { Extern FA, Function FB which calls FA },
285// Module C { Extern FB, Function FC which calls FB },
286// execute FC, FB, FA
287TEST_F(MCJITMultipleModuleTest, three_module_chain_case) {
Tim Northover39ddb3f2013-10-02 16:11:07 +0000288 SKIP_UNSUPPORTED_PLATFORM;
289
Ahmed Charles56440fd2014-03-06 05:51:42 +0000290 std::unique_ptr<Module> A, B, C;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000291 Function *FA, *FB, *FC;
292 createThreeModuleChainedCallsCase(A, FA, B, FB, C, FC);
293
Ahmed Charles96c9d952014-03-05 10:19:29 +0000294 createJIT(A.release());
295 TheJIT->addModule(B.release());
296 TheJIT->addModule(C.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000297
298 uint64_t ptr = TheJIT->getFunctionAddress(FC->getName().str());
299 checkAdd(ptr);
300
301 ptr = TheJIT->getFunctionAddress(FB->getName().str());
302 checkAdd(ptr);
303
304 ptr = TheJIT->getFunctionAddress(FA->getName().str());
305 checkAdd(ptr);
306}
307
308// Module A { Function FA },
309// Module B { Extern FA, Function FB which calls FA },
310// Module C { Extern FB, Function FC which calls FB },
311// execute FA, FB, FC
312TEST_F(MCJITMultipleModuleTest, three_modules_chain_case_reverse_order) {
Tim Northover39ddb3f2013-10-02 16:11:07 +0000313 SKIP_UNSUPPORTED_PLATFORM;
314
Ahmed Charles56440fd2014-03-06 05:51:42 +0000315 std::unique_ptr<Module> A, B, C;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000316 Function *FA, *FB, *FC;
317 createThreeModuleChainedCallsCase(A, FA, B, FB, C, FC);
318
Ahmed Charles96c9d952014-03-05 10:19:29 +0000319 createJIT(A.release());
320 TheJIT->addModule(B.release());
321 TheJIT->addModule(C.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000322
323 uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
324 checkAdd(ptr);
325
326 ptr = TheJIT->getFunctionAddress(FB->getName().str());
327 checkAdd(ptr);
328
329 ptr = TheJIT->getFunctionAddress(FC->getName().str());
330 checkAdd(ptr);
331}
332
333// Module A { Extern FB, Function FA which calls FB1 },
334// Module B { Extern FA, Function FB1, Function FB2 which calls FA },
335// execute FA, then FB1
336// FIXME: this test case is not supported by MCJIT
337TEST_F(MCJITMultipleModuleTest, cross_module_dependency_case) {
338 SKIP_UNSUPPORTED_PLATFORM;
339
Ahmed Charles56440fd2014-03-06 05:51:42 +0000340 std::unique_ptr<Module> A, B;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000341 Function *FA, *FB1, *FB2;
342 createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
343
Ahmed Charles96c9d952014-03-05 10:19:29 +0000344 createJIT(A.release());
345 TheJIT->addModule(B.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000346
347 uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
348 checkAccumulate(ptr);
349
350 ptr = TheJIT->getFunctionAddress(FB1->getName().str());
351 checkAccumulate(ptr);
352}
353
354// Module A { Extern FB, Function FA which calls FB1 },
355// Module B { Extern FA, Function FB1, Function FB2 which calls FA },
356// execute FB1 then FA
357// FIXME: this test case is not supported by MCJIT
358TEST_F(MCJITMultipleModuleTest, cross_module_dependency_case_reverse_order) {
359 SKIP_UNSUPPORTED_PLATFORM;
360
Ahmed Charles56440fd2014-03-06 05:51:42 +0000361 std::unique_ptr<Module> A, B;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000362 Function *FA, *FB1, *FB2;
363 createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
364
Ahmed Charles96c9d952014-03-05 10:19:29 +0000365 createJIT(A.release());
366 TheJIT->addModule(B.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000367
368 uint64_t ptr = TheJIT->getFunctionAddress(FB1->getName().str());
369 checkAccumulate(ptr);
370
371 ptr = TheJIT->getFunctionAddress(FA->getName().str());
372 checkAccumulate(ptr);
373}
374
375// Module A { Extern FB1, Function FA which calls FB1 },
376// Module B { Extern FA, Function FB1, Function FB2 which calls FA },
377// execute FB1 then FB2
378// FIXME: this test case is not supported by MCJIT
379TEST_F(MCJITMultipleModuleTest, cross_module_dependency_case3) {
380 SKIP_UNSUPPORTED_PLATFORM;
381
Ahmed Charles56440fd2014-03-06 05:51:42 +0000382 std::unique_ptr<Module> A, B;
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000383 Function *FA, *FB1, *FB2;
384 createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
385
Ahmed Charles96c9d952014-03-05 10:19:29 +0000386 createJIT(A.release());
387 TheJIT->addModule(B.release());
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000388
389 uint64_t ptr = TheJIT->getFunctionAddress(FB1->getName().str());
390 checkAccumulate(ptr);
391
392 ptr = TheJIT->getFunctionAddress(FB2->getName().str());
393 checkAccumulate(ptr);
394}
395}