1 //===- MCJITTest.cpp - Unit tests for the MCJIT ---------------------------===//
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 // This test suite verifies basic MCJIT functionality such as making function
11 // calls, using global variables, and compiling multpile modules.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ExecutionEngine/MCJIT.h"
16 #include "MCJITTestBase.h"
17 #include "SectionMemoryManager.h"
18 #include "gtest/gtest.h"
22 class MCJITTest : public testing::Test, public MCJITTestBase {
25 virtual void SetUp() {
26 M.reset(createEmptyModule("<main>"));
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.
36 TEST_F(MCJITTest, empty_module) {
38 //EXPECT_NE(0, TheJIT->getObjectImage())
39 // << "Unable to generate executable loaded object image";
43 TEST_F(MCJITTest, global_variable) {
44 SKIP_UNSUPPORTED_PLATFORM;
47 GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
49 void *globalPtr = TheJIT->getPointerToGlobal(Global);
50 EXPECT_TRUE(0 != globalPtr)
51 << "Unable to get pointer to global value from JIT";
53 EXPECT_EQ(initialValue, *(int32_t*)globalPtr)
54 << "Unexpected initial value of global";
57 TEST_F(MCJITTest, add_function) {
58 SKIP_UNSUPPORTED_PLATFORM;
60 Function *F = insertAddFunction(M.get());
62 void *addPtr = TheJIT->getPointerToFunction(F);
63 EXPECT_TRUE(0 != addPtr)
64 << "Unable to get pointer to function from JIT";
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));
72 TEST_F(MCJITTest, run_main) {
73 SKIP_UNSUPPORTED_PLATFORM;
76 Function *Main = insertMainFunction(M.get(), 6);
78 void *vPtr = TheJIT->getPointerToFunction(Main);
79 EXPECT_TRUE(0 != vPtr)
80 << "Unable to get pointer to main() from JIT";
82 int (*FuncPtr)(void) = (int(*)(void))(intptr_t)vPtr;
83 int returnCode = FuncPtr();
84 EXPECT_EQ(returnCode, rc);
87 TEST_F(MCJITTest, return_global) {
88 SKIP_UNSUPPORTED_PLATFORM;
90 int32_t initialNum = 7;
91 GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum);
93 Function *ReturnGlobal = startFunction<int32_t(void)>(M.get(),
95 Value *ReadGlobal = Builder.CreateLoad(GV);
96 endFunctionWithRet(ReturnGlobal, ReadGlobal);
99 void *rgvPtr = TheJIT->getPointerToFunction(ReturnGlobal);
100 EXPECT_TRUE(0 != rgvPtr);
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";
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.
116 TEST_F(MCJITTest, increment_global) {
117 SKIP_UNSUPPORTED_PLATFORM;
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);
129 void *gvPtr = TheJIT->getPointerToGlobal(GV);
130 EXPECT_EQ(initialNum, *(int32_t*)gvPtr);
132 void *vPtr = TheJIT->getPointerToFunction(IncrementGlobal);
133 EXPECT_TRUE(0 != vPtr)
134 << "Unable to get pointer to main() from JIT";
136 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
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
146 TEST_F(MCJITTest, multiple_functions) {
147 SKIP_UNSUPPORTED_PLATFORM;
149 unsigned int numLevels = 23;
150 int32_t innerRetVal= 5;
152 Function *Inner = startFunction<int32_t(void)>(M.get(), "Inner");
153 endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal)));
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);
167 void *vPtr = TheJIT->getPointerToFunction(Outer);
168 EXPECT_TRUE(0 != vPtr)
169 << "Unable to get pointer to outer function from JIT";
171 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
172 EXPECT_EQ(innerRetVal, FuncPtr())
173 << "Incorrect result returned from function";
176 // FIXME: ExecutionEngine has no support empty modules
178 TEST_F(MCJITTest, multiple_empty_modules) {
179 SKIP_UNSUPPORTED_PLATFORM;
183 EXPECT_NE(0, TheJIT->getObjectImage())
184 << "Unable to generate executable loaded object image";
186 TheJIT->addModule(createEmptyModule("<other module>"));
187 TheJIT->addModule(createEmptyModule("<other other module>"));
190 EXPECT_NE(0, TheJIT->getObjectImage())
191 << "Unable to generate executable loaded object image";
195 // FIXME: MCJIT must support multiple modules
197 TEST_F(MCJITTest, multiple_modules) {
198 SKIP_UNSUPPORTED_PLATFORM;
200 Function *Callee = insertAddFunction(M.get());
203 // caller function is defined in a different module
204 M.reset(createEmptyModule("<caller module>"));
206 Function *CalleeRef = insertExternalReferenceToFunction(M.get(), Callee);
207 Function *Caller = insertSimpleCallFunction(M.get(), CalleeRef);
209 TheJIT->addModule(M.take());
211 // get a function pointer in a module that was not used in EE construction
212 void *vPtr = TheJIT->getPointerToFunction(Caller);
214 << "Unable to get pointer to caller function from JIT";
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));
221 // ensure caller is destroyed before callee (free use before def)