mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
Load cached object without compiling LLVM module
This commit is contained in:
parent
3990e5b97a
commit
9c244ed08e
3 changed files with 40 additions and 12 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <llvm/IR/Module.h>
|
#include <llvm/IR/Module.h>
|
||||||
|
#include <llvm/IR/LLVMContext.h>
|
||||||
#include <llvm/Support/Path.h>
|
#include <llvm/Support/Path.h>
|
||||||
#include <llvm/Support/FileSystem.h>
|
#include <llvm/Support/FileSystem.h>
|
||||||
#include <llvm/Support/raw_os_ostream.h>
|
#include <llvm/Support/raw_os_ostream.h>
|
||||||
|
|
@ -23,6 +24,32 @@ ObjectCache* Cache::getObjectCache()
|
||||||
return &objectCache;
|
return &objectCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
llvm::MemoryBuffer* lastObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<llvm::Module> Cache::getObject(std::string const& id)
|
||||||
|
{
|
||||||
|
assert(!lastObject);
|
||||||
|
llvm::SmallString<256> cachePath;
|
||||||
|
llvm::sys::path::system_temp_directory(false, cachePath);
|
||||||
|
llvm::sys::path::append(cachePath, "evm_objs", id);
|
||||||
|
|
||||||
|
if (auto r = llvm::MemoryBuffer::getFile(cachePath.str(), -1, false))
|
||||||
|
lastObject = llvm::MemoryBuffer::getMemBufferCopy(r.get()->getBuffer());
|
||||||
|
else if (r.getError() != std::make_error_code(std::errc::no_such_file_or_directory))
|
||||||
|
std::cerr << r.getError().message(); // TODO: Add log
|
||||||
|
|
||||||
|
if (lastObject) // if object found create fake module
|
||||||
|
{
|
||||||
|
auto module = std::unique_ptr<llvm::Module>(new llvm::Module(id, llvm::getGlobalContext()));
|
||||||
|
auto mainFuncType = llvm::FunctionType::get(llvm::IntegerType::get(llvm::getGlobalContext(), 32), {}, false);
|
||||||
|
auto func = llvm::Function::Create(mainFuncType, llvm::Function::ExternalLinkage, id, module.get());
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::MemoryBuffer const* _object)
|
void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::MemoryBuffer const* _object)
|
||||||
{
|
{
|
||||||
|
|
@ -43,16 +70,9 @@ void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::Memory
|
||||||
|
|
||||||
llvm::MemoryBuffer* ObjectCache::getObject(llvm::Module const* _module)
|
llvm::MemoryBuffer* ObjectCache::getObject(llvm::Module const* _module)
|
||||||
{
|
{
|
||||||
auto&& id = _module->getModuleIdentifier();
|
auto o = lastObject;
|
||||||
llvm::SmallString<256> cachePath;
|
lastObject = nullptr;
|
||||||
llvm::sys::path::system_temp_directory(false, cachePath);
|
return o;
|
||||||
llvm::sys::path::append(cachePath, "evm_objs", id);
|
|
||||||
|
|
||||||
if (auto r = llvm::MemoryBuffer::getFile(cachePath.str(), -1, false))
|
|
||||||
return llvm::MemoryBuffer::getMemBufferCopy(r.get()->getBuffer());
|
|
||||||
else if (r.getError() != std::make_error_code(std::errc::no_such_file_or_directory))
|
|
||||||
std::cerr << r.getError().message(); // TODO: Add log
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ class Cache
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static ObjectCache* getObjectCache();
|
static ObjectCache* getObjectCache();
|
||||||
|
static std::unique_ptr<llvm::Module> getObject(std::string const& id);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,13 @@ ReturnCode ExecutionEngine::run(bytes const& _code, RuntimeData* _data, Env* _en
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto module = Compiler({}).compile(_code, mainFuncName);
|
bool objectCacheEnabled = true;
|
||||||
|
auto objectCache = objectCacheEnabled ? Cache::getObjectCache() : nullptr;
|
||||||
|
std::unique_ptr<llvm::Module> module;
|
||||||
|
if (objectCache)
|
||||||
|
module = Cache::getObject(mainFuncName);
|
||||||
|
if (!module)
|
||||||
|
module = Compiler({}).compile(_code, mainFuncName);
|
||||||
//module->dump();
|
//module->dump();
|
||||||
if (!ee)
|
if (!ee)
|
||||||
{
|
{
|
||||||
|
|
@ -100,7 +106,8 @@ ReturnCode ExecutionEngine::run(bytes const& _code, RuntimeData* _data, Env* _en
|
||||||
module.release(); // Successfully created llvm::ExecutionEngine takes ownership of the module
|
module.release(); // Successfully created llvm::ExecutionEngine takes ownership of the module
|
||||||
memoryManager.release(); // and memory manager
|
memoryManager.release(); // and memory manager
|
||||||
|
|
||||||
ee->setObjectCache(Cache::getObjectCache());
|
if (objectCache)
|
||||||
|
ee->setObjectCache(objectCache);
|
||||||
entryFuncPtr = (EntryFuncPtr)ee->getFunctionAddress(mainFuncName);
|
entryFuncPtr = (EntryFuncPtr)ee->getFunctionAddress(mainFuncName);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue