Ignore:
Timestamp:
Aug 18, 2015, 12:16:01 PM (4 years ago)
Author:
cameron
Message:

Factor out execution engine code from Pablo compiler into new JIT_to_ExecutionEngine routine.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp

    r4726 r4730  
    4141#include <llvm/Support/Host.h>
    4242#include <llvm/Transforms/Scalar.h>
    43 #include <llvm/ExecutionEngine/ExecutionEngine.h>
    44 #include <llvm/ExecutionEngine/MCJIT.h>
    4543#include <llvm/IRReader/IRReader.h>
    4644#include <llvm/Bitcode/ReaderWriter.h>
     
    6765PabloCompiler::PabloCompiler()
    6866: mMod(nullptr)
    69 , mExecutionEngine(nullptr)
    7067, mBuilder(nullptr)
    7168, mCarryManager(nullptr)
     
    10198}
    10299
    103 CompiledPabloFunction PabloCompiler::compile(PabloFunction & function) {
    104 
    105     Examine(function);
    106 
    107     InitializeNativeTarget();
    108     InitializeNativeTargetAsmPrinter();
    109     InitializeNativeTargetAsmParser();
    110 
     100llvm::Function * PabloCompiler::compile(PabloFunction & function) {
    111101    Module * module = new Module("", getGlobalContext());
    112 
    113     mMod = module;
    114 
    115     std::string errMessage;
    116     #ifdef USE_LLVM_3_5
    117     EngineBuilder builder(mMod);
    118     #else
    119     EngineBuilder builder(std::move(std::unique_ptr<Module>(mMod)));
    120     #endif
    121     builder.setErrorStr(&errMessage);
    122     builder.setMCPU(sys::getHostCPUName());
    123     #ifdef USE_LLVM_3_5
    124     builder.setUseMCJIT(true);
    125     #endif
    126     builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
    127     mExecutionEngine = builder.create();
    128     if (mExecutionEngine == nullptr) {
    129         throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
    130     }
    131     DeclareDebugFunctions();
    132 
    133     auto func = compile(function, mMod);
    134 
     102   
     103    auto func = compile(function, module);
     104   
    135105    //Display the IR that has been generated by this module.
    136106    if (LLVM_UNLIKELY(DumpGeneratedIR)) {
     
    140110    verifyModule(*module, &dbgs());
    141111
    142     mExecutionEngine->finalizeObject();
    143     ExecutionEngine * engine = mExecutionEngine;
    144     mExecutionEngine = nullptr; // <-- pass ownership of the execution engine to the caller
    145 
    146     return CompiledPabloFunction(func, engine);
     112    return func;
    147113}
    148114
     
    205171    ReturnInst::Create(mMod->getContext(), mBuilder->GetInsertBlock());
    206172   
    207 
    208173    // Clean up
    209174    delete mCarryManager; mCarryManager = nullptr;
     
    350315        //This function can be used for testing to print the contents of a register from JIT'd code to the terminal window.
    351316        mPrintRegisterFunction = mMod->getOrInsertFunction("wrapped_print_register", Type::getVoidTy(mMod->getContext()), Type::getInt8PtrTy(mMod->getContext()), mBitBlockType, NULL);
    352         if (mExecutionEngine) mExecutionEngine->addGlobalMapping(cast<GlobalValue>(mPrintRegisterFunction), (void *)&wrapped_print_register);
    353317    }
    354318}
     
    562526        externalFunction->setCallingConv(llvm::CallingConv::C);
    563527
    564         if (mExecutionEngine) mExecutionEngine->addGlobalMapping(externalFunction, proto->getFunctionPtr());
    565528
    566529        AllocaInst * outputStruct = mBuilder->CreateAlloca(outputType);
     
    805768}
    806769
    807 CompiledPabloFunction::CompiledPabloFunction(Function * function, ExecutionEngine * executionEngine)
    808 : FunctionPointer(executionEngine->getPointerToFunction(function))
    809 , mFunction(function)
    810 , mExecutionEngine(executionEngine)
    811 {
    812 
    813 }
    814 
    815 // Clean up the memory for the compiled function once we're finished using it.
    816 CompiledPabloFunction::~CompiledPabloFunction() {
    817     if (mExecutionEngine) {
    818         assert (mFunction);
    819         // mExecutionEngine->freeMachineCodeForFunction(mFunction); // This function only prints a "not supported" message. Reevaluate with LLVM 3.6.
    820         delete mExecutionEngine;
    821     }
    822 }
    823 
    824 }
     770}
Note: See TracChangeset for help on using the changeset viewer.