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 "gtest/gtest.h"
21 class MCJITTest : public testing::Test, public MCJITTestBase {
24 virtual void SetUp() {
25 M.reset(createEmptyModule("<main>"));
31 // FIXME: Ensure creating an execution engine does not crash when constructed
32 // with a null module.
34 TEST_F(MCJITTest, null_module) {
39 // FIXME: In order to JIT an empty module, there needs to be
40 // an interface to ExecutionEngine that forces compilation but
41 // does not require retrieval of a pointer to a function/global.
43 TEST_F(MCJITTest, empty_module) {
45 //EXPECT_NE(0, TheJIT->getObjectImage())
46 // << "Unable to generate executable loaded object image";
50 TEST_F(MCJITTest, global_variable) {
51 SKIP_UNSUPPORTED_PLATFORM;
54 GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
56 void *globalPtr = TheJIT->getPointerToGlobal(Global);
57 EXPECT_TRUE(0 != globalPtr)
58 << "Unable to get pointer to global value from JIT";
60 EXPECT_EQ(initialValue, *(int32_t*)globalPtr)
61 << "Unexpected initial value of global";
64 TEST_F(MCJITTest, add_function) {
65 SKIP_UNSUPPORTED_PLATFORM;
67 Function *F = insertAddFunction(M.get());
69 uint64_t addPtr = TheJIT->getFunctionAddress(F->getName().str());
70 EXPECT_TRUE(0 != addPtr)
71 << "Unable to get pointer to function from JIT";
73 ASSERT_TRUE(addPtr != 0) << "Unable to get pointer to function .";
74 int (*AddPtr)(int, int) = (int(*)(int, int))addPtr ;
75 EXPECT_EQ(0, AddPtr(0, 0));
76 EXPECT_EQ(1, AddPtr(1, 0));
77 EXPECT_EQ(3, AddPtr(1, 2));
78 EXPECT_EQ(-5, AddPtr(-2, -3));
79 EXPECT_EQ(30, AddPtr(10, 20));
80 EXPECT_EQ(-30, AddPtr(-10, -20));
81 EXPECT_EQ(-40, AddPtr(-10, -30));
84 TEST_F(MCJITTest, run_main) {
85 SKIP_UNSUPPORTED_PLATFORM;
88 Function *Main = insertMainFunction(M.get(), 6);
90 uint64_t ptr = TheJIT->getFunctionAddress(Main->getName().str());
92 << "Unable to get pointer to main() from JIT";
94 int (*FuncPtr)(void) = (int(*)(void))ptr;
95 int returnCode = FuncPtr();
96 EXPECT_EQ(returnCode, rc);
99 TEST_F(MCJITTest, return_global) {
100 SKIP_UNSUPPORTED_PLATFORM;
102 int32_t initialNum = 7;
103 GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum);
105 Function *ReturnGlobal = startFunction<int32_t(void)>(M.get(),
107 Value *ReadGlobal = Builder.CreateLoad(GV);
108 endFunctionWithRet(ReturnGlobal, ReadGlobal);
111 uint64_t rgvPtr = TheJIT->getFunctionAddress(ReturnGlobal->getName().str());
112 EXPECT_TRUE(0 != rgvPtr);
114 int32_t(*FuncPtr)(void) = (int32_t(*)(void))rgvPtr;
115 EXPECT_EQ(initialNum, FuncPtr())
116 << "Invalid value for global returned from JITted function";
119 // FIXME: This case fails due to a bug with getPointerToGlobal().
120 // The bug is due to MCJIT not having an implementation of getPointerToGlobal()
121 // which results in falling back on the ExecutionEngine implementation that
122 // allocates a new memory block for the global instead of using the same
123 // global variable that is emitted by MCJIT. Hence, the pointer (gvPtr below)
124 // has the correct initial value, but updates to the real global (accessed by
125 // JITted code) are not propagated. Instead, getPointerToGlobal() should return
126 // a pointer into the loaded ObjectImage to reference the emitted global.
128 TEST_F(MCJITTest, increment_global) {
129 SKIP_UNSUPPORTED_PLATFORM;
131 int32_t initialNum = 5;
132 Function *IncrementGlobal = startFunction<int32_t(void)>(M.get(), "IncrementGlobal");
133 GlobalVariable *GV = insertGlobalInt32(M.get(), "my_global", initialNum);
134 Value *DerefGV = Builder.CreateLoad(GV);
135 Value *AddResult = Builder.CreateAdd(DerefGV,
136 ConstantInt::get(Context, APInt(32, 1)));
137 Builder.CreateStore(AddResult, GV);
138 endFunctionWithRet(IncrementGlobal, AddResult);
141 void *gvPtr = TheJIT->getPointerToGlobal(GV);
142 EXPECT_EQ(initialNum, *(int32_t*)gvPtr);
144 void *vPtr = TheJIT->getFunctionAddress(IncrementGlobal->getName().str());
145 EXPECT_TRUE(0 != vPtr)
146 << "Unable to get pointer to main() from JIT";
148 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
150 for(int i = 1; i < 3; ++i) {
151 int32_t result = FuncPtr();
152 EXPECT_EQ(initialNum + i, result); // OK
153 EXPECT_EQ(initialNum + i, *(int32_t*)gvPtr); // FAILS
158 // PR16013: XFAIL this test on ARM, which currently can't handle multiple relocations.
159 #if !defined(__arm__)
161 TEST_F(MCJITTest, multiple_functions) {
162 SKIP_UNSUPPORTED_PLATFORM;
164 unsigned int numLevels = 23;
165 int32_t innerRetVal= 5;
167 Function *Inner = startFunction<int32_t(void)>(M.get(), "Inner");
168 endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal)));
171 for (unsigned int i = 0; i < numLevels; ++i) {
172 std::stringstream funcName;
173 funcName << "level_" << i;
174 Outer = startFunction<int32_t(void)>(M.get(), funcName.str());
175 Value *innerResult = Builder.CreateCall(Inner);
176 endFunctionWithRet(Outer, innerResult);
182 uint64_t ptr = TheJIT->getFunctionAddress(Outer->getName().str());
183 EXPECT_TRUE(0 != ptr)
184 << "Unable to get pointer to outer function from JIT";
186 int32_t(*FuncPtr)(void) = (int32_t(*)(void))ptr;
187 EXPECT_EQ(innerRetVal, FuncPtr())
188 << "Incorrect result returned from function";
191 #endif /*!defined(__arm__)*/