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" |
| 25 | #include "llvm/Target/TargetSelect.h" |
| 26 | #include "llvm/Type.h" |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) { |
| 33 | std::vector<const Type*> params; |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame^] | 34 | const FunctionType *FTy = FunctionType::get(G->getType()->getElementType(), |
Dan Gohman | c6f40b6 | 2009-07-11 13:56:14 +0000 | [diff] [blame] | 35 | params, false); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 36 | Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M); |
| 37 | BasicBlock *Entry = BasicBlock::Create("entry", F); |
| 38 | IRBuilder<> builder(Entry); |
| 39 | Value *Load = builder.CreateLoad(G); |
| 40 | const Type *GTy = G->getType()->getElementType(); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 41 | Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL)); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 42 | builder.CreateStore(Add, G); |
| 43 | builder.CreateRet(Add); |
| 44 | return F; |
| 45 | } |
| 46 | |
| 47 | // Regression test for a bug. The JIT used to allocate globals inside the same |
| 48 | // memory block used for the function, and when the function code was freed, |
| 49 | // the global was left in the same place. This test allocates a function |
| 50 | // that uses and global, deallocates it, and then makes sure that the global |
| 51 | // stays alive after that. |
| 52 | TEST(JIT, GlobalInFunction) { |
| 53 | LLVMContext context; |
| 54 | Module *M = new Module("<main>", context); |
| 55 | ExistingModuleProvider *MP = new ExistingModuleProvider(M); |
| 56 | |
| 57 | JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager(); |
| 58 | // Tell the memory manager to poison freed memory so that accessing freed |
| 59 | // memory is more easily tested. |
| 60 | MemMgr->setPoisonMemory(true); |
| 61 | std::string Error; |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 62 | OwningPtr<ExecutionEngine> JIT(EngineBuilder(MP) |
Daniel Dunbar | d370d77 | 2009-07-18 06:08:49 +0000 | [diff] [blame] | 63 | .setEngineKind(EngineKind::JIT) |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 64 | .setErrorStr(&Error) |
| 65 | .setJITMemoryManager(MemMgr) |
| 66 | // The next line enables the fix: |
| 67 | .setAllocateGVsWithCode(false) |
| 68 | .create()); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 69 | ASSERT_EQ(Error, ""); |
| 70 | |
| 71 | // Create a global variable. |
| 72 | const Type *GTy = Type::Int32Ty; |
| 73 | GlobalVariable *G = new GlobalVariable( |
| 74 | *M, |
| 75 | GTy, |
| 76 | false, // Not constant. |
| 77 | GlobalValue::InternalLinkage, |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 78 | context.getNullValue(GTy), |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 79 | "myglobal"); |
| 80 | |
| 81 | // Make a function that points to a global. |
| 82 | Function *F1 = makeReturnGlobal("F1", G, M); |
| 83 | |
| 84 | // Get the pointer to the native code to force it to JIT the function and |
| 85 | // allocate space for the global. |
| 86 | void (*F1Ptr)(); |
| 87 | // Hack to avoid ISO C++ warning about casting function pointers. |
| 88 | *(void**)(void*)&F1Ptr = JIT->getPointerToFunction(F1); |
| 89 | |
| 90 | // Since F1 was codegen'd, a pointer to G should be available. |
| 91 | int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G); |
| 92 | ASSERT_NE((int32_t*)NULL, GPtr); |
| 93 | EXPECT_EQ(0, *GPtr); |
| 94 | |
| 95 | // F1() should increment G. |
| 96 | F1Ptr(); |
| 97 | EXPECT_EQ(1, *GPtr); |
| 98 | |
| 99 | // Make a second function identical to the first, referring to the same |
| 100 | // global. |
| 101 | Function *F2 = makeReturnGlobal("F2", G, M); |
| 102 | // Hack to avoid ISO C++ warning about casting function pointers. |
| 103 | void (*F2Ptr)(); |
| 104 | *(void**)(void*)&F2Ptr = JIT->getPointerToFunction(F2); |
| 105 | |
| 106 | // F2() should increment G. |
| 107 | F2Ptr(); |
| 108 | EXPECT_EQ(2, *GPtr); |
| 109 | |
| 110 | // Deallocate F1. |
| 111 | JIT->freeMachineCodeForFunction(F1); |
| 112 | |
| 113 | // F2() should *still* increment G. |
| 114 | F2Ptr(); |
| 115 | EXPECT_EQ(3, *GPtr); |
| 116 | } |
| 117 | |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 118 | // This code is copied from JITEventListenerTest, but it only runs once for all |
| 119 | // the tests in this directory. Everything seems fine, but that's strange |
| 120 | // behavior. |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 121 | class JITEnvironment : public testing::Environment { |
| 122 | virtual void SetUp() { |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 123 | // Required to create a JIT. |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 124 | InitializeNativeTarget(); |
| 125 | } |
| 126 | }; |
| 127 | testing::Environment* const jit_env = |
| 128 | testing::AddGlobalTestEnvironment(new JITEnvironment); |
| 129 | |
| 130 | } |