Ignore:
Timestamp:
May 24, 2017, 12:06:43 PM (2 years ago)
Author:
nmedfort
Message:

Eliminated ExecutionEngine? memory leak. Intentionally broke compatibility with prior versions to ensure unchecked in projects are restructured.

File:
1 edited

Legend:

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

    r5473 r5474  
    174174static std::vector<std::string> parsedPropertyValues;
    175175
    176 std::string PTXFilename = "icgrep.ptx";
    177176size_t * startPoints = nullptr;
    178177size_t * accumBytes = nullptr;
     
    236235
    237236uint64_t GrepEngine::doGrep(const int32_t fileDescriptor, const uint32_t fileIdx) const {
    238     assert (mGrepFunction);
     237    assert (mGrepDriver);
    239238    typedef uint64_t (*GrepFunctionType)(int32_t fileDescriptor, const uint32_t fileIdx);
    240     return reinterpret_cast<GrepFunctionType>(mGrepFunction)(fileDescriptor, fileIdx);
     239    auto f = reinterpret_cast<GrepFunctionType>(mGrepDriver->getMain());
     240    return f(fileDescriptor, fileIdx);
    241241}
    242242
    243243void GrepEngine::doGrep(const char * buffer, const uint64_t length, const uint32_t fileIdx) const {
    244     assert (mGrepFunction);
     244    assert (mGrepDriver);
    245245    typedef uint64_t (*GrepFunctionType)(const char * buffer, const uint64_t length, const uint32_t fileIdx);
    246     reinterpret_cast<GrepFunctionType>(mGrepFunction)(buffer, length, fileIdx);
     246    auto f = reinterpret_cast<GrepFunctionType>(mGrepDriver->getMain());
     247    f(buffer, length, fileIdx);
    247248}
    248249
     
    391392void GrepEngine::grepCodeGen_nvptx(std::vector<re::RE *> REs, const GrepModeType grepMode, const bool UTF_16) {
    392393
    393     NVPTXDriver pxDriver("engine");
    394     auto & idb = pxDriver.getBuilder();
     394    assert (mGrepDriver == nullptr);
     395
     396    mGrepDriver = new NVPTXDriver("engine");
     397    auto & idb = mGrepDriver->getBuilder();
    395398    Module * M = idb->getModule();
    396399
     
    432435    Value * bufferSize = idb->CreateLoad(idb->CreateGEP(bufferSizesPtr, bid));
    433436
    434     StreamSetBuffer * ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8), 1));
    435     kernel::Kernel * sourceK = pxDriver.addKernelInstance(make_unique<kernel::MemorySourceKernel>(idb, inputStreamType, segmentSize));
     437    StreamSetBuffer * ByteStream = mGrepDriver->addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8), 1));
     438    kernel::Kernel * sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::MemorySourceKernel>(idb, inputStreamType, segmentSize));
    436439    sourceK->setInitialArguments({inputStream, bufferSize});
    437     pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
    438 
    439     StreamSetBuffer * BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize * bufferSegments));   
    440     kernel::Kernel * s2pk = pxDriver.addKernelInstance(make_unique<kernel::S2PKernel>(idb));
    441     pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
     440    mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
     441
     442    StreamSetBuffer * BasisBits = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize * bufferSegments));
     443    kernel::Kernel * s2pk = mGrepDriver->addKernelInstance(make_unique<kernel::S2PKernel>(idb));
     444    mGrepDriver->makeKernelCall(s2pk, {ByteStream}, {BasisBits});
    442445 
    443     StreamSetBuffer * LineBreakStream = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));   
    444     kernel::Kernel * linebreakK = pxDriver.addKernelInstance(make_unique<kernel::LineBreakKernelBuilder>(idb, encodingBits));
    445     pxDriver.makeKernelCall(linebreakK, {BasisBits}, {LineBreakStream});
     446    StreamSetBuffer * LineBreakStream = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     447    kernel::Kernel * linebreakK = mGrepDriver->addKernelInstance(make_unique<kernel::LineBreakKernelBuilder>(idb, encodingBits));
     448    mGrepDriver->makeKernelCall(linebreakK, {BasisBits}, {LineBreakStream});
    446449   
    447450    const auto n = REs.size();
     
    450453
    451454    for(unsigned i = 0; i < n; ++i){
    452         StreamSetBuffer * MatchResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    453         kernel::Kernel * icgrepK = pxDriver.addKernelInstance(make_unique<kernel::ICGrepKernel>(idb, REs[i]));
    454         pxDriver.makeKernelCall(icgrepK, {BasisBits, LineBreakStream}, {MatchResults});
     455        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     456        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance(make_unique<kernel::ICGrepKernel>(idb, REs[i]));
     457        mGrepDriver->makeKernelCall(icgrepK, {BasisBits, LineBreakStream}, {MatchResults});
    455458        MatchResultsBufs[i] = MatchResults;
    456459    }
    457460    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
    458461    if (REs.size() > 1) {
    459         MergedResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    460         kernel::Kernel * streamsMergeK = pxDriver.addKernelInstance(make_unique<kernel::StreamsMerge>(idb, 1, REs.size()));
    461         pxDriver.makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
     462        MergedResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     463        kernel::Kernel * streamsMergeK = mGrepDriver->addKernelInstance(make_unique<kernel::StreamsMerge>(idb, 1, REs.size()));
     464        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
    462465    }
    463466
    464467    kernel::MatchCount matchCountK(idb);
    465     pxDriver.addKernelCall(matchCountK, {MergedResults}, {});
    466     pxDriver.generatePipelineIR();
     468    mGrepDriver->addKernelCall(matchCountK, {MergedResults}, {});
     469    mGrepDriver->generatePipelineIR();
    467470
    468471    idb->setKernel(&matchCountK);
     
    475478    idb->CreateRetVoid();
    476479
    477     pxDriver.finalizeAndCompile(mainFunc, PTXFilename);
     480    mGrepDriver->finalizeObject();
    478481}
    479482
    480483void GrepEngine::grepCodeGen(std::vector<re::RE *> REs, const GrepModeType grepMode, const bool UTF_16, GrepSource grepSource, const GrepType grepType) {
    481484
    482     ParabixDriver pxDriver("engine");
    483     auto & idb = pxDriver.getBuilder();
     485    assert (mGrepDriver == nullptr);
     486    mGrepDriver = new ParabixDriver("engine");
     487    auto & idb = mGrepDriver->getBuilder();
    484488    Module * M = idb->getModule();
    485489
     
    515519        fileIdx->setName("fileIdx");
    516520
    517         ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8)));
    518 
    519         sourceK = pxDriver.addKernelInstance(make_unique<kernel::MemorySourceKernel>(idb, idb->getInt8PtrTy(), segmentSize));
     521        ByteStream = mGrepDriver->addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8)));
     522
     523        sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::MemorySourceKernel>(idb, idb->getInt8PtrTy(), segmentSize));
    520524        sourceK->setInitialArguments({buffer, length});
    521525
     
    532536        fileIdx->setName("fileIdx");
    533537
    534         ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8)));
     538        ByteStream = mGrepDriver->addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8)));
    535539
    536540        if (grepSource == GrepSource::File) {
    537             sourceK = pxDriver.addKernelInstance(make_unique<kernel::MMapSourceKernel>(idb, segmentSize));
     541            sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::MMapSourceKernel>(idb, segmentSize));
    538542            sourceK->setInitialArguments({fileDescriptor});
    539543        } else { // if (grepSource == GrepSource::StdIn) {
    540             sourceK = pxDriver.addKernelInstance(make_unique<kernel::ReadSourceKernel>(idb, segmentSize));
     544            sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::ReadSourceKernel>(idb, segmentSize));
    541545            sourceK->setInitialArguments({idb->getInt32(STDIN_FILENO)});
    542546        }
    543547    }
    544548
    545     pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
    546     StreamSetBuffer * BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize * bufferSegments));
    547    
    548     kernel::Kernel * s2pk = pxDriver.addKernelInstance(make_unique<kernel::S2PKernel>(idb));
    549     pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
    550    
    551     kernel::Kernel * linebreakK = pxDriver.addKernelInstance(make_unique<kernel::LineBreakKernelBuilder>(idb, encodingBits));
    552     StreamSetBuffer * LineBreakStream = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    553     pxDriver.makeKernelCall(linebreakK, {BasisBits}, {LineBreakStream});
     549    mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
     550    StreamSetBuffer * BasisBits = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize * bufferSegments));
     551   
     552    kernel::Kernel * s2pk = mGrepDriver->addKernelInstance(make_unique<kernel::S2PKernel>(idb));
     553    mGrepDriver->makeKernelCall(s2pk, {ByteStream}, {BasisBits});
     554   
     555    kernel::Kernel * linebreakK = mGrepDriver->addKernelInstance(make_unique<kernel::LineBreakKernelBuilder>(idb, encodingBits));
     556    StreamSetBuffer * LineBreakStream = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     557    mGrepDriver->makeKernelCall(linebreakK, {BasisBits}, {LineBreakStream});
    554558   
    555559    const auto n = REs.size();
     
    558562
    559563    for(unsigned i = 0; i < n; ++i){
    560         StreamSetBuffer * MatchResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    561         kernel::Kernel * icgrepK = pxDriver.addKernelInstance(make_unique<kernel::ICGrepKernel>(idb, REs[i]));
    562         pxDriver.makeKernelCall(icgrepK, {BasisBits, LineBreakStream}, {MatchResults});
     564        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     565        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance(make_unique<kernel::ICGrepKernel>(idb, REs[i]));
     566        mGrepDriver->makeKernelCall(icgrepK, {BasisBits, LineBreakStream}, {MatchResults});
    563567        MatchResultsBufs[i] = MatchResults;
    564568    }
    565569    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
    566570    if (REs.size() > 1) {
    567         MergedResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    568         kernel::Kernel * streamsMergeK = pxDriver.addKernelInstance(make_unique<kernel::StreamsMerge>(idb, 1, REs.size()));
    569         pxDriver.makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
     571        MergedResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     572        kernel::Kernel * streamsMergeK = mGrepDriver->addKernelInstance(make_unique<kernel::StreamsMerge>(idb, 1, REs.size()));
     573        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
    570574    }
    571575   
    572576    if (InvertMatch) {
    573         kernel::Kernel * invertK = pxDriver.addKernelInstance(make_unique<kernel::InvertMatchesKernel>(idb));
     577        kernel::Kernel * invertK = mGrepDriver->addKernelInstance(make_unique<kernel::InvertMatchesKernel>(idb));
    574578        StreamSetBuffer * OriginalMatches = MergedResults;
    575         MergedResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    576         pxDriver.makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {MergedResults});
     579        MergedResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     580        mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {MergedResults});
    577581    }
    578582    if (MatchLimit > 0) {
    579         kernel::Kernel * untilK = pxDriver.addKernelInstance(make_unique<kernel::UntilNkernel>(idb));
     583        kernel::Kernel * untilK = mGrepDriver->addKernelInstance(make_unique<kernel::UntilNkernel>(idb));
    580584        untilK->setInitialArguments({idb->getSize(MatchLimit)});
    581585        StreamSetBuffer * AllMatches = MergedResults;
    582         MergedResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    583         pxDriver.makeKernelCall(untilK, {AllMatches}, {MergedResults});
     586        MergedResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     587        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {MergedResults});
    584588    }
    585589    if (grepMode != NormalMode) {
    586         kernel::MatchCount matchCountK(idb);
    587         pxDriver.addKernelCall(matchCountK, {MergedResults}, {});
    588         pxDriver.generatePipelineIR();
    589         idb->setKernel(&matchCountK);
     590        kernel::Kernel * matchCountK = mGrepDriver->addKernelInstance(make_unique<kernel::MatchCount>(idb));
     591        mGrepDriver->makeKernelCall(matchCountK, {MergedResults}, {});
     592        mGrepDriver->generatePipelineIR();
     593        idb->setKernel(matchCountK);
    590594        Value * matchedLineCount = idb->getScalarField("matchedLineCount");
    591595        matchedLineCount = idb->CreateZExt(matchedLineCount, int64Ty);
    592596        idb->CreateRet(matchedLineCount);
    593         pxDriver.linkAndFinalize();
    594597    } else {
    595         kernel::ScanMatchKernel scanMatchK(idb, grepType, encodingBits);
    596         scanMatchK.setInitialArguments({fileIdx});
    597         pxDriver.addKernelCall(scanMatchK, {MergedResults, LineBreakStream, ByteStream}, {});
     598        kernel::Kernel * scanMatchK = mGrepDriver->addKernelInstance(make_unique<kernel::ScanMatchKernel>(idb, grepType, encodingBits));
     599        scanMatchK->setInitialArguments({fileIdx});
     600        mGrepDriver->makeKernelCall(scanMatchK, {MergedResults, LineBreakStream, ByteStream}, {});
    598601        switch (grepType) {
    599602            case GrepType::Normal:
    600603                if (UTF_16) {
    601                     pxDriver.LinkFunction(scanMatchK, "matcher", &wrapped_report_match<uint16_t>);
     604                    mGrepDriver->LinkFunction(*scanMatchK, "matcher", &wrapped_report_match<uint16_t>);
    602605                } else {
    603                     pxDriver.LinkFunction(scanMatchK, "matcher", &wrapped_report_match<uint8_t>);
     606                    mGrepDriver->LinkFunction(*scanMatchK, "matcher", &wrapped_report_match<uint8_t>);
    604607                }
    605608                break;
    606609            case GrepType::NameExpression:
    607                 pxDriver.LinkFunction(scanMatchK, "matcher", &insert_codepoints);
     610                mGrepDriver->LinkFunction(*scanMatchK, "matcher", &insert_codepoints);
    608611                break;
    609612            case GrepType::PropertyValue:
    610                 pxDriver.LinkFunction(scanMatchK, "matcher", &insert_property_values);
     613                mGrepDriver->LinkFunction(*scanMatchK, "matcher", &insert_property_values);
    611614                break;
    612615        }
    613         pxDriver.generatePipelineIR();
     616        mGrepDriver->generatePipelineIR();
    614617        idb->CreateRet(idb->getInt64(0));
    615         pxDriver.linkAndFinalize();
    616     }
    617 
    618     mGrepFunction = pxDriver.getPointerToMain();
    619 }
    620 
     618    }
     619    mGrepDriver->finalizeObject();
     620}
    621621
    622622re::CC * GrepEngine::grepCodepoints() {
     
    645645
    646646GrepEngine::GrepEngine()
    647 : mGrepFunction(nullptr) {
    648 
    649 }
    650 }
     647: mGrepDriver(nullptr) {
     648
     649}
     650
     651GrepEngine::~GrepEngine() {
     652    delete mGrepDriver;
     653}
     654
     655}
Note: See TracChangeset for help on using the changeset viewer.