[LLVMdev] Capturing the assembler from MCJIT

Josh Klontz josh.klontz at gmail.com
Fri Mar 7 14:48:32 PST 2014


Frank,

Perhaps someone more knowledgeable will shoot this approach down, but what
I do for this case is copy the module after running the function pass
manager and pass it through:

    const string extension = fileName.substr(fileName.find_last_of(".") +
1);
    string errorInfo;
    tool_output_file output(fileName.c_str(), errorInfo, sys::fs::F_None);
    PassManager pm;
    formatted_raw_ostream fos(output.os());
    targetMachine->addPassesToEmitFile(pm, fos, extension == "s" ?
TargetMachine::CGFT_AssemblyFile : TargetMachine::CGFT_ObjectFile);
    pm.run(*module);

This code was basically taken from `llc` and I believe MCJIT does
essentially (exactly?) the same thing.

-Josh


On Fri, Mar 7, 2014 at 4:23 PM, Frank Winter <fwinter at jlab.org> wrote:

> Is there a way to capture the assembler when using MCJIT? I was searching
> the lists and this popped up:
>
> http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-March/047873.html
>
> However this is quite elaborate and I was wondering if there's not an
> easier way to do this..?!
>
> The setup I am using:
>
>     llvm::InitializeNativeTarget();
>     InitializeNativeTargetAsmPrinter(); // MCJIT
>     InitializeNativeTargetAsmParser(); // MCJIT
>
>     Mod = llvm::ParseIRFile( INPUT_FILE , Err, llvm::getGlobalContext());
>
>     Mod->setTargetTriple(llvm::sys::getProcessTriple());
>
>     llvm::EngineBuilder engineBuilder(Mod);
>     engineBuilder.setEngineKind(llvm::EngineKind::JIT);
> engineBuilder.setOptLevel(llvm::CodeGenOpt::Aggressive);
>     TheExecutionEngine = engineBuilder.setUseMCJIT(true).create(); //
> MCJIT
>
>     targetMachine = engineBuilder.selectTarget();
>
>     mainFunc = Mod->getFunction("main");
>
>     llvm::FunctionPassManager *functionPassManager;
>
>     llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
>     initializeScalarOpts(registry);
>
>     functionPassManager = new llvm::FunctionPassManager(Mod);
> functionPassManager->add(llvm::createVerifierPass(llvm::
> PrintMessageAction));
> targetMachine->addAnalysisPasses(*functionPassManager);
>     functionPassManager->add(new llvm::TargetLibraryInfo(llvm::
> Triple(Mod->getTargetTriple())));
>     functionPassManager->add(new llvm::DataLayout(Mod));
> functionPassManager->add(llvm::createBasicAliasAnalysisPass());
>     functionPassManager->add(llvm::createLICMPass());
>     functionPassManager->add(llvm::createGVNPass());
> functionPassManager->add(llvm::createPromoteMemoryToRegisterPass());
> functionPassManager->add(llvm::createLoopVectorizePass());
> functionPassManager->add(llvm::createEarlyCSEPass());
> functionPassManager->add(llvm::createInstructionCombiningPass());
> functionPassManager->add(llvm::createCFGSimplificationPass());
>
>     functionPassManager->run(*mainFunc);
>
>     TheExecutionEngine->finalizeObject();  // MCJIT
>
>     void *fptr = TheExecutionEngine->getPointerToFunction( mainFunc );
>
>
> Thanks,
> Frank
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140307/32dc0b0d/attachment.html>


More information about the llvm-dev mailing list