Changeset 5399


Ignore:
Timestamp:
Apr 8, 2017, 12:56:07 PM (2 years ago)
Author:
nmedfort
Message:

Bug fixes for object cache. Minor optimization of FunctionTypeBuilder?.

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

Legend:

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

    r5398 r5399  
    206206
    207207
    208 Function * editdPipeline(Module * m, IDISA::IDISA_Builder * iBuilder, const std::vector<std::string> & patterns) {
    209    
     208void editdPipeline(ParabixDriver & pxDriver, const std::vector<std::string> & patterns) {
     209   
     210    IDISA::IDISA_Builder * const iBuilder = pxDriver.getIDISA_Builder();
     211    Module * const m = iBuilder->getModule();
    210212    Type * const sizeTy = iBuilder->getSizeTy();
    211213    Type * const voidTy = iBuilder->getVoidTy();
     
    214216    Function * const main = cast<Function>(m->getOrInsertFunction("Main", voidTy, inputType, sizeTy, nullptr));
    215217    main->setCallingConv(CallingConv::C);
    216     Function::arg_iterator args = main->arg_begin();
    217    
     218    auto args = main->arg_begin();
    218219    Value * const inputStream = &*(args++);
    219220    inputStream->setName("input");
    220221    Value * const fileSize = &*(args++);
    221222    fileSize->setName("fileSize");
    222    
     223    iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", main,0));
     224
     225
    223226    ExternalFileBuffer ChStream(iBuilder, iBuilder->getStreamSetTy(4));
    224227    SingleBlockBuffer MatchResults(iBuilder, iBuilder->getStreamSetTy(editDistance + 1));
    225228
    226229    MMapSourceKernel mmapK(iBuilder);
    227     std::unique_ptr<Module> mmapM = mmapK.createKernelModule({}, {&ChStream});
    228230    mmapK.setInitialArguments({fileSize});
    229    
     231    pxDriver.addKernelCall(mmapK, {}, {&ChStream});
     232
    230233    PabloKernel editdk(iBuilder, "editd",
    231234                        {Binding{iBuilder->getStreamSetTy(4), "pat"}},
    232235                        {Binding{iBuilder->getStreamSetTy(editDistance + 1), "E"}});
    233236
    234     buildPatternKernel(editdk, iBuilder, patterns);
    235 
    236     kernel::editdScanKernel editdScanK(iBuilder, editDistance);
    237    
    238     std::unique_ptr<Module> editdM = editdk.createKernelModule({&ChStream}, {&MatchResults});
    239     std::unique_ptr<Module> scanM = editdScanK.createKernelModule({&MatchResults}, {});               
    240    
    241     mmapK.addKernelDeclarations(m);
    242     editdk.addKernelDeclarations(m);
    243     editdScanK.addKernelDeclarations(m);
    244 
    245     iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", main,0));
    246 
    247237    ChStream.setStreamSetBuffer(inputStream);
    248238    MatchResults.allocateBuffer();
    249    
    250     generatePipelineLoop(iBuilder, {&mmapK, &editdk, &editdScanK});
     239
     240    buildPatternKernel(editdk, iBuilder, patterns);
     241    pxDriver.addKernelCall(editdk, {&ChStream}, {&MatchResults});
     242
     243    kernel::editdScanKernel editdScanK(iBuilder, editDistance);
     244    pxDriver.addKernelCall(editdScanK, {&MatchResults}, {});
    251245       
     246    pxDriver.generatePipelineIR();
     247
    252248    iBuilder->CreateRetVoid();
    253    
    254     Linker L(*m);
    255     L.linkInModule(std::move(mmapM));
    256     L.linkInModule(std::move(editdM));
    257     L.linkInModule(std::move(scanM));
    258    
    259     return main;
     249
     250    pxDriver.JITcompileMain();
     251    pxDriver.linkAndFinalize();
    260252}
    261253
     
    280272}
    281273
    282 //
    283 // Handler for errors reported through llvm::report_fatal_error.  Report
    284 // and signal error code 2 (grep convention).
    285 //
    286 static void error_handler(void *UserData, const std::string &Message, bool GenCrashDiag) {
    287     throw std::runtime_error(Message);
    288 }
    289 
    290 
    291274void preprocessPipeline(ParabixDriver & pxDriver) {
    292 
    293     llvm::install_fatal_error_handler(&error_handler);
    294275
    295276    IDISA::IDISA_Builder * iBuilder = pxDriver.getIDISA_Builder();
     
    342323    iBuilder->CreateRetVoid();
    343324
    344     errs() << "JitCompileMain()\n";
    345 
    346325    pxDriver.JITcompileMain();
    347 
    348     errs() << "linkAndFinalize()\n";
    349 
    350326    pxDriver.linkAndFinalize();
    351 
    352     errs() << "done\n";
    353327}
    354328
     
    369343typedef void (*editdFunctionType)(char * byte_data, size_t filesize);
    370344
    371 editdFunctionType editdCodeGen(const std::vector<std::string> & patterns) {
    372                            
     345editdFunctionType editdCodeGen(const std::vector<std::string> & patterns) {                           
    373346    LLVMContext TheContext;
    374347    Module * M = new Module("editd", TheContext);
    375     IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
    376 
    377     llvm::Function * main_IR = editdPipeline(M, idb, patterns);
    378 
    379     ExecutionEngine * editdEngine = JIT_to_ExecutionEngine(M);
    380    
    381     editdEngine->finalizeObject();
    382 
     348    IDISA::IDISA_Builder * const idb = IDISA::GetIDISA_Builder(M);
     349    ParabixDriver pxDriver(idb);
     350    editdPipeline(pxDriver, patterns);
     351    auto f = reinterpret_cast<editdFunctionType>(pxDriver.getPointerToMain());
    383352    delete idb;
    384     return reinterpret_cast<editdFunctionType>(editdEngine->getPointerToFunction(main_IR));
     353    return f;
    385354}
    386355
  • icGREP/icgrep-devel/icgrep/grep_engine.cpp

    r5398 r5399  
    288288template<typename CodeUnit>
    289289void wrapped_report_match(const size_t lineNum, size_t line_start, size_t line_end, const CodeUnit * const buffer, const size_t filesize, const size_t fileIdx) {
     290
     291//    errs() << lineNum << " : (" << line_start << ", " << line_end << ", " << filesize << ")\n";
     292
    290293    assert (buffer);
    291294    assert (line_start <= line_end);
     
    573576
    574577    #ifdef CUDA_ENABLED
    575     Function * mainCPUFn = nullptr;
    576578    if(codegen::NVPTX){
    577579        Function * kernelFunction = generateGPUKernel(M, iBuilder, CountOnly);
     
    584586
    585587        Compile2PTX(M, IRFilename, PTXFilename);
    586         mainCPUFn = generateCPUKernel(cpuM, CPUBuilder, mGrepType);
     588        Function * mainCPUFn = generateCPUKernel(cpuM, CPUBuilder, mGrepType);
    587589        if (CountOnly) return;
    588590    }
  • icGREP/icgrep-devel/icgrep/kernels/kernel.cpp

    r5398 r5399  
    120120}
    121121
    122 std::unique_ptr<Module> KernelBuilder::createKernelStub() {
     122std::unique_ptr<Module> KernelBuilder::createKernelStub(const StreamSetBuffers & inputs, const StreamSetBuffers & outputs) {
     123    setCallParameters(inputs, outputs);
    123124    std::string cacheName = getName() + "_" + iBuilder->getBuilderUniqueName();
    124125    for (auto & b: mStreamSetInputBuffers) {
     
    128129        cacheName += ":" + b->getUniqueID();
    129130    }
     131    prepareKernel();
    130132    return make_unique<Module>(cacheName, iBuilder->getContext());
    131133}
    132134
    133 void KernelBuilder::setCallParameters(const std::vector<StreamSetBuffer *> & inputs, const std::vector<StreamSetBuffer *> & outputs) {
     135void KernelBuilder::setCallParameters(const StreamSetBuffers & inputs, const StreamSetBuffers & outputs) {
     136    assert (mStreamSetInputBuffers.empty());
    134137    mStreamSetInputBuffers.assign(inputs.begin(), inputs.end());
    135138    for (unsigned i = 0; i < mStreamSetInputBuffers.size(); ++i) {
     
    144147                           + std::to_string(mStreamSetInputBuffers.size()));
    145148    }
    146    
     149    assert (mStreamSetOutputBuffers.empty());
    147150    mStreamSetOutputBuffers.assign(outputs.begin(), outputs.end());
    148151    for (unsigned i = 0; i < mStreamSetOutputBuffers.size(); ++i) {
     
    156159                           + " output stream sets but generateKernel() was given "
    157160                           + std::to_string(mStreamSetOutputBuffers.size()));
    158     }
    159     prepareKernel(); // possibly overridden by the KernelBuilder subtype
    160    
     161    }       
    161162}   
    162163
     
    170171
    171172
    172 std::unique_ptr<Module> KernelBuilder::createKernelModule(const std::vector<StreamSetBuffer *> & inputs, const std::vector<StreamSetBuffer *> & outputs) {
     173std::unique_ptr<Module> KernelBuilder::createKernelModule(const StreamSetBuffers & inputs, const StreamSetBuffers & outputs) {
    173174    auto saveModule = iBuilder->getModule();
    174175    auto savePoint = iBuilder->saveIP();
    175     auto module = createKernelStub();
     176    auto module = createKernelStub(inputs, outputs);
    176177    iBuilder->setModule(module.get());
    177178    generateKernel(inputs, outputs);
     
    181182}
    182183
    183 void KernelBuilder::generateKernel(const std::vector<StreamSetBuffer *> & inputs, const std::vector<StreamSetBuffer *> & outputs) {
     184void KernelBuilder::generateKernel(const StreamSetBuffers & inputs, const StreamSetBuffers & outputs) {
    184185    setCallParameters(inputs, outputs);
     186    prepareKernel(); // possibly overridden by the KernelBuilder subtype
    185187    generateKernel();
    186188}
  • icGREP/icgrep-devel/icgrep/kernels/kernel.h

    r5398 r5399  
    3030    using StreamPort = std::pair<Port, unsigned>;
    3131    using StreamMap = boost::container::flat_map<std::string, StreamPort>;
     32    using StreamSetBuffers = std::vector<parabix::StreamSetBuffer *>;
    3233public:
    3334   
     
    6566    // Create a module stub for the kernel, populated only with its Module ID.     
    6667    //
    67     std::unique_ptr<llvm::Module> createKernelStub();
     68    std::unique_ptr<llvm::Module> createKernelStub(const StreamSetBuffers & inputs, const StreamSetBuffers & outputs);
    6869   
    6970    // Create a module for the kernel, including the kernel state type declaration and
    7071    // the full implementation of all required methods.     
    7172    //
    72     std::unique_ptr<llvm::Module> createKernelModule(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
    73    
    74     void setCallParameters(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
     73    std::unique_ptr<llvm::Module> createKernelModule(const StreamSetBuffers & inputs, const StreamSetBuffers & outputs);
     74   
     75    void setCallParameters(const StreamSetBuffers & inputs, const StreamSetBuffers & outputs);
    7576
    7677    // Generate the Kernel to the current module (iBuilder->getModule()).
    7778    void generateKernel();
    78     void generateKernel(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
     79    void generateKernel(const StreamSetBuffers & inputs, const StreamSetBuffers & outputs);
    7980   
    8081    void createInstance() override;
  • icGREP/icgrep-devel/icgrep/kernels/linebreak_kernel.cpp

    r5385 r5399  
    2828, std::string linebreak
    2929, unsigned basisBitsCount)
    30 : PabloKernel(iBuilder, linebreak +"_kernel", {Binding{iBuilder->getStreamSetTy(basisBitsCount), "basis"}}, {Binding{iBuilder->getStreamSetTy(1), "linebreak", Add1()}}) {
     30: PabloKernel(iBuilder, "Parabix:" + linebreak + "_kernel", {Binding{iBuilder->getStreamSetTy(basisBitsCount), "basis"}}, {Binding{iBuilder->getStreamSetTy(1), "linebreak", Add1()}}) {
    3131
    3232    CC_Compiler ccc(this, getInput(0));
  • icGREP/icgrep-devel/icgrep/kernels/stdin_kernel.cpp

    r5398 r5399  
    6868
    6969StdInKernel::StdInKernel(IDISA::IDISA_Builder * iBuilder, unsigned blocksPerSegment, unsigned codeUnitWidth)
    70 : SegmentOrientedKernel(iBuilder, "stdin_source", {}, {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "InputStream"}}, {}, {}, {})
     70: SegmentOrientedKernel(iBuilder, "Parabix:stdin_source", {}, {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "InputStream"}}, {}, {}, {})
    7171, mSegmentBlocks(blocksPerSegment)
    7272, mCodeUnitWidth(codeUnitWidth) {
  • icGREP/icgrep-devel/icgrep/object_cache.cpp

    r5394 r5399  
    152152    if (ModID.substr(0, PrefixLength) != Prefix)
    153153        return false;
    154 
    155154    CacheName = CacheDir;
    156155    sys::path::append(CacheName, ModID.substr(PrefixLength) + ".o");
  • icGREP/icgrep-devel/icgrep/toolchain.cpp

    r5398 r5399  
    322322            mCache = llvm::make_unique<ParabixObjectCache>(codegen::ObjectCacheDir);
    323323        }
    324         if (mCache) {
    325             mEngine->setObjectCache(mCache.get());
    326         }
     324        assert (mCache);
     325        mEngine->setObjectCache(mCache.get());
    327326    }
    328327}
     
    365364    assert (mModuleMap.count(&kb) == 0);
    366365    mKernelList.push_back(&kb);
    367     Module * saveM = iBuilder->getModule();
    368     mModuleMap.emplace(&kb, std::move(kb.createKernelStub()));
    369     assert (iBuilder->getModule() == saveM);
    370     kb.setCallParameters(inputs, outputs);
     366    mModuleMap.emplace(&kb, std::move(kb.createKernelStub(inputs, outputs)));
    371367}
    372368
     
    394390void ParabixDriver::linkAndFinalize() {
    395391    for (kernel::KernelBuilder * kb : mKernelList) {
    396         Module * saveM = iBuilder->getModule();
    397392        const auto f = mModuleMap.find(kb);
    398393        if (LLVM_UNLIKELY(f == mModuleMap.end())) {
     
    400395        }
    401396        std::unique_ptr<Module> km(std::move(f->second));
    402         std::string moduleID = km->getModuleIdentifier();
    403         std::string signature;
    404         if (kb->moduleIDisSignature()) {
    405             signature = moduleID;
    406         } else {
    407             kb->generateKernelSignature(signature);
    408         }
    409         if (!(mCache && mCache->loadCachedObjectFile(moduleID, signature))) {
     397        bool uncachedObject = true;
     398        if (mCache) {
     399            std::string moduleID = km->getModuleIdentifier();
     400            std::string signature;
     401            if (kb->moduleIDisSignature()) {
     402                signature = moduleID;
     403            } else {
     404                kb->generateKernelSignature(signature);
     405            }
     406            if (mCache->loadCachedObjectFile(moduleID, signature)) {
     407                uncachedObject = false;
     408            }
     409        }
     410        if (uncachedObject) {
     411            Module * const saveM = iBuilder->getModule();
    410412            iBuilder->setModule(km.get());
    411413            kb->generateKernel();
    412         }
    413         iBuilder->setModule(saveM);
     414            iBuilder->setModule(saveM);
     415        }       
    414416        mEngine->addModule(std::move(km));
    415417    }
     
    421423    return mEngine->getPointerToNamedFunction("Main");
    422424}
    423 
  • icGREP/icgrep-devel/icgrep/toolchain.h

    r5398 r5399  
    100100namespace {
    101101
    102 // NOTE: Currently, LLVM TypeBuilder only work for up to 5 arguments. The following templates
    103 // avoid that limit but should be deprecated if the TypeBuilder ever supports n-ary functions.
     102// NOTE: Currently, LLVM TypeBuilder can deduce FuntionTypes for only up to 5 arguments. The following
     103// templates have no limit but should be deprecated if the TypeBuilder ever supports n-ary functions.
    104104
    105 template <typename... Args>
     105template<unsigned i, typename... Args>
    106106class ParameterTypeBuilder;
    107107
    108 template<typename A1, typename... An>
    109 struct ParameterTypeBuilder<A1, An...> {
    110     static void get(llvm::LLVMContext & C, std::vector<llvm::Type *> & params) {
    111         ParameterTypeBuilder<A1>::get(C, params);
    112         ParameterTypeBuilder<An...>::get(C, params);
     108template<unsigned i, typename A1, typename... An>
     109struct ParameterTypeBuilder<i, A1, An...> {
     110    static void get(llvm::LLVMContext & C, llvm::Type ** params) {
     111        ParameterTypeBuilder<i, A1>::get(C, params);
     112        ParameterTypeBuilder<i + 1, An...>::get(C, params);
    113113    }
    114114};
    115115
    116 template<typename A>
    117 struct ParameterTypeBuilder<A> {
    118     static void get(llvm::LLVMContext & C, std::vector<llvm::Type *> & params) {
    119         params.push_back(llvm::TypeBuilder<A, false>::get(C));
     116template<unsigned i, typename A>
     117struct ParameterTypeBuilder<i, A> {
     118    static void get(llvm::LLVMContext & C, llvm::Type ** params) {
     119        params[i] = llvm::TypeBuilder<A, false>::get(C);
    120120    }
    121121};
     
    127127struct FunctionTypeBuilder<R(Args...)> {
    128128    static llvm::FunctionType * get(llvm::LLVMContext & C) {
    129         auto result = llvm::TypeBuilder<R, false>::get(C);
    130         std::vector<llvm::Type *> params(0);
    131         params.reserve(sizeof...(Args));
    132         ParameterTypeBuilder<Args...>::get(C, params);
    133         return llvm::FunctionType::get(result, params, false);
     129        llvm::Type * params[sizeof...(Args)];
     130        ParameterTypeBuilder<0, Args...>::get(C, params);
     131        return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), params, false);
    134132    }
    135133};
     
    138136struct FunctionTypeBuilder<R()> {
    139137    static llvm::FunctionType * get(llvm::LLVMContext & C) {
    140         auto result = llvm::TypeBuilder<R, false>::get(C);
    141         return llvm::FunctionType::get(result, false);
     138        return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), false);
    142139    }
    143140};
     
    148145void ParabixDriver::addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const {
    149146    llvm::FunctionType * const type = FunctionTypeBuilder<ExternalFunctionType>::get(iBuilder->getContext());
    150     assert ("FunctionTypeBuilder did not correctly resolve the current function type." && type);
     147    assert ("FunctionTypeBuilder did not resolve a function type." && type);
    151148    addExternalLink(kb, name, type, reinterpret_cast<void *>(functionPtr));
    152149}
Note: See TracChangeset for help on using the changeset viewer.