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

Last change on this file since 5425 was 5413, checked in by cameron, 3 years ago

Factor out CountOnly? and InvertMatches? as separate kernels

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 <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
31inline std::string makeSignature(RE * const re_ast) {
32    std::string signature = Printer_RE::PrintRE(re_ast);
33    return signature;
34}
35
36ICgrepKernelBuilder::ICgrepKernelBuilder (IDISA::IDISA_Builder * const iBuilder, RE * const re_ast)
37: PabloKernel(iBuilder, "",
38              {Binding{iBuilder->getStreamSetTy(8), "basis"}, Binding{iBuilder->getStreamSetTy(1, 1), "linebreak"}},
39              {Binding{iBuilder->getStreamSetTy(1, 1), "matches"}},
40              {},
41              {})
42, mRE(re_ast)
43, mSignature(makeSignature(re_ast)) {
44    setName("Parabix:" + sha1sum(mSignature));
45}
46
47std::string ICgrepKernelBuilder::generateKernelSignature(std::string moduleId) {
48    return mSignature;
49}
50
51void ICgrepKernelBuilder::prepareKernel() {
52    re2pablo_compiler(this, regular_expression_passes(mRE));
53    pablo_function_passes(this);
54    PabloKernel::prepareKernel();
55}
56
57void InvertMatchesKernel::generateDoBlockMethod() {
58    Value * input = loadInputStreamBlock("matchedLines", iBuilder->getInt32(0));
59    Value * lbs = loadInputStreamBlock("lineBreaks", iBuilder->getInt32(0));
60    Value * inverted = iBuilder->CreateXor(input, lbs);
61    storeOutputStreamBlock("nonMatches", iBuilder->getInt32(0), inverted);
62}
63
64InvertMatchesKernel::InvertMatchesKernel(IDISA::IDISA_Builder * builder)
65: BlockOrientedKernel(builder, "Invert", {Binding{builder->getStreamSetTy(1, 1), "matchedLines"}, Binding{builder->getStreamSetTy(1, 1), "lineBreaks"}}, {Binding{builder->getStreamSetTy(1, 1), "nonMatches"}}, {}, {}, {}) {
66    setNoTerminateAttribute(true);
67   
68}
69
70
71PopcountKernel::PopcountKernel (IDISA::IDISA_Builder * const iBuilder)
72: PabloKernel(iBuilder, "Popcount",
73              {Binding{iBuilder->getStreamSetTy(1), "toCount"}},
74              {},
75              {},
76              {Binding{iBuilder->getSizeTy(), "countResult"}}) {
77   
78    auto pb = this->getEntryBlock();
79    const auto toCount = pb->createExtract(getInputStreamVar("toCount"), pb->getInteger(0));
80    pablo::Var * countResult = getOutputScalarVar("countResult");
81    pb->createAssign(countResult, pb->createCount(toCount));
82}
Note: See TracBrowser for help on using the repository browser.