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

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

partial refactoring check in with change for Linda.

File size: 2.8 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 <IR_Gen/idisa_builder.h>  // for IDISA_Builder
12#include <pablo/builder.hpp>  // for PabloBuilder
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 (IDISA::IDISA_Builder * const 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::prepareKernel() {
47    re2pablo_compiler(this, regular_expression_passes(mRE));
48    pablo_function_passes(this);
49    PabloKernel::prepareKernel();
50}
51
52void InvertMatchesKernel::generateDoBlockMethod() {
53    Value * input = loadInputStreamBlock("matchedLines", iBuilder->getInt32(0));
54    Value * lbs = loadInputStreamBlock("lineBreaks", iBuilder->getInt32(0));
55    Value * inverted = iBuilder->CreateXor(input, lbs);
56    storeOutputStreamBlock("nonMatches", iBuilder->getInt32(0), inverted);
57}
58
59InvertMatchesKernel::InvertMatchesKernel(IDISA::IDISA_Builder * builder)
60: BlockOrientedKernel(builder, "Invert", {Binding{builder->getStreamSetTy(1, 1), "matchedLines"}, Binding{builder->getStreamSetTy(1, 1), "lineBreaks"}}, {Binding{builder->getStreamSetTy(1, 1), "nonMatches"}}, {}, {}, {}) {
61    setNoTerminateAttribute(true);
62   
63}
64
65
66PopcountKernel::PopcountKernel (IDISA::IDISA_Builder * const iBuilder)
67: PabloKernel(iBuilder, "Popcount",
68              {Binding{iBuilder->getStreamSetTy(1), "toCount"}},
69              {},
70              {},
71              {Binding{iBuilder->getSizeTy(), "countResult"}}) {
72   
73    auto pb = this->getEntryBlock();
74    const auto toCount = pb->createExtract(getInputStreamVar("toCount"), pb->getInteger(0));
75    pablo::Var * countResult = getOutputScalarVar("countResult");
76    pb->createAssign(countResult, pb->createCount(toCount));
77}
Note: See TracBrowser for help on using the repository browser.