source: icGREP/icgrep-devel/icgrep/lz4/grep/lz4_grep_base_generator.cpp @ 6144

Last change on this file since 6144 was 6144, checked in by xwa163, 6 weeks ago

lz4_grep: Init checkin for utf8 character class multiplexing

File size: 21.6 KB
Line 
1
2#include "lz4_grep_base_generator.h"
3
4#include <boost/iostreams/device/mapped_file.hpp>
5
6#include <llvm/Support/PrettyStackTrace.h>
7
8#include <cc/cc_compiler.h>
9
10#include <kernels/cc_kernel.h>
11#include <kernels/s2p_kernel.h>
12#include <kernels/p2s_kernel.h>
13#include <kernels/source_kernel.h>
14#include <kernels/stdout_kernel.h>
15#include <kernels/fake_stream_generating_kernel.h>
16#include <re/re_toolchain.h>
17
18#include <re/collect_ccs.h>
19#include <re/replaceCC.h>
20
21#include <re/casing.h>
22#include <re/exclude_CC.h>
23#include <re/to_utf8.h>
24#include <re/re_analysis.h>
25#include <re/re_name_resolve.h>
26#include <re/re_name_gather.h>
27#include <re/re_multiplex.h>
28#include <re/re_utility.h>
29
30#include <UCD/resolve_properties.h>
31#include <kernels/charclasses.h>
32#include <kernels/grep_kernel.h>
33#include <kernels/UCD_property_kernel.h>
34#include <kernels/grapheme_kernel.h>
35#include <kernels/linebreak_kernel.h>
36#include <kernels/streams_merge.h>
37#include <kernels/scanmatchgen.h>
38#include <kernels/until_n.h>
39#include <re/grapheme_clusters.h>
40#include <re/printer_re.h>
41#include <llvm/Support/raw_ostream.h>
42#include <llvm/Support/Debug.h>
43#include <kernels/lz4/lz4_block_decoder.h>
44
45#include <re/re_seq.h>
46#include <kernels/kernel_builder.h>
47#include <re/re_alt.h>
48
49namespace re { class CC; }
50
51using namespace llvm;
52using namespace parabix;
53using namespace kernel;
54using namespace grep;
55using namespace re;
56
57LZ4GrepBaseGenerator::LZ4GrepBaseGenerator()
58        : LZ4BaseGenerator(),
59          u8NonFinalRe(makeAlt({makeByte(0xC2, 0xF4),
60                                makeSeq({makeByte(0xE0, 0xF4), makeByte(0x80, 0xBF)}),
61                                makeSeq({makeByte(0xF0, 0xF4), makeByte(0x80, 0xBF), makeByte(0x80, 0xBF)})}))
62{
63    mGrepRecordBreak = grep::GrepRecordBreakKind::LF;
64    mMoveMatchesToEOL = true;
65}
66
67
68
69void LZ4GrepBaseGenerator::generateScanMatchGrepPipeline(re::RE* regex) {
70    auto & iBuilder = mPxDriver.getBuilder();
71    this->generateScanMatchMainFunc(iBuilder);
72
73    StreamSetBuffer* compressedByteStream = this->loadByteStream();
74
75    StreamSetBuffer * const uncompressedByteStream = this->byteStreamDecompression(compressedByteStream);
76    StreamSetBuffer * uncompressedBitStream = this->s2p(uncompressedByteStream);
77
78    StreamSetBuffer * LineBreakStream;
79    StreamSetBuffer * Matches;
80    std::tie(LineBreakStream, Matches) = grep(regex, uncompressedBitStream);
81
82    kernel::Kernel * scanMatchK = mPxDriver.addKernelInstance<kernel::ScanMatchKernel>(iBuilder);
83    scanMatchK->setInitialArguments({match_accumulator});
84    mPxDriver.makeKernelCall(scanMatchK, {Matches, LineBreakStream, uncompressedByteStream}, {});
85    mPxDriver.LinkFunction(*scanMatchK, "accumulate_match_wrapper", &accumulate_match_wrapper);
86    mPxDriver.LinkFunction(*scanMatchK, "finalize_match_wrapper", &finalize_match_wrapper);
87
88    mPxDriver.generatePipelineIR();
89    mPxDriver.deallocateBuffers();
90
91    iBuilder->CreateRetVoid();
92
93    mPxDriver.finalizeObject();
94}
95
96
97void LZ4GrepBaseGenerator::generateCountOnlyGrepPipeline(re::RE* regex, bool enableMultiplexing, bool utf8CC){
98    if (enableMultiplexing) {
99        this->generateMultiplexingCountOnlyGrepPipeline(regex, utf8CC);
100    } else {
101        this->generateFullyDecompressionCountOnlyGrepPipeline(regex);
102    }
103}
104
105
106void LZ4GrepBaseGenerator::initREs(re::RE * RE) {
107    if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
108        mBreakCC = re::makeCC(re::makeCC(0x0A, 0x0D), re::makeCC(re::makeCC(0x85), re::makeCC(0x2028, 0x2029)));
109    } else if (mGrepRecordBreak == GrepRecordBreakKind::Null) {
110        mBreakCC = re::makeByte(0);  // Null
111    } else {
112        mBreakCC = re::makeByte(0x0A); // LF
113    }
114    re::RE * anchorRE = mBreakCC;
115    if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
116        re::Name * anchorName = re::makeName("UTF8_LB", re::Name::Type::Unicode);
117        anchorName->setDefinition(re::makeUnicodeBreak());
118        anchorRE = anchorName;
119    }
120
121    mRE = RE;
122    bool allAnchored = true;
123
124    if (!hasEndAnchor(mRE)) allAnchored = false;
125    mRE = resolveModesAndExternalSymbols(mRE);
126    mRE = re::exclude_CC(mRE, mBreakCC);
127    mRE = resolveAnchors(mRE, anchorRE);
128    re::gatherUnicodeProperties(mRE, mUnicodeProperties);
129    mRE = regular_expression_passes(mRE);
130
131    if (allAnchored && (mGrepRecordBreak != GrepRecordBreakKind::Unicode)) mMoveMatchesToEOL = false;
132
133}
134
135
136parabix::StreamSetBuffer * LZ4GrepBaseGenerator::linefeedStreamFromUncompressedBits(
137        parabix::StreamSetBuffer *uncompressedBasisBits) {
138    auto & idb = mPxDriver.getBuilder();
139    const unsigned baseBufferSize = this->getDefaultBufferBlocks();
140    StreamSetBuffer * LineFeedStream = mPxDriver.addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
141    kernel::Kernel * linefeedK = mPxDriver.addKernelInstance<kernel::LineFeedKernelBuilder>(idb, Binding{idb->getStreamSetTy(8), "basis", FixedRate(), Principal()}, cc::BitNumbering::BigEndian);
142    mPxDriver.makeKernelCall(linefeedK, {uncompressedBasisBits}, {LineFeedStream});
143    return LineFeedStream;
144}
145
146
147
148
149
150
151std::pair<parabix::StreamSetBuffer *, parabix::StreamSetBuffer *> LZ4GrepBaseGenerator::multiplexingGrep(
152        re::RE *RE,
153        parabix::StreamSetBuffer *compressedByteStream,
154        parabix::StreamSetBuffer *compressedBitStream,
155        bool utf8CC
156) {
157
158    this->initREs(RE);
159    auto mGrepDriver = &mPxDriver;
160
161    auto & idb = mGrepDriver->getBuilder();
162    const unsigned baseBufferSize = this->getDefaultBufferBlocks();
163    int MaxCountFlag = 0;
164
165    //  Regular Expression Processing and Analysis Phase
166    const auto nREs = 1;
167
168    std::vector<StreamSetBuffer *> MatchResultsBufs(nREs);
169
170
171    std::map<std::string, StreamSetBuffer *> propertyStream;
172
173    std::vector<std::string> externalStreamNames;
174    std::set<re::Name *> UnicodeProperties;
175
176    StreamSetBuffer* fakeMatchCopiedBits = nullptr;
177    StreamSetBuffer* u8NoFinalStream = nullptr;
178    StreamSetBuffer * uncompressedCharClasses = nullptr;
179
180    re::CC* linefeedCC = nullptr;
181
182
183    if (utf8CC) {
184        re::Seq* seq = re::makeSeq();
185        re::RE* targetRe = mRE;
186
187        linefeedCC = re::makeCC(0x0A);
188
189        seq->push_back(targetRe);
190        seq->push_back(std::move(linefeedCC));
191
192        std::vector<re::CC*> UnicodeSets = re::collectCCs(seq, &cc::Unicode, std::set<re::Name *>({re::makeZeroWidth("\\b{g}")}));;
193
194        mpx = make_unique<cc::MultiplexedAlphabet>("mpx", UnicodeSets);
195        mRE = transformCCs(mpx.get(), targetRe);
196
197
198        std::vector<re::CC *> mpx_basis = mpx->getMultiplexedCCs();
199        auto numOfCharacterClasses = mpx_basis.size();
200        StreamSetBuffer * CharClasses = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(numOfCharacterClasses), baseBufferSize, 1);
201
202        kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(mpx_basis), false, cc::BitNumbering::BigEndian);
203        mGrepDriver->makeKernelCall(ccK, {compressedBitStream}, {CharClasses});
204
205
206
207        StreamSetBuffer* compressedNonFinalStream = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
208        kernel::Kernel * nonFinalK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, u8NonFinalRe, externalStreamNames, std::vector<cc::Alphabet *>(), cc::BitNumbering::BigEndian);
209        mGrepDriver->makeKernelCall(nonFinalK, {compressedBitStream}, {compressedNonFinalStream});
210
211
212
213        auto decompressedStreams = this->decompressBitStreams(compressedByteStream, {CharClasses, compressedNonFinalStream});
214
215//        uncompressedCharClasses = this->decompressBitStream(compressedByteStream, CharClasses);
216        uncompressedCharClasses = decompressedStreams[0];
217        u8NoFinalStream = decompressedStreams[1];
218
219
220
221        auto fakeStreams = this->generateFakeStreams(idb, uncompressedCharClasses, std::vector<unsigned>{8});
222        fakeMatchCopiedBits = fakeStreams[0];
223
224    } else {
225        re::Seq* seq = re::makeSeq();
226        re::RE* targetRe = mRE;
227        targetRe = re::toUTF8(targetRe, true);
228
229        linefeedCC = re::makeByte(0x0A);
230
231        seq->push_back(targetRe);
232        seq->push_back(std::move(linefeedCC));
233
234        std::vector<re::CC*> UnicodeSets = re::collectCCs(seq, &cc::Byte, std::set<re::Name *>({re::makeZeroWidth("\\b{g}")}));
235
236        mpx = make_unique<cc::MultiplexedAlphabet>("mpx", UnicodeSets);
237        mRE = transformCCs(mpx.get(), targetRe);
238
239        std::vector<re::CC *> mpx_basis = mpx->getMultiplexedCCs();
240        auto numOfCharacterClasses = mpx_basis.size();
241        StreamSetBuffer * CharClasses = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(numOfCharacterClasses), baseBufferSize, 1);
242
243        kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::ByteClassesKernel>(idb, std::move(mpx_basis), false, cc::BitNumbering::BigEndian);
244        mGrepDriver->makeKernelCall(ccK, {compressedBitStream}, {CharClasses});
245
246        uncompressedCharClasses = this->decompressBitStream(compressedByteStream, CharClasses);
247        auto fakeStreams = this->generateFakeStreams(idb, uncompressedCharClasses, std::vector<unsigned>{8, 1});
248        fakeMatchCopiedBits = fakeStreams[0];
249        u8NoFinalStream = fakeStreams[1];
250    }
251
252
253
254
255
256    StreamSetBuffer * const MatchResults = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
257
258
259    StreamSetBuffer * LineBreakStream = mPxDriver.addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), this->getDefaultBufferBlocks(), 1);
260    kernel::Kernel * lineFeedGrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, transformCCs(mpx.get(), linefeedCC), externalStreamNames, std::vector<cc::Alphabet *>{mpx.get()}, cc::BitNumbering::BigEndian);
261    mGrepDriver->makeKernelCall(lineFeedGrepK, {fakeMatchCopiedBits, uncompressedCharClasses}, {LineBreakStream});
262
263
264    externalStreamNames.push_back("UTF8_nonfinal");
265
266    kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mRE, externalStreamNames, std::vector<cc::Alphabet *>{mpx.get()}, cc::BitNumbering::BigEndian);
267    mGrepDriver->makeKernelCall(icgrepK, {fakeMatchCopiedBits, u8NoFinalStream, uncompressedCharClasses}, {MatchResults});
268    MatchResultsBufs[0] = MatchResults;
269
270    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
271
272    StreamSetBuffer * Matches = MergedResults;
273    if (mMoveMatchesToEOL) {
274        StreamSetBuffer * OriginalMatches = Matches;
275        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance<kernel::MatchedLinesKernel>(idb);
276        Matches = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
277        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
278    }
279
280    if (MaxCountFlag > 0) {
281        kernel::Kernel * untilK = mGrepDriver->addKernelInstance<kernel::UntilNkernel>(idb);
282        untilK->setInitialArguments({idb->getSize(MaxCountFlag)});
283        StreamSetBuffer * const AllMatches = Matches;
284        Matches = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
285        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
286    }
287
288    return std::pair<StreamSetBuffer *, StreamSetBuffer *>(LineBreakStream, Matches);
289};
290
291std::pair<parabix::StreamSetBuffer *, parabix::StreamSetBuffer *> LZ4GrepBaseGenerator::grep(
292        re::RE *RE, parabix::StreamSetBuffer *uncompressedBasisBits, bool ccMultiplexing) {
293
294    this->initREs(RE);
295    auto mGrepDriver = &mPxDriver;
296
297    auto & idb = mGrepDriver->getBuilder();
298    // TODO: until we automate stream buffer sizing, use this calculation to determine how large our matches buffer needs to be.
299    const unsigned baseBufferSize = this->getDefaultBufferBlocks();
300    int MaxCountFlag = 0;
301
302    //  Regular Expression Processing and Analysis Phase
303    const auto nREs = 1;
304
305    std::vector<StreamSetBuffer *> MatchResultsBufs(nREs);
306
307    StreamSetBuffer * LineBreakStream = this->linefeedStreamFromUncompressedBits(uncompressedBasisBits);
308
309
310    std::map<std::string, StreamSetBuffer *> propertyStream;
311
312    for(unsigned i = 0; i < nREs; ++i) {
313        std::vector<std::string> externalStreamNames;
314        std::vector<StreamSetBuffer *> icgrepInputSets = {uncompressedBasisBits};
315
316        if (ccMultiplexing) {
317            const auto UnicodeSets = re::collectCCs(mRE, &cc::Unicode, std::set<re::Name *>({re::makeZeroWidth("\\b{g}")}));
318            StreamSetBuffer * const MatchResults = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
319
320            std::unique_ptr<cc::MultiplexedAlphabet> mpx = make_unique<cc::MultiplexedAlphabet>("mpx", UnicodeSets);
321            mRE = transformCCs(mpx.get(), mRE);
322            std::vector<re::CC *> mpx_basis = mpx->getMultiplexedCCs();
323            auto numOfCharacterClasses = mpx_basis.size();
324            StreamSetBuffer * CharClasses = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(numOfCharacterClasses), baseBufferSize, 1);
325            kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(mpx_basis), false, cc::BitNumbering::BigEndian);
326            mGrepDriver->makeKernelCall(ccK, {uncompressedBasisBits}, {CharClasses});
327
328            kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mRE, externalStreamNames, std::vector<cc::Alphabet *>{mpx.get()}, cc::BitNumbering::BigEndian);
329            icgrepInputSets.push_back(CharClasses);
330            mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
331            MatchResultsBufs[i] = MatchResults;
332        } else {
333            std::set<re::Name *> UnicodeProperties;
334
335
336
337
338            StreamSetBuffer* nonFinalStream = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
339            kernel::Kernel * nonFinalK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, u8NonFinalRe, externalStreamNames, std::vector<cc::Alphabet *>(), cc::BitNumbering::BigEndian);
340            mGrepDriver->makeKernelCall(nonFinalK, icgrepInputSets, {nonFinalStream});
341            icgrepInputSets.push_back(nonFinalStream);
342            externalStreamNames.push_back("UTF8_nonfinal");
343
344
345
346
347
348            StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
349            kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mRE, externalStreamNames, std::vector<cc::Alphabet *>(), cc::BitNumbering::BigEndian);
350            mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
351            MatchResultsBufs[i] = MatchResults;
352        }
353    }
354
355    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
356
357    StreamSetBuffer * Matches = MergedResults;
358    if (mMoveMatchesToEOL) {
359        StreamSetBuffer * OriginalMatches = Matches;
360        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance<kernel::MatchedLinesKernel>(idb);
361        Matches = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
362        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
363    }
364
365    if (MaxCountFlag > 0) {
366        kernel::Kernel * untilK = mGrepDriver->addKernelInstance<kernel::UntilNkernel>(idb);
367        untilK->setInitialArguments({idb->getSize(MaxCountFlag)});
368        StreamSetBuffer * const AllMatches = Matches;
369        Matches = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
370        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
371    }
372
373    return std::pair<StreamSetBuffer *, StreamSetBuffer *>(LineBreakStream, Matches);
374
375}
376
377void LZ4GrepBaseGenerator::invokeScanMatchGrep(char* fileBuffer, size_t blockStart, size_t blockEnd, bool hasBlockChecksum) {
378    auto main = this->getScanMatchGrepMainFunction();
379    std::ostringstream s;
380    EmitMatch accum("", false, false, s);
381
382    main(fileBuffer, blockStart, blockEnd, hasBlockChecksum, reinterpret_cast<intptr_t>(&accum));
383    llvm::outs() << s.str();
384}
385
386
387
388void LZ4GrepBaseGenerator::generateMultiplexingCountOnlyGrepPipeline(re::RE *regex, bool utf8CC) {
389    auto & iBuilder = mPxDriver.getBuilder();
390    this->generateCountOnlyMainFunc(iBuilder);
391
392    StreamSetBuffer *compressedByteStream = nullptr, *compressedBasisBits = nullptr;
393    std::tie(compressedByteStream, compressedBasisBits) = this->loadByteStreamAndBitStream();
394
395    StreamSetBuffer * LineBreakStream;
396    StreamSetBuffer * Matches;
397    std::tie(LineBreakStream, Matches) = multiplexingGrep(regex, compressedByteStream, compressedBasisBits, utf8CC);
398
399    kernel::Kernel * matchCountK = mPxDriver.addKernelInstance<kernel::PopcountKernel>(iBuilder);
400    mPxDriver.makeKernelCall(matchCountK, {Matches}, {});
401    mPxDriver.generatePipelineIR();
402
403    iBuilder->setKernel(matchCountK);
404    Value * matchedLineCount = iBuilder->getAccumulator("countResult");
405    matchedLineCount = iBuilder->CreateZExt(matchedLineCount, iBuilder->getInt64Ty());
406
407    mPxDriver.deallocateBuffers();
408
409    iBuilder->CreateRet(matchedLineCount);
410
411    mPxDriver.finalizeObject();
412}
413
414
415void LZ4GrepBaseGenerator::generateFullyDecompressionCountOnlyGrepPipeline(re::RE *regex) {
416    auto & iBuilder = mPxDriver.getBuilder();
417    this->generateCountOnlyMainFunc(iBuilder);
418
419    StreamSetBuffer * const uncompressedBitStream = this->generateUncompressedBitStreams();
420
421    StreamSetBuffer * LineBreakStream;
422    StreamSetBuffer * Matches;
423
424    std::tie(LineBreakStream, Matches) = grep(regex, uncompressedBitStream);
425
426    kernel::Kernel * matchCountK = mPxDriver.addKernelInstance<kernel::PopcountKernel>(iBuilder);
427    mPxDriver.makeKernelCall(matchCountK, {Matches}, {});
428    mPxDriver.generatePipelineIR();
429
430    iBuilder->setKernel(matchCountK);
431    Value * matchedLineCount = iBuilder->getAccumulator("countResult");
432    matchedLineCount = iBuilder->CreateZExt(matchedLineCount, iBuilder->getInt64Ty());
433
434    mPxDriver.deallocateBuffers();
435
436    iBuilder->CreateRet(matchedLineCount);
437
438    mPxDriver.finalizeObject();
439}
440
441
442ScanMatchGrepMainFunctionType LZ4GrepBaseGenerator::getScanMatchGrepMainFunction() {
443    return reinterpret_cast<ScanMatchGrepMainFunctionType>(mPxDriver.getMain());
444}
445CountOnlyGrepMainFunctionType LZ4GrepBaseGenerator::getCountOnlyGrepMainFunction() {
446    return reinterpret_cast<CountOnlyGrepMainFunctionType>(mPxDriver.getMain());
447}
448
449void LZ4GrepBaseGenerator::generateCountOnlyMainFunc(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
450    Module * M = iBuilder->getModule();
451    Type * const int64Ty = iBuilder->getInt64Ty();
452    Type * const sizeTy = iBuilder->getSizeTy();
453    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
454//    Type * const voidTy = iBuilder->getVoidTy();
455    Type * const inputType = iBuilder->getInt8PtrTy();
456
457    Function * const main = cast<Function>(M->getOrInsertFunction("Main", int64Ty, inputType, sizeTy, sizeTy, boolTy, nullptr));
458    main->setCallingConv(CallingConv::C);
459    Function::arg_iterator args = main->arg_begin();
460    mInputStream = &*(args++);
461    mInputStream->setName("input");
462
463    mHeaderSize = &*(args++);
464    mHeaderSize->setName("mHeaderSize");
465
466    mFileSize = &*(args++);
467    mFileSize->setName("mFileSize");
468
469    mHasBlockChecksum = &*(args++);
470    mHasBlockChecksum->setName("mHasBlockChecksum");
471    // TODO for now, we do not handle blockCheckSum
472    mHasBlockChecksum = iBuilder->getInt1(false);
473
474    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
475}
476
477void LZ4GrepBaseGenerator::generateScanMatchMainFunc(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
478    Module * M = iBuilder->getModule();
479    Type * const sizeTy = iBuilder->getSizeTy();
480    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
481    Type * const voidTy = iBuilder->getVoidTy();
482    Type * const inputType = iBuilder->getInt8PtrTy();
483    Type * const intAddrTy = iBuilder->getIntAddrTy();
484
485    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, intAddrTy, nullptr));
486    main->setCallingConv(CallingConv::C);
487    Function::arg_iterator args = main->arg_begin();
488    mInputStream = &*(args++);
489    mInputStream->setName("input");
490
491    mHeaderSize = &*(args++);
492    mHeaderSize->setName("mHeaderSize");
493
494    mFileSize = &*(args++);
495    mFileSize->setName("mFileSize");
496
497    mHasBlockChecksum = &*(args++);
498    mHasBlockChecksum->setName("mHasBlockChecksum");
499
500    match_accumulator = &*(args++);
501    match_accumulator->setName("match_accumulator");
502
503    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
504}
505
506std::vector<parabix::StreamSetBuffer *>
507LZ4GrepBaseGenerator::generateFakeStreams(const std::unique_ptr<kernel::KernelBuilder> &idb,
508                                          parabix::StreamSetBuffer *refStream, std::vector<unsigned> numOfStreams) {
509
510    std::vector<StreamSetBuffer *> outputStreams;
511    for (unsigned i = 0; i < numOfStreams.size(); i++) {
512        outputStreams.push_back(mPxDriver.addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(numOfStreams[i]),
513                                                                  this->getDefaultBufferBlocks(), 1));
514    }
515    Kernel* fakeStreamGeneratorK = mPxDriver.addKernelInstance<FakeStreamGeneratingKernel>(idb, refStream->getNumOfStreams(), numOfStreams);
516    mPxDriver.makeKernelCall(fakeStreamGeneratorK, {refStream}, outputStreams);
517    return outputStreams;
518}
519
520std::vector<parabix::StreamSetBuffer *>
521LZ4GrepBaseGenerator::decompressBitStreams(parabix::StreamSetBuffer *compressedByteStream,
522                                           std::vector<parabix::StreamSetBuffer *> compressedBitStreams) {
523    // Default implementation here will be slow
524    std::vector<parabix::StreamSetBuffer *> retVec;
525    for (unsigned i = 0; i < compressedBitStreams.size(); i++) {
526        retVec.push_back(this->decompressBitStream(compressedByteStream, compressedBitStreams[i]));
527    }
528    return retVec;
529}
Note: See TracBrowser for help on using the repository browser.