Unexpected memory occupy when load bitcode module

  1. If i want to release the module’s memory resources which held by JIT, i need dispose the whole JIT?

No. The JIT will hold the module only as long as it is needed: If you trigger compilation of the module by looking up one of its symbols then the ThreadSafeModule will be thrown away as soon as compilation is done. Or you can remove the resource tracker – see below.

  1. I saw LLVMOrcLLJITAddLLVMIRModuleWithRT has a resource tracker LLVMOrcResourceTrackerRef args, does this struct impact the resources management with JIT?

Yes. ResourceTrackers track the resources associated with sets of symbols. E.g. If you add a Module that defines symbols {a, b, c} using RT, then RT is responsible for tracking whatever resources are currently held to provide the definitions of {a, b, c} (in addition to whatever else the tracker was already tracking). Initially the resource backing {a, b, c} is the module itself, and if you immediately call remove on the tracker then you will just free the module. On the other hand if you call lookup("a") and trigger compilation of the module then the module will be thrown away and the tracker now tracks the raw compiled memory for {a, b, c}.

So the right way to think about resource management is to trust that ORC will aggressively throw away unnecessary program representations, and resource trackers let you think about the parts of your JIT’d program that you want to keep or throw away, regardless of what resources happen to be backing them.

One note of caution: ResourceTrackers do not manage dependencies. If you build a program with the following call graph:

 A (aded with RT1)
   -> B (added with RT2)
     -> C (added with RT3)

You can call remove on RT2 and this will throw away all resources associated with B and leave A with a dangling reference. It’s the client’s job to track these dependencies and make sure that they don’t leave or use dangling references.

1 Like