Ignore:
Timestamp:
Jul 5, 2017, 10:34:41 AM (2 years ago)
Author:
cameron
Message:

Decouple finalizeMatchResults, creating separate MatchedLinesKernel?

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

Legend:

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

    r5493 r5548  
    8383}
    8484
     85bool GrepEngine::matchesNeedToBeMovedToEOL() const {
     86    if ((Mode == QuietMode) | (Mode == FilesWithMatch) | (Mode == FilesWithoutMatch)) {
     87        return false;
     88    }
     89    else if (LineRegexpFlag) {
     90        return false;
     91    }
     92    // TODO: return false for other cases based on regexp analysis, e.g., regexp ends with $.
     93    return true;
     94}
     95   
    8596void GrepEngine::doGrep(const std::string & fileName) const{
    8697#ifdef CUDA_ENABLED
     
    306317    Type * const int64tyPtr = PointerType::get(int64Ty, 1);
    307318    Type * const voidTy = idb->getVoidTy();
     319   
     320    size_t MatchLimit = ((grepMode == QuietMode) | (grepMode == FilesWithMatch) | (grepMode == FilesWithoutMatch)) ? 1 : MaxCountFlag;
    308321
    309322    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", voidTy, int64tyPtr, sizeTyPtr, sizeTyPtr, int64tyPtr, nullptr));
     
    362375        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
    363376    }
    364 
     377    StreamSetBuffer * Matches = MergedResults;
     378
     379    if (matchesNeedToBeMovedToEOL()) {
     380        StreamSetBuffer * OriginalMatches = Matches;
     381        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance(make_unique<kernel::MatchedLinesKernel>(idb));
     382        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     383        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
     384    }
     385   
     386    if (InvertMatchFlag) {
     387        kernel::Kernel * invertK = mGrepDriver->addKernelInstance(make_unique<kernel::InvertMatchesKernel>(idb));
     388        StreamSetBuffer * OriginalMatches = Matches;
     389        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     390        mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {Matches});
     391    }
     392    if (MatchLimit > 0) {
     393        kernel::Kernel * untilK = mGrepDriver->addKernelInstance(make_unique<kernel::UntilNkernel>(idb));
     394        untilK->setInitialArguments({idb->getSize(MatchLimit)});
     395        StreamSetBuffer * AllMatches = Matches;
     396        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     397        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
     398    }
     399   
    365400    kernel::Kernel * matchCountK = mGrepDriver->addKernelInstance(make_unique<kernel::PopcountKernel>(idb));
    366     mGrepDriver->makeKernelCall(matchCountK, {MergedResults}, {});
     401    mGrepDriver->makeKernelCall(matchCountK, {Matches}, {});
    367402    mGrepDriver->generatePipelineIR();
    368403    idb->setKernel(matchCountK);
     
    441476        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
    442477    }
     478    StreamSetBuffer * Matches = MergedResults;
     479   
     480    if (matchesNeedToBeMovedToEOL()) {
     481        StreamSetBuffer * OriginalMatches = Matches;
     482        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance(make_unique<kernel::MatchedLinesKernel>(idb));
     483        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     484        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
     485    }
    443486   
    444487    if (InvertMatchFlag) {
    445488        kernel::Kernel * invertK = mGrepDriver->addKernelInstance(make_unique<kernel::InvertMatchesKernel>(idb));
    446         StreamSetBuffer * OriginalMatches = MergedResults;
    447         MergedResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    448         mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {MergedResults});
     489        StreamSetBuffer * OriginalMatches = Matches;
     490        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     491        mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {Matches});
    449492    }
    450493    if (MatchLimit > 0) {
    451494        kernel::Kernel * untilK = mGrepDriver->addKernelInstance(make_unique<kernel::UntilNkernel>(idb));
    452495        untilK->setInitialArguments({idb->getSize(MatchLimit)});
    453         StreamSetBuffer * AllMatches = MergedResults;
    454         MergedResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    455         mGrepDriver->makeKernelCall(untilK, {AllMatches}, {MergedResults});
     496        StreamSetBuffer * AllMatches = Matches;
     497        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     498        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
    456499    }
    457500    if (grepMode == NormalMode) {
    458501        kernel::Kernel * scanMatchK = mGrepDriver->addKernelInstance(make_unique<kernel::ScanMatchKernel>(idb, GrepType::Normal, encodingBits));
    459502        scanMatchK->setInitialArguments({fileIdx});
    460         mGrepDriver->makeKernelCall(scanMatchK, {MergedResults, LineBreakStream, ByteStream}, {});
     503        mGrepDriver->makeKernelCall(scanMatchK, {Matches, LineBreakStream, ByteStream}, {});
    461504        if (UTF_16) {
    462505            mGrepDriver->LinkFunction(*scanMatchK, "matcher", &wrapped_report_match<uint16_t>);
     
    465508        }
    466509        mGrepDriver->generatePipelineIR();
     510        mGrepDriver->deallocateBuffers();
     511
    467512        idb->CreateRet(idb->getInt64(0));
    468513    } else {
    469514        kernel::Kernel * matchCountK = mGrepDriver->addKernelInstance(make_unique<kernel::PopcountKernel>(idb));
    470         mGrepDriver->makeKernelCall(matchCountK, {MergedResults}, {});
     515        mGrepDriver->makeKernelCall(matchCountK, {Matches}, {});
    471516        mGrepDriver->generatePipelineIR();
    472517        idb->setKernel(matchCountK);
    473518        Value * matchedLineCount = idb->getAccumulator("countResult");
    474519        matchedLineCount = idb->CreateZExt(matchedLineCount, int64Ty);
     520        mGrepDriver->deallocateBuffers();
    475521        idb->CreateRet(matchedLineCount);
    476522    }
     
    546592    pxDriver.makeKernelCall(icgrepK, {BasisBits, LineBreakStream}, {MatchResults});
    547593   
     594    StreamSetBuffer * MatchedLines = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize));
     595    kernel::Kernel * matchedLinesK = pxDriver.addKernelInstance(make_unique<kernel::MatchedLinesKernel>(idb));
     596    pxDriver.makeKernelCall(matchedLinesK, {MatchResults, LineBreakStream}, {MatchedLines});
     597   
    548598    kernel::Kernel * scanMatchK = pxDriver.addKernelInstance(make_unique<kernel::ScanMatchKernel>(idb, GrepType::NameExpression, 8));
    549599    scanMatchK->setInitialArguments({idb->getInt32(0)});
    550     pxDriver.makeKernelCall(scanMatchK, {MatchResults, LineBreakStream, ByteStream}, {});
     600    pxDriver.makeKernelCall(scanMatchK, {MatchedLines, LineBreakStream, ByteStream}, {});
    551601    pxDriver.LinkFunction(*scanMatchK, "matcher", &insert_codepoints);
    552602    pxDriver.generatePipelineIR();
     
    618668    kernel::Kernel * icgrepK = pxDriver.addKernelInstance(make_unique<kernel::ICGrepKernel>(idb, propertyValuePattern));
    619669    pxDriver.makeKernelCall(icgrepK, {BasisBits, LineBreakStream}, {MatchResults});
    620    
     670
     671    StreamSetBuffer * MatchedLines = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize));
     672    kernel::Kernel * matchedLinesK = pxDriver.addKernelInstance(make_unique<kernel::MatchedLinesKernel>(idb));
     673    pxDriver.makeKernelCall(matchedLinesK, {MatchResults, LineBreakStream}, {MatchedLines});
     674
    621675    kernel::Kernel * scanMatchK = pxDriver.addKernelInstance(make_unique<kernel::ScanMatchKernel>(idb, GrepType::PropertyValue, 8));
    622676    scanMatchK->setInitialArguments({idb->getInt32(0)});
    623     pxDriver.makeKernelCall(scanMatchK, {MatchResults, LineBreakStream, ByteStream}, {});
     677    pxDriver.makeKernelCall(scanMatchK, {MatchedLines, LineBreakStream, ByteStream}, {});
    624678    pxDriver.LinkFunction(*scanMatchK, "matcher", &insert_property_values);
    625679    pxDriver.generatePipelineIR();
  • icGREP/icgrep-devel/icgrep/grep_engine.h

    r5484 r5548  
    3636
    3737    void doGrep(const std::string & fileName) const;
     38   
     39    bool matchesNeedToBeMovedToEOL() const;
    3840
    3941    uint64_t doGrep(const std::string & fileName, const uint32_t fileIdx) const;
  • icGREP/icgrep-devel/icgrep/kernels/grep_kernel.cpp

    r5491 r5548  
    1111#include <kernels/kernel_builder.h>
    1212#include <pablo/builder.hpp>
     13#include <pablo/boolean.h>
    1314#include <pablo/pe_count.h>
     15#include <pablo/pe_matchstar.h>
    1416
    1517#include <llvm/Support/raw_ostream.h>
     
    5456}
    5557
     58void MatchedLinesKernel::generatePabloMethod() {
     59    auto pb = this->getEntryBlock();
     60    PabloAST * matchResults = pb->createExtract(getInputStreamVar("matchResults"), pb->getInteger(0));
     61    PabloAST * lineBreaks = pb->createExtract(getInputStreamVar("lineBreaks"), pb->getInteger(0));
     62    PabloAST * notLB = pb->createNot(lineBreaks);
     63    PabloAST * match_follow = pb->createMatchStar(matchResults, notLB);
     64    Var * const matchedLines = getOutputStreamVar("matchedLines");
     65    pb->createAssign(pb->createExtract(matchedLines, pb->getInteger(0)), pb->createAnd(match_follow, lineBreaks));
     66}
     67
     68MatchedLinesKernel::MatchedLinesKernel (const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
     69: PabloKernel(iBuilder, "MatchedLines",
     70              {Binding{iBuilder->getStreamSetTy(1), "matchResults"}, Binding{iBuilder->getStreamSetTy(1), "lineBreaks"}},
     71              {Binding{iBuilder->getStreamSetTy(1), "matchedLines"}},
     72              {},
     73              {}) {
     74}
     75
     76
    5677void InvertMatchesKernel::generateDoBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder) {
    5778    Value * input = iBuilder->loadInputStreamBlock("matchedLines", iBuilder->getInt32(0));
     
    7495}
    7596
    76 
    7797PopcountKernel::PopcountKernel (const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
    7898: PabloKernel(iBuilder, "Popcount",
     
    81101              {},
    82102              {Binding{iBuilder->getSizeTy(), "countResult"}}) {
    83    
    84103}
    85104
  • icGREP/icgrep-devel/icgrep/kernels/grep_kernel.h

    r5491 r5548  
    2828};
    2929
     30class MatchedLinesKernel : public pablo::PabloKernel {
     31public:
     32    MatchedLinesKernel(const std::unique_ptr<kernel::KernelBuilder> & builder);
     33    bool isCachable() const override { return true; }
     34    bool hasSignature() const override { return false; }
     35protected:
     36    void generatePabloMethod() override;   
     37};
     38
    3039class InvertMatchesKernel : public BlockOrientedKernel {
    3140public:
     
    3443    void generateDoBlockMethod(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
    3544};
    36 
    3745
    3846class PopcountKernel : public pablo::PabloKernel {
     
    4452    void generatePabloMethod() override;   
    4553};
    46 
     54   
    4755}
    4856#endif
  • icGREP/icgrep-devel/icgrep/re/re_compiler.cpp

    r5413 r5548  
    198198}
    199199
    200 void RE_Compiler::finalizeMatchResult(MarkerType match_result) {
    201     PabloAST * match_follow = mPB.createMatchStar(markerVar(match_result), mAny);
    202     PabloAST * matches = mPB.createAnd(match_follow, mLineBreak, "matchedLine3s");
     200void RE_Compiler::compile(RE * re) {
     201    MarkerType match_results = compile(re, mPB);
    203202    Var * const output = mKernel->getOutputStreamVar("matches");
    204     mPB.createAssign(mPB.createExtract(output, mPB.getInteger(0)), matches);
    205 }
    206 
     203    mPB.createAssign(mPB.createExtract(output, mPB.getInteger(0)), markerVar(match_results));
     204}
     205   
    207206MarkerType RE_Compiler::compile(RE * re, PabloBuilder & pb) {
    208207    return process(re, makeMarker(MarkerPosition::FinalPostPositionUnit, pb.createOnes()), pb);
    209208}
    210 
     209   
    211210MarkerType RE_Compiler::process(RE * re, MarkerType marker, PabloBuilder & pb) {
    212211    if (isa<Name>(re)) {
  • icGREP/icgrep-devel/icgrep/re/re_compiler.h

    r5413 r5548  
    6363    void initializeRequiredStreams(const unsigned encodingBits, pablo::Var * linebreak);
    6464    void compileUnicodeNames(RE *& re);
    65     void finalizeMatchResult(MarkerType match_result);
    66     MarkerType compile(RE * re) {
    67         return compile(re, mPB);
    68     }
     65    void compile(RE * re);
    6966
    7067    static LLVM_ATTRIBUTE_NORETURN void UnsupportedRE(std::string errmsg);
  • icGREP/icgrep-devel/icgrep/re/re_toolchain.cpp

    r5493 r5548  
    9898    re_compiler.initializeRequiredStreams(basis->getType()->getArrayNumElements(), linebreak);
    9999    re_compiler.compileUnicodeNames(re_ast);
    100     re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
     100    re_compiler.compile(re_ast);
    101101}
    102102
Note: See TracChangeset for help on using the changeset viewer.