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

Last change on this file since 5781 was 5769, checked in by cameron, 23 months ago

Decoupling case-insensitive transform from parser

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