<div dir="ltr">Hi Cheng,<div><br></div><div>You can't cache individual functions, so I'm not sure why you would use function names as keys?</div><div><br></div><div>Holding StringRefs at the moment seems dangerous. MCJIT instances own the objects that are added to them, so you have two cases:</div><div><br></div><div>(1) The object has previously been added to this MCJIT instance, in which case it's also still part of the JIT'd process, so adding it again is redundant (and probably a bug).</div><div><br></div><div>OR</div><div><br></div><div>(2) This is a new MCJIT instance to which the object has not been added. If the old MCJIT instance has been deleted (which seems likely) then the memory backing your object cache is also gone and you have a bug.</div><div><br></div><div>This is why I used getMemBufferCopy above - it ensures that your objects outlive your JIT instance.</div><div><br></div><div>FYI, you should probably have some sort of invalidation test on notifyObjectCompiled, like:</div><div><br></div><div>if (isCacheStaleFor(M)) {</div><div>  CachedObjs[M] = ...;</div><div>}</div><div><br></div><div>Otherwise you'll end up constantly replacing the object in your cache.</div><div><br></div><div>Cheers,</div><div>Lang.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 18, 2014 at 11:00 AM, Cheng Zhu <span dir="ltr"><<a href="mailto:chengzhu@gmail.com" target="_blank">chengzhu@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thank you Lang. That should work. I was planing to use llvm::StringRef as the value of the CacheObjs map, and use function name string as key earlier. I guess that should work too and less storage use right?</div><div class="gmail_extra"><div><div class="h5"><br><div class="gmail_quote">On Thu, Sep 18, 2014 at 10:43 AM, Lang Hames <span dir="ltr"><<a href="mailto:lhames@gmail.com" target="_blank">lhames@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Cheng,<div><br></div><div>You could use a raw_svector_ostream to write to a memory buffer, but that's unnecessarily complicated. You just need to clone the memory buffer pointed to by Obj. Something along the lines of:</div><br><font face="courier new, monospace">class MyCache : public ObjectCache {<br>public:<br>  void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {</font><div><font face="courier new, monospace">    CachedObjs[M] =</font></div><div><font face="courier new, monospace">      MemoryBuffer::getMemBufferCopy(Obj.getBuffer(),</font></div><div><font face="courier new, monospace">                                     Obj.getBufferIdentifier());</font></div><div><font face="courier new, monospace">  }</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">  std::unique_ptr<MemoryBuffer> getObject(const Module *M) override {</font></div><div><font face="courier new, monospace">    MemoryBuffer& B = *CachedObjs[M];</font></div><div><font face="courier new, monospace">    return MemoryBuffer::getMemBufferCopy(B.getBuffer(), B.getBufferIdentifier());</font></div><div><font face="courier new, monospace">  }</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">private:</font><div><font face="courier new, monospace">  std::map<Module*, std::unique_ptr<MemoryBuffer>> CachedObjs;</font></div><div><font face="courier new, monospace">};</font><div><div><br></div><div><br></div><div>Cheers,</div><div>Lang.</div><div><br></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Wed, Sep 17, 2014 at 6:18 PM, Cheng Zhu <span dir="ltr"><<a href="mailto:chengzhu@gmail.com" target="_blank">chengzhu@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr">Hi, All<div><br></div><div>I m not sure if this question has been asked or not. I'd like cache the MCJIT compiled object into memory buffer so it can be reused later. I followed the Andy Kaylor's example wrote an inherited class from ObjectCache and use raw_fd_ostream to save the cache and load the cache from a file. I checked raw_ostream and its subclass, maybe I am wrong but I don't see one is fit to save to memory buffer. Any suggestions?</div><div><br></div><div>Thank you very much<span><font color="#888888"><br clear="all"><div><br></div>-- <br>Best regards<br><br>Cheng
</font></span></div></div>
<br></div></div>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br>Best regards<br><br>Cheng
</font></span></div>
</blockquote></div><br></div>