<div dir="ltr">Hi Christian,<div><br></div><div>ORC doesn't have any locks internally at the moment. Approach (1) is the recommended solution. I'm working on a refactor that should be out in a few weeks that will improve threading support, and I expect to put more effort into multi-threaded performance in the next few months.</div><div><br></div><div>Out of interest, are you saying that approach (1) was a regression compared to MCJIT's behavior?</div><div><br></div><div>Cheers,</div><div>Lang.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 6, 2017 at 12:28 PM, Christian Schafmeister via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto" style="word-wrap:break-word"><div style="word-wrap:break-word"><div><br></div><div>How would I go about enabling the ORC JIT to compile code in multiple threads?</div><div><br></div><div>Some background:</div><div>I’ve switched Clasp (an implementation of Common Lisp that uses llvm as the backend - <a href="http://github.com/drmeister/clasp" target="_blank">github.com/drmeister/clasp</a>) over to using ORC as its JIT.  I did this by following the Kaleidoscope tutorial.    I have a ClaspJIT_O class that copies the KaleidoscopeJIT class.</div><div><a href="https://github.com/drmeister/clasp/blob/analyze/include/clasp/llvmo/llvmoExpose.h#L4370" target="_blank">https://github.com/drmeister/<wbr>clasp/blob/analyze/include/<wbr>clasp/llvmo/llvmoExpose.h#<wbr>L4370</a></div><div><br></div><div>Clasp is multithreaded and it needs to compile code in multiple threads because it uses the JIT to generate dispatch functions for multiple dispatch/generic functions.  To make this possible, every thread gets its own LLVMContext and every type and llvm::Module that is linked into JITted code in each thread is initialized lazily and thread-locally.   Despite this - I experience frequent, random crashes when I try to use the ORC JIT from multiple threads.  </div><div><br></div><div>Here’s what I’ve tried:</div><div><br></div><div>(1) This works:   wrap a lock/mutex around access to one ClaspJIT_O object, the calls to ClaspJIT_O::addModule and a call to jitFinalizeReplFunction are protected by the lock:</div><div><a href="https://github.com/drmeister/clasp/blob/dev/src/llvmo/llvmoExpose.cc#L3999" target="_blank">https://github.com/drmeister/<wbr>clasp/blob/dev/src/llvmo/<wbr>llvmoExpose.cc#L3999</a></div><div><br></div><div>The Common Lisp code that does the lock and calls these functions:</div><div><a href="https://github.com/drmeister/clasp/blob/analyze/src/lisp/kernel/cmp/jit-setup.lsp#L598" target="_blank">https://github.com/drmeister/<wbr>clasp/blob/analyze/src/lisp/<wbr>kernel/cmp/jit-setup.lsp#L598</a></div><div><br></div><div>This throttles the system and limits one thread at a time to add modules to the JIT and lookup symbols in the JIT.  It’s not bad - I can live with it.</div><div><br></div><div>(2) This fails:  Keep a thread local copy of a ClaspJIT_O object that is lazily initialized as soon as any compilation happens in a thread.</div><div><br></div><div>(3) This fails: Keep a thread local copy of a ClaspJIT_O object that is initialized as in #2 AND wrap a lock/mutex around ClaspJIT_O::addModule and a call to jitFinalizeReplFunction.   What I thought I was testing here was if there was some global resource that ORC uses and despite having multiple thread-local ClaspJIT_O objects the different threads were trampling that common global resource.</div><div><br></div><div>I can provide many more details on request.</div><div><br></div><div>Christian Schafmeister</div><div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Professor, Chemistry</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Temple University</div>
</div>
<br></div></div><br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>