blob: 4644bf3c2620ca0493646dcb6cf45b555180f913 [file] [log] [blame]
Andrew Kaylor2d6d5852012-10-04 20:29:44 +00001//===- MCJITTest.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 basic MCJIT functionality such as making function
11// calls, using global variables, and compiling multpile modules.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ExecutionEngine/MCJIT.h"
16#include "MCJITTestBase.h"
17#include "SectionMemoryManager.h"
18#include "gtest/gtest.h"
19
20using namespace llvm;
21
22class MCJITTest : public testing::Test, public MCJITTestBase {
23protected:
24
25 virtual void SetUp() {
26 M.reset(createEmptyModule("<main>"));
27 }
28};
29
30namespace {
31
32// FIXME: In order to JIT an empty module, there needs to be
33// an interface to ExecutionEngine that forces compilation but
34// does require retrieval of a pointer to a function/global.
35/*
36TEST_F(MCJITTest, empty_module) {
37 createJIT(M.take());
38 //EXPECT_NE(0, TheJIT->getObjectImage())
39 // << "Unable to generate executable loaded object image";
40}
41*/
42
43TEST_F(MCJITTest, global_variable) {
44 SKIP_UNSUPPORTED_PLATFORM;
45
46 int initialValue = 5;
47 GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
48 createJIT(M.take());
49 void *globalPtr = TheJIT->getPointerToGlobal(Global);
50 EXPECT_TRUE(0 != globalPtr)
51 << "Unable to get pointer to global value from JIT";
52
53 EXPECT_EQ(initialValue, *(int32_t*)globalPtr)
54 << "Unexpected initial value of global";
55}
56
57TEST_F(MCJITTest, add_function) {
58 SKIP_UNSUPPORTED_PLATFORM;
59
60 Function *F = insertAddFunction(M.get());
61 createJIT(M.take());
62 void *addPtr = TheJIT->getPointerToFunction(F);
63 EXPECT_TRUE(0 != addPtr)
64 << "Unable to get pointer to function from JIT";
65
66 int (*AddPtrTy)(int, int) = (int(*)(int, int))(intptr_t)addPtr;
67 EXPECT_EQ(0, AddPtrTy(0, 0));
68 EXPECT_EQ(3, AddPtrTy(1, 2));
69 EXPECT_EQ(-5, AddPtrTy(-2, -3));
70}
71
72TEST_F(MCJITTest, run_main) {
73 SKIP_UNSUPPORTED_PLATFORM;
74
75 int rc = 6;
76 Function *Main = insertMainFunction(M.get(), 6);
77 createJIT(M.take());
78 void *vPtr = TheJIT->getPointerToFunction(Main);
79 EXPECT_TRUE(0 != vPtr)
80 << "Unable to get pointer to main() from JIT";
81
82 int (*FuncPtr)(void) = (int(*)(void))(intptr_t)vPtr;
83 int returnCode = FuncPtr();
84 EXPECT_EQ(returnCode, rc);
85}
86
87TEST_F(MCJITTest, return_global) {
88 SKIP_UNSUPPORTED_PLATFORM;
89
90 int32_t initialNum = 7;
91 GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum);
92
93 Function *ReturnGlobal = startFunction<int32_t(void)>(M.get(),
94 "ReturnGlobal");
95 Value *ReadGlobal = Builder.CreateLoad(GV);
96 endFunctionWithRet(ReturnGlobal, ReadGlobal);
97
98 createJIT(M.take());
99 void *rgvPtr = TheJIT->getPointerToFunction(ReturnGlobal);
100 EXPECT_TRUE(0 != rgvPtr);
101
102 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)rgvPtr;
103 EXPECT_EQ(initialNum, FuncPtr())
104 << "Invalid value for global returned from JITted function";
105}
106
107// FIXME: This case fails due to a bug with getPointerToGlobal().
108// The bug is due to MCJIT not having an implementation of getPointerToGlobal()
109// which results in falling back on the ExecutionEngine implementation that
110// allocates a new memory block for the global instead of using the same
111// global variable that is emitted by MCJIT. Hence, the pointer (gvPtr below)
112// has the correct initial value, but updates to the real global (accessed by
113// JITted code) are not propagated. Instead, getPointerToGlobal() should return
114// a pointer into the loaded ObjectImage to reference the emitted global.
115/*
116TEST_F(MCJITTest, increment_global) {
117 SKIP_UNSUPPORTED_PLATFORM;
118
119 int32_t initialNum = 5;
120 Function *IncrementGlobal = startFunction<int32_t(void)>(M.get(), "IncrementGlobal");
121 GlobalVariable *GV = insertGlobalInt32(M.get(), "my_global", initialNum);
122 Value *DerefGV = Builder.CreateLoad(GV);
123 Value *AddResult = Builder.CreateAdd(DerefGV,
124 ConstantInt::get(Context, APInt(32, 1)));
125 Builder.CreateStore(AddResult, GV);
126 endFunctionWithRet(IncrementGlobal, AddResult);
127
128 createJIT(M.take());
129 void *gvPtr = TheJIT->getPointerToGlobal(GV);
130 EXPECT_EQ(initialNum, *(int32_t*)gvPtr);
131
132 void *vPtr = TheJIT->getPointerToFunction(IncrementGlobal);
133 EXPECT_TRUE(0 != vPtr)
134 << "Unable to get pointer to main() from JIT";
135
136 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
137
138 for(int i = 1; i < 3; ++i) {
139 int32_t result = FuncPtr();
140 EXPECT_EQ(initialNum + i, result); // OK
141 EXPECT_EQ(initialNum + i, *(int32_t*)gvPtr); // FAILS
142 }
143}
144*/
145
146TEST_F(MCJITTest, multiple_functions) {
147 SKIP_UNSUPPORTED_PLATFORM;
148
149 unsigned int numLevels = 23;
150 int32_t innerRetVal= 5;
151
152 Function *Inner = startFunction<int32_t(void)>(M.get(), "Inner");
153 endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal)));
154
155 Function *Outer;
156 for (unsigned int i = 0; i < numLevels; ++i) {
157 std::stringstream funcName;
158 funcName << "level_" << i;
159 Outer = startFunction<int32_t(void)>(M.get(), funcName.str());
160 Value *innerResult = Builder.CreateCall(Inner);
161 endFunctionWithRet(Outer, innerResult);
162
163 Inner = Outer;
164 }
165
166 createJIT(M.take());
167 void *vPtr = TheJIT->getPointerToFunction(Outer);
168 EXPECT_TRUE(0 != vPtr)
169 << "Unable to get pointer to outer function from JIT";
170
171 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
172 EXPECT_EQ(innerRetVal, FuncPtr())
173 << "Incorrect result returned from function";
174}
175
176// FIXME: ExecutionEngine has no support empty modules
177/*
178TEST_F(MCJITTest, multiple_empty_modules) {
179 SKIP_UNSUPPORTED_PLATFORM;
180
181 createJIT(M.take());
182 // JIT-compile
183 EXPECT_NE(0, TheJIT->getObjectImage())
184 << "Unable to generate executable loaded object image";
185
186 TheJIT->addModule(createEmptyModule("<other module>"));
187 TheJIT->addModule(createEmptyModule("<other other module>"));
188
189 // JIT again
190 EXPECT_NE(0, TheJIT->getObjectImage())
191 << "Unable to generate executable loaded object image";
192}
193*/
194
195// FIXME: MCJIT must support multiple modules
196/*
197TEST_F(MCJITTest, multiple_modules) {
198 SKIP_UNSUPPORTED_PLATFORM;
199
200 Function *Callee = insertAddFunction(M.get());
201 createJIT(M.take());
202
203 // caller function is defined in a different module
204 M.reset(createEmptyModule("<caller module>"));
205
206 Function *CalleeRef = insertExternalReferenceToFunction(M.get(), Callee);
207 Function *Caller = insertSimpleCallFunction(M.get(), CalleeRef);
208
209 TheJIT->addModule(M.take());
210
211 // get a function pointer in a module that was not used in EE construction
212 void *vPtr = TheJIT->getPointerToFunction(Caller);
213 EXPECT_NE(0, vPtr)
214 << "Unable to get pointer to caller function from JIT";
215
216 int(*FuncPtr)(int, int) = (int(*)(int, int))(intptr_t)vPtr;
217 EXPECT_EQ(0, FuncPtr(0, 0));
218 EXPECT_EQ(30, FuncPtr(10, 20));
219 EXPECT_EQ(-30, FuncPtr(-10, -20));
220
221 // ensure caller is destroyed before callee (free use before def)
222 M.reset();
223}
224*/
225
226}