<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>On Apr 22, 2013, at 10:06 AM, "Kaylor, Andrew" <<a href="mailto:andrew.kaylor@intel.com">andrew.kaylor@intel.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">The state management in MCJIT is quite sloppy right now.  I agree that invalidating the code cache is an issue that needs to be considered.  It seems to me that MCJIT itself ought to be able to do that when it needs to be done if it were paying attention.  At the very least, it could happen in the 'finalizeObject' method.  That's somewhat tangential to the patch at hand, but it is a consideration.  If there's something quick I can do to MCJIT to make this work, that's probably preferable to pushing something into the C-interface implementation.  I'll give that some thought today, but if anyone else is interested I'd be happy to make it a discussion rather than just a private rumination.</div></blockquote><div><br></div><div>Hopefully we can do this without exposing more stuff via the C API.  I think that finalizeObject() should do this, but I will think about it some more.</div><br><blockquote type="cite"><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br>Otherwise, my main question about the patch has to do with the nature of the C-interface API.  Is that API treated as a contract that needs to be respected from release to release or are we free to tinker with it as needed?  The thing that worries me is how this interface will survive the transition to multiple module support.<br></div></blockquote><div><br></div><div>I believe that this is the goal of the C API, yes.  It is also a goal of this patch to be forward-compatible in this way.</div><div><br></div><div>My patch defends against this in two ways:</div><div><br></div><div>1) When creating the MCJIT via the C API, I just follow the same convention as other ExecutionEngines do: you specify a module, but you can add one later.  Right now calling AddModule on an MCJIT instance will crash, and the documentation tells you this.  Once the MCJIT supports multiple modules, I believe that this should Just Work - you will then be able to call AddModule.</div><div><br></div><div>2) I don't expose finalizeObject() directly.  Instead I created a new API called FinalizeAllObjects(), which requires that all modules associated with the execution engine get finalized at the time of call.  My understanding is that it is safe to finalizeObject() if you've already done it before - it's currently idempotent.  So if MCJIT goes multi-module, then this API will still have a well-defined behavior, and this behavior will not be different from what my patch does.</div><div><br></div><div>-Filip</div><div><br></div><br><blockquote type="cite"><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br>-Andy<br><br>-----Original Message-----<br>From: <a href="mailto:llvm-commits-bounces@cs.uiuc.edu">llvm-commits-bounces@cs.uiuc.edu</a> [<a href="mailto:llvm-commits-bounces@cs.uiuc.edu">mailto:llvm-commits-bounces@cs.uiuc.edu</a>] On Behalf Of David Tweed<br>Sent: Monday, April 22, 2013 7:33 AM<br>To: 'Eric Christopher'; Filip Pizlo<br>Cc: <a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>Subject: RE: [PATCH][llvm-c] Expose MC JIT<br><br>Hi,<br><br>Not a comment on the general idea (which seems like a good one), but asking detail question: some architectures (such as ARM) invalidating cache entries at more times than, eg, x86. (IIRC ARM needs to invalidate the cache at the transition time between being "data" and "instructions".) It looks to me like this can be done in LLVMFinalizeAllObjects(), but I'm cc:ing someone much, much more knowledgeable about this than me... It's certainly desirable that the API provides enough points at which the MC JIT is in command that it can call all the (possibly platform specific) permissions/cache invalidation operations needed on that memory without user code needing to do it.<br><br>Other than that, the patch looks like a good patch to me (but again Andrew is the main authority).<br><br>Cheers,<br>Dave<br><br><br>-----Original Message-----<br>From: <a href="mailto:llvm-commits-bounces@cs.uiuc.edu">llvm-commits-bounces@cs.uiuc.edu</a><br>[<a href="mailto:llvm-commits-bounces@cs.uiuc.edu">mailto:llvm-commits-bounces@cs.uiuc.edu</a>] On Behalf Of Eric Christopher<br>Sent: 22 April 2013 14:22<br>To: Filip Pizlo<br>Cc: <a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>Subject: Re: [PATCH][llvm-c] Expose MC JIT<br><br>The API and rationale seem totally reasonable to me. Up to Andy to approve though.<br><br>-eric<br><br>On Sun, Apr 21, 2013 at 7:14 PM, Filip Pizlo <<a href="mailto:fpizlo@apple.com">fpizlo@apple.com</a>> wrote:<br><blockquote type="cite">OK - I have a new patch for review, which incorporates Andrew's feedback.<br><br>The patch exposes the MCJIT via the C API.<br><br>The current API uses the SectionMemoryManager by default.  I plan to<br></blockquote>expose<br><blockquote type="cite">the ability to have the C API call back into the client's code, and<span class="Apple-converted-space"> </span><br>use<br></blockquote>the<br><blockquote type="cite">client's own memory manager, in the future.  But even then, the<span class="Apple-converted-space"> </span><br>default<br></blockquote>will<br><blockquote type="cite">be SectionMemoryManager.  Because this requires calling<br></blockquote>applyPermissions(),<br><blockquote type="cite">I also expose the ExecutionEngine::finalizeObject() method.  This was<br></blockquote>tricky<br><blockquote type="cite">- I take it that in the future, this method will take a Module*M<span class="Apple-converted-space"> </span><br>parameter to specify which module to finalize.  In order to not create<span class="Apple-converted-space"> </span><br>future confusion in the C API, I expose this as<span class="Apple-converted-space"> </span><br>LLVMFinalizeAllObjects() and specify the API's semantics as being that<span class="Apple-converted-space"> </span><br>all objects associated with the execution engine should be finalized.<br><br>The patch also exposes the NoFramePointerElim option.  The manner in<span class="Apple-converted-space"> </span><br>which options are exposed is designed for forward compatibility; you<span class="Apple-converted-space"> </span><br>supply an options struct along with a size which you zero-fill prior<span class="Apple-converted-space"> </span><br>to<br></blockquote>manipulating.<br><blockquote type="cite">This is similar to the idiom I've seen used in other C APIs like<br></blockquote>BerkeleyDB.<br><blockquote type="cite">I considered having separate C function calls for each option, in the<br></blockquote>style<br><blockquote type="cite">of the ExecutionEngineBuilder API - but while that idiom feels right<span class="Apple-converted-space"> </span><br>to me in C++, it feels less C-like.  As well, the current options<span class="Apple-converted-space"> </span><br>approach<br></blockquote>exposes<br><blockquote type="cite">not just parts of the Builder but also part of TargetOptions (namely,<span class="Apple-converted-space"> </span><br>NoFramePointerElim).  It's also more concise in practice.<br><br>I plan to expose more innards through the LLVMMCJITCompilerOptions in<span class="Apple-converted-space"> </span><br>the future.  I'd be happy to do more of that in one go if that was<span class="Apple-converted-space"> </span><br>preferred; but I thought that a baby step would be the best thing for now.<br><br><br><br>-Filip<br><br><br>On Apr 21, 2013, at 6:26 PM, Filip Pizlo <<a href="mailto:fpizlo@apple.com">fpizlo@apple.com</a>> wrote:<br><br><br>On Apr 15, 2013, at 10:42 AM, "Kaylor, Andrew"<span class="Apple-converted-space"> </span><br><<a href="mailto:andrew.kaylor@intel.com">andrew.kaylor@intel.com</a>><br>wrote:<br><br>OK, let me start by saying that MCJIT does take ownership of the<span class="Apple-converted-space"> </span><br>memory manager.  It doesn't use an OwningPtr, which would make this<span class="Apple-converted-space"> </span><br>clear, but it does delete the pointer in its destructor.  I think this<span class="Apple-converted-space"> </span><br>is happening because we needed some finer control over when the MM got<span class="Apple-converted-space"> </span><br>deleted.  I<br></blockquote>should<br><blockquote type="cite">probably revisit this and at least add some comments to make it clear<br></blockquote>what's<br><blockquote type="cite">happening and why.  It might not even be an issue anymore, because I<span class="Apple-converted-space"> </span><br>did some work a while ago to try to clean up object ownership issues.<br><br><br>Actually, I was just confused.  MCJIT deletes MemMgr, which is always<span class="Apple-converted-space"> </span><br>aliased to Dyld, as far as I can tell.  So I was just wrong. :-)<br><br><br>That said, I have been meaning for some time to break apart the JIT<span class="Apple-converted-space"> </span><br>and MCJIT interfaces.  The fact that they are both abstracted by<br></blockquote>ExecutionEngine<br><blockquote type="cite">and EngineBuilder complicates that, but it really needs to be done (as<span class="Apple-converted-space"> </span><br>you are seeing).<br><br>For now, would it be possible to have the C-interface provide a<span class="Apple-converted-space"> </span><br>wrapper<br></blockquote>that<br><blockquote type="cite">supplies empty implementations of the irrelevant functions when<span class="Apple-converted-space"> </span><br>creating a memory manager for MCJIT?<br><br><br>I think this is sensible.  I will proceed in this way.<br><br>Thanks!<br><br><br>-Andy<br><br>From: Filip Pizlo [<a href="mailto:fpizlo@apple.com">mailto:fpizlo@apple.com</a>]<br>Sent: Saturday, April 13, 2013 1:18 AM<br>To: Kaylor, Andrew<br>Cc: <a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>Subject: Re: [PATCH][llvm-c] Expose MC JIT<br><br>Ah - good thing you pointed this out.  I just realized that my patch<span class="Apple-converted-space"> </span><br>is wrong.  Perhaps I can get some feedback on the best way to architect this.<br><br>Here's the problem:<br><br>- MCJIT does not take ownership of the memory manager.  Hence<span class="Apple-converted-space"> </span><br>allocating<br></blockquote>one<br><blockquote type="cite">in the constructor is wrong; it'll leak when MCJIT dies.  But deleting<span class="Apple-converted-space"> </span><br>the memory manager passed to MCJIT would be a change in behavior, and<span class="Apple-converted-space"> </span><br>I'm not sure if it's in line with either what existing users expect or<span class="Apple-converted-space"> </span><br>what was intended.  Insofar as the JIT instance corresponds to<span class="Apple-converted-space"> </span><br>ownership of<br></blockquote>modules,<br><blockquote type="cite">it feels like it shouldn't also take ownership of the memory manager;<span class="Apple-converted-space"> </span><br>for example you might imagine wanting to throw away the MCJIT but keep<span class="Apple-converted-space"> </span><br>the<br></blockquote>code<br><blockquote type="cite">it generated and continue to use the memory manager to track it - and<span class="Apple-converted-space"> </span><br>eventually free it.  But EngineBuilder currently claims that the<span class="Apple-converted-space"> </span><br>ExecutionEngine takes ownership of the JMM - I'm assuming that this is<br></blockquote>just<br><blockquote type="cite">wrong documentation, and that EngineBuilder's use of the same JMM<span class="Apple-converted-space"> </span><br>option<br></blockquote>for<br><blockquote type="cite">both JIT and MCJIT is just not right.<br><br>- I'd like to expose SectionMemoryManager and, eventually in a<span class="Apple-converted-space"> </span><br>separate patch, the ability to create custom RTDyldMemoryManagers via the C API.<br></blockquote>I'd<br><blockquote type="cite">prefer this to be an RTDyldMemoryManager and not a JITMemoryManager,<span class="Apple-converted-space"> </span><br>since the latter has a load of methods that are not relevant to MCJIT.  <br>But EngineBuilder wants a JITMemoryManager.  This would mean that the<span class="Apple-converted-space"> </span><br>C API would have to pass its RTDyldMemoryManager via a cast to<span class="Apple-converted-space"> </span><br>JITMemoryManager just so MCJIT could then use it as an<span class="Apple-converted-space"> </span><br>RTDyldMemoryManager again.  Seems wrong.  I'm assuming that the<span class="Apple-converted-space"> </span><br>correct long-term thing is to fix the EngineBuilder to not pass the<span class="Apple-converted-space"> </span><br>JMM to the MCJIT, since it's good to expose the fact that the MCJIT<span class="Apple-converted-space"> </span><br>actually just wants an RTDyldMemoryManager<br></blockquote>instead.<br><blockquote type="cite"><br>In short, I'd like to have a separate EngineBuilder setting for the<span class="Apple-converted-space"> </span><br>RTDyldMemoryManager.  If this is specified and you end up using the<span class="Apple-converted-space"> </span><br>JIT<br></blockquote>and<br><blockquote type="cite">not MCJIT, you get an error.  If you use the MCJIT, then the<span class="Apple-converted-space"> </span><br>RTDyldMemoryManager option overrides the JMM option.  Or something<br></blockquote>similar.<br><blockquote type="cite"><br>Does that make sense?<br><br>-Filip<br><br><br>On Apr 12, 2013, at 5:38 PM, Filip Pizlo <<a href="mailto:fpizlo@apple.com">fpizlo@apple.com</a>> wrote:<br><br><br>Thanks for the feedback!  I will try this change and see what happens.<br><br>-Filip<br><br><br>On Apr 12, 2013, at 5:35 PM, "Kaylor, Andrew"<span class="Apple-converted-space"> </span><br><<a href="mailto:andrew.kaylor@intel.com">andrew.kaylor@intel.com</a>><br>wrote:<br><br>Hi Filip,<br><br>I'll take a closer look at your patches on Monday, but my initial<span class="Apple-converted-space"> </span><br>input is that the default memory manager used should be<span class="Apple-converted-space"> </span><br>SectionMemoryManager rather than the DefaultJITMemoryManager.<br><br>Thanks,<br>Andy<br><br>From: <a href="mailto:llvm-commits-bounces@cs.uiuc.edu">llvm-commits-bounces@cs.uiuc.edu</a><span class="Apple-converted-space"> </span><br>[<a href="mailto:llvm-commits-bounces@cs.uiuc.edu">mailto:llvm-commits-bounces@cs.uiuc.edu</a>] On Behalf Of Filip Pizlo<br>Sent: Friday, April 12, 2013 4:49 PM<br>To: <a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>Subject: Re: [PATCH][llvm-c] Expose MC JIT<br><br>Revised patches included.<br><br>I added additional ruggedizing to the LLVMCreateMCJITCompilerForModule<span class="Apple-converted-space"> </span><br>function, so that if it detects that the passed struct is larger than<span class="Apple-converted-space"> </span><br>expected, it reports an error instead of continuing.<br><br><br>_______________________________________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits<br><br><br>_______________________________________________<br>llvm-commits mailing list<br>llvm-commits@cs.uiuc.edu<br>http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits<br><br><br><br>_______________________________________________<br>llvm-commits mailing list<br>llvm-commits@cs.uiuc.edu<br>http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits<br><br></blockquote><br>_______________________________________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits<br><br><br><br><br><br>_______________________________________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a></div></blockquote></div><br></body></html>