Ignore:
Timestamp:
Apr 25, 2017, 2:33:31 PM (2 years ago)
Author:
nmedfort
Message:

Changes towards separate compilation

File:
1 edited

Legend:

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

    r5419 r5425  
    1212#include <boost/filesystem.hpp>
    1313#include <IR_Gen/idisa_builder.h>
    14 #include <IR_Gen/idisa_target.h>
    1514#include <UCD/UnicodeNameData.h>
    1615#include <UCD/resolve_properties.h>
     
    2827#include <re/re_cc.h>
    2928#include <re/re_toolchain.h>
    30 #include <kernels/toolchain.h>
     29#include <toolchain/toolchain.h>
    3130#include <iostream>
    3231#include <sstream>
     
    194193}
    195194
    196 inline void linkGrepFunction(ParabixDriver & pxDriver, const GrepType grepType, const bool UTF_16, kernel::KernelBuilder & kernel) {
    197     switch (grepType) {
    198         case GrepType::Normal:
    199             if (UTF_16) {
    200                 pxDriver.addExternalLink(kernel, "matcher", &wrapped_report_match<uint16_t>);
    201             } else {
    202                 pxDriver.addExternalLink(kernel, "matcher", &wrapped_report_match<uint8_t>);
    203             }
    204             break;
    205         case GrepType::NameExpression:
    206             pxDriver.addExternalLink(kernel, "matcher", &insert_codepoints);
    207             break;
    208         case GrepType::PropertyValue:
    209             pxDriver.addExternalLink(kernel, "matcher", &insert_property_values);
    210             break;
    211     }
    212 }
    213 
    214 void GrepEngine::grepCodeGen(std::string moduleName, std::vector<re::RE *> REs, const bool CountOnly, const bool UTF_16, GrepSource grepSource, const GrepType grepType) {
    215 
    216     Module * M = new Module(moduleName + ":icgrep", getGlobalContext());;
    217     IDISA::IDISA_Builder * iBuilder = IDISA::GetIDISA_Builder(M);;
    218     ParabixDriver pxDriver(iBuilder);
     195void GrepEngine::grepCodeGen(const std::string & moduleName, std::vector<re::RE *> REs, const bool CountOnly, const bool UTF_16, GrepSource grepSource, const GrepType grepType) {
     196
     197    ParabixDriver pxDriver(moduleName + ":icgrep");
     198    auto idb = pxDriver.getIDISA_Builder();
     199    Module * M = idb->getModule();
    219200
    220201    const unsigned segmentSize = codegen::SegmentSize;
     
    222203    const unsigned encodingBits = UTF_16 ? 16 : 8;
    223204
    224     Type * const int64Ty = iBuilder->getInt64Ty();
    225     Type * const int32Ty = iBuilder->getInt32Ty();
     205    Type * const int64Ty = idb->getInt64Ty();
     206    Type * const int32Ty = idb->getInt32Ty();
    226207
    227208    Function * mainFunc = nullptr;
     
    232213    if (grepSource == GrepSource::Internal) {
    233214
    234         mainFunc = cast<Function>(M->getOrInsertFunction("Main", int64Ty, iBuilder->getInt8PtrTy(), int64Ty, int32Ty, nullptr));
     215        mainFunc = cast<Function>(M->getOrInsertFunction("Main", int64Ty, idb->getInt8PtrTy(), int64Ty, int32Ty, nullptr));
    235216        mainFunc->setCallingConv(CallingConv::C);
    236         iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
    237         Function::arg_iterator args = mainFunc->arg_begin();
     217        idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
     218        auto args = mainFunc->arg_begin();
    238219
    239220        Value * const buffer = &*(args++);
     
    242223        Value * length = &*(args++);
    243224        length->setName("length");
    244         length = iBuilder->CreateZExtOrTrunc(length, iBuilder->getSizeTy());
     225        length = idb->CreateZExtOrTrunc(length, idb->getSizeTy());
    245226
    246227        fileIdx = &*(args++);
    247228        fileIdx->setName("fileIdx");
    248229
    249         ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
    250 
    251         sourceK = pxDriver.addKernelInstance(make_unique<kernel::FileSourceKernel>(iBuilder, iBuilder->getInt8PtrTy(), segmentSize));
     230        ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(idb, idb->getStreamSetTy(1, 8)));
     231
     232        sourceK = pxDriver.addKernelInstance(make_unique<kernel::FileSourceKernel>(idb, idb->getInt8PtrTy(), segmentSize));
    252233        sourceK->setInitialArguments({buffer, length});
    253234
    254235    } else {
    255236
    256         mainFunc = cast<Function>(M->getOrInsertFunction("Main", int64Ty, iBuilder->getInt32Ty(), int32Ty, nullptr));
     237        mainFunc = cast<Function>(M->getOrInsertFunction("Main", int64Ty, idb->getInt32Ty(), int32Ty, nullptr));
    257238        mainFunc->setCallingConv(CallingConv::C);
    258         iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
    259         Function::arg_iterator args = mainFunc->arg_begin();
     239        idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
     240        auto args = mainFunc->arg_begin();
    260241
    261242        Value * const fileDescriptor = &*(args++);
     
    265246
    266247        if (grepSource == GrepSource::File) {
    267             ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
    268             sourceK = pxDriver.addKernelInstance(make_unique<kernel::MMapSourceKernel>(iBuilder, segmentSize));
     248            ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(idb, idb->getStreamSetTy(1, 8)));
     249            sourceK = pxDriver.addKernelInstance(make_unique<kernel::MMapSourceKernel>(idb, segmentSize));
    269250            sourceK->setInitialArguments({fileDescriptor});
    270251        } else { // if (grepSource == GrepSource::StdIn) {
    271             ByteStream = pxDriver.addBuffer(make_unique<ExtensibleBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize));
    272             sourceK = pxDriver.addKernelInstance(make_unique<kernel::StdInKernel>(iBuilder, segmentSize));
     252            ByteStream = pxDriver.addBuffer(make_unique<ExtensibleBuffer>(idb, idb->getStreamSetTy(1, 8), segmentSize));
     253            sourceK = pxDriver.addKernelInstance(make_unique<kernel::StdInKernel>(idb, segmentSize));
    273254        }
    274255    }
    275256
    276257    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
    277     StreamSetBuffer * BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), segmentSize * bufferSegments));
     258    StreamSetBuffer * BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize * bufferSegments));
    278259   
    279     kernel::KernelBuilder * s2pk = pxDriver.addKernelInstance(make_unique<kernel::S2PKernel>(iBuilder));
     260    kernel::KernelBuilder * s2pk = pxDriver.addKernelInstance(make_unique<kernel::S2PKernel>(idb));
    280261    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
    281262   
    282     kernel::KernelBuilder * linebreakK = pxDriver.addKernelInstance(make_unique<kernel::LineBreakKernelBuilder>(iBuilder, encodingBits));
    283     StreamSetBuffer * LineBreakStream = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     263    kernel::KernelBuilder * linebreakK = pxDriver.addKernelInstance(make_unique<kernel::LineBreakKernelBuilder>(idb, encodingBits));
     264    StreamSetBuffer * LineBreakStream = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    284265    pxDriver.makeKernelCall(linebreakK, {BasisBits}, {LineBreakStream});
    285266   
     
    289270
    290271    for(unsigned i = 0; i < n; ++i){
    291         StreamSetBuffer * MatchResults = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    292         kernel::KernelBuilder * icgrepK = pxDriver.addKernelInstance(make_unique<kernel::ICgrepKernelBuilder>(iBuilder, REs[i]));
     272        StreamSetBuffer * MatchResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     273        kernel::KernelBuilder * icgrepK = pxDriver.addKernelInstance(make_unique<kernel::ICgrepKernelBuilder>(idb, REs[i]));
    293274        pxDriver.makeKernelCall(icgrepK, {BasisBits, LineBreakStream}, {MatchResults});
    294275        MatchResultsBufs[i] = MatchResults;
     
    296277    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
    297278    if (REs.size() > 1) {
    298         MergedResults = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    299         kernel::KernelBuilder * streamsMergeK = pxDriver.addKernelInstance(make_unique<kernel::StreamsMerge>(iBuilder, 1, REs.size()));
     279        MergedResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     280        kernel::KernelBuilder * streamsMergeK = pxDriver.addKernelInstance(make_unique<kernel::StreamsMerge>(idb, 1, REs.size()));
    300281        pxDriver.makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
    301282    }
    302283   
    303284    if (AlgorithmOptionIsSet(re::InvertMatches)) {
    304         kernel::KernelBuilder * invertK = pxDriver.addKernelInstance(make_unique<kernel::InvertMatchesKernel>(iBuilder));
     285        kernel::KernelBuilder * invertK = pxDriver.addKernelInstance(make_unique<kernel::InvertMatchesKernel>(idb));
    305286        StreamSetBuffer * OriginalMatches = MergedResults;
    306         MergedResults = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     287        MergedResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    307288        pxDriver.makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {MergedResults});
    308289    }
    309290    if (CountOnly) {
    310         kernel::MatchCount matchCountK(iBuilder);
     291        kernel::MatchCount matchCountK(idb);
    311292        pxDriver.addKernelCall(matchCountK, {MergedResults}, {});
    312293        pxDriver.generatePipelineIR();
    313294        Value * matchedLineCount = matchCountK.getScalarField("matchedLineCount");
    314         matchedLineCount = iBuilder->CreateZExt(matchedLineCount, int64Ty);
    315         iBuilder->CreateRet(matchedLineCount);
     295        matchedLineCount = idb->CreateZExt(matchedLineCount, int64Ty);
     296        idb->CreateRet(matchedLineCount);
    316297        pxDriver.linkAndFinalize();
    317298    } else {
    318         kernel::ScanMatchKernel scanMatchK(iBuilder, grepType, encodingBits);
     299        kernel::ScanMatchKernel scanMatchK(idb, grepType, encodingBits);
    319300        scanMatchK.setInitialArguments({fileIdx});
    320301        pxDriver.addKernelCall(scanMatchK, {MergedResults, LineBreakStream, ByteStream}, {});
    321         linkGrepFunction(pxDriver, grepType, UTF_16, scanMatchK);
     302        switch (grepType) {
     303            case GrepType::Normal:
     304                if (UTF_16) {
     305                    pxDriver.LinkFunction(scanMatchK, "matcher", &wrapped_report_match<uint16_t>);
     306                } else {
     307                    pxDriver.LinkFunction(scanMatchK, "matcher", &wrapped_report_match<uint8_t>);
     308                }
     309                break;
     310            case GrepType::NameExpression:
     311                pxDriver.LinkFunction(scanMatchK, "matcher", &insert_codepoints);
     312                break;
     313            case GrepType::PropertyValue:
     314                pxDriver.LinkFunction(scanMatchK, "matcher", &insert_property_values);
     315                break;
     316        }
    322317        pxDriver.generatePipelineIR();
    323         iBuilder->CreateRet(iBuilder->getInt64(0));
     318        idb->CreateRet(idb->getInt64(0));
    324319        pxDriver.linkAndFinalize();
    325320    }
Note: See TracChangeset for help on using the changeset viewer.