Changeset 4730 for icGREP


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.

Location:
icGREP/icgrep-devel/icgrep
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/compiler.cpp

    r4722 r4730  
    6565namespace icgrep {
    6666
    67 CompiledPabloFunction compile(const Encoding encoding, const std::vector<std::string> regexps, const ModeFlagSet initialFlags) {
     67llvm::Function * compile(const Encoding encoding, const std::vector<std::string> regexps, const ModeFlagSet initialFlags) {
    6868    std::vector<RE *> REs;
    6969    RE * re_ast = nullptr;
     
    170170    PabloCompiler pablo_compiler;
    171171    try {
    172         CompiledPabloFunction retVal = pablo_compiler.compile(function);
     172        llvm::Function * retVal = pablo_compiler.compile(function);
    173173        releaseSlabAllocatorMemory();
    174174        return retVal;
  • icGREP/icgrep-devel/icgrep/compiler.h

    r4516 r4730  
    1313#include <pablo/pablo_compiler.h>
    1414#include <string>
     15#include <llvm/IR/Function.h>
    1516
    1617namespace icgrep {
    1718
    18     pablo::CompiledPabloFunction compile(const Encoding encoding, const std::vector<std::string> regexps, const re::ModeFlagSet initialFlags);
     19    llvm::Function * compile(const Encoding encoding, const std::vector<std::string> regexps, const re::ModeFlagSet initialFlags);
    1920
    2021}
  • icGREP/icgrep-devel/icgrep/icgrep.cpp

    r4726 r4730  
    1313#include "compiler.h"
    1414#include "pablo/pablo_compiler.h"
     15#include <llvm/IR/Function.h>
     16#include <llvm/ExecutionEngine/ExecutionEngine.h>
     17#include <llvm/ExecutionEngine/MCJIT.h>
     18#include <llvm/IRReader/IRReader.h>
     19#include <llvm/Support/SourceMgr.h>
     20#include <llvm/Support/CommandLine.h>
     21#include <llvm/Support/TargetSelect.h>
     22#include <llvm/Support/Host.h>
     23
    1524#include "do_grep.h"
    16 
    17 #include "llvm/Support/CommandLine.h"
    1825
    1926static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
     
    3845static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
    3946static cl::opt<std::string> RegexFilename("f", cl::desc("Take regular expressions (one per line) from a file"), cl::value_desc("regex file"), cl::init(""), cl::cat(aRegexSourceOptions));
     47
     48
     49
     50ExecutionEngine * JIT_to_ExecutionEngine (llvm::Function * f) {
     51
     52    InitializeNativeTarget();
     53    InitializeNativeTargetAsmPrinter();
     54    InitializeNativeTargetAsmParser();
     55
     56    std::string errMessage;
     57    EngineBuilder builder(std::move(std::unique_ptr<Module>(f->getParent())));
     58    builder.setErrorStr(&errMessage);
     59    builder.setMCPU(sys::getHostCPUName());
     60
     61    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
     62    ExecutionEngine * engine = builder.create();
     63    if (engine == nullptr) {
     64        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
     65    }
     66    //engine->addGlobalMapping(cast<GlobalValue>(mPrintRegisterFunction), (void *)&wrapped_print_register);
     67    // engine->addGlobalMapping(externalFunction, proto->getFunctionPtr());
     68
     69    engine->finalizeObject();
     70    return engine;
     71}
    4072
    4173
     
    106138    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
    107139   
    108     const auto llvm_codegen = icgrep::compile(encoding, regexVector, globalFlags);
    109     if (llvm_codegen.FunctionPointer) {
    110         GrepExecutor grepEngine = GrepExecutor(llvm_codegen.FunctionPointer);
     140   
     141    llvm::Function * llvm_codegen = icgrep::compile(encoding, regexVector, globalFlags);
     142   
     143    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(llvm_codegen);
     144   
     145    void * functionPointer = engine->getPointerToFunction(llvm_codegen);
     146   
     147    if (functionPointer) {
     148        GrepExecutor grepEngine = GrepExecutor(functionPointer);
    111149        grepEngine.setCountOnlyOption(CountOnly);
    112150        grepEngine.setNormalizeLineBreaksOption(NormalizeLineBreaks);
     
    120158    }
    121159   
     160    //engine->freeMachineCodeForFunction(llvm_codegen); // This function only prints a "not supported" message. Reevaluate with LLVM 3.6.
     161    delete engine;
     162
    122163    return 0;
    123164}
  • 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}
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.h

    r4726 r4730  
    5757class While;
    5858
    59 struct CompiledPabloFunction {
    60     void * const        FunctionPointer;
    61 private:
    62     Function *          mFunction;
    63     ExecutionEngine *   mExecutionEngine;
    64 public:
    65     CompiledPabloFunction(Function * function, ExecutionEngine * executionEngine);
    66 
    67     inline Function * getLLVMFunction() const {
    68         return mFunction;
    69     }
    70 
    71     inline ExecutionEngine * getExecutionEngine() const {
    72         return mExecutionEngine;
    73     }
    74 
    75     inline CompiledPabloFunction(CompiledPabloFunction && cpf)
    76     : FunctionPointer(cpf.FunctionPointer)
    77     , mFunction(cpf.mFunction)
    78     , mExecutionEngine(cpf.mExecutionEngine)
    79     {
    80         cpf.mFunction = nullptr;
    81         cpf.mExecutionEngine = nullptr;
    82     }
    83 
    84     ~CompiledPabloFunction();
    85 
    86 };
    8759#if (BLOCK_SIZE==256)
    8860#define USE_UADD_OVERFLOW
     
    10476    PabloCompiler();
    10577    ~PabloCompiler();
    106     CompiledPabloFunction compile(pablo::PabloFunction & function);
     78    Function * compile(pablo::PabloFunction & function);
    10779    Function * compile(pablo::PabloFunction & function, Module *module);
    10880    Module *getModule();
     
    146118
    147119    Module *                            mMod;
    148     ExecutionEngine *                   mExecutionEngine;
    149120    IRBuilder <> *                      mBuilder;
    150121
Note: See TracChangeset for help on using the changeset viewer.