[llvm-dev] ObjectCache and getFunctionAddress issue

koffie drinker via llvm-dev llvm-dev at lists.llvm.org
Thu Jul 7 02:52:55 PDT 2016

Hi all,

I'm trying to add pre-compiled object cache to my run-time.
I've implemented the object cache as follow:

class EngineObjectCache : public llvm::ObjectCache {
std::unordered_map<std::string, std::unique_ptr<llvm::MemoryBuffer>>

virtual void notifyObjectCompiled(const llvm::Module *M,
llvm::MemoryBufferRef Obj) {
auto id = M->getModuleIdentifier();
auto iter = CachedObjs.find(id);
if (iter == CachedObjs.end()) {
auto buf = llvm::MemoryBuffer::getMemBufferCopy(Obj.getBuffer(),
CachedObjs.insert(std::make_pair(id, std::move(buf)));
virtual std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module
*M) {
auto id = M->getModuleIdentifier();
auto iter = CachedObjs.find(id);
if (iter != CachedObjs.end()) {
llvm::MemoryBuffer& B = *iter->second;
return llvm::MemoryBuffer::getMemBufferCopy(B.getBuffer(),
return nullptr;

When I generate the code for the first time, everything works fine. the
objects in CachedObjs are dumped to disk and reloaded for the next run.
However with the next run (thus using the cached objects) the
executionengine->getFunctionAddress() *sometimes *returns nullptr for a
function that exists in the cache. I've traced into the cache call, and
could see that my getObject() returned the right object. The object is
loaded and no error was reported by ( MCJIT::generateCodeForModule(Module

  // Load the object into the dynamic linker.
  // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
  ErrorOr<std::unique_ptr<object::ObjectFile>> LoadedObject =
  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =

  if (Dyld.hasError())

after the generateCodeForModule call, the findExistingSymbol is invoked.
For some reason it cannot find my symbol. My symbol exists in the Module
that was used as input for generateCodeForModule. It uses the object cache
and that code was valid and generated in the previous run.

I tried to traced further in RuntimeDyld::SymbolInfo getSymbol()
and could see that the GlobalSymbolTable did not contained my symbol.

I obviously forgot something or did something wrong, what puzzeld me is
that is quite semi-random. Even the most simple statements such as int x =
1; (no dependencies) sometime fails.

I did not hardcode any ptrs in the IR so the objects should be reusable
over multiple instances.
Any one got a clue ?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160707/2f54d8bf/attachment.html>

More information about the llvm-dev mailing list