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 {
27 void MCJITTest::SetUp() {
28 M.reset(createEmptyModule("<main>"));
33 // FIXME: Ensure creating an execution engine does not crash when constructed
34 // with a null module.
36 TEST_F(MCJITTest, null_module) {
41 // FIXME: In order to JIT an empty module, there needs to be
42 // an interface to ExecutionEngine that forces compilation but
43 // does not require retrieval of a pointer to a function/global.
45 TEST_F(MCJITTest, empty_module) {
47 //EXPECT_NE(0, TheJIT->getObjectImage())
48 // << "Unable to generate executable loaded object image";
52 TEST_F(MCJITTest, global_variable) {
53 SKIP_UNSUPPORTED_PLATFORM;
56 GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
58 void *globalPtr = TheJIT->getPointerToGlobal(Global);
59 EXPECT_TRUE(0 != globalPtr)
60 << "Unable to get pointer to global value from JIT";
62 EXPECT_EQ(initialValue, *(int32_t*)globalPtr)
63 << "Unexpected initial value of global";
66 TEST_F(MCJITTest, add_function) {
67 SKIP_UNSUPPORTED_PLATFORM;
69 Function *F = insertAddFunction(M.get());
71 uint64_t addPtr = TheJIT->getFunctionAddress(F->getName().str());
72 EXPECT_TRUE(0 != addPtr)
73 << "Unable to get pointer to function from JIT";
75 ASSERT_TRUE(addPtr != 0) << "Unable to get pointer to function .";
76 int (*AddPtr)(int, int) = (int(*)(int, int))addPtr ;
77 EXPECT_EQ(0, AddPtr(0, 0));
78 EXPECT_EQ(1, AddPtr(1, 0));
79 EXPECT_EQ(3, AddPtr(1, 2));
80 EXPECT_EQ(-5, AddPtr(-2, -3));
81 EXPECT_EQ(30, AddPtr(10, 20));
82 EXPECT_EQ(-30, AddPtr(-10, -20));
83 EXPECT_EQ(-40, AddPtr(-10, -30));
86 TEST_F(MCJITTest, run_main) {
87 SKIP_UNSUPPORTED_PLATFORM;
90 Function *Main = insertMainFunction(M.get(), 6);
92 uint64_t ptr = TheJIT->getFunctionAddress(Main->getName().str());
94 << "Unable to get pointer to main() from JIT";
96 int (*FuncPtr)(void) = (int(*)(void))ptr;
97 int returnCode = FuncPtr();
98 EXPECT_EQ(returnCode, rc);
101 TEST_F(MCJITTest, return_global) {
102 SKIP_UNSUPPORTED_PLATFORM;
104 int32_t initialNum = 7;
105 GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum);
107 Function *ReturnGlobal = startFunction<int32_t(void)>(M.get(),
109 Value *ReadGlobal = Builder.CreateLoad(GV);
110 endFunctionWithRet(ReturnGlobal, ReadGlobal);
113 uint64_t rgvPtr = TheJIT->getFunctionAddress(ReturnGlobal->getName().str());
114 EXPECT_TRUE(0 != rgvPtr);
116 int32_t(*FuncPtr)(void) = (int32_t(*)(void))rgvPtr;
117 EXPECT_EQ(initialNum, FuncPtr())
118 << "Invalid value for global returned from JITted function";
121 // FIXME: This case fails due to a bug with getPointerToGlobal().
122 // The bug is due to MCJIT not having an implementation of getPointerToGlobal()
123 // which results in falling back on the ExecutionEngine implementation that
124 // allocates a new memory block for the global instead of using the same
125 // global variable that is emitted by MCJIT. Hence, the pointer (gvPtr below)
126 // has the correct initial value, but updates to the real global (accessed by
127 // JITted code) are not propagated. Instead, getPointerToGlobal() should return
128 // a pointer into the loaded ObjectImage to reference the emitted global.
130 TEST_F(MCJITTest, increment_global) {
131 SKIP_UNSUPPORTED_PLATFORM;
133 int32_t initialNum = 5;
134 Function *IncrementGlobal = startFunction<int32_t(void)>(M.get(), "IncrementGlobal");
135 GlobalVariable *GV = insertGlobalInt32(M.get(), "my_global", initialNum);
136 Value *DerefGV = Builder.CreateLoad(GV);
137 Value *AddResult = Builder.CreateAdd(DerefGV,
138 ConstantInt::get(Context, APInt(32, 1)));
139 Builder.CreateStore(AddResult, GV);
140 endFunctionWithRet(IncrementGlobal, AddResult);
143 void *gvPtr = TheJIT->getPointerToGlobal(GV);
144 EXPECT_EQ(initialNum, *(int32_t*)gvPtr);
146 void *vPtr = TheJIT->getFunctionAddress(IncrementGlobal->getName().str());
147 EXPECT_TRUE(0 != vPtr)
148 << "Unable to get pointer to main() from JIT";
150 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
152 for(int i = 1; i < 3; ++i) {
153 int32_t result = FuncPtr();
154 EXPECT_EQ(initialNum + i, result); // OK
155 EXPECT_EQ(initialNum + i, *(int32_t*)gvPtr); // FAILS
160 // PR16013: XFAIL this test on ARM, which currently can't handle multiple relocations.
161 #if !defined(__arm__)
163 TEST_F(MCJITTest, multiple_functions) {
164 SKIP_UNSUPPORTED_PLATFORM;
166 unsigned int numLevels = 23;
167 int32_t innerRetVal= 5;
169 Function *Inner = startFunction<int32_t(void)>(M.get(), "Inner");
170 endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal)));
173 for (unsigned int i = 0; i < numLevels; ++i) {
174 std::stringstream funcName;
175 funcName << "level_" << i;
176 Outer = startFunction<int32_t(void)>(M.get(), funcName.str());
177 Value *innerResult = Builder.CreateCall(Inner);
178 endFunctionWithRet(Outer, innerResult);
184 uint64_t ptr = TheJIT->getFunctionAddress(Outer->getName().str());
185 EXPECT_TRUE(0 != ptr)
186 << "Unable to get pointer to outer function from JIT";
188 int32_t(*FuncPtr)(void) = (int32_t(*)(void))ptr;
189 EXPECT_EQ(innerRetVal, FuncPtr())
190 << "Incorrect result returned from function";
193 #endif /*!defined(__arm__)*/