Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 1 | //===- 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 | |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/MCJIT.h" |
| 17 | #include "MCJITTestBase.h" |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | class MCJITTest : public testing::Test, public MCJITTestBase { |
| 22 | protected: |
| 23 | |
| 24 | virtual void SetUp() { |
| 25 | M.reset(createEmptyModule("<main>")); |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | // FIXME: In order to JIT an empty module, there needs to be |
| 32 | // an interface to ExecutionEngine that forces compilation but |
| 33 | // does require retrieval of a pointer to a function/global. |
| 34 | /* |
| 35 | TEST_F(MCJITTest, empty_module) { |
| 36 | createJIT(M.take()); |
| 37 | //EXPECT_NE(0, TheJIT->getObjectImage()) |
| 38 | // << "Unable to generate executable loaded object image"; |
| 39 | } |
| 40 | */ |
| 41 | |
| 42 | TEST_F(MCJITTest, global_variable) { |
| 43 | SKIP_UNSUPPORTED_PLATFORM; |
| 44 | |
| 45 | int initialValue = 5; |
| 46 | GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue); |
| 47 | createJIT(M.take()); |
| 48 | void *globalPtr = TheJIT->getPointerToGlobal(Global); |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 49 | MM->applyPermissions(); |
Amara Emerson | b586aae | 2012-10-31 17:44:16 +0000 | [diff] [blame] | 50 | static_cast<SectionMemoryManager*>(MM)->invalidateInstructionCache(); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 51 | EXPECT_TRUE(0 != globalPtr) |
| 52 | << "Unable to get pointer to global value from JIT"; |
| 53 | |
| 54 | EXPECT_EQ(initialValue, *(int32_t*)globalPtr) |
| 55 | << "Unexpected initial value of global"; |
| 56 | } |
| 57 | |
| 58 | TEST_F(MCJITTest, add_function) { |
| 59 | SKIP_UNSUPPORTED_PLATFORM; |
| 60 | |
| 61 | Function *F = insertAddFunction(M.get()); |
| 62 | createJIT(M.take()); |
| 63 | void *addPtr = TheJIT->getPointerToFunction(F); |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 64 | MM->applyPermissions(); |
Amara Emerson | b586aae | 2012-10-31 17:44:16 +0000 | [diff] [blame] | 65 | static_cast<SectionMemoryManager*>(MM)->invalidateInstructionCache(); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 66 | EXPECT_TRUE(0 != addPtr) |
| 67 | << "Unable to get pointer to function from JIT"; |
| 68 | |
| 69 | int (*AddPtrTy)(int, int) = (int(*)(int, int))(intptr_t)addPtr; |
| 70 | EXPECT_EQ(0, AddPtrTy(0, 0)); |
| 71 | EXPECT_EQ(3, AddPtrTy(1, 2)); |
| 72 | EXPECT_EQ(-5, AddPtrTy(-2, -3)); |
| 73 | } |
| 74 | |
| 75 | TEST_F(MCJITTest, run_main) { |
| 76 | SKIP_UNSUPPORTED_PLATFORM; |
| 77 | |
| 78 | int rc = 6; |
| 79 | Function *Main = insertMainFunction(M.get(), 6); |
| 80 | createJIT(M.take()); |
| 81 | void *vPtr = TheJIT->getPointerToFunction(Main); |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 82 | MM->applyPermissions(); |
Amara Emerson | b586aae | 2012-10-31 17:44:16 +0000 | [diff] [blame] | 83 | static_cast<SectionMemoryManager*>(MM)->invalidateInstructionCache(); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 84 | EXPECT_TRUE(0 != vPtr) |
| 85 | << "Unable to get pointer to main() from JIT"; |
| 86 | |
| 87 | int (*FuncPtr)(void) = (int(*)(void))(intptr_t)vPtr; |
| 88 | int returnCode = FuncPtr(); |
| 89 | EXPECT_EQ(returnCode, rc); |
| 90 | } |
| 91 | |
| 92 | TEST_F(MCJITTest, return_global) { |
| 93 | SKIP_UNSUPPORTED_PLATFORM; |
| 94 | |
| 95 | int32_t initialNum = 7; |
| 96 | GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum); |
| 97 | |
| 98 | Function *ReturnGlobal = startFunction<int32_t(void)>(M.get(), |
| 99 | "ReturnGlobal"); |
| 100 | Value *ReadGlobal = Builder.CreateLoad(GV); |
| 101 | endFunctionWithRet(ReturnGlobal, ReadGlobal); |
| 102 | |
| 103 | createJIT(M.take()); |
| 104 | void *rgvPtr = TheJIT->getPointerToFunction(ReturnGlobal); |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 105 | MM->applyPermissions(); |
Amara Emerson | b586aae | 2012-10-31 17:44:16 +0000 | [diff] [blame] | 106 | static_cast<SectionMemoryManager*>(MM)->invalidateInstructionCache(); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 107 | EXPECT_TRUE(0 != rgvPtr); |
| 108 | |
| 109 | int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)rgvPtr; |
| 110 | EXPECT_EQ(initialNum, FuncPtr()) |
| 111 | << "Invalid value for global returned from JITted function"; |
| 112 | } |
| 113 | |
| 114 | // FIXME: This case fails due to a bug with getPointerToGlobal(). |
| 115 | // The bug is due to MCJIT not having an implementation of getPointerToGlobal() |
| 116 | // which results in falling back on the ExecutionEngine implementation that |
| 117 | // allocates a new memory block for the global instead of using the same |
| 118 | // global variable that is emitted by MCJIT. Hence, the pointer (gvPtr below) |
| 119 | // has the correct initial value, but updates to the real global (accessed by |
| 120 | // JITted code) are not propagated. Instead, getPointerToGlobal() should return |
| 121 | // a pointer into the loaded ObjectImage to reference the emitted global. |
| 122 | /* |
| 123 | TEST_F(MCJITTest, increment_global) { |
| 124 | SKIP_UNSUPPORTED_PLATFORM; |
| 125 | |
| 126 | int32_t initialNum = 5; |
| 127 | Function *IncrementGlobal = startFunction<int32_t(void)>(M.get(), "IncrementGlobal"); |
| 128 | GlobalVariable *GV = insertGlobalInt32(M.get(), "my_global", initialNum); |
| 129 | Value *DerefGV = Builder.CreateLoad(GV); |
| 130 | Value *AddResult = Builder.CreateAdd(DerefGV, |
| 131 | ConstantInt::get(Context, APInt(32, 1))); |
| 132 | Builder.CreateStore(AddResult, GV); |
| 133 | endFunctionWithRet(IncrementGlobal, AddResult); |
| 134 | |
| 135 | createJIT(M.take()); |
| 136 | void *gvPtr = TheJIT->getPointerToGlobal(GV); |
| 137 | EXPECT_EQ(initialNum, *(int32_t*)gvPtr); |
| 138 | |
| 139 | void *vPtr = TheJIT->getPointerToFunction(IncrementGlobal); |
| 140 | EXPECT_TRUE(0 != vPtr) |
| 141 | << "Unable to get pointer to main() from JIT"; |
| 142 | |
| 143 | int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr; |
| 144 | |
| 145 | for(int i = 1; i < 3; ++i) { |
| 146 | int32_t result = FuncPtr(); |
| 147 | EXPECT_EQ(initialNum + i, result); // OK |
| 148 | EXPECT_EQ(initialNum + i, *(int32_t*)gvPtr); // FAILS |
| 149 | } |
| 150 | } |
| 151 | */ |
| 152 | |
| 153 | TEST_F(MCJITTest, multiple_functions) { |
| 154 | SKIP_UNSUPPORTED_PLATFORM; |
| 155 | |
| 156 | unsigned int numLevels = 23; |
| 157 | int32_t innerRetVal= 5; |
| 158 | |
| 159 | Function *Inner = startFunction<int32_t(void)>(M.get(), "Inner"); |
| 160 | endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal))); |
| 161 | |
| 162 | Function *Outer; |
| 163 | for (unsigned int i = 0; i < numLevels; ++i) { |
| 164 | std::stringstream funcName; |
| 165 | funcName << "level_" << i; |
| 166 | Outer = startFunction<int32_t(void)>(M.get(), funcName.str()); |
| 167 | Value *innerResult = Builder.CreateCall(Inner); |
| 168 | endFunctionWithRet(Outer, innerResult); |
| 169 | |
| 170 | Inner = Outer; |
| 171 | } |
| 172 | |
| 173 | createJIT(M.take()); |
| 174 | void *vPtr = TheJIT->getPointerToFunction(Outer); |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 175 | MM->applyPermissions(); |
Amara Emerson | b586aae | 2012-10-31 17:44:16 +0000 | [diff] [blame] | 176 | static_cast<SectionMemoryManager*>(MM)->invalidateInstructionCache(); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 177 | EXPECT_TRUE(0 != vPtr) |
| 178 | << "Unable to get pointer to outer function from JIT"; |
| 179 | |
| 180 | int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr; |
| 181 | EXPECT_EQ(innerRetVal, FuncPtr()) |
| 182 | << "Incorrect result returned from function"; |
| 183 | } |
| 184 | |
| 185 | // FIXME: ExecutionEngine has no support empty modules |
| 186 | /* |
| 187 | TEST_F(MCJITTest, multiple_empty_modules) { |
| 188 | SKIP_UNSUPPORTED_PLATFORM; |
| 189 | |
| 190 | createJIT(M.take()); |
| 191 | // JIT-compile |
| 192 | EXPECT_NE(0, TheJIT->getObjectImage()) |
| 193 | << "Unable to generate executable loaded object image"; |
| 194 | |
| 195 | TheJIT->addModule(createEmptyModule("<other module>")); |
| 196 | TheJIT->addModule(createEmptyModule("<other other module>")); |
| 197 | |
| 198 | // JIT again |
| 199 | EXPECT_NE(0, TheJIT->getObjectImage()) |
| 200 | << "Unable to generate executable loaded object image"; |
| 201 | } |
| 202 | */ |
| 203 | |
| 204 | // FIXME: MCJIT must support multiple modules |
| 205 | /* |
| 206 | TEST_F(MCJITTest, multiple_modules) { |
| 207 | SKIP_UNSUPPORTED_PLATFORM; |
| 208 | |
| 209 | Function *Callee = insertAddFunction(M.get()); |
| 210 | createJIT(M.take()); |
| 211 | |
| 212 | // caller function is defined in a different module |
| 213 | M.reset(createEmptyModule("<caller module>")); |
| 214 | |
| 215 | Function *CalleeRef = insertExternalReferenceToFunction(M.get(), Callee); |
| 216 | Function *Caller = insertSimpleCallFunction(M.get(), CalleeRef); |
| 217 | |
| 218 | TheJIT->addModule(M.take()); |
| 219 | |
| 220 | // get a function pointer in a module that was not used in EE construction |
| 221 | void *vPtr = TheJIT->getPointerToFunction(Caller); |
| 222 | EXPECT_NE(0, vPtr) |
| 223 | << "Unable to get pointer to caller function from JIT"; |
| 224 | |
| 225 | int(*FuncPtr)(int, int) = (int(*)(int, int))(intptr_t)vPtr; |
| 226 | EXPECT_EQ(0, FuncPtr(0, 0)); |
| 227 | EXPECT_EQ(30, FuncPtr(10, 20)); |
| 228 | EXPECT_EQ(-30, FuncPtr(-10, -20)); |
| 229 | |
| 230 | // ensure caller is destroyed before callee (free use before def) |
| 231 | M.reset(); |
| 232 | } |
| 233 | */ |
| 234 | |
| 235 | } |