<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hi Dibyendu,</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Yes indeed that is what happened. So to prevent that I need to check<br>if VModuleKey was ever initialized ... how do I do that? I would have<br>to have to add another flag to track the initialized state.<br>The reason for it not being initialized is that sometimes I cannot<br>generate JIT code because some bytecode is not yet supported.</blockquote><div><br></div><div>I'm not sure I follow. In ORCv1 VModuleKeys are just integers: initialization is up to you, and the standard way to get a unique key is to always initialize keys by calling ExecutionSession::allocateVModuleKey(). The intent is that you should use a unique key for each module that you add, and keep a copy of the key if it is associated with a module if you want to be able to remove that module later. E.g.</div><div><br></div><div>// Module that I will never want to remove:</div><div>CODLayer.addModule(ES.createVModuleKey(), std::move(M)); // unique throwaway key.</div><div><br></div><div>// Module that I want to be able to remove later:</div><div>auto TemporaryModuleKey = ES.createVModuleKey();<br></div><div>CODLayer.addModule(TemporaryModuleKey, std::move(M));</div><div>// Do stuff.</div><div>CODLayer.removeModule(TemporaryModuleKey);<br></div><div><br></div><div>The main use cases of removable modules that I'm aware of are: (1) temporary expressions in REPLs, and (2) lower-optimized modules in re-optimizing JITs. In the former case you can usually name the key individually (as above). In the latter case you usually want to stash it in a map or context object somewhere and remove it after you've replaced the low-optimization-level code with a higher optimization level.</div><div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">> There should not be any check other than the assertion. Assertions aren't for recoverable errors or logging, they're only for verifying that code is being used according to contract. In this case the contract is that removeModule is only called with valid keys. If the assertion is triggering then either the key you're using is invalid, or my implementation of CompileOnDemandLayer (or the assert itself) is invalid. We just need to fix the offending code.<br>><br>Sure - in that case there ought be some value of VModuleKey that is<br>invalid. Example NULL for pointers. Otherwise users have to track<br>separately whether it is initialized or not.<br></blockquote><div><br></div><div><div>In ORCv1 the solution is to always use ES.createVModuleKey(). Users should only have to keep track keys that they want to be able to remove.</div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Having said that just as free(NULL) doesn't do anything, and<br>fclose(NULL) doesn't either, it seems better to not continue to try to<br>access 'I' if it doesn't exist. Or the assertion should trigger even<br>in release builds.</blockquote></div><div><br></div><div>I don't have strong opinions about this idiom when applied to free or fclose, but I definitely wouldn't want it to apply here. What is a programmer who calls "removeModule(<invalid key>)" trying to do? Whatever it is, it's definitely a mistake. The contract is: Only call removeModule with a valid key that is associated with a module that you want to remove. Tracking that is definitely up to the client, but they'd have to do that anyway, otherwise how would you know what needed removing?</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Ideally it should be possible to delete modules after the code is compiled.<br>And also delete compiled functions if they are no longer needed.<br>In my case, the language is Lua based, functions are garbage<br>collected. So any associated stuff should be deleted when the function<br>is collected. Sometimes each function is in its own module, but<br>sometimes several functions are in one module. Either way when the<br>last function in a module is deleted, I invoke removeModule().</blockquote></div><div><br></div><div>Yep. That makes perfect sense. You just want to create a unique key for each module and track it in your garbage collection context somewhere.</div><div><br></div><div>-- Lang.</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 13, 2019 at 2:33 PM Dibyendu Majumdar <<a href="mailto:mobile@majumdar.org.uk">mobile@majumdar.org.uk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Hi Lang,<br>
<br>
On Tue, 13 Aug 2019 at 22:15, Lang Hames <<a href="mailto:lhames@gmail.com" target="_blank">lhames@gmail.com</a>> wrote:<br>
><br>
>> 1) Can 0 ever be a valid VModuleKey? How can one reliably detect an invalid VModuleKey?<br>
><br>
><br>
> I believe 0 was a valid VModuleKey in ORCv1. The assertion is checking the the VModuleKey is present in the LogicalDylibs map. That means that you have to have used that key in an addModule call, e.g.:<br>
><br>
> CODLayer.addModule(K, std::move(M));<br>
><br>
> and not subsequently removed the key with a call to removeModule.<br>
<br>
Yes indeed that is what happened. So to prevent that I need to check<br>
if VModuleKey was ever initialized ... how do I do that? I would have<br>
to have to add another flag to track the initialized state.<br>
The reason for it not being initialized is that sometimes I cannot<br>
generate JIT code because some bytecode is not yet supported.<br>
<br>
><br>
> 2) Secondly it seems to me that  following the assertion there should be a check so that the code dosn't continue? It is causing segmentation fault in release builds.<br>
><br>
> There should not be any check other than the assertion. Assertions aren't for recoverable errors or logging, they're only for verifying that code is being used according to contract. In this case the contract is that removeModule is only called with valid keys. If the assertion is triggering then either the key you're using is invalid, or my implementation of CompileOnDemandLayer (or the assert itself) is invalid. We just need to fix the offending code.<br>
><br>
<br>
Sure - in that case there ought be some value of VModuleKey that is<br>
invalid. Example NULL for pointers. Otherwise users have to track<br>
separately whether it is initialized or not.<br>
Having said that just as free(NULL) doesn't do anything, and<br>
fclose(NULL) doesn't either, it seems better to not continue to try to<br>
access 'I' if it doesn't exist. Or the assertion should trigger even<br>
in release builds.<br>
<br>
> Out of interest: What are your plans for removeModule? It's currently unimplemented in ORCv2. Understanding people's use cases will help with design and prioritization.<br>
><br>
<br>
Ideally it should be possible to delete modules after the code is compiled.<br>
And also delete compiled functions if they are no longer needed.<br>
In my case, the language is Lua based, functions are garbage<br>
collected. So any associated stuff should be deleted when the function<br>
is collected. Sometimes each function is in its own module, but<br>
sometimes several functions are in one module. Either way when the<br>
last function in a module is deleted, I invoke removeModule().<br>
<br>
I think in ORCv2 stuff never gets deleted?<br>
<br>
Regards<br>
Dibyendu<br>
<br>
> On Tue, Aug 13, 2019 at 1:50 PM Dibyendu Majumdar via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>><br>
>> Hi,<br>
>><br>
>> I am getting following assertion failure when attempting to remove a module.<br>
>><br>
>> /llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h:311: llvm::Error<br>
>> llvm::orc::LegacyCompileOnDemandLayer<BaseLayerT, CompileCallbackMgrT,<br>
>> IndirectStubsMgrT>::removeModule(llvm::orc::VModuleKey) [with<br>
>> BaseLayerT = llvm::orc::LegacyIRTransformLayer<llvm::orc::LegacyIRCompileLayer<llvm::orc::LegacyRTDyldObjectLinkingLayer,<br>
>> llvm::orc::SimpleCompiler>,<br>
>> std::function<std::unique_ptr<llvm::Module>(std::unique_ptr<llvm::Module>)><br>
>> >; CompileCallbackMgrT = llvm::orc::JITCompileCallbackManager;<br>
>> IndirectStubsMgrT = llvm::orc::IndirectStubsManager;<br>
>> llvm::orc::VModuleKey = long unsigned int]: Assertion `I !=<br>
>> LogicalDylibs.end() && "VModuleKey K not valid here"' failed.<br>
>><br>
>> 1) Can 0 ever be a valid VModuleKey? How can one reliably detect an<br>
>> invalid VModuleKey?<br>
>> 2) Secondly it seems to me that  following the assertion there should<br>
>> be a check so that the code dosn't continue? It is causing<br>
>> segmentation fault in release builds.<br>
>><br>
>> Regards<br>
>> Dibyendu<br>
>> _______________________________________________<br>
>> LLVM Developers mailing list<br>
>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div></div></div></div>