blob: ff5b6e3deccca61fd6e0ec9c151e70254c005119 [file] [log] [blame]
Andrew Kaylorced4e8f2013-04-25 21:02:36 +00001//===- MCJITObjectCacheTest.cpp - Unit tests for MCJIT object caching -----===//
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
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000010#include "MCJITTestBase.h"
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000011#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/StringMap.h"
13#include "llvm/ADT/StringSet.h"
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000014#include "llvm/ExecutionEngine/MCJIT.h"
15#include "llvm/ExecutionEngine/ObjectCache.h"
16#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000017#include "gtest/gtest.h"
18
19using namespace llvm;
20
21namespace {
22
23class TestObjectCache : public ObjectCache {
24public:
25 TestObjectCache() : DuplicateInserted(false) { }
26
Rafael Espindola48af1c22014-08-19 18:44:46 +000027 void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000028 // If we've seen this module before, note that.
29 const std::string ModuleID = M->getModuleIdentifier();
30 if (ObjMap.find(ModuleID) != ObjMap.end())
31 DuplicateInserted = true;
32 // Store a copy of the buffer in our map.
33 ObjMap[ModuleID] = copyBuffer(Obj);
34 }
35
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000036 std::unique_ptr<MemoryBuffer> getObject(const Module *M) override {
Andrew Kaylorb595f532013-06-28 21:40:16 +000037 const MemoryBuffer* BufferFound = getObjectInternal(M);
38 ModulesLookedUp.insert(M->getModuleIdentifier());
39 if (!BufferFound)
Craig Topper66f09ad2014-06-08 22:29:17 +000040 return nullptr;
Andrew Kaylorb595f532013-06-28 21:40:16 +000041 // Our test cache wants to maintain ownership of its object buffers
42 // so we make a copy here for the execution engine.
Rafael Espindola3560ff22014-08-27 20:03:13 +000043 return MemoryBuffer::getMemBufferCopy(BufferFound->getBuffer());
Andrew Kaylorb595f532013-06-28 21:40:16 +000044 }
45
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000046 // Test-harness-specific functions
47 bool wereDuplicatesInserted() { return DuplicateInserted; }
48
49 bool wasModuleLookedUp(const Module *M) {
50 return ModulesLookedUp.find(M->getModuleIdentifier())
51 != ModulesLookedUp.end();
52 }
53
54 const MemoryBuffer* getObjectInternal(const Module* M) {
55 // Look for the module in our map.
56 const std::string ModuleID = M->getModuleIdentifier();
57 StringMap<const MemoryBuffer *>::iterator it = ObjMap.find(ModuleID);
58 if (it == ObjMap.end())
Craig Topper66f09ad2014-06-08 22:29:17 +000059 return nullptr;
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000060 return it->second;
61 }
62
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000063private:
Rafael Espindola48af1c22014-08-19 18:44:46 +000064 MemoryBuffer *copyBuffer(MemoryBufferRef Buf) {
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000065 // Create a local copy of the buffer.
Rafael Espindola3560ff22014-08-27 20:03:13 +000066 std::unique_ptr<MemoryBuffer> NewBuffer =
67 MemoryBuffer::getMemBufferCopy(Buf.getBuffer());
Rafael Espindolabb415ea2014-08-13 18:59:01 +000068 MemoryBuffer *Ret = NewBuffer.get();
69 AllocatedBuffers.push_back(std::move(NewBuffer));
70 return Ret;
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000071 }
72
73 StringMap<const MemoryBuffer *> ObjMap;
74 StringSet<> ModulesLookedUp;
Rafael Espindolabb415ea2014-08-13 18:59:01 +000075 SmallVector<std::unique_ptr<MemoryBuffer>, 2> AllocatedBuffers;
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000076 bool DuplicateInserted;
77};
78
79class MCJITObjectCacheTest : public testing::Test, public MCJITTestBase {
80protected:
81
82 enum {
83 OriginalRC = 6,
84 ReplacementRC = 7
85 };
86
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000087 void SetUp() override {
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000088 M.reset(createEmptyModule("<main>"));
89 Main = insertMainFunction(M.get(), OriginalRC);
90 }
91
92 void compileAndRun(int ExpectedRC = OriginalRC) {
93 // This function shouldn't be called until after SetUp.
Ahmed Charles56440fd2014-03-06 05:51:42 +000094 ASSERT_TRUE(bool(TheJIT));
Craig Topper66f09ad2014-06-08 22:29:17 +000095 ASSERT_TRUE(nullptr != Main);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000096
David Tweed2e7efed2013-05-17 10:01:46 +000097 // We may be using a null cache, so ensure compilation is valid.
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000098 TheJIT->finalizeObject();
99 void *vPtr = TheJIT->getPointerToFunction(Main);
100
Craig Topper66f09ad2014-06-08 22:29:17 +0000101 EXPECT_TRUE(nullptr != vPtr)
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000102 << "Unable to get pointer to main() from JIT";
103
104 int (*FuncPtr)(void) = (int(*)(void))(intptr_t)vPtr;
105 int returnCode = FuncPtr();
106 EXPECT_EQ(returnCode, ExpectedRC);
107 }
108
109 Function *Main;
110};
111
112TEST_F(MCJITObjectCacheTest, SetNullObjectCache) {
113 SKIP_UNSUPPORTED_PLATFORM;
114
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000115 createJIT(std::move(M));
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000116
Craig Topper66f09ad2014-06-08 22:29:17 +0000117 TheJIT->setObjectCache(nullptr);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000118
119 compileAndRun();
120}
121
122
123TEST_F(MCJITObjectCacheTest, VerifyBasicObjectCaching) {
124 SKIP_UNSUPPORTED_PLATFORM;
125
Ahmed Charles56440fd2014-03-06 05:51:42 +0000126 std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000127
128 // Save a copy of the module pointer before handing it off to MCJIT.
129 const Module * SavedModulePointer = M.get();
130
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000131 createJIT(std::move(M));
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000132
133 TheJIT->setObjectCache(Cache.get());
134
135 // Verify that our object cache does not contain the module yet.
136 const MemoryBuffer *ObjBuffer = Cache->getObjectInternal(SavedModulePointer);
Craig Topper66f09ad2014-06-08 22:29:17 +0000137 EXPECT_EQ(nullptr, ObjBuffer);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000138
139 compileAndRun();
140
141 // Verify that MCJIT tried to look-up this module in the cache.
142 EXPECT_TRUE(Cache->wasModuleLookedUp(SavedModulePointer));
143
144 // Verify that our object cache now contains the module.
145 ObjBuffer = Cache->getObjectInternal(SavedModulePointer);
Craig Topper66f09ad2014-06-08 22:29:17 +0000146 EXPECT_TRUE(nullptr != ObjBuffer);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000147
148 // Verify that the cache was only notified once.
149 EXPECT_FALSE(Cache->wereDuplicatesInserted());
150}
151
152TEST_F(MCJITObjectCacheTest, VerifyLoadFromCache) {
153 SKIP_UNSUPPORTED_PLATFORM;
154
Ahmed Charles56440fd2014-03-06 05:51:42 +0000155 std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000156
157 // Compile this module with an MCJIT engine
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000158 createJIT(std::move(M));
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000159 TheJIT->setObjectCache(Cache.get());
160 TheJIT->finalizeObject();
161
162 // Destroy the MCJIT engine we just used
163 TheJIT.reset();
164
165 // Create a new memory manager.
Lang Hames4a5697e2014-12-03 00:51:19 +0000166 MM.reset(new SectionMemoryManager());
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000167
168 // Create a new module and save it. Use a different return code so we can
169 // tell if MCJIT compiled this module or used the cache.
170 M.reset(createEmptyModule("<main>"));
171 Main = insertMainFunction(M.get(), ReplacementRC);
172 const Module * SecondModulePointer = M.get();
173
174 // Create a new MCJIT instance to load this module then execute it.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000175 createJIT(std::move(M));
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000176 TheJIT->setObjectCache(Cache.get());
177 compileAndRun();
178
179 // Verify that MCJIT tried to look-up this module in the cache.
180 EXPECT_TRUE(Cache->wasModuleLookedUp(SecondModulePointer));
181
182 // Verify that MCJIT didn't try to cache this again.
183 EXPECT_FALSE(Cache->wereDuplicatesInserted());
184}
185
186TEST_F(MCJITObjectCacheTest, VerifyNonLoadFromCache) {
187 SKIP_UNSUPPORTED_PLATFORM;
188
Ahmed Charles56440fd2014-03-06 05:51:42 +0000189 std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000190
191 // Compile this module with an MCJIT engine
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000192 createJIT(std::move(M));
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000193 TheJIT->setObjectCache(Cache.get());
194 TheJIT->finalizeObject();
195
196 // Destroy the MCJIT engine we just used
197 TheJIT.reset();
198
199 // Create a new memory manager.
Lang Hames4a5697e2014-12-03 00:51:19 +0000200 MM.reset(new SectionMemoryManager());
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000201
202 // Create a new module and save it. Use a different return code so we can
203 // tell if MCJIT compiled this module or used the cache. Note that we use
204 // a new module name here so the module shouldn't be found in the cache.
205 M.reset(createEmptyModule("<not-main>"));
206 Main = insertMainFunction(M.get(), ReplacementRC);
207 const Module * SecondModulePointer = M.get();
208
209 // Create a new MCJIT instance to load this module then execute it.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000210 createJIT(std::move(M));
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000211 TheJIT->setObjectCache(Cache.get());
212
213 // Verify that our object cache does not contain the module yet.
214 const MemoryBuffer *ObjBuffer = Cache->getObjectInternal(SecondModulePointer);
Craig Topper66f09ad2014-06-08 22:29:17 +0000215 EXPECT_EQ(nullptr, ObjBuffer);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000216
217 // Run the function and look for the replacement return code.
218 compileAndRun(ReplacementRC);
219
220 // Verify that MCJIT tried to look-up this module in the cache.
221 EXPECT_TRUE(Cache->wasModuleLookedUp(SecondModulePointer));
222
223 // Verify that our object cache now contains the module.
224 ObjBuffer = Cache->getObjectInternal(SecondModulePointer);
Craig Topper66f09ad2014-06-08 22:29:17 +0000225 EXPECT_TRUE(nullptr != ObjBuffer);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000226
227 // Verify that MCJIT didn't try to cache this again.
228 EXPECT_FALSE(Cache->wereDuplicatesInserted());
229}
230
231} // Namespace
232