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

Last change on this file since 6014 was 5989, checked in by cameron, 14 months ago

AbortOnNull? functionality - initial check-in

File size: 25.4 KB
Line 
1/*
2 *  Copyright (c) 2018 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_cc.h>
10#include <re/re_name.h>
11#include <re/re_toolchain.h>
12#include <re/re_reverse.h>
13#include <grep/grep_engine.h>
14#include <pablo/codegenstate.h>
15#include <pablo/pablo_toolchain.h>
16#include <kernels/kernel_builder.h>
17#include <pablo/builder.hpp>
18#include <pablo/pe_ones.h>          // for Ones
19#include <pablo/pe_var.h>           // for Var
20#include <pablo/pe_zeroes.h>        // for Zeroes
21#include <pablo/pe_infile.h>
22#include <pablo/boolean.h>
23#include <pablo/pe_count.h>
24#include <pablo/pe_matchstar.h>
25#include <pablo/pe_pack.h>
26#include <cc/cc_compiler.h>         // for CC_Compiler
27#include <cc/alphabet.h>
28#include <cc/multiplex_CCs.h>
29#include <re/re_compiler.h>
30#include <UCD/ucd_compiler.hpp>
31#include <llvm/IR/Module.h>
32#include <llvm/Support/raw_ostream.h>
33
34using namespace kernel;
35using namespace pablo;
36using namespace re;
37using namespace llvm;
38
39inline static std::string sha1sum(const std::string & str) {
40    char buffer[41];    // 40 hex-digits and the terminating null
41    uint32_t digest[5]; // 160 bits in total
42    boost::uuids::detail::sha1 sha1;
43    sha1.process_bytes(str.c_str(), str.size());
44    sha1.get_digest(digest);
45    snprintf(buffer, sizeof(buffer), "%.8x%.8x%.8x%.8x%.8x",
46             digest[0], digest[1], digest[2], digest[3], digest[4]);
47    return std::string(buffer);
48}
49
50
51UnicodeLineBreakKernel::UnicodeLineBreakKernel(const std::unique_ptr<kernel::KernelBuilder> & kb)
52: PabloKernel(kb,
53              "UTF8_LB",
54              {Binding{kb->getStreamSetTy(8), "basis"}, Binding{kb->getStreamSetTy(1), "lf", FixedRate(), LookAhead(1)}},
55              {Binding{kb->getStreamSetTy(1, 1), "UTF8_LB", FixedRate()}}) {
56}
57
58void UnicodeLineBreakKernel::generatePabloMethod() {
59        PabloBuilder pb(getEntryScope());
60        cc::Parabix_CC_Compiler ccc(getEntryScope(), getInputStreamSet("basis"));
61        UCD::UCDCompiler ucdCompiler(ccc);
62   
63    Name * breakChars = re::makeName("breakChars", makeCC(makeCC(makeCC(0x0A, 0x0D), makeCC(0x85)), makeCC(0x2028,0x2029)));
64    UCD::UCDCompiler::NameMap nameMap;
65    nameMap.emplace(breakChars, nullptr);
66    ucdCompiler.generateWithDefaultIfHierarchy(nameMap, pb);
67    auto f = nameMap.find(breakChars);
68    if (f == nameMap.end()) llvm::report_fatal_error("UnicodeLineBreakKernel compilation failure");
69    PabloAST * breakStream = f-> second;
70    PabloAST * const LF = pb.createExtract(getInput(1), pb.getInteger(0), "LF");
71    PabloAST * const CR = ccc.compileCC(makeByte(0x0D));
72    Var * const CR_before_LF = pb.createVar("CR_before_LFCR_before_LF", pb.createZeroes());
73    auto crb = pb.createScope();
74    pb.createIf(CR, crb);
75    PabloAST * const lookaheadLF = crb.createLookahead(LF, 1, "lookaheadLF");
76    crb.createAssign(CR_before_LF, crb.createAnd(CR, lookaheadLF));
77    breakStream = pb.createXor(breakStream, CR_before_LF);  // Remove CR_before_LF from breakStream
78    Var * const UTF8_LB = getOutputStreamVar("UTF8_LB");
79    pb.createAssign(pb.createExtract(UTF8_LB, pb.getInteger(0)), breakStream);
80}
81
82void RequiredStreams_UTF8::generatePabloMethod() {
83    PabloBuilder pb(getEntryScope());
84    cc::Parabix_CC_Compiler ccc(getEntryScope(), getInputStreamSet("basis"));
85   
86    PabloAST * const LF = pb.createExtract(getInput(1), pb.getInteger(0), "LF");
87    PabloAST * const CR = ccc.compileCC(makeByte(0x0D));
88    PabloAST * const LF_VT_FF_CR = ccc.compileCC("LF,VT,FF,CR", makeByte(0x0A, 0x0D), pb);
89    Var * const LineBreak = pb.createVar("LineBreak", LF_VT_FF_CR);
90   
91    // Remove the CR of any CR+LF
92    Var * const CRLF = pb.createVar("CRLF", pb.createZeroes());
93    auto crb = pb.createScope();
94    pb.createIf(CR, crb);
95    PabloAST * const lookaheadLF = crb.createLookahead(LF, 1, "lookaheadLF");
96    PabloAST * const crlf = crb.createAnd(CR, lookaheadLF);
97    crb.createAssign(CRLF, crlf);
98    PabloAST * removedCRLF = crb.createAnd(LineBreak, crb.createNot(CRLF));
99    crb.createAssign(LineBreak, removedCRLF);
100
101   
102    Zeroes * const ZEROES = pb.createZeroes();
103    PabloAST * const u8pfx = ccc.compileCC(makeByte(0xC0, 0xFF));
104
105
106    Var * const nonFinal = pb.createVar("nonFinal", u8pfx);
107    Var * const u8invalid = pb.createVar("u8invalid", ZEROES);
108    Var * const valid_pfx = pb.createVar("valid_pfx", u8pfx);
109
110    auto it = pb.createScope();
111    pb.createIf(u8pfx, it);
112    PabloAST * const u8pfx2 = ccc.compileCC(makeByte(0xC2, 0xDF), it);
113    PabloAST * const u8pfx3 = ccc.compileCC(makeByte(0xE0, 0xEF), it);
114    PabloAST * const u8pfx4 = ccc.compileCC(makeByte(0xF0, 0xF4), it);
115    PabloAST * const u8suffix = ccc.compileCC("u8suffix", makeByte(0x80, 0xBF), it);
116   
117    //
118    // Two-byte sequences
119    Var * const anyscope = it.createVar("anyscope", ZEROES);
120    auto it2 = it.createScope();
121    it.createIf(u8pfx2, it2);
122    it2.createAssign(anyscope, it2.createAdvance(u8pfx2, 1));
123    PabloAST * NEL = it2.createAnd(it2.createAdvance(ccc.compileCC(makeByte(0xC2), it2), 1), ccc.compileCC(makeByte(0x85), it2), "NEL");
124    it2.createAssign(LineBreak, it2.createOr(LineBreak, NEL));
125
126
127    //
128    // Three-byte sequences   
129    Var * const EF_invalid = it.createVar("EF_invalid", ZEROES);
130    auto it3 = it.createScope();
131    it.createIf(u8pfx3, it3);
132    PabloAST * const u8scope32 = it3.createAdvance(u8pfx3, 1);
133    it3.createAssign(nonFinal, it3.createOr(nonFinal, u8scope32));
134    PabloAST * const u8scope33 = it3.createAdvance(u8pfx3, 2);
135    PabloAST * const u8scope3X = it3.createOr(u8scope32, u8scope33);
136    it3.createAssign(anyscope, it3.createOr(anyscope, u8scope3X));
137    PabloAST * const E0_invalid = it3.createAnd(it3.createAdvance(ccc.compileCC(makeByte(0xE0), it3), 1), ccc.compileCC(makeByte(0x80, 0x9F), it3));
138    PabloAST * const ED_invalid = it3.createAnd(it3.createAdvance(ccc.compileCC(makeByte(0xED), it3), 1), ccc.compileCC(makeByte(0xA0, 0xBF), it3));
139    PabloAST * const EX_invalid = it3.createOr(E0_invalid, ED_invalid);
140    it3.createAssign(EF_invalid, EX_invalid);
141    PabloAST * E2_80 = it3.createAnd(it3.createAdvance(ccc.compileCC(makeByte(0xE2), it3), 1), ccc.compileCC(makeByte(0x80), it3));
142    PabloAST * LS_PS = it3.createAnd(it3.createAdvance(E2_80, 1), ccc.compileCC(makeByte(0xA8,0xA9), it3), "LS_PS");
143    it3.createAssign(LineBreak, it3.createOr(LineBreak, LS_PS));
144
145    //
146    // Four-byte sequences
147    auto it4 = it.createScope();
148    it.createIf(u8pfx4, it4);
149    PabloAST * const u8scope42 = it4.createAdvance(u8pfx4, 1, "u8scope42");
150    PabloAST * const u8scope43 = it4.createAdvance(u8scope42, 1, "u8scope43");
151    PabloAST * const u8scope44 = it4.createAdvance(u8scope43, 1, "u8scope44");
152    PabloAST * const u8scope4nonfinal = it4.createOr(u8scope42, u8scope43);
153    it4.createAssign(nonFinal, it4.createOr(nonFinal, u8scope4nonfinal));
154    PabloAST * const u8scope4X = it4.createOr(u8scope4nonfinal, u8scope44);
155    it4.createAssign(anyscope, it4.createOr(anyscope, u8scope4X));
156    PabloAST * const F0_invalid = it4.createAnd(it4.createAdvance(ccc.compileCC(makeByte(0xF0), it4), 1), ccc.compileCC(makeByte(0x80, 0x8F), it4));
157    PabloAST * const F4_invalid = it4.createAnd(it4.createAdvance(ccc.compileCC(makeByte(0xF4), it4), 1), ccc.compileCC(makeByte(0x90, 0xBF), it4));
158    PabloAST * const FX_invalid = it4.createOr(F0_invalid, F4_invalid);
159    it4.createAssign(EF_invalid, it4.createOr(EF_invalid, FX_invalid));
160   
161    //
162    // Invalid cases
163    PabloAST * const legalpfx = it.createOr(it.createOr(u8pfx2, u8pfx3), u8pfx4);
164    //  Any scope that does not have a suffix byte, and any suffix byte that is not in
165    //  a scope is a mismatch, i.e., invalid UTF-8.
166    PabloAST * const mismatch = it.createXor(anyscope, u8suffix);
167    //
168    PabloAST * const pfx_invalid = it.createXor(valid_pfx, legalpfx);
169    it.createAssign(u8invalid, it.createOr(pfx_invalid, it.createOr(mismatch, EF_invalid)));
170    PabloAST * const u8valid = it.createNot(u8invalid, "u8valid");
171    //
172    //
173    it.createAssign(nonFinal, it.createAnd(nonFinal, u8valid));
174    pb.createAssign(nonFinal, pb.createOr(nonFinal, CRLF));
175    //PabloAST * unterminatedLineAtEOF = pb.createAtEOF(pb.createAdvance(pb.createNot(LineBreak), 1), "unterminatedLineAtEOF");
176   
177    Var * const required = getOutputStreamVar("nonFinal");
178    pb.createAssign(pb.createExtract(required, pb.getInteger(0)), nonFinal);
179    pb.createAssign(pb.createExtract(getOutputStreamVar("UnicodeLB"), pb.getInteger(0)), LineBreak);//pb.createOr(LineBreak, unterminatedLineAtEOF, "EOL"));
180}
181
182RequiredStreams_UTF8::RequiredStreams_UTF8(const std::unique_ptr<kernel::KernelBuilder> & kb)
183: PabloKernel(kb, "RequiredStreams_UTF8",
184// input
185{Binding{kb->getStreamSetTy(8), "basis"},
186 Binding{kb->getStreamSetTy(1), "lf", FixedRate(), LookAhead(1)}},
187// output
188{Binding{kb->getStreamSetTy(1), "nonFinal", FixedRate()},
189 Binding{kb->getStreamSetTy(1), "UnicodeLB", FixedRate()}}) {
190
191}
192
193void RequiredStreams_UTF16::generatePabloMethod() {
194    PabloBuilder pb(getEntryScope());
195    cc::Parabix_CC_Compiler ccc(getEntryScope(), getInputStreamSet("basis"));
196   
197    PabloAST * u16hi_hi_surrogate = ccc.compileCC(makeCC(0xD800, 0xDBFF, &cc::UTF16));    //u16hi_hi_surrogate = [\xD8-\xDB]
198    PabloAST * u16hi_lo_surrogate = ccc.compileCC(makeCC(0xDC00, 0xDFFF, &cc::UTF16));    //u16hi_lo_surrogate = [\xDC-\xDF]
199   
200    PabloAST * invalidTemp = pb.createAdvance(u16hi_hi_surrogate, 1, "InvalidTemp");
201    PabloAST * u16invalid = pb.createXor(invalidTemp, u16hi_lo_surrogate, "u16invalid");
202
203    PabloAST * u16valid = pb.createNot(u16invalid, "u16valid");
204    PabloAST * nonFinal = pb.createAnd(u16hi_hi_surrogate, u16valid, "nonfinal");
205
206    PabloAST * u16single_temp = pb.createOr(ccc.compileCC(makeCC(0x0000, 0xD7FF, &cc::UTF16)), ccc.compileCC(makeCC(0xE000, 0xFFFF, &cc::UTF16)));
207    PabloAST * u16single = pb.createAnd(u16single_temp, pb.createNot(u16invalid));
208
209    PabloAST * const nonFinalCodeUnits = pb.createExtract(getInput(1), pb.getInteger(0));
210    PabloAST * const initial = pb.createOr(u16single, u16hi_hi_surrogate, "initial");
211    PabloAST * const final = pb.createNot(pb.createOr(pb.createOr(u16hi_hi_surrogate, u16invalid), nonFinalCodeUnits), "final");
212
213    Var * const required = getOutputStreamVar("required");
214    pb.createAssign(pb.createExtract(required, pb.getInteger(0)), initial);
215    pb.createAssign(pb.createExtract(required, pb.getInteger(1)), nonFinal);
216    pb.createAssign(pb.createExtract(required, pb.getInteger(2)), final);
217
218}
219
220RequiredStreams_UTF16::RequiredStreams_UTF16(const std::unique_ptr<kernel::KernelBuilder> & kb)
221: PabloKernel(kb, "RequiredStreams_UTF16",               
222// inputs
223{Binding{kb->getStreamSetTy(8), "basis"}},
224// output
225{Binding{kb->getStreamSetTy(3), "required", FixedRate(), Add1()}}) {
226
227}
228
229ICGrepSignature::ICGrepSignature(re::RE * const re_ast)
230: mRE(re_ast)
231, mSignature(Printer_RE::PrintRE(mRE)) {
232   
233}
234
235// Helper to compute stream set inputs to pass into PabloKernel constructor.
236inline std::vector<Binding> icGrepInputs(const std::unique_ptr<kernel::KernelBuilder> & b,
237                                         const std::vector<std::string> & externals,
238                                         const std::vector<cc::Alphabet *> & alphabets) {
239    std::vector<Binding> streamSetInputs = {
240        Binding{b->getStreamSetTy(8), "basis"},
241    };
242    for (auto & e : externals) {
243        streamSetInputs.push_back(Binding{b->getStreamSetTy(1, 1), e});
244    }
245    for (const auto & alphabet : alphabets) {
246        unsigned basis_size = cast<cc::MultiplexedAlphabet>(alphabet)->getMultiplexedCCs().size();
247        streamSetInputs.push_back(Binding{b->getStreamSetTy(basis_size, 1), alphabet->getName() + "_basis"});
248    }
249    return streamSetInputs;
250}
251
252ICGrepKernel::ICGrepKernel(const std::unique_ptr<kernel::KernelBuilder> & b, RE * const re, std::vector<std::string> externals, std::vector<cc::Alphabet *> alphabets)
253: ICGrepSignature(re)
254, PabloKernel(b, "ic" + sha1sum(mSignature),
255// inputs
256icGrepInputs(b, externals, alphabets),
257// output
258{Binding{b->getStreamSetTy(1, 1), "matches", FixedRate(), Add1()}})
259, mExternals(externals)
260, mAlphabets(alphabets) {
261}
262
263std::string ICGrepKernel::makeSignature(const std::unique_ptr<kernel::KernelBuilder> &) {
264    return mSignature;
265}
266
267void ICGrepKernel::generatePabloMethod() {
268    PabloBuilder pb(getEntryScope());
269    cc::Parabix_CC_Compiler ccc(getEntryScope(), getInputStreamSet("basis"));
270    RE_Compiler re_compiler(getEntryScope(), ccc);
271    for (auto & e : mExternals) {
272        re_compiler.addPrecompiled(e, pb.createExtract(getInputStreamVar(e), pb.getInteger(0)));
273    }
274    for (auto a : mAlphabets) {
275        auto mpx_basis = getInputStreamSet(a->getName() + "_basis");
276        re_compiler.addAlphabet(a, mpx_basis);
277    }
278    PabloAST * const matches = re_compiler.compile(mRE);
279    Var * const output = getOutputStreamVar("matches");
280    pb.createAssign(pb.createExtract(output, pb.getInteger(0)), matches);
281}
282
283
284ByteGrepSignature::ByteGrepSignature(RE * re)
285: mRE(re)
286, mSignature(Printer_RE::PrintRE(re) ) {
287}
288
289ByteGrepKernel::ByteGrepKernel(const std::unique_ptr<kernel::KernelBuilder> & b, RE * const re, std::vector<std::string> externals)
290: ByteGrepSignature(re)
291, PabloKernel(b, "byteGrep" + sha1sum(mSignature),
292              // inputs
293{Binding{b->getStreamSetTy(1, 8), "byteData"}},
294              // output
295{Binding{b->getStreamSetTy(1, 1), "matches", FixedRate(), Add1()}})
296, mExternals(externals) {
297    for (auto & e : externals) {
298        mStreamSetInputs.push_back(Binding{b->getStreamSetTy(1, 1), e});
299    }
300}
301
302std::string ByteGrepKernel::makeSignature(const std::unique_ptr<kernel::KernelBuilder> &) {
303    return mSignature;
304}
305
306
307void ByteGrepKernel::generatePabloMethod() {
308    PabloBuilder pb(getEntryScope());
309    PabloAST * u8bytes = pb.createExtract(getInput(0), pb.getInteger(0));
310    cc::Direct_CC_Compiler dcc(getEntryScope(), u8bytes);
311    RE_Compiler re_byte_compiler(getEntryScope(), dcc);
312    for (auto & e : mExternals) {
313        re_byte_compiler.addPrecompiled(e, pb.createExtract(getInputStreamVar(e), pb.getInteger(0)));
314    }
315    PabloAST * const matches = re_byte_compiler.compile(mRE);
316   
317    Var * const output = getOutputStreamVar("matches");
318    pb.createAssign(pb.createExtract(output, pb.getInteger(0)), matches);
319}
320
321// Helper to compute stream set inputs to pass into PabloKernel constructor.
322inline std::vector<Binding> byteBitGrepInputs(const std::unique_ptr<kernel::KernelBuilder> & b,
323                                              const std::vector<std::string> & externals) {
324    std::vector<Binding> streamSetInputs = {
325        Binding{b->getStreamSetTy(1, 8), "bytedata"},
326    };
327    for (auto & e : externals) {
328        streamSetInputs.push_back(Binding{b->getStreamSetTy(1, 1), e});
329    }
330    return streamSetInputs;
331}
332
333ByteBitGrepSignature::ByteBitGrepSignature(RE * prefix, RE * suffix)
334: mPrefixRE(prefix)
335, mSuffixRE(suffix)
336, mSignature(Printer_RE::PrintRE(mPrefixRE) + Printer_RE::PrintRE(mSuffixRE) ) {
337}
338
339ByteBitGrepKernel::ByteBitGrepKernel(const std::unique_ptr<kernel::KernelBuilder> & b, RE * const prefixRE, RE * const suffixRE, std::vector<std::string> externals)
340: ByteBitGrepSignature(prefixRE, suffixRE)
341, PabloKernel(b, "bBc" + sha1sum(mSignature),
342              // inputs
343              byteBitGrepInputs(b, externals),
344              // output
345{Binding{b->getStreamSetTy(1, 1), "matches", FixedRate(), Add1()}})
346, mExternals(externals) {
347}
348
349std::string ByteBitGrepKernel::makeSignature(const std::unique_ptr<kernel::KernelBuilder> &) {
350    return mSignature;
351}
352
353
354void ByteBitGrepKernel::generatePabloMethod() {
355    PabloBuilder pb(getEntryScope());
356    PabloAST * u8bytes = pb.createExtract(getInput(0), pb.getInteger(0));
357    cc::Direct_CC_Compiler dcc(getEntryScope(), u8bytes);
358    RE_Compiler re_byte_compiler(getEntryScope(), dcc);
359    for (auto & e : mExternals) {
360        re_byte_compiler.addPrecompiled(e, pb.createExtract(getInputStreamVar(e), pb.getInteger(0)));
361    }
362    PabloAST * const prefixMatches = re_byte_compiler.compile(mPrefixRE);
363    Var * const final_matches = pb.createVar("final_matches", pb.createZeroes());
364    PabloBlock * scope1 = getEntryScope()->createScope();
365    pb.createIf(prefixMatches, scope1);
366   
367    PabloAST * nybbles[2];
368    nybbles[0] = scope1->createPackL(scope1->getInteger(8), u8bytes);
369    nybbles[1] = scope1->createPackH(scope1->getInteger(8), u8bytes);
370   
371    PabloAST * bitpairs[4];
372    for (unsigned i = 0; i < 2; i++) {
373        bitpairs[2*i] = scope1->createPackL(scope1->getInteger(4), nybbles[i]);
374        bitpairs[2*i + 1] = scope1->createPackH(scope1->getInteger(4), nybbles[i]);
375    }
376   
377    std::vector<PabloAST *> basis(8);
378    for (unsigned i = 0; i < 4; i++) {
379        // The subtraction 7-bit is because of the confusion between
380        // little-endian and big-endian bit numbering of bytes.
381        // We should fix this, switching to little-endian numbering throughout.
382        basis[7-2*i] = scope1->createPackL(scope1->getInteger(2), bitpairs[i]);
383        basis[7-(2*i + 1)] = scope1->createPackH(scope1->getInteger(2), bitpairs[i]);
384    }
385   
386    cc::Parabix_CC_Compiler ccc(scope1, basis);
387    RE_Compiler re_compiler(scope1, ccc);
388    scope1->createAssign(final_matches, re_compiler.compile(mSuffixRE, prefixMatches));
389    Var * const output = getOutputStreamVar("matches");
390    pb.createAssign(pb.createExtract(output, pb.getInteger(0)), final_matches);
391}
392
393
394void MatchedLinesKernel::generatePabloMethod() {
395    PabloBuilder pb(getEntryScope());
396    PabloAST * matchResults = pb.createExtract(getInputStreamVar("matchResults"), pb.getInteger(0));
397    PabloAST * lineBreaks = pb.createExtract(getInputStreamVar("lineBreaks"), pb.getInteger(0));
398    PabloAST * notLB = pb.createNot(lineBreaks);
399    PabloAST * match_follow = pb.createMatchStar(matchResults, notLB);
400    PabloAST * unterminatedLineAtEOF = pb.createAtEOF(pb.createAdvance(notLB, 1), "unterminatedLineAtEOF");
401    Var * const matchedLines = getOutputStreamVar("matchedLines");
402    pb.createAssign(pb.createExtract(matchedLines, pb.getInteger(0)), pb.createAnd(match_follow, pb.createOr(lineBreaks, unterminatedLineAtEOF)));
403}
404
405MatchedLinesKernel::MatchedLinesKernel (const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
406: PabloKernel(iBuilder, "MatchedLines",
407// inputs
408{Binding{iBuilder->getStreamSetTy(1), "matchResults"}
409,Binding{iBuilder->getStreamSetTy(1), "lineBreaks"}},
410// output
411{Binding{iBuilder->getStreamSetTy(1), "matchedLines", FixedRate(), Add1()}}) {
412
413}
414
415
416void InvertMatchesKernel::generateDoBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder) {
417    Value * input = iBuilder->loadInputStreamBlock("matchedLines", iBuilder->getInt32(0));
418    Value * lbs = iBuilder->loadInputStreamBlock("lineBreaks", iBuilder->getInt32(0));
419    Value * inverted = iBuilder->CreateXor(input, lbs);
420    iBuilder->storeOutputStreamBlock("nonMatches", iBuilder->getInt32(0), inverted);
421}
422
423InvertMatchesKernel::InvertMatchesKernel(const std::unique_ptr<kernel::KernelBuilder> & builder)
424: BlockOrientedKernel("Invert",
425// Inputs
426{Binding{builder->getStreamSetTy(1, 1), "matchedLines"}, Binding{builder->getStreamSetTy(1, 1), "lineBreaks"}},
427// Outputs
428{Binding{builder->getStreamSetTy(1, 1), "nonMatches"}},
429// Input/Output Scalars and internal state
430{}, {}, {}) {
431
432}
433
434
435void PopcountKernel::generatePabloMethod() {
436    auto pb = this->getEntryScope();
437    const auto toCount = pb->createExtract(getInputStreamVar("toCount"), pb->getInteger(0));
438    pablo::Var * countResult = getOutputScalarVar("countResult");
439   
440    pb->createAssign(countResult, pb->createCount(pb->createInFile(toCount)));
441}
442
443PopcountKernel::PopcountKernel (const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
444: PabloKernel(iBuilder, "Popcount",
445{Binding{iBuilder->getStreamSetTy(1), "toCount"}},
446{},
447{},
448{Binding{iBuilder->getSizeTy(), "countResult"}}) {
449
450}
451
452
453void AbortOnNull::generateMultiBlockLogic(const std::unique_ptr<KernelBuilder> & b, llvm::Value * const numOfStrides) {
454    Module * const m = b->getModule();
455    DataLayout DL(m);
456    IntegerType * const intPtrTy = DL.getIntPtrType(m->getContext());
457    Type * voidPtrTy = b->getVoidPtrTy();
458    const auto blocksPerStride = getStride() / b->getBitBlockWidth();
459    Constant * const BLOCKS_PER_STRIDE = b->getSize(blocksPerStride);
460    BasicBlock * const entry = b->GetInsertBlock();
461    BasicBlock * const strideLoop = b->CreateBasicBlock("strideLoop");
462    BasicBlock * const stridesDone = b->CreateBasicBlock("stridesDone");
463    BasicBlock * const nullByteDetection = b->CreateBasicBlock("nullByteDetection");
464    BasicBlock * const nullByteFound = b->CreateBasicBlock("nullByteFound");
465    BasicBlock * const finalStride = b->CreateBasicBlock("finalStride");
466    BasicBlock * const segmentDone = b->CreateBasicBlock("segmentDone");
467
468    Value * const numOfBlocks = b->CreateMul(numOfStrides, BLOCKS_PER_STRIDE);
469    Value * availItems = b->getAvailableItemCount("bytedata");
470    //
471    // Fast loop to prove that there are no null bytes in a multiblock region.
472    // We repeatedly combine byte packs using a SIMD unsigned min operation
473    // (implemented as a Select/ICmpULT combination).
474    //
475    Value * byteStreamBasePtr = b->getInputStreamBlockPtr("bytedata", b->getSize(0), b->getSize(0));
476    Value * outputStreamBasePtr = b->getOutputStreamBlockPtr("untilNull", b->getSize(0), b->getSize(0));
477
478    //
479    // We set up a a set of eight accumulators to accumulate the minimum byte
480    // values seen at each position in a block.   The initial min value at
481    // each position is 0xFF (all ones).
482    Value * blockMin[8];
483    for (unsigned i = 0; i < 8; i++) {
484        blockMin[i] = b->fwCast(8, b->allOnes());
485    }
486    // If we're in the final block bypass the fast loop.
487    b->CreateCondBr(mIsFinal, finalStride, strideLoop);
488   
489    b->SetInsertPoint(strideLoop);
490    PHINode * const baseBlockIndex = b->CreatePHI(b->getSizeTy(), 2);
491    baseBlockIndex->addIncoming(ConstantInt::get(baseBlockIndex->getType(), 0), entry);
492    PHINode * const blocksRemaining = b->CreatePHI(b->getSizeTy(), 2);
493    blocksRemaining->addIncoming(numOfBlocks, entry);
494    for (unsigned i = 0; i < 8; i++) {
495        Value * next = b->CreateBlockAlignedLoad(b->CreateGEP(byteStreamBasePtr, {baseBlockIndex, b->getSize(i)}));
496        b->CreateBlockAlignedStore(next, b->CreateGEP(outputStreamBasePtr, {baseBlockIndex, b->getSize(i)}));
497        next = b->fwCast(8, next);
498        blockMin[i] = b->CreateSelect(b->CreateICmpULT(next, blockMin[i]), next, blockMin[i]);
499    }
500    Value * nextBlockIndex = b->CreateAdd(baseBlockIndex, ConstantInt::get(baseBlockIndex->getType(), 1));
501    Value * nextRemaining = b->CreateSub(blocksRemaining, ConstantInt::get(blocksRemaining->getType(), 1));
502    baseBlockIndex->addIncoming(nextBlockIndex, strideLoop);
503    blocksRemaining->addIncoming(nextRemaining, strideLoop);
504    b->CreateCondBr(b->CreateICmpUGT(nextRemaining, ConstantInt::getNullValue(blocksRemaining->getType())), strideLoop, stridesDone);
505   
506    b->SetInsertPoint(stridesDone);
507    // Combine the 8 blockMin values.
508    for (unsigned i = 0; i < 4; i++) {
509        blockMin[i] = b->CreateSelect(b->CreateICmpULT(blockMin[i], blockMin[i+4]), blockMin[i], blockMin[i+4]);
510    }
511    for (unsigned i = 0; i < 2; i++) {
512        blockMin[i] = b->CreateSelect(b->CreateICmpULT(blockMin[i], blockMin[i+4]), blockMin[i], blockMin[i+2]);
513    }
514    blockMin[0] = b->CreateSelect(b->CreateICmpULT(blockMin[0], blockMin[1]), blockMin[0], blockMin[1]);
515    Value * anyNull = b->bitblock_any(b->simd_eq(8, blockMin[0], b->allZeroes()));
516   
517    b->CreateCondBr(anyNull, nullByteDetection, segmentDone);
518   
519   
520    b->SetInsertPoint(finalStride);
521    b->CreateMemCpy(b->CreatePointerCast(outputStreamBasePtr, voidPtrTy), b->CreatePointerCast(byteStreamBasePtr, voidPtrTy), availItems, 1);
522    b->CreateBr(nullByteDetection);
523   
524    b->SetInsertPoint(nullByteDetection);
525    //  Find the exact location using memchr, which should be fast enough.
526    //
527    Value * ptrToNull = b->CreateMemChr(b->CreatePointerCast(byteStreamBasePtr, voidPtrTy), b->getInt32(0), availItems);
528    Value * ptrAddr = b->CreatePtrToInt(ptrToNull, intPtrTy);
529    b->CreateCondBr(b->CreateICmpEQ(ptrAddr, ConstantInt::getNullValue(intPtrTy)), segmentDone, nullByteFound);
530   
531    // A null byte has been located; set the termination code and call the signal handler.
532    b->SetInsertPoint(nullByteFound);
533    Value * nullPosn = b->CreateSub(b->CreatePtrToInt(ptrToNull, intPtrTy), b->CreatePtrToInt(byteStreamBasePtr, intPtrTy));
534    b->setTerminationSignal();
535    Function * const dispatcher = m->getFunction("signal_dispatcher"); assert (dispatcher);
536    Value * handler = b->getScalarField("handler_address");
537    b->CreateCall(dispatcher, {handler, ConstantInt::get(b->getInt32Ty(), static_cast<unsigned>(grep::GrepSignal::BinaryFile))});
538    b->CreateBr(segmentDone);
539   
540    b->SetInsertPoint(segmentDone);
541    PHINode * const produced = b->CreatePHI(b->getSizeTy(), 3);
542    produced->addIncoming(nullPosn, nullByteFound);
543    produced->addIncoming(availItems, stridesDone);
544    produced->addIncoming(availItems, nullByteDetection);
545    Value * producedCount = b->getProducedItemCount("untilNull");
546    producedCount = b->CreateAdd(producedCount, produced);
547    b->setProducedItemCount("untilNull", producedCount);
548}
549
550AbortOnNull::AbortOnNull(const std::unique_ptr<kernel::KernelBuilder> & b)
551: MultiBlockKernel("AbortOnNull",
552                   // inputs
553{Binding{b->getStreamSetTy(1, 8), "bytedata"}},
554                   // outputs
555{Binding{b->getStreamSetTy(1, 8), "untilNull", FixedRate(), Deferred()}},
556                   // input scalars
557{Binding{b->getIntAddrTy(), "handler_address"}},
558{}, {}) {
559    addAttribute(CanTerminateEarly());
560}
Note: See TracBrowser for help on using the repository browser.