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

Last change on this file since 5955 was 5955, checked in by xwa163, 17 months ago

Init commit for LZ4Grep

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