2013-04-25 23:02:36 +02:00
|
|
|
//===- MCJITObjectCacheTest.cpp - Unit tests for MCJIT object caching -----===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-04-25 23:02:36 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-07 12:48:04 +01:00
|
|
|
#include "MCJITTestBase.h"
|
2013-04-25 23:02:36 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringSet.h"
|
|
|
|
#include "llvm/ExecutionEngine/MCJIT.h"
|
|
|
|
#include "llvm/ExecutionEngine/ObjectCache.h"
|
|
|
|
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TestObjectCache : public ObjectCache {
|
|
|
|
public:
|
|
|
|
TestObjectCache() : DuplicateInserted(false) { }
|
|
|
|
|
2014-08-19 20:44:46 +02:00
|
|
|
void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
|
2013-04-25 23:02:36 +02:00
|
|
|
// If we've seen this module before, note that.
|
|
|
|
const std::string ModuleID = M->getModuleIdentifier();
|
|
|
|
if (ObjMap.find(ModuleID) != ObjMap.end())
|
|
|
|
DuplicateInserted = true;
|
|
|
|
// Store a copy of the buffer in our map.
|
|
|
|
ObjMap[ModuleID] = copyBuffer(Obj);
|
|
|
|
}
|
|
|
|
|
2015-04-11 04:11:45 +02:00
|
|
|
std::unique_ptr<MemoryBuffer> getObject(const Module *M) override {
|
2013-06-28 23:40:16 +02:00
|
|
|
const MemoryBuffer* BufferFound = getObjectInternal(M);
|
|
|
|
ModulesLookedUp.insert(M->getModuleIdentifier());
|
|
|
|
if (!BufferFound)
|
2014-06-09 00:29:17 +02:00
|
|
|
return nullptr;
|
2013-06-28 23:40:16 +02:00
|
|
|
// Our test cache wants to maintain ownership of its object buffers
|
|
|
|
// so we make a copy here for the execution engine.
|
2014-08-27 22:03:13 +02:00
|
|
|
return MemoryBuffer::getMemBufferCopy(BufferFound->getBuffer());
|
2013-06-28 23:40:16 +02:00
|
|
|
}
|
|
|
|
|
2013-04-25 23:02:36 +02:00
|
|
|
// Test-harness-specific functions
|
|
|
|
bool wereDuplicatesInserted() { return DuplicateInserted; }
|
|
|
|
|
|
|
|
bool wasModuleLookedUp(const Module *M) {
|
|
|
|
return ModulesLookedUp.find(M->getModuleIdentifier())
|
|
|
|
!= ModulesLookedUp.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const MemoryBuffer* getObjectInternal(const Module* M) {
|
|
|
|
// Look for the module in our map.
|
|
|
|
const std::string ModuleID = M->getModuleIdentifier();
|
|
|
|
StringMap<const MemoryBuffer *>::iterator it = ObjMap.find(ModuleID);
|
|
|
|
if (it == ObjMap.end())
|
2014-06-09 00:29:17 +02:00
|
|
|
return nullptr;
|
2013-04-25 23:02:36 +02:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-08-19 20:44:46 +02:00
|
|
|
MemoryBuffer *copyBuffer(MemoryBufferRef Buf) {
|
2013-04-25 23:02:36 +02:00
|
|
|
// Create a local copy of the buffer.
|
2014-08-27 22:03:13 +02:00
|
|
|
std::unique_ptr<MemoryBuffer> NewBuffer =
|
|
|
|
MemoryBuffer::getMemBufferCopy(Buf.getBuffer());
|
2014-08-13 20:59:01 +02:00
|
|
|
MemoryBuffer *Ret = NewBuffer.get();
|
|
|
|
AllocatedBuffers.push_back(std::move(NewBuffer));
|
|
|
|
return Ret;
|
2013-04-25 23:02:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StringMap<const MemoryBuffer *> ObjMap;
|
|
|
|
StringSet<> ModulesLookedUp;
|
2014-08-13 20:59:01 +02:00
|
|
|
SmallVector<std::unique_ptr<MemoryBuffer>, 2> AllocatedBuffers;
|
2013-04-25 23:02:36 +02:00
|
|
|
bool DuplicateInserted;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MCJITObjectCacheTest : public testing::Test, public MCJITTestBase {
|
|
|
|
protected:
|
|
|
|
enum {
|
|
|
|
OriginalRC = 6,
|
|
|
|
ReplacementRC = 7
|
|
|
|
};
|
|
|
|
|
2015-04-11 04:11:45 +02:00
|
|
|
void SetUp() override {
|
2013-04-25 23:02:36 +02:00
|
|
|
M.reset(createEmptyModule("<main>"));
|
|
|
|
Main = insertMainFunction(M.get(), OriginalRC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void compileAndRun(int ExpectedRC = OriginalRC) {
|
|
|
|
// This function shouldn't be called until after SetUp.
|
2014-03-06 06:51:42 +01:00
|
|
|
ASSERT_TRUE(bool(TheJIT));
|
2014-06-09 00:29:17 +02:00
|
|
|
ASSERT_TRUE(nullptr != Main);
|
2013-04-25 23:02:36 +02:00
|
|
|
|
2013-05-17 12:01:46 +02:00
|
|
|
// We may be using a null cache, so ensure compilation is valid.
|
2013-04-25 23:02:36 +02:00
|
|
|
TheJIT->finalizeObject();
|
|
|
|
void *vPtr = TheJIT->getPointerToFunction(Main);
|
|
|
|
|
2014-06-09 00:29:17 +02:00
|
|
|
EXPECT_TRUE(nullptr != vPtr)
|
2013-04-25 23:02:36 +02:00
|
|
|
<< "Unable to get pointer to main() from JIT";
|
|
|
|
|
2015-11-04 23:32:32 +01:00
|
|
|
int (*FuncPtr)() = (int(*)())(intptr_t)vPtr;
|
2013-04-25 23:02:36 +02:00
|
|
|
int returnCode = FuncPtr();
|
|
|
|
EXPECT_EQ(returnCode, ExpectedRC);
|
|
|
|
}
|
|
|
|
|
|
|
|
Function *Main;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(MCJITObjectCacheTest, SetNullObjectCache) {
|
|
|
|
SKIP_UNSUPPORTED_PLATFORM;
|
|
|
|
|
2014-08-19 06:04:25 +02:00
|
|
|
createJIT(std::move(M));
|
2013-04-25 23:02:36 +02:00
|
|
|
|
2014-06-09 00:29:17 +02:00
|
|
|
TheJIT->setObjectCache(nullptr);
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
compileAndRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MCJITObjectCacheTest, VerifyBasicObjectCaching) {
|
|
|
|
SKIP_UNSUPPORTED_PLATFORM;
|
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
// Save a copy of the module pointer before handing it off to MCJIT.
|
|
|
|
const Module * SavedModulePointer = M.get();
|
|
|
|
|
2014-08-19 06:04:25 +02:00
|
|
|
createJIT(std::move(M));
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
TheJIT->setObjectCache(Cache.get());
|
|
|
|
|
|
|
|
// Verify that our object cache does not contain the module yet.
|
|
|
|
const MemoryBuffer *ObjBuffer = Cache->getObjectInternal(SavedModulePointer);
|
2014-06-09 00:29:17 +02:00
|
|
|
EXPECT_EQ(nullptr, ObjBuffer);
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
compileAndRun();
|
|
|
|
|
|
|
|
// Verify that MCJIT tried to look-up this module in the cache.
|
|
|
|
EXPECT_TRUE(Cache->wasModuleLookedUp(SavedModulePointer));
|
|
|
|
|
|
|
|
// Verify that our object cache now contains the module.
|
|
|
|
ObjBuffer = Cache->getObjectInternal(SavedModulePointer);
|
2014-06-09 00:29:17 +02:00
|
|
|
EXPECT_TRUE(nullptr != ObjBuffer);
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
// Verify that the cache was only notified once.
|
|
|
|
EXPECT_FALSE(Cache->wereDuplicatesInserted());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MCJITObjectCacheTest, VerifyLoadFromCache) {
|
|
|
|
SKIP_UNSUPPORTED_PLATFORM;
|
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
// Compile this module with an MCJIT engine
|
2014-08-19 06:04:25 +02:00
|
|
|
createJIT(std::move(M));
|
2013-04-25 23:02:36 +02:00
|
|
|
TheJIT->setObjectCache(Cache.get());
|
|
|
|
TheJIT->finalizeObject();
|
|
|
|
|
|
|
|
// Destroy the MCJIT engine we just used
|
|
|
|
TheJIT.reset();
|
|
|
|
|
|
|
|
// Create a new memory manager.
|
2014-12-03 01:51:19 +01:00
|
|
|
MM.reset(new SectionMemoryManager());
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
// Create a new module and save it. Use a different return code so we can
|
|
|
|
// tell if MCJIT compiled this module or used the cache.
|
|
|
|
M.reset(createEmptyModule("<main>"));
|
|
|
|
Main = insertMainFunction(M.get(), ReplacementRC);
|
|
|
|
const Module * SecondModulePointer = M.get();
|
|
|
|
|
|
|
|
// Create a new MCJIT instance to load this module then execute it.
|
2014-08-19 06:04:25 +02:00
|
|
|
createJIT(std::move(M));
|
2013-04-25 23:02:36 +02:00
|
|
|
TheJIT->setObjectCache(Cache.get());
|
|
|
|
compileAndRun();
|
|
|
|
|
|
|
|
// Verify that MCJIT tried to look-up this module in the cache.
|
|
|
|
EXPECT_TRUE(Cache->wasModuleLookedUp(SecondModulePointer));
|
|
|
|
|
|
|
|
// Verify that MCJIT didn't try to cache this again.
|
|
|
|
EXPECT_FALSE(Cache->wereDuplicatesInserted());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MCJITObjectCacheTest, VerifyNonLoadFromCache) {
|
|
|
|
SKIP_UNSUPPORTED_PLATFORM;
|
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
// Compile this module with an MCJIT engine
|
2014-08-19 06:04:25 +02:00
|
|
|
createJIT(std::move(M));
|
2013-04-25 23:02:36 +02:00
|
|
|
TheJIT->setObjectCache(Cache.get());
|
|
|
|
TheJIT->finalizeObject();
|
|
|
|
|
|
|
|
// Destroy the MCJIT engine we just used
|
|
|
|
TheJIT.reset();
|
|
|
|
|
|
|
|
// Create a new memory manager.
|
2014-12-03 01:51:19 +01:00
|
|
|
MM.reset(new SectionMemoryManager());
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
// Create a new module and save it. Use a different return code so we can
|
|
|
|
// tell if MCJIT compiled this module or used the cache. Note that we use
|
|
|
|
// a new module name here so the module shouldn't be found in the cache.
|
|
|
|
M.reset(createEmptyModule("<not-main>"));
|
|
|
|
Main = insertMainFunction(M.get(), ReplacementRC);
|
|
|
|
const Module * SecondModulePointer = M.get();
|
|
|
|
|
|
|
|
// Create a new MCJIT instance to load this module then execute it.
|
2014-08-19 06:04:25 +02:00
|
|
|
createJIT(std::move(M));
|
2013-04-25 23:02:36 +02:00
|
|
|
TheJIT->setObjectCache(Cache.get());
|
|
|
|
|
|
|
|
// Verify that our object cache does not contain the module yet.
|
|
|
|
const MemoryBuffer *ObjBuffer = Cache->getObjectInternal(SecondModulePointer);
|
2014-06-09 00:29:17 +02:00
|
|
|
EXPECT_EQ(nullptr, ObjBuffer);
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
// Run the function and look for the replacement return code.
|
|
|
|
compileAndRun(ReplacementRC);
|
|
|
|
|
|
|
|
// Verify that MCJIT tried to look-up this module in the cache.
|
|
|
|
EXPECT_TRUE(Cache->wasModuleLookedUp(SecondModulePointer));
|
|
|
|
|
|
|
|
// Verify that our object cache now contains the module.
|
|
|
|
ObjBuffer = Cache->getObjectInternal(SecondModulePointer);
|
2014-06-09 00:29:17 +02:00
|
|
|
EXPECT_TRUE(nullptr != ObjBuffer);
|
2013-04-25 23:02:36 +02:00
|
|
|
|
|
|
|
// Verify that MCJIT didn't try to cache this again.
|
|
|
|
EXPECT_FALSE(Cache->wereDuplicatesInserted());
|
|
|
|
}
|
|
|
|
|
2015-11-04 23:32:32 +01:00
|
|
|
} // end anonymous namespace
|