[LLVMdev] Memory allocation (or deallocation) model?

Chris Lattner sabre at nondot.org
Mon Dec 3 18:56:55 PST 2007

On Mon, 3 Dec 2007, Benjamin Smedberg wrote:
> I've been reading the headers and
> http://llvm.org/releases/2.1/docs/ProgrammersManual.html and I'm still
> confused about a pretty fundamental point... who is expected to clean up
> various objects when we're finished with them, and when?

Ok, different objects have different life times.  The important ones are:

(most) Constants and Types are uniqued and live "forever", where forever 
means "until llvm_shutdown() is called".  The exceptions to this case are 
tied to a specific module, and are thus deleted when the module is.

Other IR objects (like instructions) have very simple ownership.  An 
instruction is owned by its basic block, a bb is owned by the function, a 
function is owned by thet module.

If you're JIT'ing from a module, the JIT object (an instance of 
ExecutionEngine) owns the module.

> Let's say I've created a module, retrieved a bunch of types, created a
> function, a basic block, and instructions and inserted them all together.
> I've linked it all to an execution engine, and gotten a function pointer to
> actually call using getPointerToFunctionOrStub:
> * Does the function pointer stay good as long as the execution engine is alive?

Yes, or until you explicitly free it with 
EE->freeMachineCodeForFunction(F).  Note that you can free the IR for the 
function body after you JIT the function by using F->deleteBody().

> * Once the function is JITted, will all those types/instructions/functions
> still be allocated in memory?

Yes, unless you free them.

> ** If so, is there a way to explicitly free them?


> * Can I "delete executionEngine" to free up all the memory?

Yep, delete it, then call llvm_shutdown().



More information about the llvm-dev mailing list