source: icGREP/icgrep-devel/icgrep/toolchain/grep_pipeline.cpp @ 5861

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

Using DirectCC builder updates; speedup wc -l

File size: 5.4 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 = 8;
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    StreamSetBuffer * LineFeedStream = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
66    #ifdef USE_DIRECT_LF_BUILDER
67    kernel::Kernel * linefeedK = pxDriver.addKernelInstance<kernel::LineFeedKernelBuilder>(idb, Binding{idb->getStreamSetTy(1, 8), "byteStream", FixedRate(), Principal()});
68    pxDriver.makeKernelCall(linefeedK, {ByteStream}, {LineFeedStream});
69    #endif
70
71    StreamSetBuffer * BasisBits = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize);
72    kernel::Kernel * s2pk = pxDriver.addKernelInstance<kernel::S2PKernel>(idb);
73    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
74
75    #ifndef USE_DIRECT_LF_BUILDER
76    kernel::Kernel * linefeedK = pxDriver.addKernelInstance<kernel::LineFeedKernelBuilder>(idb, Binding{idb->getStreamSetTy(8), "basis", FixedRate(), Principal()});
77    pxDriver.makeKernelCall(linefeedK, {BasisBits}, {LineFeedStream});
78    #endif
79
80    kernel::Kernel * linebreakK = pxDriver.addKernelInstance<kernel::LineBreakKernelBuilder>(idb, 8);
81    StreamSetBuffer * LineBreakStream = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
82    StreamSetBuffer * CRLFStream = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
83    pxDriver.makeKernelCall(linebreakK, {BasisBits, LineFeedStream}, {LineBreakStream, CRLFStream});
84   
85    kernel::Kernel * requiredStreamsK = pxDriver.addKernelInstance<kernel::RequiredStreams_UTF8>(idb);
86    StreamSetBuffer * RequiredStreams = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(3, 1), segmentSize);
87    pxDriver.makeKernelCall(requiredStreamsK, {BasisBits}, {RequiredStreams});
88   
89    StreamSetBuffer * MatchResults = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
90    kernel::Kernel * icgrepK = pxDriver.addKernelInstance<kernel::ICGrepKernel>(idb, pattern);
91    pxDriver.makeKernelCall(icgrepK, {BasisBits, LineBreakStream, CRLFStream, RequiredStreams}, {MatchResults});
92   
93    StreamSetBuffer * MatchedLines = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
94    kernel::Kernel * matchedLinesK = pxDriver.addKernelInstance<kernel::MatchedLinesKernel>(idb);
95    pxDriver.makeKernelCall(matchedLinesK, {MatchResults, LineBreakStream}, {MatchedLines});
96   
97    kernel::Kernel * scanMatchK = pxDriver.addKernelInstance<kernel::ScanMatchKernel>(idb);
98    scanMatchK->setInitialArguments({ConstantInt::get(idb->getIntAddrTy(), reinterpret_cast<intptr_t>(accum))});
99    pxDriver.makeKernelCall(scanMatchK, {MatchedLines, LineBreakStream, ByteStream}, {});
100    pxDriver.LinkFunction(*scanMatchK, "accumulate_match_wrapper", &accumulate_match_wrapper);
101    pxDriver.LinkFunction(*scanMatchK, "finalize_match_wrapper", &finalize_match_wrapper);
102    pxDriver.generatePipelineIR();
103    pxDriver.deallocateBuffers();
104    idb->CreateRetVoid();
105    pxDriver.finalizeObject();
106   
107    typedef void (*GrepFunctionType)(const char * buffer, const size_t length);
108    auto f = reinterpret_cast<GrepFunctionType>(pxDriver.getMain());
109    f(search_buffer, bufferLength);
110}
111}
Note: See TracBrowser for help on using the repository browser.