<div dir="ltr">Hi all,<div><br></div><div>I'm trying to add pre-compiled object cache to my run-time.</div><div>I've implemented the object cache as follow:</div><div><br></div><div><div><span class="" style="white-space:pre">    </span>class EngineObjectCache : public llvm::ObjectCache {</div><div><span class="" style="white-space:pre">       </span>private:</div><div><span class="" style="white-space:pre">           </span>std::unordered_map<std::string, std::unique_ptr<llvm::MemoryBuffer>> CachedObjs;</div><div><br></div><div><span class="" style="white-space:pre">      </span>public:</div><div><span class="" style="white-space:pre">            </span>virtual void notifyObjectCompiled(const llvm::Module *M, llvm::MemoryBufferRef Obj) {<span class="" style="white-space:pre">             </span></div><div><span class="" style="white-space:pre">                   </span>auto id = M->getModuleIdentifier();</div><div><span class="" style="white-space:pre">                     </span>auto iter = CachedObjs.find(id);</div><div><span class="" style="white-space:pre">                   </span>if (iter == CachedObjs.end()) {</div><div><span class="" style="white-space:pre">                            </span>auto buf = llvm::MemoryBuffer::getMemBufferCopy(Obj.getBuffer(), Obj.getBufferIdentifier());</div><div><span class="" style="white-space:pre">                               </span>CachedObjs.insert(std::make_pair(id, std::move(buf)));</div><div><span class="" style="white-space:pre">                     </span>}</div><div><span class="" style="white-space:pre">          </span>};</div><div><span class="" style="white-space:pre">         </span></div><div><span class="" style="white-space:pre">           </span>virtual std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module *M) {</div><div><span class="" style="white-space:pre">                       </span>auto id = M->getModuleIdentifier();</div><div><span class="" style="white-space:pre">                     </span>auto iter = CachedObjs.find(id);</div><div><span class="" style="white-space:pre">                   </span>if (iter != CachedObjs.end()) {</div><div><span class="" style="white-space:pre">                            </span>llvm::MemoryBuffer& B = *iter->second;</div><div><span class="" style="white-space:pre">                              </span>return llvm::MemoryBuffer::getMemBufferCopy(B.getBuffer(), B.getBufferIdentifier());</div><div><span class="" style="white-space:pre">                       </span>}</div><div><span class="" style="white-space:pre">                  </span>else</div><div><span class="" style="white-space:pre">                               </span>return nullptr;</div><div><span class="" style="white-space:pre">            </span>};</div><div><span class="" style="white-space:pre"> </span>}</div></div><div><br></div><div>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() <b>sometimes </b>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 *M)):</div><div><br></div><div><div><div>  // Load the object into the dynamic linker.</div><div>  // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).</div><div>  ErrorOr<std::unique_ptr<object::ObjectFile>> LoadedObject =</div><div>    object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());</div><div>  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =</div><div>    Dyld.loadObject(*LoadedObject.get());</div><div><br></div><div>  if (Dyld.hasError())</div><div>    report_fatal_error(Dyld.getErrorString());</div></div></div><div><br></div><div><br></div><div>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.</div><div><br></div><div>I tried to traced further in RuntimeDyld::SymbolInfo getSymbol()</div><div>and could see that the GlobalSymbolTable did not contained my symbol.</div><div><br></div><div>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.</div><div><br></div><div>I did not hardcode any ptrs in the IR so the objects should be reusable over multiple instances.</div><div>Any one got a clue ?</div><div><br></div><div>Cheers,</div><div><br></div><div><br></div></div>