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

Last change on this file since 5540 was 5491, checked in by cameron, 2 years ago

Update popcount kernel to new Pablo interface

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