1 //===- JITEmitter.cpp - Unit tests for the JIT code emitter ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
31 Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
32 std::vector<const Type*> params;
33 const FunctionType *FTy = FunctionType::get(G->getType()->getElementType(),
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);
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) {
53 Module *M = new Module("<main>", context);
54 ExistingModuleProvider *MP = new ExistingModuleProvider(M);
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);
61 OwningPtr<ExecutionEngine> JIT(ExecutionEngine::createJIT(
66 false)); // This last argument enables the fix.
69 // Create a global variable.
70 const Type *GTy = Type::Int32Ty;
71 GlobalVariable *G = new GlobalVariable(
74 false, // Not constant.
75 GlobalValue::InternalLinkage,
76 context.getNullValue(GTy),
79 // Make a function that points to a global.
80 Function *F1 = makeReturnGlobal("F1", G, M);
82 // Get the pointer to the native code to force it to JIT the function and
83 // allocate space for the global.
85 // Hack to avoid ISO C++ warning about casting function pointers.
86 *(void**)(void*)&F1Ptr = JIT->getPointerToFunction(F1);
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);
93 // F1() should increment G.
97 // Make a second function identical to the first, referring to the same
99 Function *F2 = makeReturnGlobal("F2", G, M);
100 // Hack to avoid ISO C++ warning about casting function pointers.
102 *(void**)(void*)&F2Ptr = JIT->getPointerToFunction(F2);
104 // F2() should increment G.
109 JIT->freeMachineCodeForFunction(F1);
111 // F2() should *still* increment G.
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();
124 testing::Environment* const jit_env =
125 testing::AddGlobalTestEnvironment(new JITEnvironment);