<div dir="ltr">Hi Mo,<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">I’m new to JIT and just started experiencing on some example code with ORCv2, but could not get my head around with the thread safety.</blockquote><div><br></div><div>The short version is that ThreadSafeContext is a pair of an LLVMContext and a mutex, and that mutex is locked whenever you call ThreadSafeModule::withModuleDo on a module using that ThreadSafeContext.</div><div><br></div><div>That means that two Modules that share a Context can't be accessed simultaneously (when wrapped in and accessed through ThreadSafeModule and ThreadSafeContext).</div><div><div><br></div><div>In this system you rarely have to do anything explicitly. The idea is that you access the module via withModuleDo and that takes care of the locking for you.</div><br class="gmail-Apple-interchange-newline"></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">Scenario 1, Application has multiple threads and each thread spawns its own JIT instance, within each JIT instance, there are multiple ThreadSafeModules share one ThreadSafeContext<br>Do I have to lock TSM/ TSC?</blockquote><div><br></div><div>You should always use withModuleDo to access the module, regardless of your setup. This prevents you from writing non-thread-safe code that might be difficult to debug if you add threading in later. What's important here is knowing the locking implications of the access pattern, since they'll affect how much parallelism you can get out of your JIT.</div><div><br></div><div>In your Scenario 1 each thread accesses a single LLVMContext. There are no LLVMContexts shared between threads, so no chance for lock contention. That's good.</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"> Scenario 2, If JIT itself has multiple compilation threads, like via setting NumCompileThreads in LLJIT classes, and each ThreadSafeModule has its own ThreadSafeContext, so context is not shared</blockquote><div><br></div><div>In this scenario each JIT has multiple threads, but each thread is operating on a single module with its own LLVMContext. Again there is no chance for lock contention, so no barrier to parallelism.</div><div><br></div><div>The only scenario where locking becomes relevant is when you have multiple threads and a single ThreadSafeContext shared between two or more ThreadSafeModules. Consider a JIT with 10 threads and 10 llvm::Modules all sharing a single ThreadSafeContext. If you issue a lookup that triggers materialization of all 10 modules simultaneously then they'll be sent to each of your 10 worker threads to be compiled, but as soon as the 1st worker thread takes the context lock the other 9 threads will be blocked from progressing. Eventually the 1st worker thread will relinquish the lock and the next worker thread will take it, preventing the other 8 threads from progressing, and so on. You have 10 threads, but only one thread at a time can actually do any work -- the single LLVMContext has serialized compilation.</div><div><br></div><div>My recommendation is to always start with one context per module, and only share contexts if you see memory consumption from the contexts becoming an issue.</div><div><br></div><div>-- Lang.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jul 8, 2021 at 12:13 AM mo xiaoming via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</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 all,<br>
<br>
I’m new to JIT and just started experiencing on some example code with ORCv2, but could not get my head around with the thread safety.<br>
<br>
Scenario 1, Application has multiple threads and each thread spawns its own JIT instance, within each JIT instance, there are multiple ThreadSafeModules share one ThreadSafeContext<br>
<br>
Do I have to lock TSM/ TSC?<br>
<br>
Scenario 2, If JIT itself has multiple compilation threads, like via setting NumCompileThreads in LLJIT classes, and each ThreadSafeModule has its own ThreadSafeContext, so context is not shared<br>
<br>
In this case, do I have to lock? If so, what’s the granularity of each lock should be, anything before IR generation in one locking?<br>
<br>
<br>
I think in scenario 1, locking is not needed because of no concurrent access. And I believe locking is a mandatory in scenario 2, but I don’t know why...<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>