[llvm-dev] JIT compiler - showing generated machine code

Caldarale, Charles R via llvm-dev llvm-dev at lists.llvm.org
Wed Apr 6 05:26:10 PDT 2016


> From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] 
> On Behalf Of Sanjoy Das via llvm-dev
> Subject: Re: [llvm-dev] JIT compiler - showing generated machine code

> Russell Wallace via llvm-dev wrote:
> > When using LLVM as a JIT compiler, you can use module.dump() to show the generated 
> > intermediate code, which is good.
> >
> > Is there similarly a programmatic way to show the generated x64 machine code in 
> > assembly format?

> I'm not sure if there is a ready-made solution, but you can consider using the MC 
> component of LLVM to easily write yourself a disassembler (easily == 100 to 200 LOC, 
> if I recall correctly).  For inspiration, take a look at how "llvm-mc --disassemble" 
> works.

After generating code for the module, we use something like this to run an additional pass to display the assembly in the log:

    legacy::PassManager* pMPasses = new legacy::PassManager();
    target_machine->Options.MCOptions.AsmVerbose = true;
    if (target_machine->addPassesToEmitFile(*pMPasses, *llvm_log,
                                            TargetMachine::CGFT_AssemblyFile)) {
        llvm_log->flush();
        delete pMPasses;
        sysLogger("** could not add emit file pass **");
    } else {
        pMPasses->run(*pMod);
        delete pMPasses;
    }

We found that setting AsmVerbose early would often display different x86 code than was actually in the final functions.  There might well be better ways to do this these days.

 - Chuck



More information about the llvm-dev mailing list