1 //===- ExecutionEngineTest.cpp - Unit tests for ExecutionEngine -----------===//
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 "llvm/ExecutionEngine/Interpreter.h"
11 #include "llvm/IR/DerivedTypes.h"
12 #include "llvm/IR/GlobalVariable.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "gtest/gtest.h"
21 class ExecutionEngineTest : public testing::Test {
23 ExecutionEngineTest() {
24 auto Owner = make_unique<Module>("<main>", getGlobalContext());
26 Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create());
29 virtual void SetUp() {
30 ASSERT_TRUE(Engine.get() != nullptr) << "EngineBuilder returned error: '"
34 GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) {
35 return new GlobalVariable(*M, T, false, // Not constant.
36 GlobalValue::ExternalLinkage, nullptr, Name);
40 Module *M; // Owned by ExecutionEngine.
41 std::unique_ptr<ExecutionEngine> Engine;
44 TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
46 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
48 Engine->addGlobalMapping(G1, &Mem1);
49 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
51 Engine->updateGlobalMapping(G1, &Mem2);
52 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
53 Engine->updateGlobalMapping(G1, nullptr);
54 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G1));
55 Engine->updateGlobalMapping(G1, &Mem2);
56 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
59 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
60 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G2))
61 << "The NULL return shouldn't depend on having called"
62 << " updateGlobalMapping(..., NULL)";
63 // Check that update...() can be called before add...().
64 Engine->updateGlobalMapping(G2, &Mem1);
65 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2));
66 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1))
67 << "A second mapping shouldn't affect the first.";
70 TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
72 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
75 Engine->addGlobalMapping(G1, &Mem1);
76 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
78 Engine->updateGlobalMapping(G1, &Mem2);
79 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
80 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
83 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
84 Engine->updateGlobalMapping(G2, &Mem1);
85 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
86 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
87 Engine->updateGlobalMapping(G1, nullptr);
88 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1))
89 << "Removing one mapping doesn't affect a different one.";
90 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem2));
91 Engine->updateGlobalMapping(G2, &Mem2);
92 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
93 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2))
94 << "Once a mapping is removed, we can point another GV at the"
95 << " now-free address.";
98 TEST_F(ExecutionEngineTest, ClearModuleMappings) {
100 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
103 Engine->addGlobalMapping(G1, &Mem1);
104 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
106 Engine->clearGlobalMappingsFromModule(M);
108 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
111 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
112 // After clearing the module mappings, we can assign a new GV to the
114 Engine->addGlobalMapping(G2, &Mem1);
115 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
118 TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) {
120 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
122 Engine->addGlobalMapping(G1, &Mem1);
123 // Make sure the reverse mapping is enabled.
124 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
125 // When the GV goes away, the ExecutionEngine should remove any
126 // mappings that refer to it.
127 G1->eraseFromParent();
128 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));