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