blob: 7ccd2546c063b0250427b85261db3d56465239ae [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"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000017#include "gtest/gtest.h"
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000018
19using namespace llvm;
20
21class MCJITTest : public testing::Test, public MCJITTestBase {
22protected:
23
24 virtual void SetUp() {
25 M.reset(createEmptyModule("<main>"));
26 }
27};
28
29namespace {
30
Andrew Kaylor70c1ea42013-10-01 01:48:36 +000031// FIXME: Ensure creating an execution engine does not crash when constructed
32// with a null module.
33/*
34TEST_F(MCJITTest, null_module) {
35 createJIT(0);
36}
37*/
38
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000039// FIXME: In order to JIT an empty module, there needs to be
40// an interface to ExecutionEngine that forces compilation but
Andrew Kaylor70c1ea42013-10-01 01:48:36 +000041// does not require retrieval of a pointer to a function/global.
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000042/*
43TEST_F(MCJITTest, empty_module) {
44 createJIT(M.take());
45 //EXPECT_NE(0, TheJIT->getObjectImage())
46 // << "Unable to generate executable loaded object image";
47}
48*/
49
50TEST_F(MCJITTest, global_variable) {
51 SKIP_UNSUPPORTED_PLATFORM;
52
53 int initialValue = 5;
54 GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
55 createJIT(M.take());
56 void *globalPtr = TheJIT->getPointerToGlobal(Global);
57 EXPECT_TRUE(0 != globalPtr)
58 << "Unable to get pointer to global value from JIT";
59
60 EXPECT_EQ(initialValue, *(int32_t*)globalPtr)
61 << "Unexpected initial value of global";
62}
63
64TEST_F(MCJITTest, add_function) {
65 SKIP_UNSUPPORTED_PLATFORM;
66
67 Function *F = insertAddFunction(M.get());
68 createJIT(M.take());
Andrew Kaylor70c1ea42013-10-01 01:48:36 +000069 uint64_t addPtr = TheJIT->getFunctionAddress(F->getName().str());
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000070 EXPECT_TRUE(0 != addPtr)
71 << "Unable to get pointer to function from JIT";
72
Andrew Kaylor70c1ea42013-10-01 01:48:36 +000073 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));
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000082}
83
84TEST_F(MCJITTest, run_main) {
85 SKIP_UNSUPPORTED_PLATFORM;
86
87 int rc = 6;
88 Function *Main = insertMainFunction(M.get(), 6);
89 createJIT(M.take());
Andrew Kaylor70c1ea42013-10-01 01:48:36 +000090 uint64_t ptr = TheJIT->getFunctionAddress(Main->getName().str());
91 EXPECT_TRUE(0 != ptr)
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000092 << "Unable to get pointer to main() from JIT";
93
Andrew Kaylor70c1ea42013-10-01 01:48:36 +000094 int (*FuncPtr)(void) = (int(*)(void))ptr;
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000095 int returnCode = FuncPtr();
96 EXPECT_EQ(returnCode, rc);
97}
98
99TEST_F(MCJITTest, return_global) {
100 SKIP_UNSUPPORTED_PLATFORM;
101
102 int32_t initialNum = 7;
103 GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum);
104
105 Function *ReturnGlobal = startFunction<int32_t(void)>(M.get(),
106 "ReturnGlobal");
107 Value *ReadGlobal = Builder.CreateLoad(GV);
108 endFunctionWithRet(ReturnGlobal, ReadGlobal);
109
110 createJIT(M.take());
Andrew Kaylor70c1ea42013-10-01 01:48:36 +0000111 uint64_t rgvPtr = TheJIT->getFunctionAddress(ReturnGlobal->getName().str());
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000112 EXPECT_TRUE(0 != rgvPtr);
113
Andrew Kaylor70c1ea42013-10-01 01:48:36 +0000114 int32_t(*FuncPtr)(void) = (int32_t(*)(void))rgvPtr;
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000115 EXPECT_EQ(initialNum, FuncPtr())
116 << "Invalid value for global returned from JITted function";
117}
118
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.
127/*
128TEST_F(MCJITTest, increment_global) {
129 SKIP_UNSUPPORTED_PLATFORM;
130
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);
139
140 createJIT(M.take());
141 void *gvPtr = TheJIT->getPointerToGlobal(GV);
142 EXPECT_EQ(initialNum, *(int32_t*)gvPtr);
143
Andrew Kaylor70c1ea42013-10-01 01:48:36 +0000144 void *vPtr = TheJIT->getFunctionAddress(IncrementGlobal->getName().str());
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000145 EXPECT_TRUE(0 != vPtr)
146 << "Unable to get pointer to main() from JIT";
147
148 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
149
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
154 }
155}
156*/
157
David Tweedabb38fe2013-05-17 10:01:46 +0000158// PR16013: XFAIL this test on ARM, which currently can't handle multiple relocations.
159#if !defined(__arm__)
160
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000161TEST_F(MCJITTest, multiple_functions) {
162 SKIP_UNSUPPORTED_PLATFORM;
163
164 unsigned int numLevels = 23;
165 int32_t innerRetVal= 5;
166
167 Function *Inner = startFunction<int32_t(void)>(M.get(), "Inner");
168 endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal)));
169
170 Function *Outer;
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);
177
178 Inner = Outer;
179 }
180
181 createJIT(M.take());
Andrew Kaylor70c1ea42013-10-01 01:48:36 +0000182 uint64_t ptr = TheJIT->getFunctionAddress(Outer->getName().str());
183 EXPECT_TRUE(0 != ptr)
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000184 << "Unable to get pointer to outer function from JIT";
185
Andrew Kaylor70c1ea42013-10-01 01:48:36 +0000186 int32_t(*FuncPtr)(void) = (int32_t(*)(void))ptr;
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000187 EXPECT_EQ(innerRetVal, FuncPtr())
188 << "Incorrect result returned from function";
189}
190
David Tweedabb38fe2013-05-17 10:01:46 +0000191#endif /*!defined(__arm__)*/
192
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000193}