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

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

Restructuring work for the Driver classes. Start of work to eliminate the memory leaks with the ExecutionEngine?. Replaced custom AlignedMalloc? with backend call to std::aligned_malloc. Salvaged some work on DistributionPass? for reevaluation.

File size: 3.0 KB
RevLine 
[5404]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>
[5436]11#include <kernels/kernel_builder.h>
12#include <pablo/builder.hpp>
[5413]13#include <pablo/pe_count.h>
[5404]14
[5464]15#include <llvm/Support/raw_ostream.h>
16
[5404]17using namespace kernel;
18using namespace pablo;
19using namespace re;
20using namespace llvm;
21
[5408]22inline static std::string sha1sum(const std::string & str) {
[5404]23    char buffer[41];    // 40 hex-digits and the terminating null
[5408]24    uint32_t digest[5]; // 160 bits in total
[5404]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
[5454]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),
[5404]43              {Binding{iBuilder->getStreamSetTy(8), "basis"}, Binding{iBuilder->getStreamSetTy(1, 1), "linebreak"}},
[5454]44              {Binding{iBuilder->getStreamSetTy(1, 1), "matches"}}) {
45
[5404]46}
47
[5454]48std::string ICGrepKernel::makeSignature(const std::unique_ptr<kernel::KernelBuilder> &) {
[5404]49    return mSignature;
50}
51
[5454]52void ICGrepKernel::generatePabloMethod() {
53    re2pablo_compiler(this, mRE);
[5404]54}
[5413]55
[5440]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));
[5413]59    Value * inverted = iBuilder->CreateXor(input, lbs);
[5440]60    iBuilder->storeOutputStreamBlock("nonMatches", iBuilder->getInt32(0), inverted);
[5413]61}
62
[5436]63InvertMatchesKernel::InvertMatchesKernel(const std::unique_ptr<kernel::KernelBuilder> & builder)
[5435]64: BlockOrientedKernel("Invert", {Binding{builder->getStreamSetTy(1, 1), "matchedLines"}, Binding{builder->getStreamSetTy(1, 1), "lineBreaks"}}, {Binding{builder->getStreamSetTy(1, 1), "nonMatches"}}, {}, {}, {}) {
65    setNoTerminateAttribute(true);   
[5413]66}
67
68
[5436]69PopcountKernel::PopcountKernel (const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
[5413]70: PabloKernel(iBuilder, "Popcount",
71              {Binding{iBuilder->getStreamSetTy(1), "toCount"}},
72              {},
73              {},
74              {Binding{iBuilder->getSizeTy(), "countResult"}}) {
75   
76    auto pb = this->getEntryBlock();
77    const auto toCount = pb->createExtract(getInputStreamVar("toCount"), pb->getInteger(0));
78    pablo::Var * countResult = getOutputScalarVar("countResult");
79    pb->createAssign(countResult, pb->createCount(toCount));
80}
Note: See TracBrowser for help on using the repository browser.