source: icGREP/icgrep-devel/icgrep/lz4/LZ4GrepGenerator.cpp @ 6020

Last change on this file since 6020 was 6020, checked in by xwa163, 12 months ago
  1. New version of lz4_swizzled_match_copy kernel with higher performance
  2. Adjust related pipeline code
  3. Remove legacy comments
File size: 23.3 KB
Line 
1
2#include "LZ4GrepGenerator.h"
3
4#include <boost/filesystem.hpp>
5#include <boost/iostreams/device/mapped_file.hpp>
6
7#include <llvm/Support/CommandLine.h>
8#include <llvm/Support/PrettyStackTrace.h>
9
10#include <cc/cc_compiler.h>
11
12#include <lz4FrameDecoder.h>
13#include <kernels/streamset.h>
14#include <kernels/cc_kernel.h>
15#include <kernels/s2p_kernel.h>
16#include <kernels/p2s_kernel.h>
17#include <kernels/source_kernel.h>
18#include <kernels/stdout_kernel.h>
19#include <kernels/lz4/lz4_generate_deposit_stream.h>
20#include <kernels/kernel_builder.h>
21#include <kernels/deletion.h>
22#include <kernels/swizzle.h>
23#include <kernels/pdep_kernel.h>
24#include <kernels/lz4/lz4_multiple_pdep_kernel.h>
25#include <kernels/lz4/lz4_match_copy_kernel.h>
26#include <kernels/lz4/lz4_swizzled_match_copy_kernel.h>
27#include <re/re_toolchain.h>
28
29#include <re/collect_ccs.h>
30#include <re/replaceCC.h>
31
32#include <set>
33#include "grep/grep_engine.h"
34#include "grep_interface.h"
35#include <llvm/IR/Module.h>
36#include <boost/filesystem.hpp>
37#include <UCD/resolve_properties.h>
38#include <kernels/charclasses.h>
39#include <kernels/cc_kernel.h>
40#include <kernels/grep_kernel.h>
41#include <kernels/UCD_property_kernel.h>
42#include <kernels/grapheme_kernel.h>
43#include <kernels/linebreak_kernel.h>
44#include <kernels/streams_merge.h>
45#include <kernels/source_kernel.h>
46#include <kernels/s2p_kernel.h>
47#include <kernels/scanmatchgen.h>
48#include <kernels/streamset.h>
49#include <kernels/until_n.h>
50#include <kernels/kernel_builder.h>
51#include <pablo/pablo_kernel.h>
52#include <re/re_cc.h>
53#include <re/re_name.h>
54#include <re/casing.h>
55#include <re/exclude_CC.h>
56#include <re/to_utf8.h>
57#include <re/re_toolchain.h>
58#include <toolchain/toolchain.h>
59#include <re/re_analysis.h>
60#include <re/re_name_resolve.h>
61#include <re/re_name_gather.h>
62#include <re/re_multiplex.h>
63#include <re/re_utility.h>
64#include <re/grapheme_clusters.h>
65#include <re/printer_re.h>
66#include <toolchain/toolchain.h>
67#include <toolchain/cpudriver.h>
68#include <iostream>
69#include <cc/multiplex_CCs.h>
70#include <llvm/Support/raw_ostream.h>
71#include <util/aligned_allocator.h>
72#include <sys/stat.h>
73#include <fcntl.h>
74#include <errno.h>
75#include <llvm/ADT/STLExtras.h> // for make_unique
76#include <llvm/Support/CommandLine.h>
77#include <llvm/Support/Debug.h>
78#include <sched.h>
79#include <cstdio>
80#include <cc/multiplex_CCs.h>
81
82
83namespace re { class CC; }
84
85using namespace llvm;
86using namespace parabix;
87using namespace kernel;
88using namespace grep;
89
90
91const unsigned ByteCClimit = 6;
92
93
94LZ4GrepGenerator::LZ4GrepGenerator(): LZ4Generator() {
95    mGrepRecordBreak = grep::GrepRecordBreakKind::LF;
96    mMoveMatchesToEOL = true;
97}
98
99void LZ4GrepGenerator::initREs(std::vector<re::RE *> & REs) {
100    if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
101        mBreakCC = re::makeCC(re::makeCC(0x0A, 0x0D), re::makeCC(re::makeCC(0x85), re::makeCC(0x2028, 0x2029)));
102    } else if (mGrepRecordBreak == GrepRecordBreakKind::Null) {
103        mBreakCC = re::makeByte(0);  // Null
104    } else {
105        mBreakCC = re::makeByte(0x0A); // LF
106    }
107    re::RE * anchorRE = mBreakCC;
108    if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
109        re::Name * anchorName = re::makeName("UTF8_LB", re::Name::Type::Unicode);
110        anchorName->setDefinition(re::makeUnicodeBreak());
111        anchorRE = anchorName;
112    }
113
114    mREs = REs;
115    bool allAnchored = true;
116    for(unsigned i = 0; i < mREs.size(); ++i) {
117        if (!hasEndAnchor(mREs[i])) allAnchored = false;
118        mREs[i] = resolveModesAndExternalSymbols(mREs[i]);
119        mREs[i] = re::exclude_CC(mREs[i], mBreakCC);
120        mREs[i] = resolveAnchors(mREs[i], anchorRE);
121        re::gatherUnicodeProperties(mREs[i], mUnicodeProperties);
122        mREs[i] = regular_expression_passes(mREs[i]);
123    }
124    if (allAnchored && (mGrepRecordBreak != GrepRecordBreakKind::Unicode)) mMoveMatchesToEOL = false;
125
126}
127
128
129
130std::pair<parabix::StreamSetBuffer *, parabix::StreamSetBuffer *> LZ4GrepGenerator::grepPipeline(
131        std::vector<re::RE *> &REs, parabix::StreamSetBuffer *BasisBits) {
132
133    this->initREs(REs);
134    auto mGrepDriver = &pxDriver;
135
136
137    auto & idb = mGrepDriver->getBuilder();
138    // TODO: until we automate stream buffer sizing, use this calculation to determine how large our matches buffer needs to be.
139    const unsigned baseBufferSize = this->getInputBufferBlocks();
140    bool MultithreadedSimpleRE = false;
141    bool PropertyKernels = false;
142    bool CC_Multiplexing = false;
143    bool InvertMatchFlag = false;
144    int MaxCountFlag = 0;
145
146
147
148
149    //  Regular Expression Processing and Analysis Phase
150    const auto nREs = mREs.size();
151    bool hasGCB[nREs];
152    bool anyGCB = false;
153
154    for(unsigned i = 0; i < nREs; ++i) {
155        hasGCB[i] = hasGraphemeClusterBoundary(mREs[i]);
156        anyGCB |= hasGCB[i];
157    }
158    StreamSetBuffer * LineBreakStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
159    std::vector<StreamSetBuffer *> MatchResultsBufs(nREs);
160
161    re::RE * prefixRE;
162    re::RE * suffixRE;
163    // For simple regular expressions with a small number of characters, we
164    // can bypass transposition and use the Direct CC compiler.
165//    bool isSimple = (nREs == 1) && (mGrepRecordBreak != GrepRecordBreakKind::Unicode) && (!anyGCB);
166    bool isSimple = false;
167    if (isSimple) {
168        mREs[0] = toUTF8(mREs[0]);
169    }
170    if (isSimple && byteTestsWithinLimit(mREs[0], ByteCClimit)) {
171        std::vector<std::string> externalStreamNames;
172        std::vector<StreamSetBuffer *> icgrepInputSets = {ByteStream};
173        if (MultithreadedSimpleRE && hasTriCCwithinLimit(mREs[0], ByteCClimit, prefixRE, suffixRE)) {
174            auto CCs = re::collectCCs(prefixRE, &cc::Byte);
175            for (auto cc : CCs) {
176                auto ccName = makeName(cc);
177                mREs[0] = re::replaceCC(mREs[0], cc, ccName);
178                std::string ccNameStr = ccName->getFullName();
179                StreamSetBuffer * ccStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
180                kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, ccNameStr, std::vector<re::CC *>{cc});
181                mGrepDriver->makeKernelCall(ccK, {ByteStream}, {ccStream});
182                externalStreamNames.push_back(ccNameStr);
183                icgrepInputSets.push_back(ccStream);
184            }
185        }
186        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
187        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ByteGrepKernel>(idb, mREs[0], externalStreamNames);
188        mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
189        MatchResultsBufs[0] = MatchResults;
190        kernel::Kernel * breakK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, "breakCC", std::vector<re::CC *>{mBreakCC});
191        mGrepDriver->makeKernelCall(breakK, {ByteStream}, {LineBreakStream});
192    } else if (isSimple && hasTriCCwithinLimit(mREs[0], ByteCClimit, prefixRE, suffixRE)) {
193        std::vector<std::string> externalStreamNames;
194        std::vector<StreamSetBuffer *> icgrepInputSets = {ByteStream};
195        if (MultithreadedSimpleRE) {
196            auto CCs = re::collectCCs(prefixRE, &cc::Byte);
197            for (auto cc : CCs) {
198                auto ccName = makeName(cc);
199                mREs[0] = re::replaceCC(mREs[0], cc, ccName);
200                std::string ccNameStr = ccName->getFullName();
201                StreamSetBuffer * ccStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
202                kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, ccNameStr, std::vector<re::CC *>{cc});
203                mGrepDriver->makeKernelCall(ccK, {ByteStream}, {ccStream});
204                externalStreamNames.push_back(ccNameStr);
205                icgrepInputSets.push_back(ccStream);
206            }
207        }
208        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
209        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ByteBitGrepKernel>(idb, prefixRE, suffixRE, externalStreamNames);
210        mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
211        MatchResultsBufs[0] = MatchResults;
212        kernel::Kernel * breakK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, "breakCC", std::vector<re::CC *>{mBreakCC});
213        mGrepDriver->makeKernelCall(breakK, {ByteStream}, {LineBreakStream});
214    } else {
215        StreamSetBuffer * RequiredStreams = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
216        StreamSetBuffer * UnicodeLB = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
217
218        StreamSetBuffer * LineFeedStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
219        kernel::Kernel * linefeedK = mGrepDriver->addKernelInstance<kernel::LineFeedKernelBuilder>(idb, Binding{idb->getStreamSetTy(8), "basis", FixedRate(), Principal()});
220        mGrepDriver->makeKernelCall(linefeedK, {BasisBits}, {LineFeedStream});
221
222        kernel::Kernel * requiredStreamsK = mGrepDriver->addKernelInstance<kernel::RequiredStreams_UTF8>(idb);
223        mGrepDriver->makeKernelCall(requiredStreamsK, {BasisBits, LineFeedStream}, {RequiredStreams, UnicodeLB});
224
225        if (mGrepRecordBreak == GrepRecordBreakKind::LF) {
226            LineBreakStream = LineFeedStream;
227        } else if (mGrepRecordBreak == GrepRecordBreakKind::Null) {
228            kernel::Kernel * breakK = mGrepDriver->addKernelInstance<kernel::ParabixCharacterClassKernelBuilder>(idb, "Null", std::vector<re::CC *>{mBreakCC}, 8);
229            mGrepDriver->makeKernelCall(breakK, {BasisBits}, {LineBreakStream});
230        } else {
231            LineBreakStream = UnicodeLB;
232        }
233
234        std::map<std::string, StreamSetBuffer *> propertyStream;
235        if (PropertyKernels) {
236            for (auto p : mUnicodeProperties) {
237                auto name = p->getFullName();
238                StreamSetBuffer * s = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
239                propertyStream.emplace(std::make_pair(name, s));
240                kernel::Kernel * propertyK = mGrepDriver->addKernelInstance<kernel::UnicodePropertyKernelBuilder>(idb, p);
241                mGrepDriver->makeKernelCall(propertyK, {BasisBits}, {s});
242            }
243        }
244        StreamSetBuffer * GCB_stream = nullptr;
245        if (anyGCB) {
246            GCB_stream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
247            kernel::Kernel * gcbK = mGrepDriver->addKernelInstance<kernel::GraphemeClusterBreakKernel>(idb);
248            mGrepDriver->makeKernelCall(gcbK, {BasisBits, RequiredStreams}, {GCB_stream});
249        }
250
251        for(unsigned i = 0; i < nREs; ++i) {
252            std::vector<std::string> externalStreamNames;
253            std::vector<StreamSetBuffer *> icgrepInputSets = {BasisBits};
254            if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
255                externalStreamNames.push_back("UTF8_LB");
256                icgrepInputSets.push_back(LineBreakStream);
257                externalStreamNames.push_back("UTF8_nonfinal");
258                icgrepInputSets.push_back(RequiredStreams);
259            }
260            std::set<re::Name *> UnicodeProperties;
261            if (PropertyKernels) {
262                re::gatherUnicodeProperties(mREs[i], UnicodeProperties);
263                for (auto p : UnicodeProperties) {
264                    auto name = p->getFullName();
265                    auto f = propertyStream.find(name);
266                    if (f == propertyStream.end()) report_fatal_error(name + " not found\n");
267                    externalStreamNames.push_back(name);
268                    icgrepInputSets.push_back(f->second);
269                }
270            }
271            if (hasGCB[i]) {
272                externalStreamNames.push_back("\\b{g}");
273                icgrepInputSets.push_back(GCB_stream);
274            }
275            if (CC_Multiplexing) {
276                const auto UnicodeSets = re::collectCCs(mREs[i], &cc::Unicode, std::set<re::Name *>({re::makeZeroWidth("\\b{g}")}));
277                StreamSetBuffer * const MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
278                if (UnicodeSets.size() <= 1) {
279                    kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[i], externalStreamNames);
280                    mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
281                    MatchResultsBufs[i] = MatchResults;
282                } else {
283                    mpx = make_unique<cc::MultiplexedAlphabet>("mpx", UnicodeSets);
284                    mREs[i] = transformCCs(mpx.get(), mREs[i]);
285                    std::vector<re::CC *> mpx_basis = mpx->getMultiplexedCCs();
286                    auto numOfCharacterClasses = mpx_basis.size();
287                    StreamSetBuffer * CharClasses = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(numOfCharacterClasses), baseBufferSize);
288                    kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(mpx_basis));
289                    mGrepDriver->makeKernelCall(ccK, {BasisBits}, {CharClasses});
290                    //                kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(mpx_basis), true);
291                    //                mGrepDriver->makeKernelCall(ccK, {ByteStream}, {CharClasses});
292                    kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[i], externalStreamNames, std::vector<cc::Alphabet *>{mpx.get()});
293                    icgrepInputSets.push_back(CharClasses);
294                    mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
295                    MatchResultsBufs[i] = MatchResults;
296                }
297            } else {
298                StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
299                kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[i], externalStreamNames);
300                mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
301                MatchResultsBufs[i] = MatchResults;
302            }
303        }
304    }
305
306    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
307    if (mREs.size() > 1) {
308        MergedResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
309        kernel::Kernel * streamsMergeK = mGrepDriver->addKernelInstance<kernel::StreamsMerge>(idb, 1, mREs.size());
310        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
311    }
312    StreamSetBuffer * Matches = MergedResults;
313    if (mMoveMatchesToEOL) {
314        StreamSetBuffer * OriginalMatches = Matches;
315        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance<kernel::MatchedLinesKernel>(idb);
316        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
317        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
318    }
319    if (InvertMatchFlag) {
320        kernel::Kernel * invertK = mGrepDriver->addKernelInstance<kernel::InvertMatchesKernel>(idb);
321        StreamSetBuffer * OriginalMatches = Matches;
322        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
323        mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {Matches});
324    }
325    if (MaxCountFlag > 0) {
326        kernel::Kernel * untilK = mGrepDriver->addKernelInstance<kernel::UntilNkernel>(idb);
327        untilK->setInitialArguments({idb->getSize(MaxCountFlag)});
328        StreamSetBuffer * const AllMatches = Matches;
329        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
330        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
331    }
332
333    return std::pair<StreamSetBuffer *, StreamSetBuffer *>(LineBreakStream, Matches);
334
335}
336
337
338
339
340void LZ4GrepGenerator::invokeScanMatchGrep(char* fileBuffer, size_t blockStart, size_t blockEnd, bool hasBlockChecksum) {
341    auto main = this->getScanMatchGrepMainFunction();
342    std::ostringstream s;
343    EmitMatch accum("", false, false, s);
344
345    main(fileBuffer, blockStart, blockEnd, hasBlockChecksum, reinterpret_cast<intptr_t>(&accum));
346    llvm::outs() << s.str();
347}
348
349void LZ4GrepGenerator::generateScanMatchGrepPipeline(re::RE* regex) {
350    auto & iBuilder = pxDriver.getBuilder();
351    this->generateScanMatchMainFunc(iBuilder);
352
353    StreamSetBuffer * const DecompressedByteStream = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), this->getDecompressedBufferBlocks());
354
355    // GeneratePipeline
356    this->generateLoadByteStreamAndBitStream(iBuilder);
357    this->generateExtractAndDepositMarkers(iBuilder);
358
359    auto swizzle = this->generateSwizzleExtractData(iBuilder);
360
361    //TODO buffer blocks should be decompressedBufferBlocks
362    StreamSetBuffer * depositedSwizzle0 = pxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
363    StreamSetBuffer * depositedSwizzle1 = pxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
364
365    Kernel * multiplePdepK = pxDriver.addKernelInstance<LZ4MultiplePDEPkernel>(iBuilder, 4, 2, 4);
366    pxDriver.makeKernelCall(multiplePdepK, {DepositMarker, swizzle.first, swizzle.second}, {depositedSwizzle0, depositedSwizzle1});
367
368    StreamSetBuffer * matchCopiedSwizzle0 = pxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
369    StreamSetBuffer * matchCopiedSwizzle1 = pxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
370
371    Kernel * swizzledMatchCopyK = pxDriver.addKernelInstance<LZ4SwizzledMatchCopyKernel>(iBuilder, 4, 2, 4);
372    pxDriver.makeKernelCall(swizzledMatchCopyK, {MatchOffsetMarker, M0Marker, ByteStream, depositedSwizzle0, depositedSwizzle1}, {matchCopiedSwizzle0, matchCopiedSwizzle1});
373
374
375    // Produce unswizzled bit streams
376    StreamSetBuffer * extractedbits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), this->getInputBufferBlocks());
377    Kernel * unSwizzleK = pxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
378    pxDriver.makeKernelCall(unSwizzleK, {matchCopiedSwizzle0, matchCopiedSwizzle1}, {extractedbits});
379
380
381
382    Kernel * p2sK = pxDriver.addKernelInstance<P2SKernel>(iBuilder);
383    pxDriver.makeKernelCall(p2sK, {extractedbits}, {DecompressedByteStream});
384
385    StreamSetBuffer * LineBreakStream;
386    StreamSetBuffer * Matches;
387    std::vector<re::RE*> res = {regex};
388    std::tie(LineBreakStream, Matches) = grepPipeline(res, extractedbits);
389
390    kernel::Kernel * scanMatchK = pxDriver.addKernelInstance<kernel::ScanMatchKernel>(iBuilder);
391    scanMatchK->setInitialArguments({match_accumulator});
392    pxDriver.makeKernelCall(scanMatchK, {Matches, LineBreakStream, DecompressedByteStream}, {});
393    pxDriver.LinkFunction(*scanMatchK, "accumulate_match_wrapper", &accumulate_match_wrapper);
394    pxDriver.LinkFunction(*scanMatchK, "finalize_match_wrapper", &finalize_match_wrapper);
395
396    pxDriver.generatePipelineIR();
397    pxDriver.deallocateBuffers();
398
399    iBuilder->CreateRetVoid();
400
401    pxDriver.finalizeObject();
402}
403
404void LZ4GrepGenerator::generateCountOnlyGrepPipeline(re::RE* regex) {
405    auto & iBuilder = pxDriver.getBuilder();
406    this->generateMainFunc(iBuilder);
407
408
409    // GeneratePipeline
410    this->generateLoadByteStreamAndBitStream(iBuilder);
411    this->generateExtractAndDepositMarkers(iBuilder);
412
413    auto swizzle = this->generateSwizzleExtractData(iBuilder);
414
415    StreamSetBuffer * depositedSwizzle0 = pxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
416    StreamSetBuffer * depositedSwizzle1 = pxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
417
418    Kernel * multiplePdepK = pxDriver.addKernelInstance<LZ4MultiplePDEPkernel>(iBuilder, 4, 2, 4);
419    pxDriver.makeKernelCall(multiplePdepK, {DepositMarker, swizzle.first, swizzle.second}, {depositedSwizzle0, depositedSwizzle1});
420
421
422    StreamSetBuffer * matchCopiedSwizzle0 = pxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
423    StreamSetBuffer * matchCopiedSwizzle1 = pxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
424
425    Kernel * swizzledMatchCopyK = pxDriver.addKernelInstance<LZ4SwizzledMatchCopyKernel>(iBuilder, 4, 2, 4);
426    pxDriver.makeKernelCall(swizzledMatchCopyK, {MatchOffsetMarker, M0Marker, ByteStream, depositedSwizzle0, depositedSwizzle1}, {matchCopiedSwizzle0, matchCopiedSwizzle1});
427
428    // Produce unswizzled bit streams
429    StreamSetBuffer * extractedbits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), this->getInputBufferBlocks());
430    Kernel * unSwizzleK = pxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
431    pxDriver.makeKernelCall(unSwizzleK, {matchCopiedSwizzle0, matchCopiedSwizzle1}, {extractedbits});
432
433    StreamSetBuffer * LineBreakStream;
434    StreamSetBuffer * Matches;
435    std::vector<re::RE*> res = {regex};
436    std::tie(LineBreakStream, Matches) = grepPipeline(res, extractedbits);
437
438
439    kernel::Kernel * matchCountK = pxDriver.addKernelInstance<kernel::PopcountKernel>(iBuilder);
440    pxDriver.makeKernelCall(matchCountK, {Matches}, {});
441    pxDriver.generatePipelineIR();
442
443
444    iBuilder->setKernel(matchCountK);
445    Value * matchedLineCount = iBuilder->getAccumulator("countResult");
446    matchedLineCount = iBuilder->CreateZExt(matchedLineCount, iBuilder->getInt64Ty());
447    iBuilder->CallPrintInt("aaa", matchedLineCount);
448
449    pxDriver.deallocateBuffers();
450
451    // TODO return matchedLineCount
452//        idb->CreateRet(matchedLineCount);
453
454
455    iBuilder->CreateRetVoid();
456
457    pxDriver.finalizeObject();
458}
459
460ScanMatchGrepMainFunctionType LZ4GrepGenerator::getScanMatchGrepMainFunction() {
461    return reinterpret_cast<ScanMatchGrepMainFunctionType>(pxDriver.getMain());
462}
463
464void LZ4GrepGenerator::generateScanMatchMainFunc(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
465    Module * M = iBuilder->getModule();
466    Type * const sizeTy = iBuilder->getSizeTy();
467    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
468    Type * const voidTy = iBuilder->getVoidTy();
469    Type * const inputType = iBuilder->getInt8PtrTy();
470    Type * const intAddrTy = iBuilder->getIntAddrTy();
471
472    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, intAddrTy, nullptr));
473    main->setCallingConv(CallingConv::C);
474    Function::arg_iterator args = main->arg_begin();
475    inputStream = &*(args++);
476    inputStream->setName("input");
477
478    headerSize = &*(args++);
479    headerSize->setName("headerSize");
480
481    fileSize = &*(args++);
482    fileSize->setName("fileSize");
483
484    hasBlockChecksum = &*(args++);
485    hasBlockChecksum->setName("hasBlockChecksum");
486
487    match_accumulator = &*(args++);
488    match_accumulator->setName("match_accumulator");
489
490    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
491}
Note: See TracBrowser for help on using the repository browser.