<div dir="ltr"><div>Hi, Valentin!</div><div><br></div><div>Thank you for your reply!</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Have you tried running this with assertions on? </blockquote><div>Yes.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Also my `C` knowledge isn't the best but I would have thought it should be ` LLVMContextSetDiagnosticHandler(m_ctx, &llvm_diag_handler, NULL);` </blockquote><div><font color="#000000">Actually, &llvm_diag_handler and llvm_diag_handler are equivalent in 'C'.</font></div><div dir="ltr"><br></div><div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Your handling of TSContext and TSModule seems a bit, I think you should create one TSContext per TSModule, instead of having a global one that seems to be shared between modules.<br>The module here should use the same context as the ts_context itself <a href="https://github.com/CuriousGeorgiy/tarantool/blob/f73f01ef2699704e9e6fd6dd294d13c299c4627f/src/box/sql/llvm_jit.c#L1232" target="_blank">https://github.com/CuriousGeorgiy/tarantool/blob/f73f01ef2699704e9e6fd6dd294d13c299c4627f/src/box/sql/llvm_jit.c#L1232<br></a>and after the tsm is created you can dispose the tscontext, since the context is now owned by the module.</blockquote></div><div>Fixed that, thank you for the suggestion. </div><div><br></div><div>Actually, the problem turned out to be the 'free' here:</div><div><a href="https://github.com/CuriousGeorgiy/tarantool/blob/f73f01ef2699704e9e6fd6dd294d13c299c4627f/src/box/sql/llvm_jit.c#L1234">https://github.com/CuriousGeorgiy/tarantool/blob/f73f01ef2699704e9e6fd6dd294d13c299c4627f/src/box/sql/llvm_jit.c#L1234</a><br></div><div><br></div><div>Previously I transferred the LLVM module ownership under construction between a 'build' context and an 'execution' context, so I allocated memory for the reference. Did not clean this spot up after refactoring, thus the bug and no diagnostic information.</div><div dir="ltr"><br></div><div>Thank you for your time and help in pointing out the suspicious area for me.</div><div><br></div><div>I apologize for bothering about a bug in my memory management, not related to the LLVM C API.</div><div dir="ltr"><br></div><div dir="ltr"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Regards,<div>Georgiy Lebedev</div></div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jul 18, 2021 at 4:38 PM Valentin Churavy <<a href="mailto:v.churavy@gmail.com">v.churavy@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Have you tried running this with assertions on? Also my `C` knowledge isn't the best but I would have thought it should be ` <span>LLVMContextSetDiagnosticHandler</span>(m_ctx, &llvm_diag_handler, <span>NULL</span>);`<br></div><div><br></div><div>Your handling of TSContext and TSModule seems a bit, I think you should create one TSContext per TSModule, instead of having a global one that seems to be shared between modules.</div><div><br></div><div>The module here should use the same context as the ts_context itself <a href="https://github.com/CuriousGeorgiy/tarantool/blob/f73f01ef2699704e9e6fd6dd294d13c299c4627f/src/box/sql/llvm_jit.c#L1232" target="_blank">https://github.com/CuriousGeorgiy/tarantool/blob/f73f01ef2699704e9e6fd6dd294d13c299c4627f/src/box/sql/llvm_jit.c#L1232</a></div><div>and after the tsm is created you can dispose the tscontext, since the context is now owned by the module.<br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jul 18, 2021 at 4:00 PM Georgiy Lebedev via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi!</div><div><p style="font-family:Arial,sans-serif">I am trying to utilize the Orcv2 C API. Everything goes smooth, initialized and well-formed (at least I assert for null pointers everywhere), till the point I try to lookup a symbol with LLVMOrcLLJITLookup: I receive a SIGSEGV.</p><p style="font-family:Arial,sans-serif">Providing the backtrace:</p><p style="font-family:Arial,sans-serif">* thread #1, name = 'tarantool', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)<br> * frame #0: 0x0000000001a88db0 tarantool`llvm::LLVMContext::getDiagHandlerPtr() const<br> frame #1: 0x0000000001a959d4 tarantool`llvm::legacy::PassManagerImpl::run(llvm::Module&) + 628<br> frame #2: 0x0000000000c5fa0e tarantool`llvm::orc::SimpleCompiler::operator()(llvm::Module&) + 366<br> frame #3: 0x0000000000c6fd4e tarantool`decltype(auto) llvm::orc::ThreadSafeModule::withModuleDo<llvm::orc::IRCompileLayer::IRCompiler&>(llvm::orc::IRCompileLayer::IRCompiler&) + 94<br> frame #4: 0x0000000000c6fa05 tarantool`llvm::orc::IRCompileLayer::emit(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >, llvm::orc::ThreadSafeModule) + 37<br> frame #5: 0x0000000000c6fff7 tarantool`llvm::orc::IRTransformLayer::emit(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >, llvm::orc::ThreadSafeModule) + 199<br> frame #6: 0x0000000000c6fff7 tarantool`llvm::orc::IRTransformLayer::emit(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >, llvm::orc::ThreadSafeModule) + 199<br> frame #7: 0x0000000000c396a2 tarantool`llvm::orc::BasicIRLayerMaterializationUnit::materialize(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >) + 274<br> frame #8: 0x0000000000c1fb4b tarantool`llvm::orc::ExecutionSession::materializeOnCurrentThread(std::unique_ptr<llvm::orc::MaterializationUnit, std::default_delete<llvm::orc::MaterializationUnit> >, std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >) + 27<br> frame #9: 0x0000000000c29a61 tarantool`std::_Function_handler<void (std::unique_ptr<llvm::orc::MaterializationUnit, std::default_delete<llvm::orc::MaterializationUnit> >, std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >), void (*)(std::unique_ptr<llvm::orc::MaterializationUnit, std::default_delete<llvm::orc::MaterializationUnit> >, std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >)>::_M_invoke(std::_Any_data const&, std::unique_ptr<llvm::orc::MaterializationUnit, std::default_delete<llvm::orc::MaterializationUnit> >&&, std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >&&) + 49<br> frame #10: 0x0000000000c20d31 tarantool`llvm::orc::ExecutionSession::dispatchOutstandingMUs() + 353<br> frame #11: 0x0000000000c23327 tarantool`llvm::orc::ExecutionSession::OL_completeLookup(std::unique_ptr<llvm::orc::InProgressLookupState, std::default_delete<llvm::orc::InProgressLookupState> >, std::shared_ptr<llvm::orc::AsynchronousSymbolQuery>, std::function<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> >, llvm::DenseMapInfo<llvm::orc::JITDylib*>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > > > const&)>) + 4711<br> frame #12: 0x0000000000c308fb tarantool`llvm::orc::InProgressFullLookupState::complete(std::unique_ptr<llvm::orc::InProgressLookupState, std::default_delete<llvm::orc::InProgressLookupState> >) + 251<br> frame #13: 0x0000000000c14a0a tarantool`llvm::orc::ExecutionSession::OL_applyQueryPhase1(std::unique_ptr<llvm::orc::InProgressLookupState, std::default_delete<llvm::orc::InProgressLookupState> >, llvm::Error) + 2010<br> frame #14: 0x0000000000c12c8c tarantool`llvm::orc::ExecutionSession::lookup(llvm::orc::LookupKind, std::vector<std::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>, std::allocator<std::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags> > > const&, llvm::orc::SymbolLookupSet, llvm::orc::SymbolState, llvm::unique_function<void (llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::JITEvaluatedSymbol, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::JITEvaluatedSymbol> > >)>, std::function<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> >, llvm::DenseMapInfo<llvm::orc::JITDylib*>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > > > const&)>) + 556<br> frame #15: 0x0000000000c20fc6 tarantool`llvm::orc::ExecutionSession::lookup(std::vector<std::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>, std::allocator<std::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags> > > const&, llvm::orc::SymbolLookupSet const&, llvm::orc::LookupKind, llvm::orc::SymbolState, std::function<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> >, llvm::DenseMapInfo<llvm::orc::JITDylib*>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > > > const&)>) + 486<br> frame #16: 0x0000000000c2154e tarantool`llvm::orc::ExecutionSession::lookup(std::vector<std::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>, std::allocator<std::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags> > > const&, llvm::orc::SymbolStringPtr, llvm::orc::SymbolState) + 302<br> frame #17: 0x0000000000c3b937 tarantool`llvm::orc::LLJIT::lookupLinkerMangled(llvm::orc::JITDylib&, llvm::orc::SymbolStringPtr) + 119<br> frame #18: 0x0000000000c08b36 tarantool`llvm::orc::LLJIT::lookup(llvm::orc::JITDylib&, llvm::StringRef) + 166<br> frame #19: 0x0000000000c0770b tarantool`LLVMOrcLLJITLookup + 59<br> frame #20: 0x0000000000778900 tarantool`llvm_exec_compiled_expr_list(ctx=0x0000000003271510, fn_id=0, vdbe=0x0000000002db9e18) at llvm_jit.c:443:8<br></p><div><p style="font-family:Arial,sans-serif">Apparently, the problem with the segfault is the attempt to access the DiagHandler, though I explicitly set it.<br></p><p style="font-family:Arial,sans-serif">My code can be viewed here:</p><p style="font-family:Arial,sans-serif"><a href="https://github.com/CuriousGeorgiy/tarantool/blob/CuriousGeorgiy/sql-vdbe-jit/src/box/sql/llvm_jit.h" target="_blank">https://github.com/CuriousGeorgiy/tarantool/blob/CuriousGeorgiy/sql-vdbe-jit/src/box/sql/llvm_jit.h</a><br></p><p style="font-family:Arial,sans-serif"><a href="https://github.com/CuriousGeorgiy/tarantool/blob/CuriousGeorgiy/sql-vdbe-jit/src/box/sql/llvm_jit.c" target="_blank">https://github.com/CuriousGeorgiy/tarantool/blob/CuriousGeorgiy/sql-vdbe-jit/src/box/sql/llvm_jit.c</a></p><p style="font-family:Arial,sans-serif">I understand the code I provided may be sophisticated enough, so if there is any additional information or context I can provide that would be helpful, please, let me know.<br></p></div><div>Also, this is my first time communicating via a mailing list, so, please, let me know if I am missing on something.</div></div><div><div dir="ltr"><div dir="ltr">Regards,<div>Georgiy Lebedev</div></div></div></div></div>
_______________________________________________<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>
</blockquote></div></div>