[LLVMdev] How to cache MCJIT compiled object into memory?

Lang Hames lhames at gmail.com
Thu Sep 18 11:31:26 PDT 2014


Hi Cheng,

You can't cache individual functions, so I'm not sure why you would use
function names as keys?

Holding StringRefs at the moment seems dangerous. MCJIT instances own the
objects that are added to them, so you have two cases:

(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).

OR

(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.

This is why I used getMemBufferCopy above - it ensures that your objects
outlive your JIT instance.

FYI, you should probably have some sort of invalidation test on
notifyObjectCompiled, like:

if (isCacheStaleFor(M)) {
  CachedObjs[M] = ...;
}

Otherwise you'll end up constantly replacing the object in your cache.

Cheers,
Lang.


On Thu, Sep 18, 2014 at 11:00 AM, Cheng Zhu <chengzhu at gmail.com> wrote:

> 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?
>
> On Thu, Sep 18, 2014 at 10:43 AM, Lang Hames <lhames at gmail.com> wrote:
>
>> Hi Cheng,
>>
>> 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:
>>
>> class MyCache : public ObjectCache {
>> public:
>>   void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj)
>> override {
>>     CachedObjs[M] =
>>       MemoryBuffer::getMemBufferCopy(Obj.getBuffer(),
>>                                      Obj.getBufferIdentifier());
>>   }
>>
>>   std::unique_ptr<MemoryBuffer> getObject(const Module *M) override {
>>     MemoryBuffer& B = *CachedObjs[M];
>>     return MemoryBuffer::getMemBufferCopy(B.getBuffer(),
>> B.getBufferIdentifier());
>>   }
>>
>> private:
>>   std::map<Module*, std::unique_ptr<MemoryBuffer>> CachedObjs;
>> };
>>
>>
>> Cheers,
>> Lang.
>>
>>
>> On Wed, Sep 17, 2014 at 6:18 PM, Cheng Zhu <chengzhu at gmail.com> wrote:
>>
>>> Hi, All
>>>
>>> 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?
>>>
>>> Thank you very much
>>>
>>> --
>>> Best regards
>>>
>>> Cheng
>>>
>>> _______________________________________________
>>> LLVM Developers mailing list
>>> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>>>
>>>
>>
>
>
> --
> Best regards
>
> Cheng
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140918/d2a3d852/attachment.html>


More information about the llvm-dev mailing list