Llvm context for deserializing bitcode

Hi,

I am sorry to ask a duplicate question but I can’t update the information in the original post. I’m working on deserializing a bitcode string from a clang compiler instance. I used a brand new LLVMContext with parseBitcodeFile(). The code is like:

std::string code;
clang::CompilerInstance* compiler;
auto action = std::make_unique<clang::EmitLLVMOnlyActin>();
compiler->ExecuteAction(*action);
auto module = action->takeModule();

std::string bitcode;
llvm::raw_string_ostream output(bitcode);
llvm::WriteBitcodeToFile(*module, output);

LLVMContext context = make_unique<LLVMContext>();
auto d_module = parseBitcodeFile(bitcode, *context);

auto tsm = llvm::orc::ThreadSafeModule(std::move(d_module), std::move(context);
jit->addIRModule(std::move(*tsm));
jit->lookup(FnName);

and I got the following Code strace:

*** Aborted at 1657064920 (Unix time, try 'date -d @1657064920') ***
*** Signal 11 (SIGSEGV) (0x60) received by PID 3648 (pthread TID 0x7f129a442e40) (linux TID 3648) (code: address not mapped to object), stack trace: ***
    @ 000000000038244c folly::symbolizer::(anonymous namespace)::signalHandler(int, siginfo_t*, void*)
    @ 0000000000000000 (unknown)
    @ 00000000003092a0 llvm::BasicAAWrapperPass::runOnFunction(llvm::Function&)
    @ 00000000003e6bea llvm::FPPassManager::runOnFunction(llvm::Function&)
    @ 00000000003ed332 llvm::FPPassManager::runOnModule(llvm::Module&)
    @ 00000000003e72c0 llvm::legacy::PassManagerImpl::run(llvm::Module&)
    @ 00000000000bfcf9 llvm::orc::SimpleCompiler::operator()(llvm::Module&)
    @ 00000000000fbe3b decltype(auto) llvm::orc::ThreadSafeModule::withModuleDo<llvm::orc::IRCompileLayer::IRCompiler&>(llvm::orc::IRCompileLayer::IRCompiler&)
    @ 00000000000fbb35 llvm::orc::IRCompileLayer::emit(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >, llvm::orc::ThreadSafeModule)
    @ 00000000000fc36c llvm::orc::IRTransformLayer::emit(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >, llvm::orc::ThreadSafeModule)
    @ 00000000000fc36c llvm::orc::IRTransformLayer::emit(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >, llvm::orc::ThreadSafeModule)
    @ 0000000000125a87 llvm::orc::BasicIRLayerMaterializationUnit::materialize(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >)
    @ 00000000000e4115 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> >)
    @ 00000000000eacf3 void std::__invoke_impl<void, 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> >), std::unique_ptr<llvm::orc::MaterializationUnit, std::default_delete<llvm::orc::MaterializationUnit> >, std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> > >(std::__invoke_other, 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> >), std::unique_ptr<llvm::orc::MaterializationUnit, std::default_delete<llvm::orc::MaterializationUnit> >&&, std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >&&)
    @ 00000000000e3152 llvm::orc::ExecutionSession::dispatchMaterialization(std::unique_ptr<llvm::orc::MaterializationUnit, std::default_delete<llvm::orc::MaterializationUnit> >, std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >)
    @ 00000000000d838b llvm::orc::ExecutionSession::dispatchOutstandingMUs()
    @ 00000000000dbc47 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&)>)
    @ 00000000000f1c0a llvm::orc::InProgressFullLookupState::complete(std::unique_ptr<llvm::orc::InProgressLookupState, std::default_delete<llvm::orc::InProgressLookupState> >)
    @ 00000000000c9700 llvm::orc::ExecutionSession::OL_applyQueryPhase1(std::unique_ptr<llvm::orc::InProgressLookupState, std::default_delete<llvm::orc::InProgressLookupState> >, llvm::Error)
    @ 00000000000c655a 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&)>)
    @ 00000000000d864e 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&)>)
    @ 00000000000d8bfa 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)
    @ 0000000000113100 llvm::orc::LLJIT::lookupLinkerMangled(llvm::orc::JITDylib&, llvm::orc::SymbolStringPtr)
    @ 000000000002f026 llvm::orc::LLJIT::lookup(llvm::orc::JITDylib&, llvm::StringRef)

I followed the code trace and found that the problem is the deserialization of llvm::Module. Specifically, the problem occurs because of the brand new LLVMContext, since if I use the context from action->takeLLVMContext() it works.

According to another post, the LLVMContext should not do anything on the deserialization result of llvm::Module. However, in this case it is obvious it does something. Does anyone know how could I solve this problem?

Thank you!