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