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 -----Original Message----- From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of Paul J. Lucas Sent: Tuesday, October 23, 2012 11:31 AM To: llvmdev at cs.uiuc.edu Subject: [LLVMdev] 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 _______________________________________________ LLVM Developers mailing list LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
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 On 26 October 2012 20:59, Kaylor, Andrew <andrew.kaylor at intel.com> wrote:> 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 > > > -----Original Message----- > From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of Paul J. Lucas > Sent: Tuesday, October 23, 2012 11:31 AM > To: llvmdev at cs.uiuc.edu > Subject: [LLVMdev] 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 > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
On Oct 26, 2012, at 12:59 PM, "Kaylor, Andrew" <andrew.kaylor at intel.com> wrote:> 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.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