How to emit assembler from LLJIT?

I’ve running a JIT using lljit, but to aid with debugging i’m wanting to also see the assembler that is being generated. My current attempt is to have a separate legacy::PassManager which addPassesToEmitFile to a targetMachine, which works well, but unfortunately doesn’t give me the same assembler that is being emitted by LLJIT as i’d have to replicate the exact same optimisations. In addition, it’s two passes rather than one and looks a bit weird.

So what is the right way to replicate this sort of logic with lljit?

        ::llvm::SmallVector<char, 100000> result;
        ::llvm::raw_svector_ostream ostream (result);

        if (auto machineBuilder = ::llvm::orc::JITTargetMachineBuilder::detectHost())
        {
            if (auto targetMachine = machineBuilder->createTargetMachine())
            {
                ::llvm::legacy::PassManager passManager;

                targetMachine.get()->Options.MCOptions.AsmVerbose = true;
                targetMachine.get()->addPassesToEmitFile (passManager, ostream, nullptr, ::llvm::CGFT_AssemblyFile);
                passManager.run (*targetModule);
            }
        }

The MLIR ExecutionEngine uses LLJIT and is able to dump an object file.

You should be able to infer from there what needs to be done: