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

Last change on this file since 5567 was 5561, checked in by cameron, 2 years ago

Factor out required streams into a separate kernel

File size: 31.1 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/UnicodeNameData.h>
12#include <UCD/resolve_properties.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/re_toolchain.h>
26#include <toolchain/toolchain.h>
27#include <toolchain/cpudriver.h>
28#include <toolchain/NVPTXDriver.h>
29#include <iostream>
30#include <sstream>
31#include <cc/multiplex_CCs.h>
32#include <llvm/Support/raw_ostream.h>
33#include <util/aligned_allocator.h>
34#include <sys/stat.h>
35#include <fcntl.h>
36#include <errno.h>
37#include <mutex>
38#ifdef CUDA_ENABLED
39#include <preprocess.cpp>
40#include <IR_Gen/CudaDriver.h>
41#endif
42
43using namespace parabix;
44using namespace llvm;
45
46namespace grep {
47
48static std::stringstream * resultStrs = nullptr;
49static std::vector<std::string> inputFiles;
50static std::vector<std::string> linePrefix;
51static bool grepMatchFound;
52
53size_t * startPoints = nullptr;
54size_t * accumBytes = nullptr;
55
56
57std::mutex count_mutex;
58size_t fileCount;
59
60// DoGrep thread function.
61void *DoGrepThreadFunction(void *args)
62{
63    size_t fileIdx;
64    grep::GrepEngine * grepEngine = (grep::GrepEngine *)args;
65
66    count_mutex.lock();
67    fileIdx = fileCount;
68    fileCount++;
69    count_mutex.unlock();
70
71    while (fileIdx < inputFiles.size()) {
72        size_t grepResult = grepEngine->doGrep(inputFiles[fileIdx], fileIdx);
73       
74        count_mutex.lock();
75        if (grepResult > 0) grepMatchFound = true;
76        fileIdx = fileCount;
77        fileCount++;
78        count_mutex.unlock();
79        if (QuietMode && grepMatchFound) pthread_exit(nullptr);
80    }
81
82    pthread_exit(nullptr);
83}
84
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   
96void GrepEngine::doGrep(const std::string & fileName, std::string & PTXFilename) const{
97#ifdef CUDA_ENABLED
98    const bool CountOnly = true;
99    boost::filesystem::path file(fileName);
100    if (exists(file)) {
101        if (is_directory(file)) {
102            return;
103        }
104    } else {
105        if (!NoMessagesFlag) {
106            std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
107            return;
108        }
109    }
110
111    const auto fileSize = file_size(file);
112   
113    if (fileSize > 0) {
114        try {
115            boost::iostreams::mapped_file_source source(fileName, fileSize, 0);
116            char * fileBuffer = const_cast<char *>(source.data());
117           
118            codegen::BlockSize = 128;
119            std::vector<size_t> LFPositions = preprocess(fileBuffer, fileSize);
120           
121            const unsigned numOfGroups = codegen::GroupNum;
122            if (posix_memalign((void**)&startPoints, 8, (numOfGroups+1)*sizeof(size_t)) ||
123                posix_memalign((void**)&accumBytes, 8, (numOfGroups+1)*sizeof(size_t))) {
124                std::cerr << "Cannot allocate memory for startPoints or accumBytes.\n";
125                exit(-1);
126            }
127            if(PTXFilename=="")
128                PTXFilename = mGrepDriver->getBuilder()->getModule()->getModuleIdentifier() + ".ptx";
129            RunPTX(PTXFilename, fileBuffer, fileSize, CountOnly, LFPositions, startPoints, accumBytes);
130            source.close();
131        } catch (std::exception & e) {
132            if (!NoMessagesFlag) {
133                std::cerr << "Boost mmap error: " + fileName + ": " + e.what() + " Skipped.\n";
134                return;
135            }
136        }
137    } else {
138        std::cout << 0 << std::endl;
139    }
140#endif
141}
142
143uint64_t GrepEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) const {
144    struct stat sb;
145    const int32_t fd = open(fileName.c_str(), O_RDONLY);
146    if (LLVM_UNLIKELY(fd == -1)) {
147        if (!NoMessagesFlag  && !(Mode == QuietMode)) {
148            if (errno == EACCES) {
149                resultStrs[fileIdx] << "icgrep: " << fileName << ": Permission denied.\n";
150            }
151            else if (errno == ENOENT) {
152                resultStrs[fileIdx] << "icgrep: " << fileName << ": No such file.\n";
153            }
154            else {
155                resultStrs[fileIdx] << "icgrep: " << fileName << ": Failed.\n";
156            }
157        }
158        return 0;
159    }
160    if (stat(fileName.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
161        if (!NoMessagesFlag  && !(Mode == QuietMode)) {
162            resultStrs[fileIdx] << "icgrep: " << fileName << ": Is a directory.\n";
163        }
164        close(fd);
165        return 0;
166    }
167    const auto result = doGrep(fd, fileIdx);
168    close(fd);
169    return result;
170}
171
172uint64_t GrepEngine::doGrep(const int32_t fileDescriptor, const uint32_t fileIdx) const {
173    assert (mGrepDriver);
174    typedef uint64_t (*GrepFunctionType)(int32_t fileDescriptor, const uint32_t fileIdx);
175    auto f = reinterpret_cast<GrepFunctionType>(mGrepDriver->getMain());
176   
177    uint64_t grepResult = f(fileDescriptor, fileIdx);
178    if (grepResult > 0) grepMatchFound = true;
179    else if ((Mode == NormalMode) && !resultStrs[fileIdx].str().empty()) grepMatchFound = true;
180   
181    if (Mode == CountOnly) {
182        resultStrs[fileIdx] << linePrefix[fileIdx] << grepResult << "\n";
183    }
184    else if (Mode == FilesWithMatch || Mode == FilesWithoutMatch ) {
185        size_t requiredCount = Mode == FilesWithMatch ? 1 : 0;
186        if (grepResult == requiredCount) {
187            resultStrs[fileIdx] << linePrefix[fileIdx];
188        }
189    }
190    else if (Mode == QuietMode) {
191        if (grepMatchFound) exit(MatchFoundExitCode);
192    }
193    return grepResult;
194}
195
196void initFileResult(std::vector<std::string> filenames){
197    grepMatchFound = false;
198    const int n = filenames.size();
199    linePrefix.resize(n);
200    if ((n > 1) && !NoFilenameFlag) {
201        WithFilenameFlag = true;
202    }
203    std::string fileSuffix = "";
204    bool setLinePrefix = WithFilenameFlag || (Mode == FilesWithMatch) || (Mode == FilesWithoutMatch);
205    if (setLinePrefix) {
206        if (NullFlag) {
207            fileSuffix = std::string("\0", 1);
208        }
209        else if ((Mode == NormalMode) && InitialTabFlag && !(LineNumberFlag || ByteOffsetFlag)) {
210            fileSuffix = "\t:";
211        }
212        else if ((Mode == NormalMode) || (Mode == CountOnly)) {
213            fileSuffix = ":";
214        }
215        else if ((Mode == FilesWithMatch) || (Mode == FilesWithoutMatch)) {
216            fileSuffix = "\n";
217        }
218    }
219    inputFiles = filenames;
220    resultStrs = new std::stringstream[n];
221    for (unsigned i = 0; i < inputFiles.size(); ++i) {
222        if (setLinePrefix) {
223            if (inputFiles[i] == "-") {
224                linePrefix[i] = LabelFlag + fileSuffix;
225            }
226            else {
227                linePrefix[i] = inputFiles[i] + fileSuffix;
228            }
229        }
230    }
231}
232
233template<typename CodeUnit>
234void wrapped_report_match(const size_t lineNum, size_t line_start, size_t line_end, const CodeUnit * const buffer, const size_t filesize, const size_t fileIdx) {
235
236//    errs().write_hex((size_t)buffer) << " : " << lineNum << " (" << line_start << ", " << line_end << ", " << filesize << ")\n";
237
238    assert (buffer);
239    assert (line_start <= line_end);
240    assert (line_end <= filesize);
241
242    if (WithFilenameFlag) {
243        resultStrs[fileIdx] << linePrefix[fileIdx];
244    }
245    if (LineNumberFlag) {
246        // Internally line numbers are counted from 0.  For display, adjust
247        // the line number so that lines are numbered from 1.
248        if (InitialTabFlag) {
249            resultStrs[fileIdx] << lineNum+1 << "\t:";
250        }
251        else {
252            resultStrs[fileIdx] << lineNum+1 << ":";
253        }
254    }
255
256    // If the line "starts" on the LF of a CRLF, it is actually the end of the last line.
257    if ((buffer[line_start] == 0xA) && (line_start != line_end)) {
258        ++line_start;
259    }
260
261    if (LLVM_UNLIKELY(line_end == filesize)) {
262        // The match position is at end-of-file.   We have a final unterminated line.
263        resultStrs[fileIdx].write((char *)&buffer[line_start], (line_end - line_start) * sizeof(CodeUnit));
264        if (NormalizeLineBreaksFlag) {
265            resultStrs[fileIdx] << '\n';  // terminate it
266        }
267    } else {
268        const auto end_byte = buffer[line_end];
269        if (grep::NormalizeLineBreaksFlag) {
270            if (LLVM_UNLIKELY(end_byte == 0x85)) {
271                // Line terminated with NEL, on the second byte.  Back up 1.
272                line_end -= 1;
273            } else if (LLVM_UNLIKELY(end_byte > 0xD)) {
274                // Line terminated with PS or LS, on the third byte.  Back up 2.
275                line_end -= 2;
276            }
277            resultStrs[fileIdx].write((char *)&buffer[line_start], (line_end - line_start) * sizeof(CodeUnit));
278            resultStrs[fileIdx] << '\n';
279        } else {
280            if (end_byte == 0x0D) {
281                // Check for line_end on first byte of CRLF; we don't want to access past the end of buffer.
282                if ((line_end + 1) < filesize) {
283                    if (buffer[line_end + 1] == 0x0A) {
284                        // Found CRLF; preserve both bytes.
285                        ++line_end;
286                    }
287                }
288            }
289            resultStrs[fileIdx].write((char *)&buffer[line_start], (line_end - line_start + 1) * sizeof(CodeUnit));
290        }
291    }
292}
293
294void PrintResults(){
295   
296    for (unsigned i = 0; i < inputFiles.size(); ++i){
297        std::cout << resultStrs[i].str();
298    }
299    exit(grepMatchFound ? MatchFoundExitCode : MatchNotFoundExitCode);
300}
301
302void GrepEngine::grepCodeGen_nvptx(std::vector<re::RE *> REs, const GrepModeType grepMode, const bool UTF_16) {
303
304    assert (mGrepDriver == nullptr);
305
306    mGrepDriver = new NVPTXDriver("engine");
307    auto & idb = mGrepDriver->getBuilder();
308    Module * M = idb->getModule();
309
310    const unsigned segmentSize = codegen::SegmentSize;
311    const unsigned bufferSegments = codegen::BufferSegments * codegen::ThreadNum;
312    const unsigned encodingBits = UTF_16 ? 16 : 8;
313
314    Type * const int64Ty = idb->getInt64Ty();
315    Type * const int32Ty = idb->getInt32Ty();
316    Type * const size_ty = idb->getSizeTy();
317    Type * const sizeTyPtr = PointerType::get(size_ty, 1);
318    Type * const int64tyPtr = PointerType::get(int64Ty, 1);
319    Type * const voidTy = idb->getVoidTy();
320   
321    size_t MatchLimit = ((grepMode == QuietMode) | (grepMode == FilesWithMatch) | (grepMode == FilesWithoutMatch)) ? 1 : MaxCountFlag;
322
323    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", voidTy, int64tyPtr, sizeTyPtr, sizeTyPtr, int64tyPtr, nullptr));
324    mainFunc->setCallingConv(CallingConv::C);
325    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
326    auto args = mainFunc->arg_begin();
327
328    Value * const inputPtr = &*(args++);
329    inputPtr->setName("inputPtr");
330    Value * const startPointsPtr = &*(args++);
331    startPointsPtr->setName("startPointsPtr");
332    Value * const bufferSizesPtr = &*(args++);
333    bufferSizesPtr->setName("bufferSizesPtr");
334    Value * const outputPtr = &*(args++);
335    outputPtr->setName("outputPtr");
336
337    Function * tidFunc = M->getFunction("llvm.nvvm.read.ptx.sreg.tid.x");
338    Value * tid = idb->CreateCall(tidFunc);
339    Function * bidFunc = cast<Function>(M->getOrInsertFunction("llvm.nvvm.read.ptx.sreg.ctaid.x", int32Ty, nullptr));
340    Value * bid = idb->CreateCall(bidFunc);
341
342    Value * startPoint = idb->CreateLoad(idb->CreateGEP(startPointsPtr, bid));
343    Value * startBlock = idb->CreateUDiv(startPoint, ConstantInt::get(int64Ty, idb->getBitBlockWidth()));
344    Type * const inputStreamType = PointerType::get(ArrayType::get(ArrayType::get(idb->getBitBlockType(), 8), 1), 1);   
345    Value * inputStreamPtr = idb->CreateGEP(idb->CreateBitCast(inputPtr, inputStreamType), startBlock);
346    Value * inputStream = idb->CreateGEP(inputStreamPtr, tid);
347    Value * bufferSize = idb->CreateLoad(idb->CreateGEP(bufferSizesPtr, bid));
348
349    StreamSetBuffer * ByteStream = mGrepDriver->addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8), 1));
350    kernel::Kernel * sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::MemorySourceKernel>(idb, inputStreamType, segmentSize));
351    sourceK->setInitialArguments({inputStream, bufferSize});
352    mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
353
354    StreamSetBuffer * BasisBits = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize * bufferSegments));
355    kernel::Kernel * s2pk = mGrepDriver->addKernelInstance(make_unique<kernel::S2PKernel>(idb));
356    mGrepDriver->makeKernelCall(s2pk, {ByteStream}, {BasisBits});
357 
358    StreamSetBuffer * LineBreakStream = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
359    kernel::Kernel * linebreakK = mGrepDriver->addKernelInstance(make_unique<kernel::LineBreakKernelBuilder>(idb, encodingBits));
360    mGrepDriver->makeKernelCall(linebreakK, {BasisBits}, {LineBreakStream});
361   
362    kernel::Kernel * requiredStreamsK = mGrepDriver->addKernelInstance(make_unique<kernel::RequiredStreams_UTF8>(idb));
363    StreamSetBuffer * RequiredStreams = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(4, 1), segmentSize));
364    mGrepDriver->makeKernelCall(requiredStreamsK, {BasisBits}, {RequiredStreams});
365   
366    const auto n = REs.size();
367
368    std::vector<StreamSetBuffer *> MatchResultsBufs(n);
369
370    for(unsigned i = 0; i < n; ++i){
371        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
372        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance(make_unique<kernel::ICGrepKernel>(idb, REs[i]));
373        mGrepDriver->makeKernelCall(icgrepK, {BasisBits, LineBreakStream, RequiredStreams}, {MatchResults});
374        MatchResultsBufs[i] = MatchResults;
375    }
376    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
377    if (REs.size() > 1) {
378        MergedResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
379        kernel::Kernel * streamsMergeK = mGrepDriver->addKernelInstance(make_unique<kernel::StreamsMerge>(idb, 1, REs.size()));
380        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
381    }
382    StreamSetBuffer * Matches = MergedResults;
383
384    if (matchesNeedToBeMovedToEOL()) {
385        StreamSetBuffer * OriginalMatches = Matches;
386        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance(make_unique<kernel::MatchedLinesKernel>(idb));
387        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
388        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
389    }
390   
391    if (InvertMatchFlag) {
392        kernel::Kernel * invertK = mGrepDriver->addKernelInstance(make_unique<kernel::InvertMatchesKernel>(idb));
393        StreamSetBuffer * OriginalMatches = Matches;
394        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
395        mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {Matches});
396    }
397    if (MatchLimit > 0) {
398        kernel::Kernel * untilK = mGrepDriver->addKernelInstance(make_unique<kernel::UntilNkernel>(idb));
399        untilK->setInitialArguments({idb->getSize(MatchLimit)});
400        StreamSetBuffer * AllMatches = Matches;
401        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
402        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
403    }
404   
405    kernel::Kernel * matchCountK = mGrepDriver->addKernelInstance(make_unique<kernel::PopcountKernel>(idb));
406    mGrepDriver->makeKernelCall(matchCountK, {Matches}, {});
407    mGrepDriver->generatePipelineIR();
408    idb->setKernel(matchCountK);
409    Value * matchedLineCount = idb->getAccumulator("countResult");
410    matchedLineCount = idb->CreateZExt(matchedLineCount, int64Ty);
411   
412    Value * strideBlocks = ConstantInt::get(int32Ty, idb->getStride() / idb->getBitBlockWidth());
413    Value * outputThreadPtr = idb->CreateGEP(outputPtr, idb->CreateAdd(idb->CreateMul(bid, strideBlocks), tid));
414    idb->CreateStore(matchedLineCount, outputThreadPtr);
415    idb->CreateRetVoid();
416
417    mGrepDriver->finalizeObject();
418}
419
420void GrepEngine::grepCodeGen(std::vector<re::RE *> REs, const GrepModeType grepMode, const bool UTF_16, GrepSource grepSource) {
421
422    assert (mGrepDriver == nullptr);
423    mGrepDriver = new ParabixDriver("engine");
424    auto & idb = mGrepDriver->getBuilder();
425    Module * M = idb->getModule();
426
427    const unsigned segmentSize = codegen::SegmentSize;
428    const unsigned bufferSegments = codegen::BufferSegments * codegen::ThreadNum;
429    const unsigned encodingBits = UTF_16 ? 16 : 8;
430
431    Type * const int64Ty = idb->getInt64Ty();
432    Type * const int32Ty = idb->getInt32Ty();
433
434    kernel::Kernel * sourceK = nullptr;
435   
436    size_t MatchLimit = ((grepMode == QuietMode) | (grepMode == FilesWithMatch) | (grepMode == FilesWithoutMatch)) ? 1 : MaxCountFlag;
437
438    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", int64Ty, idb->getInt32Ty(), int32Ty, nullptr));
439    mainFunc->setCallingConv(CallingConv::C);
440    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
441    auto args = mainFunc->arg_begin();
442
443    Value * const fileDescriptor = &*(args++);
444    fileDescriptor->setName("fileDescriptor");
445    Value * fileIdx = &*(args++);
446    fileIdx->setName("fileIdx");
447
448    StreamSetBuffer * ByteStream = mGrepDriver->addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8)));
449
450    if (grepSource == GrepSource::File) {
451        sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::MMapSourceKernel>(idb, segmentSize));
452    } else {
453        sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::ReadSourceKernel>(idb, segmentSize));
454    }
455    sourceK->setInitialArguments({fileDescriptor});
456
457    mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
458    StreamSetBuffer * BasisBits = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize * bufferSegments));
459   
460    kernel::Kernel * s2pk = mGrepDriver->addKernelInstance(make_unique<kernel::S2PKernel>(idb));
461    mGrepDriver->makeKernelCall(s2pk, {ByteStream}, {BasisBits});
462   
463    kernel::Kernel * linebreakK = mGrepDriver->addKernelInstance(make_unique<kernel::LineBreakKernelBuilder>(idb, encodingBits));
464    StreamSetBuffer * LineBreakStream = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
465    mGrepDriver->makeKernelCall(linebreakK, {BasisBits}, {LineBreakStream});
466   
467    kernel::Kernel * requiredStreamsK = mGrepDriver->addKernelInstance(make_unique<kernel::RequiredStreams_UTF8>(idb));
468    StreamSetBuffer * RequiredStreams = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(4, 1), segmentSize));
469    mGrepDriver->makeKernelCall(requiredStreamsK, {BasisBits}, {RequiredStreams});
470   
471    const auto n = REs.size();
472
473    std::vector<StreamSetBuffer *> MatchResultsBufs(n);
474
475    for(unsigned i = 0; i < n; ++i){
476        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
477        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance(make_unique<kernel::ICGrepKernel>(idb, REs[i]));
478        mGrepDriver->makeKernelCall(icgrepK, {BasisBits, LineBreakStream, RequiredStreams}, {MatchResults});
479        MatchResultsBufs[i] = MatchResults;
480    }
481    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
482    if (REs.size() > 1) {
483        MergedResults = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
484        kernel::Kernel * streamsMergeK = mGrepDriver->addKernelInstance(make_unique<kernel::StreamsMerge>(idb, 1, REs.size()));
485        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
486    }
487    StreamSetBuffer * Matches = MergedResults;
488   
489    if (matchesNeedToBeMovedToEOL()) {
490        StreamSetBuffer * OriginalMatches = Matches;
491        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance(make_unique<kernel::MatchedLinesKernel>(idb));
492        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
493        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
494    }
495   
496    if (InvertMatchFlag) {
497        kernel::Kernel * invertK = mGrepDriver->addKernelInstance(make_unique<kernel::InvertMatchesKernel>(idb));
498        StreamSetBuffer * OriginalMatches = Matches;
499        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
500        mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {Matches});
501    }
502    if (MatchLimit > 0) {
503        kernel::Kernel * untilK = mGrepDriver->addKernelInstance(make_unique<kernel::UntilNkernel>(idb));
504        untilK->setInitialArguments({idb->getSize(MatchLimit)});
505        StreamSetBuffer * AllMatches = Matches;
506        Matches = mGrepDriver->addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
507        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
508    }
509    if (grepMode == NormalMode) {
510        kernel::Kernel * scanMatchK = mGrepDriver->addKernelInstance(make_unique<kernel::ScanMatchKernel>(idb, GrepType::Normal, encodingBits));
511        scanMatchK->setInitialArguments({fileIdx});
512        mGrepDriver->makeKernelCall(scanMatchK, {Matches, LineBreakStream, ByteStream}, {});
513        if (UTF_16) {
514            mGrepDriver->LinkFunction(*scanMatchK, "matcher", &wrapped_report_match<uint16_t>);
515        } else {
516            mGrepDriver->LinkFunction(*scanMatchK, "matcher", &wrapped_report_match<uint8_t>);
517        }
518        mGrepDriver->generatePipelineIR();
519        mGrepDriver->deallocateBuffers();
520
521        idb->CreateRet(idb->getInt64(0));
522    } else {
523        kernel::Kernel * matchCountK = mGrepDriver->addKernelInstance(make_unique<kernel::PopcountKernel>(idb));
524        mGrepDriver->makeKernelCall(matchCountK, {Matches}, {});
525        mGrepDriver->generatePipelineIR();
526        idb->setKernel(matchCountK);
527        Value * matchedLineCount = idb->getAccumulator("countResult");
528        matchedLineCount = idb->CreateZExt(matchedLineCount, int64Ty);
529        mGrepDriver->deallocateBuffers();
530        idb->CreateRet(matchedLineCount);
531    }
532    mGrepDriver->finalizeObject();
533}
534
535GrepEngine::GrepEngine()
536: mGrepDriver(nullptr) {
537
538}
539
540GrepEngine::~GrepEngine() {
541    delete mGrepDriver;
542}
543
544
545   
546static re::CC * parsedCodePointSet = nullptr;
547
548void insert_codepoints(const size_t lineNum, const size_t line_start, const size_t line_end, const char * const buffer) {
549    assert (buffer);
550    assert (line_start <= line_end);
551    re::codepoint_t c = 0;
552    size_t line_pos = line_start;
553    while (isxdigit(buffer[line_pos])) {
554        assert (line_pos < line_end);
555        if (isdigit(buffer[line_pos])) {
556            c = (c << 4) | (buffer[line_pos] - '0');
557        }
558        else {
559            c = (c << 4) | (tolower(buffer[line_pos]) - 'a' + 10);
560        }
561        line_pos++;
562    }
563    assert(((line_pos - line_start) >= 4) && ((line_pos - line_start) <= 6)); // UCD format 4 to 6 hex digits.
564    parsedCodePointSet->insert(c);
565}
566
567re::CC * grepCodepoints(re::RE * pattern, char * UnicodeDataBuffer, size_t bufferLength) {
568    parsedCodePointSet = re::makeCC();       
569    const unsigned segmentSize = 8;
570
571    ParabixDriver pxDriver("codepointEngine");
572    auto & idb = pxDriver.getBuilder();
573    Module * M = idb->getModule();
574   
575    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getVoidTy(), idb->getInt8PtrTy(), idb->getSizeTy(), nullptr));
576    mainFunc->setCallingConv(CallingConv::C);
577    auto args = mainFunc->arg_begin();
578    Value * const buffer = &*(args++);
579    buffer->setName("buffer");
580    Value * length = &*(args++);
581    length->setName("length");
582   
583    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
584   
585    StreamSetBuffer * ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8)));
586    kernel::Kernel * sourceK = pxDriver.addKernelInstance(make_unique<kernel::MemorySourceKernel>(idb, idb->getInt8PtrTy(), segmentSize));
587    sourceK->setInitialArguments({buffer, length});
588    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
589   
590    StreamSetBuffer * BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize));
591   
592    kernel::Kernel * s2pk = pxDriver.addKernelInstance(make_unique<kernel::S2PKernel>(idb));
593    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
594   
595    kernel::Kernel * linebreakK = pxDriver.addKernelInstance(make_unique<kernel::LineBreakKernelBuilder>(idb, 8));
596    StreamSetBuffer * LineBreakStream = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize));
597    pxDriver.makeKernelCall(linebreakK, {BasisBits}, {LineBreakStream});
598   
599    kernel::Kernel * requiredStreamsK = pxDriver.addKernelInstance(make_unique<kernel::RequiredStreams_UTF8>(idb));
600    StreamSetBuffer * RequiredStreams = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(4, 1), segmentSize));
601    pxDriver.makeKernelCall(requiredStreamsK, {BasisBits}, {RequiredStreams});
602   
603    StreamSetBuffer * MatchResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize));
604    kernel::Kernel * icgrepK = pxDriver.addKernelInstance(make_unique<kernel::ICGrepKernel>(idb, pattern));
605    pxDriver.makeKernelCall(icgrepK, {BasisBits, LineBreakStream, RequiredStreams}, {MatchResults});
606   
607    StreamSetBuffer * MatchedLines = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize));
608    kernel::Kernel * matchedLinesK = pxDriver.addKernelInstance(make_unique<kernel::MatchedLinesKernel>(idb));
609    pxDriver.makeKernelCall(matchedLinesK, {MatchResults, LineBreakStream}, {MatchedLines});
610   
611    kernel::Kernel * scanMatchK = pxDriver.addKernelInstance(make_unique<kernel::ScanMatchKernel>(idb, GrepType::NameExpression, 8));
612    scanMatchK->setInitialArguments({idb->getInt32(0)});
613    pxDriver.makeKernelCall(scanMatchK, {MatchedLines, LineBreakStream, ByteStream}, {});
614    pxDriver.LinkFunction(*scanMatchK, "matcher", &insert_codepoints);
615    pxDriver.generatePipelineIR();
616    idb->CreateRetVoid();
617    pxDriver.finalizeObject();
618   
619    typedef void (*GrepFunctionType)(const char * buffer, const size_t length);
620    auto f = reinterpret_cast<GrepFunctionType>(pxDriver.getMain());
621    f(UnicodeDataBuffer, bufferLength);
622   
623    return parsedCodePointSet;   
624}
625
626   
627static std::vector<std::string> parsedPropertyValues;
628
629void insert_property_values(size_t lineNum, size_t line_start, size_t line_end, const char * buffer) {
630    assert (line_start <= line_end);
631    parsedPropertyValues.emplace_back(buffer + line_start, buffer + line_end);
632}
633
634
635const std::vector<std::string> & grepPropertyValues(const std::string& propertyName, re::RE * propertyValuePattern) {
636    ParabixDriver pxDriver("propertyValueEngine");
637    AlignedAllocator<char, 32> alloc;
638
639    parsedPropertyValues.clear();
640
641    const std::string & str = UCD::getPropertyValueGrepString(propertyName);
642
643    auto & idb = pxDriver.getBuilder();
644
645    const unsigned segmentSize = 8;
646    const auto n = str.length();
647    const auto w = idb->getBitBlockWidth() * segmentSize;
648    const auto m = w - (n % w);
649
650    char * aligned = alloc.allocate(n + m, 0);
651    std::memcpy(aligned, str.data(), n);
652    std::memset(aligned + n, 0, m);
653
654    Module * M = idb->getModule();
655   
656    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getVoidTy(), idb->getInt8PtrTy(), idb->getSizeTy(), nullptr));
657    mainFunc->setCallingConv(CallingConv::C);
658    auto args = mainFunc->arg_begin();
659    Value * const buffer = &*(args++);
660    buffer->setName("buffer");
661    Value * length = &*(args++);
662    length->setName("length");
663   
664    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
665   
666    StreamSetBuffer * ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8)));
667    kernel::Kernel * sourceK = pxDriver.addKernelInstance(make_unique<kernel::MemorySourceKernel>(idb, idb->getInt8PtrTy(), segmentSize));
668    sourceK->setInitialArguments({buffer, length});
669    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
670   
671    StreamSetBuffer * BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize));
672   
673    kernel::Kernel * s2pk = pxDriver.addKernelInstance(make_unique<kernel::S2PKernel>(idb));
674    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
675   
676    kernel::Kernel * linebreakK = pxDriver.addKernelInstance(make_unique<kernel::LineBreakKernelBuilder>(idb, 8));
677    StreamSetBuffer * LineBreakStream = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize));
678    pxDriver.makeKernelCall(linebreakK, {BasisBits}, {LineBreakStream});
679   
680    kernel::Kernel * requiredStreamsK = pxDriver.addKernelInstance(make_unique<kernel::RequiredStreams_UTF8>(idb));
681    StreamSetBuffer * RequiredStreams = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(4, 1), segmentSize));
682    pxDriver.makeKernelCall(requiredStreamsK, {BasisBits}, {RequiredStreams});
683   
684    StreamSetBuffer * MatchResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize));
685    kernel::Kernel * icgrepK = pxDriver.addKernelInstance(make_unique<kernel::ICGrepKernel>(idb, propertyValuePattern));
686    pxDriver.makeKernelCall(icgrepK, {BasisBits, LineBreakStream, RequiredStreams}, {MatchResults});
687
688    StreamSetBuffer * MatchedLines = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize));
689    kernel::Kernel * matchedLinesK = pxDriver.addKernelInstance(make_unique<kernel::MatchedLinesKernel>(idb));
690    pxDriver.makeKernelCall(matchedLinesK, {MatchResults, LineBreakStream}, {MatchedLines});
691
692    kernel::Kernel * scanMatchK = pxDriver.addKernelInstance(make_unique<kernel::ScanMatchKernel>(idb, GrepType::PropertyValue, 8));
693    scanMatchK->setInitialArguments({idb->getInt32(0)});
694    pxDriver.makeKernelCall(scanMatchK, {MatchedLines, LineBreakStream, ByteStream}, {});
695    pxDriver.LinkFunction(*scanMatchK, "matcher", &insert_property_values);
696    pxDriver.generatePipelineIR();
697    idb->CreateRetVoid();
698    pxDriver.finalizeObject();
699
700    typedef void (*GrepFunctionType)(const char * buffer, const size_t length);
701    auto f = reinterpret_cast<GrepFunctionType>(pxDriver.getMain());
702    f(aligned, n);
703   
704    alloc.deallocate(aligned, 0);
705    return parsedPropertyValues;
706}
707
708   
709}
Note: See TracBrowser for help on using the repository browser.