source: icGREP/icgrep-devel/icgrep/kernels/charclasses.cpp @ 5748

Last change on this file since 5748 was 5748, checked in by nmedfort, 19 months ago

Bug fix for segment pipeline parallel mode + memory management improvements.

File size: 3.3 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 "charclasses.h"
7#include <re/re_toolchain.h>
8#include <kernels/kernel_builder.h>
9#include <UCD/ucd_compiler.hpp>
10#include <cc/cc_compiler.h>
11#include <re/re_name.h>
12#include <boost/uuid/sha1.hpp>
13
14using NameMap = UCD::UCDCompiler::NameMap;
15
16using namespace cc;
17using namespace kernel;
18using namespace pablo;
19using namespace re;
20using namespace llvm;
21using namespace UCD;
22
23inline static std::string sha1sum(const std::string & str) {
24    char buffer[41];    // 40 hex-digits and the terminating null
25    uint32_t digest[5]; // 160 bits in total
26    boost::uuids::detail::sha1 sha1;
27    sha1.process_bytes(str.c_str(), str.size());
28    sha1.get_digest(digest);
29    snprintf(buffer, sizeof(buffer), "%.8x%.8x%.8x%.8x%.8x",
30             digest[0], digest[1], digest[2], digest[3], digest[4]);
31    return std::string(buffer);
32}
33
34inline std::string signature(const std::vector<re::CC *> & ccs) {
35    if (LLVM_UNLIKELY(ccs.empty())) {
36        return "[]";
37    } else {
38        std::string tmp;
39        raw_string_ostream out(tmp);
40        char joiner = '[';
41        for (const auto & set : ccs) {
42            out << joiner;
43            set->print(out);
44            joiner = ',';
45        }
46        out << ']';
47        return out.str();
48    }
49}
50
51CharClassesSignature::CharClassesSignature(const std::vector<CC *> &ccs)
52: mSignature(signature(ccs)) {
53
54}
55
56
57CharClassesKernel::CharClassesKernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, std::vector<CC *> && ccs)
58: CharClassesSignature(ccs)
59, PabloKernel(iBuilder,
60              "cc" + sha1sum(mSignature),
61              {Binding{iBuilder->getStreamSetTy(8), "basis"}},
62              {Binding{iBuilder->getStreamSetTy(ccs.size(), 1), "charclasses"}})
63, mCCs(std::move(ccs)) {
64
65}
66
67std::string CharClassesKernel::makeSignature(const std::unique_ptr<kernel::KernelBuilder> &) {
68    return mSignature;
69}
70
71void CharClassesKernel::generatePabloMethod() {
72    CC_Compiler ccc(this, getInput(0));
73    auto & pb = ccc.getBuilder();
74    unsigned n = mCCs.size();
75
76    NameMap nameMap;
77    std::vector<Name *> names;
78    for (unsigned i = 0; i < n; i++) {
79        Name * name = re::makeName("cc" + std::to_string(i), mCCs[i]);
80        nameMap.emplace(name, nullptr);
81        names.push_back(name);
82    }
83
84    UCD::UCDCompiler ucdCompiler(ccc);
85    if (LLVM_UNLIKELY(AlgorithmOptionIsSet(DisableIfHierarchy))) {
86        ucdCompiler.generateWithoutIfHierarchy(nameMap, pb);
87    } else {
88        ucdCompiler.generateWithDefaultIfHierarchy(nameMap, pb);
89    }
90
91    // The first UnicodeSet in the vector ccs represents the last bit of the character class basis bit streams.
92    std::reverse(names.begin(), names.end());
93    for (unsigned i = 0; i < names.size(); i++) {
94        auto t = nameMap.find(names[i]); 
95        if (t != nameMap.end()) {
96            PabloAST * const r = pb.createExtract(getOutput(0), pb.getInteger(i));
97            if (t->first->getType() == Name::Type::Byte) {
98                pb.createAssign(r, ccc.compileCC(dyn_cast<CC>(t->first->getDefinition())));
99            } else {
100                pb.createAssign(r, t->second);
101            }
102        } else {
103            throw std::runtime_error("Can't compile character classes.");
104        }
105    }
106}
107
108
Note: See TracBrowser for help on using the repository browser.