<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
span.apple-converted-space
        {mso-style-name:apple-converted-space;}
span.EmailStyle21
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.EmailStyle22
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:1866863493;
        mso-list-type:hybrid;
        mso-list-template-ids:2042254460 67698703 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1
        {mso-list-id:2042976100;
        mso-list-type:hybrid;
        mso-list-template-ids:1082424586 67698703 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l1:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0in;}
ul
        {margin-bottom:0in;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">I wasn’t sure the C API needed to support the case of JITing into another address space.  I guess it makes sense that we’d want to make that available eventually.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">I do think it would be better to have MCJIT finalize the object from getPointerToFunction(), but as I said we can’t do that until there’s another way to trigger
 code generation.  This is part of the sloppiness I referred to earlier.  There is a web of interactions going on within MCJIT and there are some constraints on the order in which things can be done.  Right now MCJIT has a patchwork way of handling this, but
 it really needs to be cleaned up to manage things in a more intentional way.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Anyway, my immediate goal here is to figure out the simplest reasonable thing we can do to get your patch working in a sustainable way.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">I don’t know if anything other than lli is relying on the current side-effects of MCJIT::getPointerToFunction().  I’d prefer to hold off on putting new things
 in the MCJIT interface until the aforementioned clean-up is done.  I was suggesting putting the call to finalizeObject in the C API because I’d rather rush something into implementation than rush something into an interface, but depending on your timeline
 for making the C API I could try to bump the priority of the MCJIT state handling clean-up on my end.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">-Andy<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> Filip Pizlo [mailto:fpizlo@apple.com]
<br>
<b>Sent:</b> Monday, April 22, 2013 11:25 AM<br>
<b>To:</b> Kaylor, Andrew<br>
<b>Cc:</b> David Tweed; Eric Christopher; llvm-commits@cs.uiuc.edu<br>
<b>Subject:</b> Re: [PATCH][llvm-c] Expose MC JIT<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">Wouldn't it be better to just have MCJIT call finalizeObject in getPointerToFunction and friends, and eradicate ExecutionEngine::finalizeObject() completely?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">OTOH, if I understand right and the goal of the MCJIT is to support the notion of JITing into someone else's address space, then finalizeObject() makes loads of sense: you want the client to be able to say when it happens. If that's true
 then it should be exposed to the C API. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><br>
-Filip<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
On Apr 22, 2013, at 10:57 AM, "Kaylor, Andrew" <<a href="mailto:andrew.kaylor@intel.com">andrew.kaylor@intel.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">It definitely won’t be necessary to expose more things through the C API.  At most, things will need to be added to the implementation.</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">I’m wondering if we can even avoid having to put the ‘finalize’ concept into the API.</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">As I understand it, the normal work flow would be something like this:</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l0 level1 lfo2"><![if !supportLists]><span style="mso-list:Ignore">1.<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Create a module and populate it</span><o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l0 level1 lfo2"><![if !supportLists]><span style="mso-list:Ignore">2.<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Create an execution engine for the module</span><o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l0 level1 lfo2"><![if !supportLists]><span style="mso-list:Ignore">3.<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Get a pointer to a function in the module</span><o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l0 level1 lfo2"><![if !supportLists]><span style="mso-list:Ignore">4.<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Execute the function</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">If that’s right, I guess MCJIT trips because while its implementation of getPointerTo[Named]Function triggers compilation it doesn’t cause permissions to be
 applied or invalidate the code cache.  This happens because MCJIT needs to handle the case where the generated code is going to be executed in another process (and possibly on another system), so it doesn’t make assumptions about when everything is in its
 final place.  The C API, however, could arguably make such assumptions.</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">What this boils down to is that somewhere between step 2 above and step 4 above we need to:</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l1 level1 lfo4"><![if !supportLists]><span style="mso-list:Ignore">1.<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Generate the code</span><o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l1 level1 lfo4"><![if !supportLists]><span style="mso-list:Ignore">2.<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Apply relocations</span><o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l1 level1 lfo4"><![if !supportLists]><span style="mso-list:Ignore">3.<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Apply memory permissions</span><o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l1 level1 lfo4"><![if !supportLists]><span style="mso-list:Ignore">4.<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Invalidate the code cache</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">MCJIT does 1 and 2, if necessary, in response to a getPointerToFunction() call.  Arguably it could also do 3 and 4 there, since in the remote case the client
 code isn’t going to want pointers to functions.  The trouble is that there are places (such as lli) where we are using that call to trigger code emission even though we may still want to move things around before 3 and 4 happen.  That wouldn’t be a problem
 if we exposed a function to trigger code emission directly, but we don’t right now.</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">However, it may be reasonable for the C API implementation to call MCJIT::finalizeObject when its getPointerToFunction equivalent is called.</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">That leaves us with invalidating the code cache.  I don’t see any reason that the memory manager shouldn’t do that automatically when the applyPermissions function
 is called.  I notice that currently invalidateInstructionCache is part of the SectionMemoryManager interface but not the RuntimeDyldMemoryManager, so that’s a problem already if we don’t just make it part of applyPermissions().</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">So what I’m thinking is that if you can add a call to MCJIT::finalizeObject in the appropriate places in the C API implementation then the FinalizeAllObjects
 method can be removed completely.  I’ll add an invalidateInstructionCache() call in the SectionMemoryManager::applyPermissions() implementation, and that should take care of the ARM issue.</span><o:p></o:p></p>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Sounds good. <o:p></o:p></p>
</div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Does that sound reasonable?</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">-Andy</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> Filip Pizlo [<a href="mailto:fpizlo@apple.com">mailto:fpizlo@apple.com</a>]
<br>
<b>Sent:</b> Monday, April 22, 2013 10:19 AM<br>
<b>To:</b> Kaylor, Andrew<br>
<b>Cc:</b> David Tweed; Eric Christopher; <a href="mailto:llvm-commits@cs.uiuc.edu">
llvm-commits@cs.uiuc.edu</a><br>
<b>Subject:</b> Re: [PATCH][llvm-c] Expose MC JIT</span><o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<div>
<p class="MsoNormal">On Apr 22, 2013, at 10:06 AM, "Kaylor, Andrew" <<a href="mailto:andrew.kaylor@intel.com">andrew.kaylor@intel.com</a>> wrote:<o:p></o:p></p>
</div>
<p class="MsoNormal"><br>
<br>
<br>
<o:p></o:p></p>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<p class="MsoNormal"><br>
<br>
<br>
<o:p></o:p></p>
<div>
<p class="MsoNormal"><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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">My patch defends against this in two ways:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">-Filip<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<p class="MsoNormal"><br>
<br>
<br>
<o:p></o:p></p>
<div>
<p class="MsoNormal"><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>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">expose<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">the ability to have the C API call back into the client's code, and<span class="apple-converted-space"> </span><br>
use<o:p></o:p></p>
<p class="MsoNormal">the<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">client's own memory manager, in the future.  But even then, the<span class="apple-converted-space"> </span><br>
default<o:p></o:p></p>
<p class="MsoNormal">will<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">be SectionMemoryManager.  Because this requires calling<o:p></o:p></p>
<p class="MsoNormal">applyPermissions(),<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">I also expose the ExecutionEngine::finalizeObject() method.  This was<o:p></o:p></p>
<p class="MsoNormal">tricky<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">- 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<o:p></o:p></p>
<p class="MsoNormal">manipulating.<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">This is similar to the idiom I've seen used in other C APIs like<o:p></o:p></p>
<p class="MsoNormal">BerkeleyDB.<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">I considered having separate C function calls for each option, in the<o:p></o:p></p>
<p class="MsoNormal">style<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">exposes<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">should<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">probably revisit this and at least add some comments to make it clear<o:p></o:p></p>
<p class="MsoNormal">what's<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">ExecutionEngine<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">that<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">one<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">modules,<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">code<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">just<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">wrong documentation, and that EngineBuilder's use of the same JMM<span class="apple-converted-space"> </span><br>
option<o:p></o:p></p>
<p class="MsoNormal">for<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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.<o:p></o:p></p>
<p class="MsoNormal">I'd<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">instead.<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal"><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<o:p></o:p></p>
<p class="MsoNormal">and<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal">similar.<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt"><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>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><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><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><o:p></o:p></p>
<p class="MsoNormal"><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><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><o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
</body>
</html>