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 "llvm/Support/DynamicLibrary.h"
17 #include "MCJITTestBase.h"
18 #include "gtest/gtest.h"
24 class MCJITTest : public testing::Test, public MCJITTestBase {
26 virtual void SetUp() { M.reset(createEmptyModule("<main>")); }
29 // FIXME: Ensure creating an execution engine does not crash when constructed
30 // with a null module.
32 TEST_F(MCJITTest, null_module) {
37 // FIXME: In order to JIT an empty module, there needs to be
38 // an interface to ExecutionEngine that forces compilation but
39 // does not require retrieval of a pointer to a function/global.
41 TEST_F(MCJITTest, empty_module) {
43 //EXPECT_NE(0, TheJIT->getObjectImage())
44 // << "Unable to generate executable loaded object image";
48 TEST_F(MCJITTest, global_variable) {
49 SKIP_UNSUPPORTED_PLATFORM;
52 GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
53 createJIT(std::move(M));
54 void *globalPtr = TheJIT->getPointerToGlobal(Global);
55 EXPECT_TRUE(nullptr != globalPtr)
56 << "Unable to get pointer to global value from JIT";
58 EXPECT_EQ(initialValue, *(int32_t*)globalPtr)
59 << "Unexpected initial value of global";
62 TEST_F(MCJITTest, add_function) {
63 SKIP_UNSUPPORTED_PLATFORM;
65 Function *F = insertAddFunction(M.get());
66 createJIT(std::move(M));
67 uint64_t addPtr = TheJIT->getFunctionAddress(F->getName().str());
68 EXPECT_TRUE(0 != addPtr)
69 << "Unable to get pointer to function from JIT";
71 ASSERT_TRUE(addPtr != 0) << "Unable to get pointer to function .";
72 int (*AddPtr)(int, int) = (int(*)(int, int))addPtr ;
73 EXPECT_EQ(0, AddPtr(0, 0));
74 EXPECT_EQ(1, AddPtr(1, 0));
75 EXPECT_EQ(3, AddPtr(1, 2));
76 EXPECT_EQ(-5, AddPtr(-2, -3));
77 EXPECT_EQ(30, AddPtr(10, 20));
78 EXPECT_EQ(-30, AddPtr(-10, -20));
79 EXPECT_EQ(-40, AddPtr(-10, -30));
82 TEST_F(MCJITTest, run_main) {
83 SKIP_UNSUPPORTED_PLATFORM;
86 Function *Main = insertMainFunction(M.get(), 6);
87 createJIT(std::move(M));
88 uint64_t ptr = TheJIT->getFunctionAddress(Main->getName().str());
90 << "Unable to get pointer to main() from JIT";
92 int (*FuncPtr)(void) = (int(*)(void))ptr;
93 int returnCode = FuncPtr();
94 EXPECT_EQ(returnCode, rc);
97 TEST_F(MCJITTest, return_global) {
98 SKIP_UNSUPPORTED_PLATFORM;
100 int32_t initialNum = 7;
101 GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum);
103 Function *ReturnGlobal = startFunction<int32_t(void)>(M.get(),
105 Value *ReadGlobal = Builder.CreateLoad(GV);
106 endFunctionWithRet(ReturnGlobal, ReadGlobal);
108 createJIT(std::move(M));
109 uint64_t rgvPtr = TheJIT->getFunctionAddress(ReturnGlobal->getName().str());
110 EXPECT_TRUE(0 != rgvPtr);
112 int32_t(*FuncPtr)(void) = (int32_t(*)(void))rgvPtr;
113 EXPECT_EQ(initialNum, FuncPtr())
114 << "Invalid value for global returned from JITted function";
117 // FIXME: This case fails due to a bug with getPointerToGlobal().
118 // The bug is due to MCJIT not having an implementation of getPointerToGlobal()
119 // which results in falling back on the ExecutionEngine implementation that
120 // allocates a new memory block for the global instead of using the same
121 // global variable that is emitted by MCJIT. Hence, the pointer (gvPtr below)
122 // has the correct initial value, but updates to the real global (accessed by
123 // JITted code) are not propagated. Instead, getPointerToGlobal() should return
124 // a pointer into the loaded ObjectImage to reference the emitted global.
126 TEST_F(MCJITTest, increment_global) {
127 SKIP_UNSUPPORTED_PLATFORM;
129 int32_t initialNum = 5;
130 Function *IncrementGlobal = startFunction<int32_t(void)>(M.get(), "IncrementGlobal");
131 GlobalVariable *GV = insertGlobalInt32(M.get(), "my_global", initialNum);
132 Value *DerefGV = Builder.CreateLoad(GV);
133 Value *AddResult = Builder.CreateAdd(DerefGV,
134 ConstantInt::get(Context, APInt(32, 1)));
135 Builder.CreateStore(AddResult, GV);
136 endFunctionWithRet(IncrementGlobal, AddResult);
139 void *gvPtr = TheJIT->getPointerToGlobal(GV);
140 EXPECT_EQ(initialNum, *(int32_t*)gvPtr);
142 void *vPtr = TheJIT->getFunctionAddress(IncrementGlobal->getName().str());
143 EXPECT_TRUE(0 != vPtr)
144 << "Unable to get pointer to main() from JIT";
146 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
148 for(int i = 1; i < 3; ++i) {
149 int32_t result = FuncPtr();
150 EXPECT_EQ(initialNum + i, result); // OK
151 EXPECT_EQ(initialNum + i, *(int32_t*)gvPtr); // FAILS
156 // PR16013: XFAIL this test on ARM, which currently can't handle multiple relocations.
157 #if !defined(__arm__)
159 TEST_F(MCJITTest, multiple_functions) {
160 SKIP_UNSUPPORTED_PLATFORM;
162 unsigned int numLevels = 23;
163 int32_t innerRetVal= 5;
165 Function *Inner = startFunction<int32_t(void)>(M.get(), "Inner");
166 endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal)));
169 for (unsigned int i = 0; i < numLevels; ++i) {
170 std::stringstream funcName;
171 funcName << "level_" << i;
172 Outer = startFunction<int32_t(void)>(M.get(), funcName.str());
173 Value *innerResult = Builder.CreateCall(Inner);
174 endFunctionWithRet(Outer, innerResult);
179 createJIT(std::move(M));
180 uint64_t ptr = TheJIT->getFunctionAddress(Outer->getName().str());
181 EXPECT_TRUE(0 != ptr)
182 << "Unable to get pointer to outer function from JIT";
184 int32_t(*FuncPtr)(void) = (int32_t(*)(void))ptr;
185 EXPECT_EQ(innerRetVal, FuncPtr())
186 << "Incorrect result returned from function";
189 #endif /*!defined(__arm__)*/
191 TEST_F(MCJITTest, multiple_decl_lookups) {
192 SKIP_UNSUPPORTED_PLATFORM;
194 Function *Foo = insertExternalReferenceToFunction<void(void)>(M.get(), "_exit");
195 createJIT(std::move(M));
196 void *A = TheJIT->getPointerToFunction(Foo);
197 void *B = TheJIT->getPointerToFunction(Foo);
199 EXPECT_TRUE(A != 0) << "Failed lookup - test not correctly configured.";
200 EXPECT_EQ(A, B) << "Repeat calls to getPointerToFunction fail.";
203 // Test weak symbol linking when the weak symbol is present in a shared
205 TEST_F(MCJITTest, weak_symbol_present) {
206 SKIP_UNSUPPORTED_PLATFORM;
209 llvm::sys::DynamicLibrary::AddSymbol("FakeWeakSymbol", &FakeWeakSymbol);
210 createJITFromAssembly(
211 "$FakeWeakSymbol = comdat any\n"
212 "@FakeWeakSymbol = linkonce_odr global i32 42, comdat, align 4\n"
213 "define i32 @weak_test(i32* %arg) {\n"
214 " %r = icmp eq i32* %arg, @FakeWeakSymbol\n"
215 " %ret = zext i1 %r to i32\n"
219 uint64_t Addr = TheJIT->getFunctionAddress("weak_test");;
220 EXPECT_TRUE(Addr != 0);
221 int32_t(*FuncPtr)(int32_t *) = (int32_t(*)(int32_t *))Addr;
222 EXPECT_EQ(FuncPtr(&FakeWeakSymbol),1);
223 EXPECT_TRUE(TheJIT->getGlobalValueAddress("FakeWeakSymbol") == 0);
226 // Test weak symbol linking when the weak symbol is not present in a
228 TEST_F(MCJITTest, weak_symbol_absent) {
229 SKIP_UNSUPPORTED_PLATFORM;
232 createJITFromAssembly(
233 " $FakeWeakSymbol2 = comdat any\n"
234 " @FakeWeakSymbol2 = linkonce_odr global i32 42, comdat, align 4\n"
235 " define i32* @get_weak() {\n"
236 " ret i32* @FakeWeakSymbol2\n"
239 (void*(*)(void))TheJIT->getFunctionAddress("get_weak");
240 EXPECT_EQ(FuncPtr(),(void*)TheJIT->getGlobalValueAddress("FakeWeakSymbol2"));