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

Lang Hames lhames at gmail.com
Thu Sep 18 10:43:33 PDT 2014


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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140918/598380bb/attachment.html>


More information about the llvm-dev mailing list