Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 1 | //===- 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 Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 10 | #include "MCJITTestBase.h" |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallVector.h" |
| 12 | #include "llvm/ADT/StringMap.h" |
| 13 | #include "llvm/ADT/StringSet.h" |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 14 | #include "llvm/ExecutionEngine/MCJIT.h" |
| 15 | #include "llvm/ExecutionEngine/ObjectCache.h" |
| 16 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class TestObjectCache : public ObjectCache { |
| 24 | public: |
| 25 | TestObjectCache() : DuplicateInserted(false) { } |
| 26 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 27 | void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override { |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 28 | // 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 Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 36 | std::unique_ptr<MemoryBuffer> getObject(const Module *M) override { |
Andrew Kaylor | b595f53 | 2013-06-28 21:40:16 +0000 | [diff] [blame] | 37 | const MemoryBuffer* BufferFound = getObjectInternal(M); |
| 38 | ModulesLookedUp.insert(M->getModuleIdentifier()); |
| 39 | if (!BufferFound) |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 40 | return nullptr; |
Andrew Kaylor | b595f53 | 2013-06-28 21:40:16 +0000 | [diff] [blame] | 41 | // Our test cache wants to maintain ownership of its object buffers |
| 42 | // so we make a copy here for the execution engine. |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 43 | return MemoryBuffer::getMemBufferCopy(BufferFound->getBuffer()); |
Andrew Kaylor | b595f53 | 2013-06-28 21:40:16 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 46 | // 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 Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 59 | return nullptr; |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 60 | return it->second; |
| 61 | } |
| 62 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 63 | private: |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 64 | MemoryBuffer *copyBuffer(MemoryBufferRef Buf) { |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 65 | // Create a local copy of the buffer. |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 66 | std::unique_ptr<MemoryBuffer> NewBuffer = |
| 67 | MemoryBuffer::getMemBufferCopy(Buf.getBuffer()); |
Rafael Espindola | bb415ea | 2014-08-13 18:59:01 +0000 | [diff] [blame] | 68 | MemoryBuffer *Ret = NewBuffer.get(); |
| 69 | AllocatedBuffers.push_back(std::move(NewBuffer)); |
| 70 | return Ret; |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | StringMap<const MemoryBuffer *> ObjMap; |
| 74 | StringSet<> ModulesLookedUp; |
Rafael Espindola | bb415ea | 2014-08-13 18:59:01 +0000 | [diff] [blame] | 75 | SmallVector<std::unique_ptr<MemoryBuffer>, 2> AllocatedBuffers; |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 76 | bool DuplicateInserted; |
| 77 | }; |
| 78 | |
| 79 | class MCJITObjectCacheTest : public testing::Test, public MCJITTestBase { |
| 80 | protected: |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 81 | enum { |
| 82 | OriginalRC = 6, |
| 83 | ReplacementRC = 7 |
| 84 | }; |
| 85 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 86 | void SetUp() override { |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 87 | M.reset(createEmptyModule("<main>")); |
| 88 | Main = insertMainFunction(M.get(), OriginalRC); |
| 89 | } |
| 90 | |
| 91 | void compileAndRun(int ExpectedRC = OriginalRC) { |
| 92 | // This function shouldn't be called until after SetUp. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 93 | ASSERT_TRUE(bool(TheJIT)); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 94 | ASSERT_TRUE(nullptr != Main); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 95 | |
David Tweed | 2e7efed | 2013-05-17 10:01:46 +0000 | [diff] [blame] | 96 | // We may be using a null cache, so ensure compilation is valid. |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 97 | TheJIT->finalizeObject(); |
| 98 | void *vPtr = TheJIT->getPointerToFunction(Main); |
| 99 | |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 100 | EXPECT_TRUE(nullptr != vPtr) |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 101 | << "Unable to get pointer to main() from JIT"; |
| 102 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 103 | int (*FuncPtr)() = (int(*)())(intptr_t)vPtr; |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 104 | int returnCode = FuncPtr(); |
| 105 | EXPECT_EQ(returnCode, ExpectedRC); |
| 106 | } |
| 107 | |
| 108 | Function *Main; |
| 109 | }; |
| 110 | |
| 111 | TEST_F(MCJITObjectCacheTest, SetNullObjectCache) { |
| 112 | SKIP_UNSUPPORTED_PLATFORM; |
| 113 | |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 114 | createJIT(std::move(M)); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 115 | |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 116 | TheJIT->setObjectCache(nullptr); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 117 | |
| 118 | compileAndRun(); |
| 119 | } |
| 120 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 121 | TEST_F(MCJITObjectCacheTest, VerifyBasicObjectCaching) { |
| 122 | SKIP_UNSUPPORTED_PLATFORM; |
| 123 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 124 | std::unique_ptr<TestObjectCache> Cache(new TestObjectCache); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 125 | |
| 126 | // Save a copy of the module pointer before handing it off to MCJIT. |
| 127 | const Module * SavedModulePointer = M.get(); |
| 128 | |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 129 | createJIT(std::move(M)); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 130 | |
| 131 | TheJIT->setObjectCache(Cache.get()); |
| 132 | |
| 133 | // Verify that our object cache does not contain the module yet. |
| 134 | const MemoryBuffer *ObjBuffer = Cache->getObjectInternal(SavedModulePointer); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 135 | EXPECT_EQ(nullptr, ObjBuffer); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 136 | |
| 137 | compileAndRun(); |
| 138 | |
| 139 | // Verify that MCJIT tried to look-up this module in the cache. |
| 140 | EXPECT_TRUE(Cache->wasModuleLookedUp(SavedModulePointer)); |
| 141 | |
| 142 | // Verify that our object cache now contains the module. |
| 143 | ObjBuffer = Cache->getObjectInternal(SavedModulePointer); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 144 | EXPECT_TRUE(nullptr != ObjBuffer); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 145 | |
| 146 | // Verify that the cache was only notified once. |
| 147 | EXPECT_FALSE(Cache->wereDuplicatesInserted()); |
| 148 | } |
| 149 | |
| 150 | TEST_F(MCJITObjectCacheTest, VerifyLoadFromCache) { |
| 151 | SKIP_UNSUPPORTED_PLATFORM; |
| 152 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 153 | std::unique_ptr<TestObjectCache> Cache(new TestObjectCache); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 154 | |
| 155 | // Compile this module with an MCJIT engine |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 156 | createJIT(std::move(M)); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 157 | TheJIT->setObjectCache(Cache.get()); |
| 158 | TheJIT->finalizeObject(); |
| 159 | |
| 160 | // Destroy the MCJIT engine we just used |
| 161 | TheJIT.reset(); |
| 162 | |
| 163 | // Create a new memory manager. |
Lang Hames | 4a5697e | 2014-12-03 00:51:19 +0000 | [diff] [blame] | 164 | MM.reset(new SectionMemoryManager()); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 165 | |
| 166 | // Create a new module and save it. Use a different return code so we can |
| 167 | // tell if MCJIT compiled this module or used the cache. |
| 168 | M.reset(createEmptyModule("<main>")); |
| 169 | Main = insertMainFunction(M.get(), ReplacementRC); |
| 170 | const Module * SecondModulePointer = M.get(); |
| 171 | |
| 172 | // Create a new MCJIT instance to load this module then execute it. |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 173 | createJIT(std::move(M)); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 174 | TheJIT->setObjectCache(Cache.get()); |
| 175 | compileAndRun(); |
| 176 | |
| 177 | // Verify that MCJIT tried to look-up this module in the cache. |
| 178 | EXPECT_TRUE(Cache->wasModuleLookedUp(SecondModulePointer)); |
| 179 | |
| 180 | // Verify that MCJIT didn't try to cache this again. |
| 181 | EXPECT_FALSE(Cache->wereDuplicatesInserted()); |
| 182 | } |
| 183 | |
| 184 | TEST_F(MCJITObjectCacheTest, VerifyNonLoadFromCache) { |
| 185 | SKIP_UNSUPPORTED_PLATFORM; |
| 186 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 187 | std::unique_ptr<TestObjectCache> Cache(new TestObjectCache); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 188 | |
| 189 | // Compile this module with an MCJIT engine |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 190 | createJIT(std::move(M)); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 191 | TheJIT->setObjectCache(Cache.get()); |
| 192 | TheJIT->finalizeObject(); |
| 193 | |
| 194 | // Destroy the MCJIT engine we just used |
| 195 | TheJIT.reset(); |
| 196 | |
| 197 | // Create a new memory manager. |
Lang Hames | 4a5697e | 2014-12-03 00:51:19 +0000 | [diff] [blame] | 198 | MM.reset(new SectionMemoryManager()); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 199 | |
| 200 | // Create a new module and save it. Use a different return code so we can |
| 201 | // tell if MCJIT compiled this module or used the cache. Note that we use |
| 202 | // a new module name here so the module shouldn't be found in the cache. |
| 203 | M.reset(createEmptyModule("<not-main>")); |
| 204 | Main = insertMainFunction(M.get(), ReplacementRC); |
| 205 | const Module * SecondModulePointer = M.get(); |
| 206 | |
| 207 | // Create a new MCJIT instance to load this module then execute it. |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 208 | createJIT(std::move(M)); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 209 | TheJIT->setObjectCache(Cache.get()); |
| 210 | |
| 211 | // Verify that our object cache does not contain the module yet. |
| 212 | const MemoryBuffer *ObjBuffer = Cache->getObjectInternal(SecondModulePointer); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 213 | EXPECT_EQ(nullptr, ObjBuffer); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 214 | |
| 215 | // Run the function and look for the replacement return code. |
| 216 | compileAndRun(ReplacementRC); |
| 217 | |
| 218 | // Verify that MCJIT tried to look-up this module in the cache. |
| 219 | EXPECT_TRUE(Cache->wasModuleLookedUp(SecondModulePointer)); |
| 220 | |
| 221 | // Verify that our object cache now contains the module. |
| 222 | ObjBuffer = Cache->getObjectInternal(SecondModulePointer); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 223 | EXPECT_TRUE(nullptr != ObjBuffer); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 224 | |
| 225 | // Verify that MCJIT didn't try to cache this again. |
| 226 | EXPECT_FALSE(Cache->wereDuplicatesInserted()); |
| 227 | } |
| 228 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 229 | } // end anonymous namespace |