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