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

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

Continued refactoring work. PabloKernel? now abstract base type with a 'generatePabloMethod' hook to generate Pablo code.

File size: 2.7 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
31ICgrepKernelBuilder::ICgrepKernelBuilder (const std::unique_ptr<kernel::KernelBuilder> & iBuilder, RE * const re)
32: PabloKernel(iBuilder, "",
33              {Binding{iBuilder->getStreamSetTy(8), "basis"}, Binding{iBuilder->getStreamSetTy(1, 1), "linebreak"}},
34              {Binding{iBuilder->getStreamSetTy(1, 1), "matches"}},
35              {},
36              {})
37, mRE(re)
38, mSignature(Printer_RE::PrintRE(re)) {
39    setName(sha1sum(mSignature));
40}
41
42std::string ICgrepKernelBuilder::makeSignature() {
43    return mSignature;
44}
45
46void ICgrepKernelBuilder::generatePabloMethod() {
47    re2pablo_compiler(this, regular_expression_passes(mRE));
48}
49
50void InvertMatchesKernel::generateDoBlockMethod() {
51    Value * input = loadInputStreamBlock("matchedLines", iBuilder->getInt32(0));
52    Value * lbs = loadInputStreamBlock("lineBreaks", iBuilder->getInt32(0));
53    Value * inverted = iBuilder->CreateXor(input, lbs);
54    storeOutputStreamBlock("nonMatches", iBuilder->getInt32(0), inverted);
55}
56
57InvertMatchesKernel::InvertMatchesKernel(const std::unique_ptr<kernel::KernelBuilder> & builder)
58: BlockOrientedKernel("Invert", {Binding{builder->getStreamSetTy(1, 1), "matchedLines"}, Binding{builder->getStreamSetTy(1, 1), "lineBreaks"}}, {Binding{builder->getStreamSetTy(1, 1), "nonMatches"}}, {}, {}, {}) {
59    setNoTerminateAttribute(true);   
60}
61
62
63PopcountKernel::PopcountKernel (const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
64: PabloKernel(iBuilder, "Popcount",
65              {Binding{iBuilder->getStreamSetTy(1), "toCount"}},
66              {},
67              {},
68              {Binding{iBuilder->getSizeTy(), "countResult"}}) {
69   
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}
Note: See TracBrowser for help on using the repository browser.