source: icGREP/icgrep-devel/icgrep/kernels/grep_kernel.cpp @ 5819

Last change on this file since 5819 was 5819, checked in by cameron, 13 months ago

Bug fix for multiplexed alphabet mode

File size: 11.8 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 */
5
6#include "grep_kernel.h"
7#include <boost/uuid/sha1.hpp>
8#include <re/printer_re.h>
9#include <re/re_toolchain.h>
10#include <pablo/pablo_toolchain.h>
11#include <kernels/kernel_builder.h>
12#include <pablo/builder.hpp>
13#include <pablo/pe_ones.h>          // for Ones
14#include <pablo/pe_var.h>           // for Var
15#include <pablo/pe_zeroes.h>        // for Zeroes
16#include <pablo/boolean.h>
17#include <pablo/pe_count.h>
18#include <pablo/pe_matchstar.h>
19#include <cc/cc_compiler.h>         // for CC_Compiler
20#include <cc/alphabet.h>
21#include <cc/multiplex_CCs.h>
22#include <re/re_compiler.h>
23#include <llvm/Support/raw_ostream.h>
24
25using namespace kernel;
26using namespace pablo;
27using namespace re;
28using namespace llvm;
29
30inline static std::string sha1sum(const std::string & str) {
31    char buffer[41];    // 40 hex-digits and the terminating null
32    uint32_t digest[5]; // 160 bits in total
33    boost::uuids::detail::sha1 sha1;
34    sha1.process_bytes(str.c_str(), str.size());
35    sha1.get_digest(digest);
36    snprintf(buffer, sizeof(buffer), "%.8x%.8x%.8x%.8x%.8x",
37             digest[0], digest[1], digest[2], digest[3], digest[4]);
38    return std::string(buffer);
39}
40
41void RequiredStreams_UTF8::generatePabloMethod() {
42   
43    cc::CC_Compiler ccc(this, getInput(0));
44    auto & pb = ccc.getBuilder();
45    Zeroes * const ZEROES = pb.createZeroes();
46    PabloAST * const u8pfx = ccc.compileCC(makeByte(0xC0, 0xFF));
47
48
49    Var * const nonFinal = pb.createVar("nonFinal", u8pfx);
50    Var * const u8invalid = pb.createVar("u8invalid", ZEROES);
51    Var * const valid_pfx = pb.createVar("valid_pfx", u8pfx);
52
53    PabloBuilder it = PabloBuilder::Create(pb);
54
55    pb.createIf(u8pfx, it);
56    PabloAST * const u8pfx2 = ccc.compileCC(makeByte(0xC2, 0xDF), it);
57    PabloAST * const u8pfx3 = ccc.compileCC(makeByte(0xE0, 0xEF), it);
58    PabloAST * const u8pfx4 = ccc.compileCC(makeByte(0xF0, 0xF4), it);
59    PabloAST * const u8suffix = ccc.compileCC("u8suffix", makeByte(0x80, 0xBF), it);
60   
61    //
62    // Two-byte sequences
63    Var * const anyscope = it.createVar("anyscope", ZEROES);
64    PabloBuilder it2 = PabloBuilder::Create(it);
65    it.createIf(u8pfx2, it2);
66    it2.createAssign(anyscope, it2.createAdvance(u8pfx2, 1));
67
68    //
69    // Three-byte sequences   
70    Var * const EF_invalid = it.createVar("EF_invalid", ZEROES);
71    PabloBuilder it3 = PabloBuilder::Create(it);
72    it.createIf(u8pfx3, it3);
73    PabloAST * const u8scope32 = it3.createAdvance(u8pfx3, 1);
74    it3.createAssign(nonFinal, it3.createOr(nonFinal, u8scope32));
75    PabloAST * const u8scope33 = it3.createAdvance(u8pfx3, 2);
76    PabloAST * const u8scope3X = it3.createOr(u8scope32, u8scope33);
77    it3.createAssign(anyscope, it3.createOr(anyscope, u8scope3X));
78    PabloAST * const E0_invalid = it3.createAnd(it3.createAdvance(ccc.compileCC(makeByte(0xE0), it3), 1), ccc.compileCC(makeByte(0x80, 0x9F), it3));
79    PabloAST * const ED_invalid = it3.createAnd(it3.createAdvance(ccc.compileCC(makeByte(0xED), it3), 1), ccc.compileCC(makeByte(0xA0, 0xBF), it3));
80    PabloAST * const EX_invalid = it3.createOr(E0_invalid, ED_invalid);
81    it3.createAssign(EF_invalid, EX_invalid);
82
83
84    //
85    // Four-byte sequences
86    PabloBuilder it4 = PabloBuilder::Create(it);
87    it.createIf(u8pfx4, it4);
88    PabloAST * const u8scope42 = it4.createAdvance(u8pfx4, 1, "u8scope42");
89    PabloAST * const u8scope43 = it4.createAdvance(u8scope42, 1, "u8scope43");
90    PabloAST * const u8scope44 = it4.createAdvance(u8scope43, 1, "u8scope44");
91    PabloAST * const u8scope4nonfinal = it4.createOr(u8scope42, u8scope43);
92    it4.createAssign(nonFinal, it4.createOr(nonFinal, u8scope4nonfinal));
93    PabloAST * const u8scope4X = it4.createOr(u8scope4nonfinal, u8scope44);
94    it4.createAssign(anyscope, it4.createOr(anyscope, u8scope4X));
95    PabloAST * const F0_invalid = it4.createAnd(it4.createAdvance(ccc.compileCC(makeByte(0xF0), it4), 1), ccc.compileCC(makeByte(0x80, 0x8F), it4));
96    PabloAST * const F4_invalid = it4.createAnd(it4.createAdvance(ccc.compileCC(makeByte(0xF4), it4), 1), ccc.compileCC(makeByte(0x90, 0xBF), it4));
97    PabloAST * const FX_invalid = it4.createOr(F0_invalid, F4_invalid);
98    it4.createAssign(EF_invalid, it4.createOr(EF_invalid, FX_invalid));
99   
100    //
101    // Invalid cases
102    PabloAST * const legalpfx = it.createOr(it.createOr(u8pfx2, u8pfx3), u8pfx4);
103    //  Any scope that does not have a suffix byte, and any suffix byte that is not in
104    //  a scope is a mismatch, i.e., invalid UTF-8.
105    PabloAST * const mismatch = it.createXor(anyscope, u8suffix);
106    //
107    PabloAST * const pfx_invalid = it.createXor(valid_pfx, legalpfx);
108    it.createAssign(u8invalid, it.createOr(pfx_invalid, it.createOr(mismatch, EF_invalid)));
109    PabloAST * const u8valid = it.createNot(u8invalid, "u8valid");
110    //
111    //
112   
113    it.createAssign(valid_pfx, it.createAnd(valid_pfx, u8valid));
114    it.createAssign(nonFinal, it.createAnd(nonFinal, u8valid));
115   
116    PabloAST * u8single = pb.createAnd(ccc.compileCC(makeByte(0x00, 0x7F)), pb.createNot(u8invalid));
117    PabloAST * const initial = pb.createOr(u8single, valid_pfx, "initial");
118    PabloAST * const final = pb.createNot(pb.createOr(nonFinal, u8invalid), "final");
119
120    Var * const required = getOutputStreamVar("required");
121    pb.createAssign(pb.createExtract(required, pb.getInteger(0)), initial);
122    pb.createAssign(pb.createExtract(required, pb.getInteger(1)), nonFinal);
123    pb.createAssign(pb.createExtract(required, pb.getInteger(2)), final);
124
125}
126
127RequiredStreams_UTF8::RequiredStreams_UTF8(const std::unique_ptr<kernel::KernelBuilder> & kb)
128: PabloKernel(kb, "RequiredStreams_UTF8",
129// input
130{Binding{kb->getStreamSetTy(8), "basis"}},
131// output
132{Binding{kb->getStreamSetTy(3), "required", FixedRate(), Add1()}}) {
133
134}
135
136void RequiredStreams_UTF16::generatePabloMethod() {
137   
138    cc::CC_Compiler ccc(this, getInput(0));
139    auto & pb = ccc.getBuilder();
140   
141    PabloAST * u16hi_hi_surrogate = ccc.compileCC(makeCC(0xD800, 0xDBFF, &cc::UTF16));    //u16hi_hi_surrogate = [\xD8-\xDB]
142    PabloAST * u16hi_lo_surrogate = ccc.compileCC(makeCC(0xDC00, 0xDFFF, &cc::UTF16));    //u16hi_lo_surrogate = [\xDC-\xDF]
143   
144    PabloAST * invalidTemp = pb.createAdvance(u16hi_hi_surrogate, 1, "InvalidTemp");
145    PabloAST * u16invalid = pb.createXor(invalidTemp, u16hi_lo_surrogate, "u16invalid");
146
147    PabloAST * u16valid = pb.createNot(u16invalid, "u16valid");
148    PabloAST * nonFinal = pb.createAnd(u16hi_hi_surrogate, u16valid, "nonfinal");
149
150    PabloAST * u16single_temp = pb.createOr(ccc.compileCC(makeCC(0x0000, 0xD7FF, &cc::UTF16)), ccc.compileCC(makeCC(0xE000, 0xFFFF, &cc::UTF16)));
151    PabloAST * u16single = pb.createAnd(u16single_temp, pb.createNot(u16invalid));
152
153    PabloAST * const nonFinalCodeUnits = pb.createExtract(getInput(1), pb.getInteger(0));
154    PabloAST * const initial = pb.createOr(u16single, u16hi_hi_surrogate, "initial");
155    PabloAST * const final = pb.createNot(pb.createOr(pb.createOr(u16hi_hi_surrogate, u16invalid), nonFinalCodeUnits), "final");
156
157    Var * const required = getOutputStreamVar("required");
158    pb.createAssign(pb.createExtract(required, pb.getInteger(0)), initial);
159    pb.createAssign(pb.createExtract(required, pb.getInteger(1)), nonFinal);
160    pb.createAssign(pb.createExtract(required, pb.getInteger(2)), final);
161
162}
163
164RequiredStreams_UTF16::RequiredStreams_UTF16(const std::unique_ptr<kernel::KernelBuilder> & kb)
165: PabloKernel(kb, "RequiredStreams_UTF16",               
166// inputs
167{Binding{kb->getStreamSetTy(8), "basis"}},
168// output
169{Binding{kb->getStreamSetTy(3), "required", FixedRate(), Add1()}}) {
170
171}
172
173ICGrepSignature::ICGrepSignature(re::RE * const re_ast)
174: mRE(re_ast)
175, mSignature(Printer_RE::PrintRE(mRE)) {
176   
177}
178
179// Helper to compute stream set inputs to pass into PabloKernel constructor.
180std::vector<Binding> icGrepInputs(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, std::vector<cc::Alphabet *> alphabets) {
181    std::vector<Binding> streamSetInputs = {Binding{iBuilder->getStreamSetTy(8), "basis"},
182        Binding{iBuilder->getStreamSetTy(1, 1), "linebreak"},
183        Binding{iBuilder->getStreamSetTy(1, 1), "cr+lf"},
184        Binding{iBuilder->getStreamSetTy(3, 1), "required"}};
185    for (unsigned i = 0; i < alphabets.size(); i++) {
186        unsigned basis_size = cast<cc::MultiplexedAlphabet>(alphabets[i])->getMultiplexedCCs().size();
187        streamSetInputs.push_back(Binding{iBuilder->getStreamSetTy(basis_size, 1), alphabets[i]->getName() + "_basis"});
188    }
189    return streamSetInputs;
190}
191
192ICGrepKernel::ICGrepKernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, RE * const re, std::vector<cc::Alphabet *> alphabets)
193: ICGrepSignature(re)
194, PabloKernel(iBuilder, "ic" + sha1sum(mSignature),
195// inputs
196icGrepInputs(iBuilder, alphabets),
197// output
198{Binding{iBuilder->getStreamSetTy(1, 1), "matches", FixedRate(), Add1()}}) {
199    mAlphabets = alphabets;
200}
201
202std::string ICGrepKernel::makeSignature(const std::unique_ptr<kernel::KernelBuilder> &) {
203    return mSignature;
204}
205
206void ICGrepKernel::generatePabloMethod() {
207    Var * const basis = getInputStreamVar("basis");
208    cc::CC_Compiler cc_compiler(this, basis);
209    RE_Compiler re_compiler(this, cc_compiler);
210    for (unsigned i = 0; i < mAlphabets.size(); i++) {
211        auto basis = getInputStreamVar(mAlphabets[i]->getName() + "_basis");
212        re_compiler.addAlphabet(mAlphabets[i], basis);
213    }
214    PabloAST * const match_post = re_compiler.compile(mRE);
215    PabloBlock * const pb = getEntryBlock();
216    Var * const output = getOutputStreamVar("matches");
217    pb->createAssign(pb->createExtract(output, pb->getInteger(0)), match_post);
218}
219
220void MatchedLinesKernel::generatePabloMethod() {
221    auto pb = this->getEntryBlock();
222    PabloAST * matchResults = pb->createExtract(getInputStreamVar("matchResults"), pb->getInteger(0));
223    PabloAST * lineBreaks = pb->createExtract(getInputStreamVar("lineBreaks"), pb->getInteger(0));
224    PabloAST * notLB = pb->createNot(lineBreaks);
225    PabloAST * match_follow = pb->createMatchStar(matchResults, notLB);
226    Var * const matchedLines = getOutputStreamVar("matchedLines");
227    pb->createAssign(pb->createExtract(matchedLines, pb->getInteger(0)), pb->createAnd(match_follow, lineBreaks));
228}
229
230MatchedLinesKernel::MatchedLinesKernel (const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
231: PabloKernel(iBuilder, "MatchedLines",
232// inputs
233{Binding{iBuilder->getStreamSetTy(1), "matchResults"}
234,Binding{iBuilder->getStreamSetTy(1), "lineBreaks"}},
235// output
236{Binding{iBuilder->getStreamSetTy(1), "matchedLines"}}) {
237
238}
239
240
241void InvertMatchesKernel::generateDoBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder) {
242    Value * input = iBuilder->loadInputStreamBlock("matchedLines", iBuilder->getInt32(0));
243    Value * lbs = iBuilder->loadInputStreamBlock("lineBreaks", iBuilder->getInt32(0));
244    Value * inverted = iBuilder->CreateXor(input, lbs);
245    iBuilder->storeOutputStreamBlock("nonMatches", iBuilder->getInt32(0), inverted);
246}
247
248InvertMatchesKernel::InvertMatchesKernel(const std::unique_ptr<kernel::KernelBuilder> & builder)
249: BlockOrientedKernel("Invert",
250// Inputs
251{Binding{builder->getStreamSetTy(1, 1), "matchedLines"}, Binding{builder->getStreamSetTy(1, 1), "lineBreaks"}},
252// Outputs
253{Binding{builder->getStreamSetTy(1, 1), "nonMatches"}},
254// Input/Output Scalars and internal state
255{}, {}, {}) {
256
257}
258
259
260void PopcountKernel::generatePabloMethod() {
261    auto pb = this->getEntryBlock();
262    const auto toCount = pb->createExtract(getInputStreamVar("toCount"), pb->getInteger(0));
263    pablo::Var * countResult = getOutputScalarVar("countResult");
264    pb->createAssign(countResult, pb->createCount(toCount));
265}
266
267PopcountKernel::PopcountKernel (const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
268: PabloKernel(iBuilder, "Popcount",
269              {Binding{iBuilder->getStreamSetTy(1), "toCount"}},
270              {},
271              {},
272              {Binding{iBuilder->getSizeTy(), "countResult"}}) {
273}
Note: See TracBrowser for help on using the repository browser.