Memory allocation (or deallocation) model?

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?

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?
* Once the function is JITted, will all those types/instructions/functions
still be allocated in memory?
** If so, is there a way to explicitly free them?
* Can I "delete executionEngine" to free up all the memory?

- --BDS

- --

Benjamin Smedberg
Platform Guru
Mozilla Corporation
benjamin@smedbergs.us
http://benjamin.smedbergs.us/

I've been reading the headers and
LLVM Programmer's Manual 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?

Yep.

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

Yep, delete it, then call llvm_shutdown().

-Chris

If an instruction is initially allocated with its Instruction
*insertAtEnd parameter defaulted to null, and then later appended to a
BasicBlock, will the BasicBlock take ownership of the Instruction?

Thanks,
-Jonathan

Yes.

There is a containment structure to the IR. If an object is inserted into that hierarchy, memory management is automatic. 'delete Function' recursively deletes GlobalVariables and Functions, and so on down to Instructions. If you remove an object from that tree, or never insert it, you are responsible for 'delete'ing it.

There are useful methods like 'eraseFromParent' which combine these operations, and others like 'removeFromParent' that allow reinsertion or 'delete'ion.

— Gordon

Yes, if/when an instruction is inserted into a block it is owned by that block. If you remove the instruction you have to delete it or reinsert it into another block.

-Chris

http://nondot.org/sabre
http://llvm.org