<div dir="rtl"><div dir="ltr">Hi Andy,</div><div dir="ltr"><br></div><div dir="ltr">Everything you write is correct. This is not enough. It's part of the API required a complete solution.</div><div dir="ltr"><br></div>

<div dir="ltr">If function replacement is not possible for a compiled module, given this API and a relink function, a solution could be a smart function replacer:</div><div dir="ltr"><br></div><div dir="ltr">If the function signature is the same, the usual case, hide/replace the old function, recompile it and relink all callers to the new function.</div>

<div dir="ltr"><br></div><div dir="ltr">If the function signature changed, hide/replace/recompile the function and its callers (possibly in a new module hiding the old copies) and relink the callers-callers to the new callers.<br>

</div><div dir="ltr"><br></div><div dir="ltr">This does leak memory of the hidden compiled functions we're replacing.</div><div dir="ltr"><br></div><div dir="ltr">If we can mange this I think that the transition from the old JIT would be much easier.<br>

</div><div dir="ltr"><br></div><div dir="ltr">Yaron<br></div><div dir="ltr"><br></div><div dir="ltr"><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div dir="ltr">2013/10/18 Kaylor, Andrew <span dir="ltr"><<a href="mailto:andrew.kaylor@intel.com" target="_blank">andrew.kaylor@intel.com</a>></span></div>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">This is a potentially tricky case.  Let me give you a hypothetical scenario:<u></u><u></u></span></p>


<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Module A contains a function FA.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Modules B and C contain functions (FB and FC) that call FA.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Module D contains a new definition of FA.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Now suppose I add modules A, B and C to MCJIT and make a call to FB.  This will generate code for modules A and B and link module B to module A’s definition
 of FA.  Then suppose I call your new function to clear the FA symbol and add module D to MCJIT.  Now if I call FC, MCJIT will generate code for modules C and D and link FC to module D’s definition of FA.  However, module B will still be linked to module A’s
 definition of FA.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">To complicate things even further, if there were some unrelated function in module B that required another unrelated function in module C then FC will be bound
 to module A’s FA when FB is called, though that might not be obvious to the user.  Even worse, if FC calls FB and FA then it would be possible to have FC linked to module A’s FA through FB and to module D’s FA through its direct call.<u></u><u></u></span></p>


<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">That’s not to say that we don’t need to address your use case.  I just don’t think the simple solution is enough.<u></u><u></u></span></p>


<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">-Andy<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<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""> Yaron Keren [mailto:<a href="mailto:yaron.keren@gmail.com" target="_blank">yaron.keren@gmail.com</a>]
<br>
<b>Sent:</b> Friday, October 18, 2013 4:39 AM<br>
<b>To:</b> <a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a>; Kaylor, Andrew<br>
<b>Subject:</b> [PATCH] MCJIT::clearSymbolAddress<u></u><u></u></span></p><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<p class="MsoNormal">The use-case is replacing a named function before creating a same-named function in a new module, such as in REPL.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
<div>
<p class="MsoNormal">With the older JIT a function could be removed from the symbol table (and memory freed) by calling freeMachineCodeForFunction. <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">The new MCJIT does not implement this function - may be impossible to modify the object - so the next best thing is to remove the symbol from the symbol table so the older copy will not be found anymore and then a new function could be
 created.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Since MCJIT uses GlobalSymbolTable in RuntimeDyldImpl.h rather than the GlobalAddressMap in ExecutionEngineState (could EEState be reused by MCJIT somehow?) the code is MCJIT specific.<u></u><u></u></p>


</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">To expose it to the programmer the function appears in EE as well with a comment and the call chain is<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">EE->MCJIT->RuntimeDyld->RuntimeDyldImpl->GlobalSymbolTable.erase().</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Yaron<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div></div></div>
</div>

</blockquote></div><br></div>