source: icGREP/icgrep-devel/icgrep/toolchain/grep_pipeline.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: 4.9 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_pipeline.h"
8#include <llvm/IR/Module.h>
9#include <boost/filesystem.hpp>
10#include <kernels/grep_kernel.h>
11#include <kernels/linebreak_kernel.h>
12#include <kernels/source_kernel.h>
13#include <kernels/s2p_kernel.h>
14#include <kernels/scanmatchgen.h>
15#include <kernels/streamset.h>
16#include <kernels/kernel_builder.h>
17#include <re/casing.h>
18#include <re/re_toolchain.h>
19#include <toolchain/toolchain.h>
20#include <re/re_name_resolve.h>   
21#include <re/re_collect_unicodesets.h>
22#include <re/re_multiplex.h>
23#include <toolchain/cpudriver.h>
24#include <llvm/Support/raw_ostream.h>
25
26using namespace parabix;
27using namespace llvm;
28using namespace kernel;
29
30namespace grep {
31void accumulate_match_wrapper(intptr_t accum_addr, const size_t lineNum, char * line_start, char * line_end) {
32    reinterpret_cast<MatchAccumulator *>(accum_addr)->accumulate_match(lineNum, line_start, line_end);
33}
34
35void finalize_match_wrapper(intptr_t accum_addr, char * buffer_end) {
36    reinterpret_cast<MatchAccumulator *>(accum_addr)->finalize_match(buffer_end);
37}
38
39void grepBuffer(re::RE * pattern, const char * search_buffer, size_t bufferLength, MatchAccumulator * accum) {
40    const unsigned segmentSize = codegen::BufferSegments * codegen::SegmentSize * codegen::ThreadNum;
41
42    pattern = resolveCaseInsensitiveMode(pattern, false);
43    pattern = regular_expression_passes(pattern);
44   
45   
46    ParabixDriver pxDriver("codepointEngine");
47    auto & idb = pxDriver.getBuilder();
48    Module * M = idb->getModule();
49   
50    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getVoidTy(), idb->getInt8PtrTy(), idb->getSizeTy(), nullptr));
51    mainFunc->setCallingConv(CallingConv::C);
52    auto args = mainFunc->arg_begin();
53    Value * const buffer = &*(args++);
54    buffer->setName("buffer");
55    Value * length = &*(args++);
56    length->setName("length");
57   
58    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
59   
60    StreamSetBuffer * ByteStream = pxDriver.addBuffer<SourceBuffer>(idb, idb->getStreamSetTy(1, 8));
61    kernel::Kernel * sourceK = pxDriver.addKernelInstance<kernel::MemorySourceKernel>(idb, idb->getInt8PtrTy());
62    sourceK->setInitialArguments({buffer, length});
63    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
64   
65
66    StreamSetBuffer * BasisBits = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize);
67    kernel::Kernel * s2pk = pxDriver.addKernelInstance<kernel::S2PKernel>(idb);
68    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
69
70    StreamSetBuffer * LineFeedStream = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
71    kernel::Kernel * linefeedK = pxDriver.addKernelInstance<kernel::LineFeedKernelBuilder>(idb, Binding{idb->getStreamSetTy(8), "basis", FixedRate(), Principal()});
72    pxDriver.makeKernelCall(linefeedK, {BasisBits}, {LineFeedStream});
73
74    StreamSetBuffer * LineBreakStream = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
75   
76    kernel::Kernel * requiredStreamsK = pxDriver.addKernelInstance<kernel::RequiredStreams_UTF8>(idb);
77    StreamSetBuffer * RequiredStreams = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(2, 1), segmentSize);
78    pxDriver.makeKernelCall(requiredStreamsK, {BasisBits, LineFeedStream}, {RequiredStreams, LineBreakStream});
79   
80    StreamSetBuffer * MatchResults = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
81    kernel::Kernel * icgrepK = pxDriver.addKernelInstance<kernel::ICGrepKernel>(idb, pattern);
82    pxDriver.makeKernelCall(icgrepK, {BasisBits, LineBreakStream, RequiredStreams}, {MatchResults});
83   
84    StreamSetBuffer * MatchedLines = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
85    kernel::Kernel * matchedLinesK = pxDriver.addKernelInstance<kernel::MatchedLinesKernel>(idb);
86    pxDriver.makeKernelCall(matchedLinesK, {MatchResults, LineBreakStream}, {MatchedLines});
87   
88    kernel::Kernel * scanMatchK = pxDriver.addKernelInstance<kernel::ScanMatchKernel>(idb);
89    scanMatchK->setInitialArguments({ConstantInt::get(idb->getIntAddrTy(), reinterpret_cast<intptr_t>(accum))});
90    pxDriver.makeKernelCall(scanMatchK, {MatchedLines, LineBreakStream, ByteStream}, {});
91    pxDriver.LinkFunction(*scanMatchK, "accumulate_match_wrapper", &accumulate_match_wrapper);
92    pxDriver.LinkFunction(*scanMatchK, "finalize_match_wrapper", &finalize_match_wrapper);
93   
94    pxDriver.generatePipelineIR();
95    pxDriver.deallocateBuffers();
96    idb->CreateRetVoid();
97    pxDriver.finalizeObject();
98   
99    typedef void (*GrepFunctionType)(const char * buffer, const size_t length);
100    auto f = reinterpret_cast<GrepFunctionType>(pxDriver.getMain());
101    f(search_buffer, bufferLength);
102}
103}
Note: See TracBrowser for help on using the repository browser.