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

Last change on this file since 5867 was 5867, checked in by cameron, 15 months ago

Fold UnicodeLineBreak? into Required_Streams_UTF8; CRLF into nonFinal

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