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

Last change on this file since 5549 was 5548, checked in by cameron, 2 years ago

Decouple finalizeMatchResults, creating separate MatchedLinesKernel?

File size: 4.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 */
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/boolean.h>
14#include <pablo/pe_count.h>
15#include <pablo/pe_matchstar.h>
16
17#include <llvm/Support/raw_ostream.h>
18
19using namespace kernel;
20using namespace pablo;
21using namespace re;
22using namespace llvm;
23
24inline static std::string sha1sum(const std::string & str) {
25    char buffer[41];    // 40 hex-digits and the terminating null
26    uint32_t digest[5]; // 160 bits in total
27    boost::uuids::detail::sha1 sha1;
28    sha1.process_bytes(str.c_str(), str.size());
29    sha1.get_digest(digest);
30    snprintf(buffer, sizeof(buffer), "%.8x%.8x%.8x%.8x%.8x",
31             digest[0], digest[1], digest[2], digest[3], digest[4]);
32    return std::string(buffer);
33}
34
35RegularExpressionOptimizer::RegularExpressionOptimizer(re::RE * const re_ast)
36: mRE(regular_expression_passes(re_ast))
37, mSignature(Printer_RE::PrintRE(mRE)) {
38
39}
40
41ICGrepKernel::ICGrepKernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, RE * const re)
42: RegularExpressionOptimizer(re)
43, PabloKernel(iBuilder,
44              "ic" + sha1sum(mSignature),
45              {Binding{iBuilder->getStreamSetTy(8), "basis"}, Binding{iBuilder->getStreamSetTy(1, 1), "linebreak"}},
46              {Binding{iBuilder->getStreamSetTy(1, 1), "matches"}}) {
47
48}
49
50std::string ICGrepKernel::makeSignature(const std::unique_ptr<kernel::KernelBuilder> &) {
51    return mSignature;
52}
53
54void ICGrepKernel::generatePabloMethod() {
55    re2pablo_compiler(this, mRE);
56}
57
58void MatchedLinesKernel::generatePabloMethod() {
59    auto pb = this->getEntryBlock();
60    PabloAST * matchResults = pb->createExtract(getInputStreamVar("matchResults"), pb->getInteger(0));
61    PabloAST * lineBreaks = pb->createExtract(getInputStreamVar("lineBreaks"), pb->getInteger(0));
62    PabloAST * notLB = pb->createNot(lineBreaks);
63    PabloAST * match_follow = pb->createMatchStar(matchResults, notLB);
64    Var * const matchedLines = getOutputStreamVar("matchedLines");
65    pb->createAssign(pb->createExtract(matchedLines, pb->getInteger(0)), pb->createAnd(match_follow, lineBreaks));
66}
67
68MatchedLinesKernel::MatchedLinesKernel (const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
69: PabloKernel(iBuilder, "MatchedLines",
70              {Binding{iBuilder->getStreamSetTy(1), "matchResults"}, Binding{iBuilder->getStreamSetTy(1), "lineBreaks"}},
71              {Binding{iBuilder->getStreamSetTy(1), "matchedLines"}},
72              {},
73              {}) {
74}
75
76
77void InvertMatchesKernel::generateDoBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder) {
78    Value * input = iBuilder->loadInputStreamBlock("matchedLines", iBuilder->getInt32(0));
79    Value * lbs = iBuilder->loadInputStreamBlock("lineBreaks", iBuilder->getInt32(0));
80    Value * inverted = iBuilder->CreateXor(input, lbs);
81    iBuilder->storeOutputStreamBlock("nonMatches", iBuilder->getInt32(0), inverted);
82}
83
84InvertMatchesKernel::InvertMatchesKernel(const std::unique_ptr<kernel::KernelBuilder> & builder)
85: BlockOrientedKernel("Invert", {Binding{builder->getStreamSetTy(1, 1), "matchedLines"}, Binding{builder->getStreamSetTy(1, 1), "lineBreaks"}}, {Binding{builder->getStreamSetTy(1, 1), "nonMatches"}}, {}, {}, {}) {
86    setNoTerminateAttribute(true);   
87}
88
89
90void PopcountKernel::generatePabloMethod() {
91    auto pb = this->getEntryBlock();
92    const auto toCount = pb->createExtract(getInputStreamVar("toCount"), pb->getInteger(0));
93    pablo::Var * countResult = getOutputScalarVar("countResult");
94    pb->createAssign(countResult, pb->createCount(toCount));
95}
96
97PopcountKernel::PopcountKernel (const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
98: PabloKernel(iBuilder, "Popcount",
99              {Binding{iBuilder->getStreamSetTy(1), "toCount"}},
100              {},
101              {},
102              {Binding{iBuilder->getSizeTy(), "countResult"}}) {
103}
104
Note: See TracBrowser for help on using the repository browser.