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

Last change on this file since 5454 was 5454, checked in by nmedfort, 2 years ago

Bug fix check in for DumpTrace?, compilation of DoBlock? / DoFinalBlock? functions. Pablo CodeMotionPass? optimized and enabled by default.

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