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

Last change on this file since 5832 was 5793, checked in by nmedfort, 19 months ago

Bug fix for pipeline: it was terminating too early when there was insufficient output space to process all of the input for a kernel.

File size: 5.0 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;
28
29namespace grep {
30void accumulate_match_wrapper(intptr_t accum_addr, const size_t lineNum, char * line_start, char * line_end) {
31    reinterpret_cast<MatchAccumulator *>(accum_addr)->accumulate_match(lineNum, line_start, line_end);
32}
33
34void finalize_match_wrapper(intptr_t accum_addr, char * buffer_end) {
35    reinterpret_cast<MatchAccumulator *>(accum_addr)->finalize_match(buffer_end);
36}
37
38void grepBuffer(re::RE * pattern, const char * search_buffer, size_t bufferLength, MatchAccumulator * accum) {
39    const unsigned segmentSize = 8;
40
41    pattern = resolveCaseInsensitiveMode(pattern, false);
42    pattern = regular_expression_passes(pattern);
43   
44   
45    ParabixDriver pxDriver("codepointEngine");
46    auto & idb = pxDriver.getBuilder();
47    Module * M = idb->getModule();
48   
49    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getVoidTy(), idb->getInt8PtrTy(), idb->getSizeTy(), nullptr));
50    mainFunc->setCallingConv(CallingConv::C);
51    auto args = mainFunc->arg_begin();
52    Value * const buffer = &*(args++);
53    buffer->setName("buffer");
54    Value * length = &*(args++);
55    length->setName("length");
56   
57    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
58   
59    StreamSetBuffer * ByteStream = pxDriver.addBuffer<SourceBuffer>(idb, idb->getStreamSetTy(1, 8));
60    kernel::Kernel * sourceK = pxDriver.addKernelInstance<kernel::MemorySourceKernel>(idb, idb->getInt8PtrTy(), segmentSize);
61    sourceK->setInitialArguments({buffer, length});
62    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
63   
64    StreamSetBuffer * BasisBits = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(8, 1), segmentSize);
65    kernel::Kernel * s2pk = pxDriver.addKernelInstance<kernel::S2PKernel>(idb);
66    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
67   
68    kernel::Kernel * linefeedK = pxDriver.addKernelInstance<kernel::LineFeedKernelBuilder>(idb, 8);
69    StreamSetBuffer * LineFeedStream = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
70    pxDriver.makeKernelCall(linefeedK, {BasisBits}, {LineFeedStream});
71
72    kernel::Kernel * linebreakK = pxDriver.addKernelInstance<kernel::LineBreakKernelBuilder>(idb, 8);
73    StreamSetBuffer * LineBreakStream = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
74    StreamSetBuffer * CRLFStream = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
75    pxDriver.makeKernelCall(linebreakK, {BasisBits, LineFeedStream}, {LineBreakStream, CRLFStream});
76   
77    kernel::Kernel * requiredStreamsK = pxDriver.addKernelInstance<kernel::RequiredStreams_UTF8>(idb);
78    StreamSetBuffer * RequiredStreams = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(3, 1), segmentSize);
79    pxDriver.makeKernelCall(requiredStreamsK, {BasisBits}, {RequiredStreams});
80   
81    StreamSetBuffer * MatchResults = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
82    kernel::Kernel * icgrepK = pxDriver.addKernelInstance<kernel::ICGrepKernel>(idb, pattern);
83    pxDriver.makeKernelCall(icgrepK, {BasisBits, LineBreakStream, CRLFStream, RequiredStreams}, {MatchResults});
84   
85    StreamSetBuffer * MatchedLines = pxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize);
86    kernel::Kernel * matchedLinesK = pxDriver.addKernelInstance<kernel::MatchedLinesKernel>(idb);
87    pxDriver.makeKernelCall(matchedLinesK, {MatchResults, LineBreakStream}, {MatchedLines});
88   
89    kernel::Kernel * scanMatchK = pxDriver.addKernelInstance<kernel::ScanMatchKernel>(idb);
90    scanMatchK->setInitialArguments({ConstantInt::get(idb->getIntAddrTy(), reinterpret_cast<intptr_t>(accum))});
91    pxDriver.makeKernelCall(scanMatchK, {MatchedLines, LineBreakStream, ByteStream}, {});
92    pxDriver.LinkFunction(*scanMatchK, "accumulate_match_wrapper", &accumulate_match_wrapper);
93    pxDriver.LinkFunction(*scanMatchK, "finalize_match_wrapper", &finalize_match_wrapper);
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.