Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 1 | //===- JITTest.cpp - Unit tests for the JIT -------------------------------===// |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 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 | #include "gtest/gtest.h" |
| 11 | #include "llvm/ADT/OwningPtr.h" |
| 12 | #include "llvm/BasicBlock.h" |
| 13 | #include "llvm/Constant.h" |
| 14 | #include "llvm/Constants.h" |
| 15 | #include "llvm/DerivedTypes.h" |
| 16 | #include "llvm/ExecutionEngine/JIT.h" |
| 17 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/GlobalValue.h" |
| 20 | #include "llvm/GlobalVariable.h" |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/ModuleProvider.h" |
| 24 | #include "llvm/Support/IRBuilder.h" |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 25 | #include "llvm/Support/TypeBuilder.h" |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetSelect.h" |
| 27 | #include "llvm/Type.h" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) { |
| 34 | std::vector<const Type*> params; |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 35 | const FunctionType *FTy = FunctionType::get(G->getType()->getElementType(), |
Dan Gohman | c6f40b6 | 2009-07-11 13:56:14 +0000 | [diff] [blame] | 36 | params, false); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 37 | Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 38 | BasicBlock *Entry = BasicBlock::Create(M->getContext(), "entry", F); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 39 | IRBuilder<> builder(Entry); |
| 40 | Value *Load = builder.CreateLoad(G); |
| 41 | const Type *GTy = G->getType()->getElementType(); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 42 | Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL)); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 43 | builder.CreateStore(Add, G); |
| 44 | builder.CreateRet(Add); |
| 45 | return F; |
| 46 | } |
| 47 | |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 48 | class JITTest : public testing::Test { |
| 49 | protected: |
| 50 | virtual void SetUp() { |
| 51 | M = new Module("<main>", Context); |
| 52 | std::string Error; |
| 53 | TheJIT.reset(EngineBuilder(M).setEngineKind(EngineKind::JIT) |
| 54 | .setErrorStr(&Error).create()); |
| 55 | ASSERT_TRUE(TheJIT.get() != NULL) << Error; |
| 56 | } |
| 57 | |
| 58 | LLVMContext Context; |
| 59 | Module *M; // Owned by ExecutionEngine. |
| 60 | OwningPtr<ExecutionEngine> TheJIT; |
| 61 | }; |
| 62 | |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 63 | // Regression test for a bug. The JIT used to allocate globals inside the same |
| 64 | // memory block used for the function, and when the function code was freed, |
| 65 | // the global was left in the same place. This test allocates a function |
| 66 | // that uses and global, deallocates it, and then makes sure that the global |
| 67 | // stays alive after that. |
| 68 | TEST(JIT, GlobalInFunction) { |
| 69 | LLVMContext context; |
| 70 | Module *M = new Module("<main>", context); |
| 71 | ExistingModuleProvider *MP = new ExistingModuleProvider(M); |
| 72 | |
| 73 | JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager(); |
| 74 | // Tell the memory manager to poison freed memory so that accessing freed |
| 75 | // memory is more easily tested. |
| 76 | MemMgr->setPoisonMemory(true); |
| 77 | std::string Error; |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 78 | OwningPtr<ExecutionEngine> JIT(EngineBuilder(MP) |
Daniel Dunbar | d370d77 | 2009-07-18 06:08:49 +0000 | [diff] [blame] | 79 | .setEngineKind(EngineKind::JIT) |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 80 | .setErrorStr(&Error) |
| 81 | .setJITMemoryManager(MemMgr) |
| 82 | // The next line enables the fix: |
| 83 | .setAllocateGVsWithCode(false) |
| 84 | .create()); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 85 | ASSERT_EQ(Error, ""); |
| 86 | |
| 87 | // Create a global variable. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 88 | const Type *GTy = Type::getInt32Ty(context); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 89 | GlobalVariable *G = new GlobalVariable( |
| 90 | *M, |
| 91 | GTy, |
| 92 | false, // Not constant. |
| 93 | GlobalValue::InternalLinkage, |
Benjamin Kramer | feba756 | 2009-07-31 20:56:31 +0000 | [diff] [blame] | 94 | Constant::getNullValue(GTy), |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 95 | "myglobal"); |
| 96 | |
| 97 | // Make a function that points to a global. |
| 98 | Function *F1 = makeReturnGlobal("F1", G, M); |
| 99 | |
| 100 | // Get the pointer to the native code to force it to JIT the function and |
| 101 | // allocate space for the global. |
Jeffrey Yasskin | 0f2ba78 | 2009-10-06 19:06:16 +0000 | [diff] [blame^] | 102 | void (*F1Ptr)() = |
| 103 | reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F1)); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 104 | |
| 105 | // Since F1 was codegen'd, a pointer to G should be available. |
| 106 | int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G); |
| 107 | ASSERT_NE((int32_t*)NULL, GPtr); |
| 108 | EXPECT_EQ(0, *GPtr); |
| 109 | |
| 110 | // F1() should increment G. |
| 111 | F1Ptr(); |
| 112 | EXPECT_EQ(1, *GPtr); |
| 113 | |
| 114 | // Make a second function identical to the first, referring to the same |
| 115 | // global. |
| 116 | Function *F2 = makeReturnGlobal("F2", G, M); |
Jeffrey Yasskin | 0f2ba78 | 2009-10-06 19:06:16 +0000 | [diff] [blame^] | 117 | void (*F2Ptr)() = |
| 118 | reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F2)); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 119 | |
| 120 | // F2() should increment G. |
| 121 | F2Ptr(); |
| 122 | EXPECT_EQ(2, *GPtr); |
| 123 | |
| 124 | // Deallocate F1. |
| 125 | JIT->freeMachineCodeForFunction(F1); |
| 126 | |
| 127 | // F2() should *still* increment G. |
| 128 | F2Ptr(); |
| 129 | EXPECT_EQ(3, *GPtr); |
| 130 | } |
| 131 | |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 132 | int PlusOne(int arg) { |
| 133 | return arg + 1; |
| 134 | } |
| 135 | |
| 136 | TEST_F(JITTest, FarCallToKnownFunction) { |
| 137 | // x86-64 can only make direct calls to functions within 32 bits of |
| 138 | // the current PC. To call anything farther away, we have to load |
| 139 | // the address into a register and call through the register. The |
| 140 | // current JIT does this by allocating a stub for any far call. |
| 141 | // There was a bug in which the JIT tried to emit a direct call when |
| 142 | // the target was already in the JIT's global mappings and lazy |
| 143 | // compilation was disabled. |
| 144 | |
| 145 | Function *KnownFunction = Function::Create( |
| 146 | TypeBuilder<int(int), false>::get(Context), |
| 147 | GlobalValue::ExternalLinkage, "known", M); |
| 148 | TheJIT->addGlobalMapping(KnownFunction, (void*)(intptr_t)PlusOne); |
| 149 | |
| 150 | // int test() { return known(7); } |
| 151 | Function *TestFunction = Function::Create( |
| 152 | TypeBuilder<int(), false>::get(Context), |
| 153 | GlobalValue::ExternalLinkage, "test", M); |
| 154 | BasicBlock *Entry = BasicBlock::Create(Context, "entry", TestFunction); |
| 155 | IRBuilder<> Builder(Entry); |
| 156 | Value *result = Builder.CreateCall( |
| 157 | KnownFunction, |
| 158 | ConstantInt::get(TypeBuilder<int, false>::get(Context), 7)); |
| 159 | Builder.CreateRet(result); |
| 160 | |
| 161 | TheJIT->EnableDlsymStubs(false); |
| 162 | TheJIT->DisableLazyCompilation(); |
| 163 | int (*TestFunctionPtr)() = reinterpret_cast<int(*)()>( |
| 164 | (intptr_t)TheJIT->getPointerToFunction(TestFunction)); |
| 165 | // This used to crash in trying to call PlusOne(). |
| 166 | EXPECT_EQ(8, TestFunctionPtr()); |
| 167 | } |
| 168 | |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 169 | // This code is copied from JITEventListenerTest, but it only runs once for all |
| 170 | // the tests in this directory. Everything seems fine, but that's strange |
| 171 | // behavior. |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 172 | class JITEnvironment : public testing::Environment { |
| 173 | virtual void SetUp() { |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 174 | // Required to create a JIT. |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 175 | InitializeNativeTarget(); |
| 176 | } |
| 177 | }; |
| 178 | testing::Environment* const jit_env = |
| 179 | testing::AddGlobalTestEnvironment(new JITEnvironment); |
| 180 | |
| 181 | } |