source: icGREP/icgrep-devel/icgrep/grep_engine.cpp @ 5784

Last change on this file since 5784 was 5784, checked in by cameron, 16 months ago

Restructuring step/tidy-up for re_passes

File size: 18.6 KB
RevLine 
[4324]1/*
[5476]2 *  Copyright (c) 2017 International Characters.
[4324]3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
[5234]7#include "grep_engine.h"
[5476]8#include "grep_interface.h"
[5267]9#include <llvm/IR/Module.h>
[5234]10#include <boost/filesystem.hpp>
[5206]11#include <UCD/resolve_properties.h>
[5585]12#include <kernels/charclasses.h>
[5142]13#include <kernels/cc_kernel.h>
[5404]14#include <kernels/grep_kernel.h>
[5357]15#include <kernels/linebreak_kernel.h>
[5338]16#include <kernels/streams_merge.h>
[5429]17#include <kernels/source_kernel.h>
[5234]18#include <kernels/s2p_kernel.h>
19#include <kernels/scanmatchgen.h>
20#include <kernels/streamset.h>
[5450]21#include <kernels/until_n.h>
[5436]22#include <kernels/kernel_builder.h>
[5087]23#include <pablo/pablo_kernel.h>
[5234]24#include <re/re_cc.h>
[5769]25#include <re/casing.h>
[5779]26#include <re/exclude_CC.h>
[5234]27#include <re/re_toolchain.h>
[5425]28#include <toolchain/toolchain.h>
[5770]29#include <re/re_name_resolve.h>
[5585]30#include <re/re_collect_unicodesets.h>
31#include <re/re_multiplex.h>
[5772]32#include <re/grapheme_clusters.h>
[5700]33#include <toolchain/toolchain.h>
[5464]34#include <toolchain/cpudriver.h>
[5234]35#include <iostream>
[5369]36#include <cc/multiplex_CCs.h>
[5377]37#include <llvm/Support/raw_ostream.h>
[5418]38#include <util/aligned_allocator.h>
[5386]39#include <sys/stat.h>
[5418]40#include <fcntl.h>
[5484]41#include <errno.h>
[5696]42#include <llvm/ADT/STLExtras.h> // for make_unique
[5700]43#include <llvm/Support/CommandLine.h>
[5735]44#include <llvm/Support/Debug.h>
[5762]45#include <sched.h>
[5377]46
[5241]47using namespace parabix;
[5267]48using namespace llvm;
[5703]49static cl::opt<int> Threads("t", cl::desc("Total number of threads."), cl::init(2));
[5241]50
[5473]51namespace grep {
52
[5704]53// Grep Engine construction and initialization.
[5770]54
[5704]55GrepEngine::GrepEngine() :
56    mGrepDriver(nullptr),
[5735]57    mNextFileToGrep(0),
58    mNextFileToPrint(0),
[5704]59    grepMatchFound(false),
[5735]60    mMoveMatchesToEOL(true),
61    mEngineThread(pthread_self()) {}
[5770]62
[5704]63GrepEngine::~GrepEngine() {
64    delete mGrepDriver;
65}
[5770]66
[5704]67QuietModeEngine::QuietModeEngine() : GrepEngine() {
68    mMoveMatchesToEOL = false;
69}
[5473]70
[5704]71MatchOnlyEngine::MatchOnlyEngine(bool showFilesWithoutMatch) :
72    GrepEngine(), mRequiredCount(showFilesWithoutMatch) {
73    mFileSuffix = NullFlag ? std::string("\0", 1) : "\n";
74    mMoveMatchesToEOL = false;
75}
[5484]76
[5704]77CountOnlyEngine::CountOnlyEngine() : GrepEngine() {
78    mFileSuffix = ":";
79}
[5484]80
[5704]81EmitMatchesEngine::EmitMatchesEngine() : GrepEngine() {
82    mFileSuffix = InitialTabFlag ? "\t:" : ":";
83    if (LineRegexpFlag) mMoveMatchesToEOL = false;
[5484]84}
[5704]85
86void GrepEngine::initFileResult(std::vector<std::string> & filenames) {
[5732]87    const unsigned n = filenames.size();
[5704]88    mResultStrs.resize(n);
[5771]89    mFileStatus.resize(n, FileStatus::Pending);
[5704]90    inputFiles = filenames;
91}
92
93// Code Generation
94//
95// All engines share a common pipeline to compute a stream of Matches from a given input Bytestream.
96
97std::pair<StreamSetBuffer *, StreamSetBuffer *> GrepEngine::grepPipeline(std::vector<re::RE *> & REs, StreamSetBuffer * ByteStream) {
98    auto & idb = mGrepDriver->getBuilder();
99    const unsigned segmentSize = codegen::SegmentSize;
100    const unsigned bufferSegments = codegen::BufferSegments * codegen::ThreadNum;
101    const unsigned encodingBits = 8;
[5770]102
[5782]103    StreamSetBuffer * BasisBits = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(encodingBits, 1), segmentSize * bufferSegments + 1);
[5755]104    kernel::Kernel * s2pk = mGrepDriver->addKernelInstance<kernel::S2PKernel>(idb);
[5704]105    mGrepDriver->makeKernelCall(s2pk, {ByteStream}, {BasisBits});
[5770]106
[5782]107    StreamSetBuffer * LineFeedStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments + 1);
108    kernel::Kernel * linefeedK = mGrepDriver->addKernelInstance<kernel::LineFeedKernelBuilder>(idb, encodingBits);
109    mGrepDriver->makeKernelCall(linefeedK, {BasisBits}, {LineFeedStream});
110
[5755]111    StreamSetBuffer * LineBreakStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5782]112    StreamSetBuffer * CRLFStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5755]113    kernel::Kernel * linebreakK = mGrepDriver->addKernelInstance<kernel::LineBreakKernelBuilder>(idb, encodingBits);
[5782]114    mGrepDriver->makeKernelCall(linebreakK, {BasisBits, LineFeedStream}, {LineBreakStream, CRLFStream});
[5770]115
[5755]116    kernel::Kernel * requiredStreamsK = mGrepDriver->addKernelInstance<kernel::RequiredStreams_UTF8>(idb);
[5782]117    StreamSetBuffer * RequiredStreams = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(3, 1), segmentSize * bufferSegments);
[5704]118    mGrepDriver->makeKernelCall(requiredStreamsK, {BasisBits}, {RequiredStreams});
[5770]119
[5704]120    const auto n = REs.size();
[5748]121    std::vector<std::vector<re::CC *>> charclasses(n);
[5704]122    std::vector<StreamSetBuffer *> MatchResultsBufs(n);
[5770]123
[5704]124    for(unsigned i = 0; i < n; ++i){
[5784]125        std::tie<re::RE*, std::vector<re::CC *>>(REs[i], charclasses[i]) = multiplexing_passes(REs[i]);
[5704]126        const auto numOfCharacterClasses = charclasses[i].size();
[5755]127        StreamSetBuffer * CharClasses = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(numOfCharacterClasses), segmentSize * bufferSegments);
128        kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(charclasses[i]));
[5704]129        mGrepDriver->makeKernelCall(ccK, {BasisBits}, {CharClasses});
[5755]130        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
131        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, REs[i], numOfCharacterClasses);
[5782]132        mGrepDriver->makeKernelCall(icgrepK, {CharClasses, LineBreakStream, CRLFStream, RequiredStreams}, {MatchResults});
[5704]133        MatchResultsBufs[i] = MatchResults;
134    }
135    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
136    if (REs.size() > 1) {
[5755]137        MergedResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
138        kernel::Kernel * streamsMergeK = mGrepDriver->addKernelInstance<kernel::StreamsMerge>(idb, 1, REs.size());
[5704]139        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
140    }
141    StreamSetBuffer * Matches = MergedResults;
[5770]142
[5704]143    if (mMoveMatchesToEOL) {
144        StreamSetBuffer * OriginalMatches = Matches;
[5755]145        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance<kernel::MatchedLinesKernel>(idb);
146        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5704]147        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
148    }
[5770]149
[5704]150    if (InvertMatchFlag) {
[5755]151        kernel::Kernel * invertK = mGrepDriver->addKernelInstance<kernel::InvertMatchesKernel>(idb);
[5704]152        StreamSetBuffer * OriginalMatches = Matches;
[5755]153        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5704]154        mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {Matches});
155    }
156    if (MaxCountFlag > 0) {
[5755]157        kernel::Kernel * untilK = mGrepDriver->addKernelInstance<kernel::UntilNkernel>(idb);
[5704]158        untilK->setInitialArguments({idb->getSize(MaxCountFlag)});
159        StreamSetBuffer * AllMatches = Matches;
[5755]160        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5704]161        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
162    }
163    return std::pair<StreamSetBuffer *, StreamSetBuffer *>(LineBreakStream, Matches);
[5700]164}
[5704]165
166// The QuietMode, MatchOnly and CountOnly engines share a common code generation main function,
167// which returns a count of the matches found (possibly subject to a MaxCount).
[5700]168//
169
[5704]170void GrepEngine::grepCodeGen(std::vector<re::RE *> REs) {
[5770]171
[5704]172    assert (mGrepDriver == nullptr);
173    mGrepDriver = new ParabixDriver("engine");
174    auto & idb = mGrepDriver->getBuilder();
175    Module * M = idb->getModule();
[5770]176
[5757]177    const auto segmentSize = codegen::SegmentSize;
178    const auto bufferSegments = codegen::BufferSegments * codegen::ThreadNum;
179
[5704]180    const unsigned encodingBits = 8;
[5770]181
[5704]182    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getInt64Ty(), idb->getInt32Ty(), nullptr));
183    mainFunc->setCallingConv(CallingConv::C);
184    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
185    auto args = mainFunc->arg_begin();
[5770]186
[5704]187    Value * const fileDescriptor = &*(args++);
188    fileDescriptor->setName("fileDescriptor");
[5770]189
[5755]190    StreamSetBuffer * ByteStream = mGrepDriver->addBuffer<SourceBuffer>(idb, idb->getStreamSetTy(1, encodingBits));
[5757]191    kernel::Kernel * sourceK = mGrepDriver->addKernelInstance<kernel::FDSourceKernel>(idb, segmentSize * bufferSegments);
[5704]192    sourceK->setInitialArguments({fileDescriptor});
193    mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
[5770]194
[5704]195    StreamSetBuffer * LineBreakStream;
196    StreamSetBuffer * Matches;
197    std::tie(LineBreakStream, Matches) = grepPipeline(REs, ByteStream);
[5770]198
[5755]199    kernel::Kernel * matchCountK = mGrepDriver->addKernelInstance<kernel::PopcountKernel>(idb);
[5704]200    mGrepDriver->makeKernelCall(matchCountK, {Matches}, {});
201    mGrepDriver->generatePipelineIR();
202    idb->setKernel(matchCountK);
203    Value * matchedLineCount = idb->getAccumulator("countResult");
204    matchedLineCount = idb->CreateZExt(matchedLineCount, idb->getInt64Ty());
205    mGrepDriver->deallocateBuffers();
206    idb->CreateRet(matchedLineCount);
207    mGrepDriver->finalizeObject();
208}
209
210//
211// The EmitMatches engine uses an EmitMatchesAccumulator object to concatenate together
212// matched lines.
213
[5700]214class EmitMatch : public MatchAccumulator {
[5703]215    friend class EmitMatchesEngine;
[5700]216public:
[5771]217    EmitMatch(std::string linePrefix, std::ostringstream & strm) : mLinePrefix(linePrefix), mLineCount(0), mTerminated(true), mResultStr(strm) {}
[5700]218    void accumulate_match(const size_t lineNum, char * line_start, char * line_end) override;
219    void finalize_match(char * buffer_end) override;
[5703]220protected:
[5700]221    std::string mLinePrefix;
222    size_t mLineCount;
[5758]223    bool mTerminated;
[5771]224    std::ostringstream & mResultStr;
[5700]225};
226
[5704]227//
228//  Default Report Match:  lines are emitted with whatever line terminators are found in the
229//  input.  However, if the final line is not terminated, a new line is appended.
[5726]230//
[5700]231void EmitMatch::accumulate_match (const size_t lineNum, char * line_start, char * line_end) {
[5758]232    if (WithFilenameFlag) {
[5771]233        mResultStr << mLinePrefix;
[5700]234    }
[5758]235    if (LineNumberFlag) {
236        // Internally line numbers are counted from 0.  For display, adjust
237        // the line number so that lines are numbered from 1.
238        if (InitialTabFlag) {
[5771]239            mResultStr << lineNum+1 << "\t:";
[5695]240        }
[5758]241        else {
[5771]242            mResultStr << lineNum+1 << ":";
[5700]243        }
[5695]244    }
[5758]245    size_t bytes = line_end - line_start + 1;
[5771]246    mResultStr.write(line_start, bytes);
[5700]247    mLineCount++;
[5758]248    unsigned last_byte = *line_end;
249    mTerminated = (last_byte >= 0x0A) && (last_byte <= 0x0D);
250    if (LLVM_UNLIKELY(!mTerminated)) {
251        if (last_byte == 0x85) {  //  Possible NEL terminator.
252            mTerminated = (bytes >= 2) && (static_cast<unsigned>(line_end[-1]) == 0xC2);
[5726]253        }
254        else {
[5758]255            // Possible LS or PS terminators.
256            mTerminated = (bytes >= 3) && (static_cast<unsigned>(line_end[-2]) == 0xE2)
257                                       && (static_cast<unsigned>(line_end[-1]) == 0x80)
258                                       && ((last_byte == 0xA8) || (last_byte == 0xA9));
[5726]259        }
[5700]260    }
261}
262
[5758]263void EmitMatch::finalize_match(char * buffer_end) {
[5771]264    if (!mTerminated) mResultStr << "\n";
[5758]265}
266
[5704]267void EmitMatchesEngine::grepCodeGen(std::vector<re::RE *> REs) {
268    assert (mGrepDriver == nullptr);
269    mGrepDriver = new ParabixDriver("engine");
270    auto & idb = mGrepDriver->getBuilder();
271    Module * M = idb->getModule();
[5770]272
[5757]273    const auto segmentSize = codegen::SegmentSize;
274    const auto bufferSegments = codegen::BufferSegments * codegen::ThreadNum;
[5704]275    const unsigned encodingBits = 8;
[5770]276
[5704]277    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getInt64Ty(), idb->getInt32Ty(), idb->getIntAddrTy(), nullptr));
278    mainFunc->setCallingConv(CallingConv::C);
279    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
280    auto args = mainFunc->arg_begin();
[5770]281
[5704]282    Value * const fileDescriptor = &*(args++);
283    fileDescriptor->setName("fileDescriptor");
284    Value * match_accumulator = &*(args++);
285    match_accumulator->setName("match_accumulator");
[5770]286
[5755]287    StreamSetBuffer * ByteStream = mGrepDriver->addBuffer<SourceBuffer>(idb, idb->getStreamSetTy(1, encodingBits));
[5757]288    kernel::Kernel * sourceK = mGrepDriver->addKernelInstance<kernel::FDSourceKernel>(idb, segmentSize * bufferSegments);
[5704]289    sourceK->setInitialArguments({fileDescriptor});
290    mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
[5770]291
[5704]292    StreamSetBuffer * LineBreakStream;
293    StreamSetBuffer * Matches;
294    std::tie(LineBreakStream, Matches) = grepPipeline(REs, ByteStream);
[5770]295
[5755]296    kernel::Kernel * scanMatchK = mGrepDriver->addKernelInstance<kernel::ScanMatchKernel>(idb);
[5704]297    scanMatchK->setInitialArguments({match_accumulator});
298    mGrepDriver->makeKernelCall(scanMatchK, {Matches, LineBreakStream, ByteStream}, {});
299    mGrepDriver->LinkFunction(*scanMatchK, "accumulate_match_wrapper", &accumulate_match_wrapper);
300    mGrepDriver->LinkFunction(*scanMatchK, "finalize_match_wrapper", &finalize_match_wrapper);
[5770]301
[5704]302    mGrepDriver->generatePipelineIR();
303    mGrepDriver->deallocateBuffers();
304    idb->CreateRet(idb->getInt64(0));
305    mGrepDriver->finalizeObject();
306}
[5700]307
308
[5704]309//
310//  The doGrep methods apply a GrepEngine to a single file, processing the results
311//  differently based on the engine type.
[5770]312
[5704]313uint64_t GrepEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
314    typedef uint64_t (*GrepFunctionType)(int32_t fileDescriptor);
315    auto f = reinterpret_cast<GrepFunctionType>(mGrepDriver->getMain());
[5770]316
[5771]317    int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx]);
[5704]318    if (fileDescriptor == -1) return 0;
[5770]319
[5704]320    uint64_t grepResult = f(fileDescriptor);
321    close(fileDescriptor);
322    return grepResult;
323}
324
325uint64_t CountOnlyEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
326    uint64_t grepResult = GrepEngine::doGrep(fileName, fileIdx);
[5771]327    if (WithFilenameFlag) mResultStrs[fileIdx] << linePrefix(fileName);
328    mResultStrs[fileIdx] << grepResult << "\n";
[5704]329    return grepResult;
330}
331
332std::string GrepEngine::linePrefix(std::string fileName) {
333    if (fileName == "-") {
334        return LabelFlag + mFileSuffix;
[5548]335    }
[5704]336    else {
337        return fileName + mFileSuffix;
[5548]338    }
339}
[5770]340
[5704]341uint64_t MatchOnlyEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
342    uint64_t grepResult = GrepEngine::doGrep(fileName, fileIdx);
343    if (grepResult == mRequiredCount) {
[5771]344       mResultStrs[fileIdx] << linePrefix(fileName);
[5704]345    }
346    return grepResult;
347}
[5700]348
[5704]349uint64_t EmitMatchesEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
350    typedef uint64_t (*GrepFunctionType)(int32_t fileDescriptor, intptr_t accum_addr);
351    auto f = reinterpret_cast<GrepFunctionType>(mGrepDriver->getMain());
[5770]352
[5771]353    int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx]);
[5704]354    if (fileDescriptor == -1) return 0;
[5771]355    EmitMatch accum(linePrefix(fileName), mResultStrs[fileIdx]);
[5740]356    f(fileDescriptor, reinterpret_cast<intptr_t>(&accum));
[5704]357    close(fileDescriptor);
358    if (accum.mLineCount > 0) grepMatchFound = true;
359    return accum.mLineCount;
360}
361
[5703]362// Open a file and return its file desciptor.
[5771]363int32_t GrepEngine::openFile(const std::string & fileName, std::ostringstream & msgstrm) {
[5693]364    if (fileName == "-") {
[5700]365        return STDIN_FILENO;
[5693]366    }
[5700]367    else {
368        struct stat sb;
369        int32_t fileDescriptor = open(fileName.c_str(), O_RDONLY);
370        if (LLVM_UNLIKELY(fileDescriptor == -1)) {
371            if (!NoMessagesFlag) {
372                if (errno == EACCES) {
[5771]373                    msgstrm << "icgrep: " << fileName << ": Permission denied.\n";
[5700]374                }
375                else if (errno == ENOENT) {
[5771]376                    msgstrm << "icgrep: " << fileName << ": No such file.\n";
[5700]377                }
378                else {
[5771]379                    msgstrm << "icgrep: " << fileName << ": Failed.\n";
[5700]380                }
[5484]381            }
[5700]382            return fileDescriptor;
383        }
384        if (stat(fileName.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
385            if (!NoMessagesFlag) {
[5771]386                msgstrm << "icgrep: " << fileName << ": Is a directory.\n";
[5484]387            }
[5700]388            close(fileDescriptor);
[5704]389            return -1;
[5484]390        }
[5700]391        return fileDescriptor;
[4788]392    }
[5700]393}
394
[5704]395// The process of searching a group of files may use a sequential or a task
396// parallel approach.
[5770]397
[5735]398void * DoGrepThreadFunction(void *args) {
[5740]399    return reinterpret_cast<GrepEngine *>(args)->DoGrepThreadMethod();
[5735]400}
[4949]401
[5704]402bool GrepEngine::searchAllFiles() {
[5735]403    const unsigned numOfThreads = Threads; // <- convert the command line value into an integer to allow stack allocation
404    pthread_t threads[numOfThreads];
[5770]405
[5735]406    for(unsigned long i = 1; i < numOfThreads; ++i) {
407        const int rc = pthread_create(&threads[i], nullptr, DoGrepThreadFunction, (void *)this);
408        if (rc) {
409            llvm::report_fatal_error("Failed to create thread: code " + std::to_string(rc));
[5484]410        }
[5735]411    }
412    // Main thread also does the work;
[5770]413
[5735]414    DoGrepThreadMethod();
415    for(unsigned i = 1; i < numOfThreads; ++i) {
416        void * status = nullptr;
417        const int rc = pthread_join(threads[i], &status);
418        if (rc) {
419            llvm::report_fatal_error("Failed to join thread: code " + std::to_string(rc));
[5704]420        }
[5484]421    }
[5704]422    return grepMatchFound;
[5377]423}
[5314]424
[5338]425
[5704]426// DoGrep thread function.
[5735]427void * GrepEngine::DoGrepThreadMethod() {
[5748]428
[5771]429    unsigned fileIdx = mNextFileToGrep++;
[5735]430    while (fileIdx < inputFiles.size()) {
[5771]431        const auto grepResult = doGrep(inputFiles[fileIdx], fileIdx);
[5735]432        mFileStatus[fileIdx] = FileStatus::GrepComplete;
[5761]433        if (grepResult > 0) {
434            grepMatchFound = true;
[5735]435        }
436        if (QuietMode && grepMatchFound) {
[5761]437            if (pthread_self() != mEngineThread) {
438                pthread_exit(nullptr);
439            }
[5735]440            return nullptr;
441        }
[5761]442        fileIdx = mNextFileToGrep++;
[5574]443    }
[5740]444
[5771]445    unsigned printIdx = mNextFileToPrint++;
[5761]446    while (printIdx < inputFiles.size()) {
447        const bool readyToPrint = ((printIdx == 0) || (mFileStatus[printIdx - 1] == FileStatus::PrintComplete)) && (mFileStatus[printIdx] == FileStatus::GrepComplete);
[5735]448        if (readyToPrint) {
[5771]449            const auto output = mResultStrs[printIdx].str();
[5761]450            if (!output.empty()) {
[5771]451                llvm::outs() << output;
[5761]452            }
453            mFileStatus[printIdx] = FileStatus::PrintComplete;
454            printIdx = mNextFileToPrint++;
455        } else {
[5735]456            mGrepDriver->performIncrementalCacheCleanupStep();
457        }
[5762]458        sched_yield();
[5735]459    }
[5761]460
[5735]461    if (pthread_self() != mEngineThread) {
462        pthread_exit(nullptr);
[5761]463    } else {
[5735]464        return nullptr;
465    }
[5703]466}
[5740]467
[5481]468}
Note: See TracBrowser for help on using the repository browser.