blob: 6b79a683bce0d86e6f72034bf445a99650175a03 [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);
Amara Emersonb586aae2012-10-31 17:44:16 +000050 static_cast<SectionMemoryManager*>(MM)->invalidateInstructionCache();
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000051 EXPECT_TRUE(0 != globalPtr)
52 << "Unable to get pointer to global value from JIT";
53
54 EXPECT_EQ(initialValue, *(int32_t*)globalPtr)
55 << "Unexpected initial value of global";
56}
57
58TEST_F(MCJITTest, add_function) {
59 SKIP_UNSUPPORTED_PLATFORM;
60
61 Function *F = insertAddFunction(M.get());
62 createJIT(M.take());
63 void *addPtr = TheJIT->getPointerToFunction(F);
Amara Emersonb586aae2012-10-31 17:44:16 +000064 static_cast<SectionMemoryManager*>(MM)->invalidateInstructionCache();
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000065 EXPECT_TRUE(0 != addPtr)
66 << "Unable to get pointer to function from JIT";
67
68 int (*AddPtrTy)(int, int) = (int(*)(int, int))(intptr_t)addPtr;
69 EXPECT_EQ(0, AddPtrTy(0, 0));
70 EXPECT_EQ(3, AddPtrTy(1, 2));
71 EXPECT_EQ(-5, AddPtrTy(-2, -3));
72}
73
74TEST_F(MCJITTest, run_main) {
75 SKIP_UNSUPPORTED_PLATFORM;
76
77 int rc = 6;
78 Function *Main = insertMainFunction(M.get(), 6);
79 createJIT(M.take());
80 void *vPtr = TheJIT->getPointerToFunction(Main);
Amara Emersonb586aae2012-10-31 17:44:16 +000081 static_cast<SectionMemoryManager*>(MM)->invalidateInstructionCache();
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000082 EXPECT_TRUE(0 != vPtr)
83 << "Unable to get pointer to main() from JIT";
84
85 int (*FuncPtr)(void) = (int(*)(void))(intptr_t)vPtr;
86 int returnCode = FuncPtr();
87 EXPECT_EQ(returnCode, rc);
88}
89
90TEST_F(MCJITTest, return_global) {
91 SKIP_UNSUPPORTED_PLATFORM;
92
93 int32_t initialNum = 7;
94 GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum);
95
96 Function *ReturnGlobal = startFunction<int32_t(void)>(M.get(),
97 "ReturnGlobal");
98 Value *ReadGlobal = Builder.CreateLoad(GV);
99 endFunctionWithRet(ReturnGlobal, ReadGlobal);
100
101 createJIT(M.take());
102 void *rgvPtr = TheJIT->getPointerToFunction(ReturnGlobal);
Amara Emersonb586aae2012-10-31 17:44:16 +0000103 static_cast<SectionMemoryManager*>(MM)->invalidateInstructionCache();
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000104 EXPECT_TRUE(0 != rgvPtr);
105
106 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)rgvPtr;
107 EXPECT_EQ(initialNum, FuncPtr())
108 << "Invalid value for global returned from JITted function";
109}
110
111// FIXME: This case fails due to a bug with getPointerToGlobal().
112// The bug is due to MCJIT not having an implementation of getPointerToGlobal()
113// which results in falling back on the ExecutionEngine implementation that
114// allocates a new memory block for the global instead of using the same
115// global variable that is emitted by MCJIT. Hence, the pointer (gvPtr below)
116// has the correct initial value, but updates to the real global (accessed by
117// JITted code) are not propagated. Instead, getPointerToGlobal() should return
118// a pointer into the loaded ObjectImage to reference the emitted global.
119/*
120TEST_F(MCJITTest, increment_global) {
121 SKIP_UNSUPPORTED_PLATFORM;
122
123 int32_t initialNum = 5;
124 Function *IncrementGlobal = startFunction<int32_t(void)>(M.get(), "IncrementGlobal");
125 GlobalVariable *GV = insertGlobalInt32(M.get(), "my_global", initialNum);
126 Value *DerefGV = Builder.CreateLoad(GV);
127 Value *AddResult = Builder.CreateAdd(DerefGV,
128 ConstantInt::get(Context, APInt(32, 1)));
129 Builder.CreateStore(AddResult, GV);
130 endFunctionWithRet(IncrementGlobal, AddResult);
131
132 createJIT(M.take());
133 void *gvPtr = TheJIT->getPointerToGlobal(GV);
134 EXPECT_EQ(initialNum, *(int32_t*)gvPtr);
135
136 void *vPtr = TheJIT->getPointerToFunction(IncrementGlobal);
137 EXPECT_TRUE(0 != vPtr)
138 << "Unable to get pointer to main() from JIT";
139
140 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
141
142 for(int i = 1; i < 3; ++i) {
143 int32_t result = FuncPtr();
144 EXPECT_EQ(initialNum + i, result); // OK
145 EXPECT_EQ(initialNum + i, *(int32_t*)gvPtr); // FAILS
146 }
147}
148*/
149
150TEST_F(MCJITTest, multiple_functions) {
151 SKIP_UNSUPPORTED_PLATFORM;
152
153 unsigned int numLevels = 23;
154 int32_t innerRetVal= 5;
155
156 Function *Inner = startFunction<int32_t(void)>(M.get(), "Inner");
157 endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal)));
158
159 Function *Outer;
160 for (unsigned int i = 0; i < numLevels; ++i) {
161 std::stringstream funcName;
162 funcName << "level_" << i;
163 Outer = startFunction<int32_t(void)>(M.get(), funcName.str());
164 Value *innerResult = Builder.CreateCall(Inner);
165 endFunctionWithRet(Outer, innerResult);
166
167 Inner = Outer;
168 }
169
170 createJIT(M.take());
171 void *vPtr = TheJIT->getPointerToFunction(Outer);
Amara Emersonb586aae2012-10-31 17:44:16 +0000172 static_cast<SectionMemoryManager*>(MM)->invalidateInstructionCache();
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000173 EXPECT_TRUE(0 != vPtr)
174 << "Unable to get pointer to outer function from JIT";
175
176 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
177 EXPECT_EQ(innerRetVal, FuncPtr())
178 << "Incorrect result returned from function";
179}
180
181// FIXME: ExecutionEngine has no support empty modules
182/*
183TEST_F(MCJITTest, multiple_empty_modules) {
184 SKIP_UNSUPPORTED_PLATFORM;
185
186 createJIT(M.take());
187 // JIT-compile
188 EXPECT_NE(0, TheJIT->getObjectImage())
189 << "Unable to generate executable loaded object image";
190
191 TheJIT->addModule(createEmptyModule("<other module>"));
192 TheJIT->addModule(createEmptyModule("<other other module>"));
193
194 // JIT again
195 EXPECT_NE(0, TheJIT->getObjectImage())
196 << "Unable to generate executable loaded object image";
197}
198*/
199
200// FIXME: MCJIT must support multiple modules
201/*
202TEST_F(MCJITTest, multiple_modules) {
203 SKIP_UNSUPPORTED_PLATFORM;
204
205 Function *Callee = insertAddFunction(M.get());
206 createJIT(M.take());
207
208 // caller function is defined in a different module
209 M.reset(createEmptyModule("<caller module>"));
210
211 Function *CalleeRef = insertExternalReferenceToFunction(M.get(), Callee);
212 Function *Caller = insertSimpleCallFunction(M.get(), CalleeRef);
213
214 TheJIT->addModule(M.take());
215
216 // get a function pointer in a module that was not used in EE construction
217 void *vPtr = TheJIT->getPointerToFunction(Caller);
218 EXPECT_NE(0, vPtr)
219 << "Unable to get pointer to caller function from JIT";
220
221 int(*FuncPtr)(int, int) = (int(*)(int, int))(intptr_t)vPtr;
222 EXPECT_EQ(0, FuncPtr(0, 0));
223 EXPECT_EQ(30, FuncPtr(10, 20));
224 EXPECT_EQ(-30, FuncPtr(-10, -20));
225
226 // ensure caller is destroyed before callee (free use before def)
227 M.reset();
228}
229*/
230
231}