Lifetime of ExecutionEngine?

Given:

  typedef MyClass* (*jit_fn_ptr_type)();

  MyClass* set_fn( llvm::Function *fn ) {
    // create an ExecutionEngine 'exec' ...
    jit_fn_ptr_type fn_ptr = (jit_fn_ptr_type)exec->getPointerToFunction( fn );
    return fn_ptr();
  }

After I call getPointerToFunction() to obtain the pointer to the JIT'd function and fun the function (that will produce an instance of MyClass on the heap), do I still need 'exec'?

Deleting it immediately after use seems to have no adverse effect. I just want to double-check that this is OK.

- Paul

Hi Paul,

I'm surprised to hear that you aren't seeing any adverse effects. As I understand it, the memory for the function pointer returned by getPointerToFunction is owned by the JITMemoryManager which was used in creating the ExecutionEngine. In the case of the legacy JIT engine, the JITMemoryManager is owned by the JITEmitter which in turn is owned by the JIT ExecutionEngine. In the case of the new MCJIT engine, the JITMemoryManager is owned by the MCJITMemoryManager which in turn is owned by the MCJIT ExecutionEngine.

In either case, deleting the ExecutionEngine should result in the JITMemoryManager being deleted and therefore also the memory in which the JITed functions are contained.

I think it's entirely possible that you just aren't seeing a problem because that memory hasn't been recycled yet. It's a problem waiting to happen.

-Andy

Yep, depending on your libc implementation it can still be usable (it
doesn't necessarily unmap pages when you free memory), with undefined
behaviour when you attempt to access it of course.

Amara

I probably should have mentioned that I'm JIT'ing the function, executing it, deleting the ExecutionEngine, and (the part I left out) never executing the JIT'd function again. It's only ever executed once. Given that, I would think it would be OK to delete the ExecutionEngine -- yes?

- Paul

Yes, in that case it's probably OK to delete it. The only other concern that comes to mind is that the ExecutionEngine also takes ownership of the Module object it is compiling, so that will be deleted too. If that's not a problem for you, then it should be OK to delete the ExecutionEngine.

-Andy