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

Last change on this file since 5816 was 5816, checked in by cameron, 21 months ago

Supporting multiple alphabets in RE compilation - initial check-in

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