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

Last change on this file since 6040 was 6040, checked in by xwa163, 16 months ago

Init checkin for bitstream_pdep_kernel with gather intrinsics

File size: 40.4 KB
Line 
1
2#include "LZ4GrepGenerator.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/lz4/lz4_generate_deposit_stream.h>
16#include <kernels/kernel_builder.h>
17#include <kernels/deletion.h>
18#include <kernels/swizzle.h>
19#include <kernels/pdep_kernel.h>
20#include <kernels/swizzled_multiple_pdep_kernel.h>
21#include <kernels/lz4/lz4_swizzled_match_copy_kernel.h>
22#include <kernels/lz4/lz4_bitstream_match_copy_kernel.h>
23#include <kernels/lz4/lz4_bitstream_not_kernel.h>
24#include <kernels/bitstream_pdep_kernel.h>
25#include <kernels/bitstream_gather_pdep_kernel.h>
26#include <re/re_toolchain.h>
27
28#include <re/collect_ccs.h>
29#include <re/replaceCC.h>
30
31#include <UCD/resolve_properties.h>
32#include <kernels/charclasses.h>
33#include <kernels/grep_kernel.h>
34#include <kernels/UCD_property_kernel.h>
35#include <kernels/grapheme_kernel.h>
36#include <kernels/linebreak_kernel.h>
37#include <kernels/streams_merge.h>
38#include <kernels/scanmatchgen.h>
39#include <kernels/until_n.h>
40#include <re/casing.h>
41#include <re/exclude_CC.h>
42#include <re/to_utf8.h>
43#include <re/re_analysis.h>
44#include <re/re_name_resolve.h>
45#include <re/re_name_gather.h>
46#include <re/re_multiplex.h>
47#include <re/re_utility.h>
48#include <re/grapheme_clusters.h>
49#include <re/printer_re.h>
50#include <llvm/Support/raw_ostream.h>
51#include <llvm/Support/Debug.h>
52
53
54
55namespace re { class CC; }
56
57using namespace llvm;
58using namespace parabix;
59using namespace kernel;
60using namespace grep;
61
62
63const unsigned ByteCClimit = 6;
64
65
66LZ4GrepGenerator::LZ4GrepGenerator(bool enableMultiplexing): LZ4Generator(), mEnableMultiplexing(enableMultiplexing) {
67    mGrepRecordBreak = grep::GrepRecordBreakKind::LF;
68    mMoveMatchesToEOL = true;
69}
70
71void LZ4GrepGenerator::initREs(std::vector<re::RE *> & REs) {
72    if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
73        mBreakCC = re::makeCC(re::makeCC(0x0A, 0x0D), re::makeCC(re::makeCC(0x85), re::makeCC(0x2028, 0x2029)));
74    } else if (mGrepRecordBreak == GrepRecordBreakKind::Null) {
75        mBreakCC = re::makeByte(0);  // Null
76    } else {
77        mBreakCC = re::makeByte(0x0A); // LF
78    }
79    re::RE * anchorRE = mBreakCC;
80    if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
81        re::Name * anchorName = re::makeName("UTF8_LB", re::Name::Type::Unicode);
82        anchorName->setDefinition(re::makeUnicodeBreak());
83        anchorRE = anchorName;
84    }
85
86    mREs = REs;
87    bool allAnchored = true;
88    for(unsigned i = 0; i < mREs.size(); ++i) {
89        if (!hasEndAnchor(mREs[i])) allAnchored = false;
90        mREs[i] = resolveModesAndExternalSymbols(mREs[i]);
91        mREs[i] = re::exclude_CC(mREs[i], mBreakCC);
92        mREs[i] = resolveAnchors(mREs[i], anchorRE);
93        re::gatherUnicodeProperties(mREs[i], mUnicodeProperties);
94        mREs[i] = regular_expression_passes(mREs[i]);
95    }
96    if (allAnchored && (mGrepRecordBreak != GrepRecordBreakKind::Unicode)) mMoveMatchesToEOL = false;
97
98}
99
100
101parabix::StreamSetBuffer * LZ4GrepGenerator::linefeedStreamFromDecompressedBits(parabix::StreamSetBuffer *decompressedBasisBits) {
102//    auto mGrepDriver = &mPxDriver;
103    const unsigned baseBufferSize = this->getInputBufferBlocks();
104    auto & idb = mPxDriver.getBuilder();
105    StreamSetBuffer * LineFeedStream = mPxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
106    kernel::Kernel * linefeedK = mPxDriver.addKernelInstance<kernel::LineFeedKernelBuilder>(idb, Binding{idb->getStreamSetTy(8), "basis", FixedRate(), Principal()});
107    mPxDriver.makeKernelCall(linefeedK, {decompressedBasisBits}, {LineFeedStream});
108    return LineFeedStream;
109}
110
111
112parabix::StreamSetBuffer * LZ4GrepGenerator::linefeedStreamFromCompressedBits() {
113    // TODO for now, swizzled form for <1 * i1> input stream is not well defined, so we can not use this pipeline
114    auto mGrepDriver = &mPxDriver;
115    const unsigned baseBufferSize = this->getInputBufferBlocks();
116    auto & idb = mGrepDriver->getBuilder();
117
118    StreamSetBuffer * CompressedLineFeedStream = mPxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
119    kernel::Kernel * linefeedK = mPxDriver.addKernelInstance<kernel::LineFeedKernelBuilder>(idb, Binding{idb->getStreamSetTy(8), "basis", FixedRate(), Principal()});
120    mPxDriver.makeKernelCall(linefeedK, {mCompressedBasisBits}, {CompressedLineFeedStream});
121
122    // Extract (Deletion)
123    StreamSetBuffer * deletedLineFeedSwizzled = mPxDriver.addBuffer<CircularCopybackBuffer>(idb, idb->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
124    Kernel * delK = mPxDriver.addKernelInstance<SwizzledDeleteByPEXTkernel>(idb, 1, 64);
125    mPxDriver.makeKernelCall(delK, {mDeletionMarker, CompressedLineFeedStream}, {deletedLineFeedSwizzled});
126
127    // TODO incomplete
128    // Deposit
129    StreamSetBuffer * depositedSwizzle0 = mPxDriver.addBuffer<CircularCopybackBuffer>(idb, idb->getStreamSetTy(4), this->getDecompressedBufferBlocks(), 1);
130//    Kernel * multiplePdepK = mPxDriver.addKernelInstance<LZ4MultiplePDEPkernel>(idb, 4, 1, 4, 64, "lineFeedMultiplePDEP");
131//    mPxDriver.makeKernelCall(multiplePdepK, {mDepositMarker, deletedLineFeedSwizzled}, {depositedSwizzle0});
132
133
134    // Match Copy
135    StreamSetBuffer * matchCopiedSwizzle0 = mPxDriver.addBuffer<CircularCopybackBuffer>(idb, idb->getStreamSetTy(4), this->getDecompressedBufferBlocks(), 1);
136    Kernel * swizzledMatchCopyK = mPxDriver.addKernelInstance<LZ4SwizzledMatchCopyKernel>(idb, 4, 1, 4, 64, "lineFeedSwizzledMatchCopy");
137    mPxDriver.makeKernelCall(swizzledMatchCopyK, {mMatchOffsetMarker, mM0Marker, mCompressedByteStream, depositedSwizzle0}, {matchCopiedSwizzle0});
138
139    // Unswizzled
140    StreamSetBuffer * lineFeedStream = mPxDriver.addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1), this->getDecompressedBufferBlocks());
141    Kernel * unSwizzleK = mPxDriver.addKernelInstance<SwizzleGenerator>(idb, 1, 1, 1, 64, "linefeed");
142    mPxDriver.makeKernelCall(unSwizzleK, {depositedSwizzle0}, {lineFeedStream});
143
144    return lineFeedStream;
145}
146
147
148std::pair<parabix::StreamSetBuffer *, parabix::StreamSetBuffer *> LZ4GrepGenerator::multiplexingGrepPipeline(std::vector<re::RE *> &REs, parabix::StreamSetBuffer *matchCopiedBasisBits) {
149
150    this->initREs(REs);
151    auto mGrepDriver = &mPxDriver;
152
153
154    auto & idb = mGrepDriver->getBuilder();
155    // TODO: until we automate stream buffer sizing, use this calculation to determine how large our matches buffer needs to be.
156    const unsigned baseBufferSize = this->getInputBufferBlocks();
157    bool MultithreadedSimpleRE = false;
158    bool PropertyKernels = false;
159    bool CC_Multiplexing = false;
160    bool InvertMatchFlag = false;
161    int MaxCountFlag = 0;
162
163
164    //  Regular Expression Processing and Analysis Phase
165    const auto nREs = mREs.size();
166    bool hasGCB[nREs];
167    bool anyGCB = false;
168
169    for(unsigned i = 0; i < nREs; ++i) {
170        hasGCB[i] = hasGraphemeClusterBoundary(mREs[i]);
171        anyGCB |= hasGCB[i];
172    }
173    StreamSetBuffer * LineBreakStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
174    std::vector<StreamSetBuffer *> MatchResultsBufs(nREs);
175
176    re::RE * prefixRE;
177    re::RE * suffixRE;
178    // For simple regular expressions with a small number of characters, we
179    // can bypass transposition and use the Direct CC compiler.
180//    bool isSimple = (nREs == 1) && (mGrepRecordBreak != GrepRecordBreakKind::Unicode) && (!anyGCB);
181    bool isSimple = false;
182    if (isSimple) {
183        mREs[0] = toUTF8(mREs[0]);
184    }
185    if (isSimple && byteTestsWithinLimit(mREs[0], ByteCClimit)) {
186        std::vector<std::string> externalStreamNames;
187        std::vector<StreamSetBuffer *> icgrepInputSets = {mCompressedByteStream};
188        if (MultithreadedSimpleRE && hasTriCCwithinLimit(mREs[0], ByteCClimit, prefixRE, suffixRE)) {
189            auto CCs = re::collectCCs(prefixRE, &cc::Byte);
190            for (auto cc : CCs) {
191                auto ccName = makeName(cc);
192                mREs[0] = re::replaceCC(mREs[0], cc, ccName);
193                std::string ccNameStr = ccName->getFullName();
194                StreamSetBuffer * ccStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
195                kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, ccNameStr, std::vector<re::CC *>{cc});
196                mGrepDriver->makeKernelCall(ccK, {mCompressedByteStream}, {ccStream});
197                externalStreamNames.push_back(ccNameStr);
198                icgrepInputSets.push_back(ccStream);
199            }
200        }
201        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
202        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ByteGrepKernel>(idb, mREs[0], externalStreamNames);
203        mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
204        MatchResultsBufs[0] = MatchResults;
205        kernel::Kernel * breakK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, "breakCC", std::vector<re::CC *>{mBreakCC});
206        mGrepDriver->makeKernelCall(breakK, {mCompressedByteStream}, {LineBreakStream});
207    } else if (isSimple && hasTriCCwithinLimit(mREs[0], ByteCClimit, prefixRE, suffixRE)) {
208        std::vector<std::string> externalStreamNames;
209        std::vector<StreamSetBuffer *> icgrepInputSets = {mCompressedByteStream};
210        if (MultithreadedSimpleRE) {
211            auto CCs = re::collectCCs(prefixRE, &cc::Byte);
212            for (auto cc : CCs) {
213                auto ccName = makeName(cc);
214                mREs[0] = re::replaceCC(mREs[0], cc, ccName);
215                std::string ccNameStr = ccName->getFullName();
216                StreamSetBuffer * ccStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
217                kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, ccNameStr, std::vector<re::CC *>{cc});
218                mGrepDriver->makeKernelCall(ccK, {mCompressedByteStream}, {ccStream});
219                externalStreamNames.push_back(ccNameStr);
220                icgrepInputSets.push_back(ccStream);
221            }
222        }
223        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
224        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ByteBitGrepKernel>(idb, prefixRE, suffixRE, externalStreamNames);
225        mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
226        MatchResultsBufs[0] = MatchResults;
227        kernel::Kernel * breakK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, "breakCC", std::vector<re::CC *>{mBreakCC});
228        mGrepDriver->makeKernelCall(breakK, {mCompressedByteStream}, {LineBreakStream});
229    } else {
230        StreamSetBuffer * RequiredStreams = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
231        StreamSetBuffer * UnicodeLB = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
232
233
234        StreamSetBuffer * LineFeedStream = this->linefeedStreamFromDecompressedBits(matchCopiedBasisBits);
235//        StreamSetBuffer * LineFeedStream = this->linefeedStreamFromCompressedBits();
236
237        kernel::Kernel * requiredStreamsK = mGrepDriver->addKernelInstance<kernel::RequiredStreams_UTF8>(idb);
238        mGrepDriver->makeKernelCall(requiredStreamsK, {matchCopiedBasisBits, LineFeedStream}, {RequiredStreams, UnicodeLB});
239
240        if (mGrepRecordBreak == GrepRecordBreakKind::LF) {
241            LineBreakStream = LineFeedStream;
242        } else if (mGrepRecordBreak == GrepRecordBreakKind::Null) {
243            kernel::Kernel * breakK = mGrepDriver->addKernelInstance<kernel::ParabixCharacterClassKernelBuilder>(idb, "Null", std::vector<re::CC *>{mBreakCC}, 8);
244            mGrepDriver->makeKernelCall(breakK, {matchCopiedBasisBits}, {LineBreakStream});
245        } else {
246            LineBreakStream = UnicodeLB;
247        }
248
249        std::map<std::string, StreamSetBuffer *> propertyStream;
250        if (PropertyKernels) {
251            for (auto p : mUnicodeProperties) {
252                auto name = p->getFullName();
253                StreamSetBuffer * s = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
254                propertyStream.emplace(std::make_pair(name, s));
255                kernel::Kernel * propertyK = mGrepDriver->addKernelInstance<kernel::UnicodePropertyKernelBuilder>(idb, p);
256                mGrepDriver->makeKernelCall(propertyK, {matchCopiedBasisBits}, {s});
257            }
258        }
259        StreamSetBuffer * GCB_stream = nullptr;
260        if (anyGCB) {
261            GCB_stream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
262            kernel::Kernel * gcbK = mGrepDriver->addKernelInstance<kernel::GraphemeClusterBreakKernel>(idb);
263            mGrepDriver->makeKernelCall(gcbK, {matchCopiedBasisBits, RequiredStreams}, {GCB_stream});
264        }
265
266        for(unsigned i = 0; i < nREs; ++i) {
267            std::vector<std::string> externalStreamNames;
268            std::vector<StreamSetBuffer *> icgrepInputSets = {matchCopiedBasisBits};
269            if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
270                externalStreamNames.push_back("UTF8_LB");
271                icgrepInputSets.push_back(LineBreakStream);
272                externalStreamNames.push_back("UTF8_nonfinal");
273                icgrepInputSets.push_back(RequiredStreams);
274            }
275            std::set<re::Name *> UnicodeProperties;
276            if (PropertyKernels) {
277                re::gatherUnicodeProperties(mREs[i], UnicodeProperties);
278                for (auto p : UnicodeProperties) {
279                    auto name = p->getFullName();
280                    auto f = propertyStream.find(name);
281                    if (f == propertyStream.end()) report_fatal_error(name + " not found\n");
282                    externalStreamNames.push_back(name);
283                    icgrepInputSets.push_back(f->second);
284                }
285            }
286            if (hasGCB[i]) {
287                externalStreamNames.push_back("\\b{g}");
288                icgrepInputSets.push_back(GCB_stream);
289            }
290            if (CC_Multiplexing) {
291                const auto UnicodeSets = re::collectCCs(mREs[i], &cc::Unicode, std::set<re::Name *>({re::makeZeroWidth("\\b{g}")}));
292                StreamSetBuffer * const MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
293                if (UnicodeSets.size() <= 1) {
294                    kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[i], externalStreamNames);
295                    mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
296                    MatchResultsBufs[i] = MatchResults;
297                } else {
298                    mpx = make_unique<cc::MultiplexedAlphabet>("mpx", UnicodeSets);
299                    mREs[i] = transformCCs(mpx.get(), mREs[i]);
300                    std::vector<re::CC *> mpx_basis = mpx->getMultiplexedCCs();
301                    auto numOfCharacterClasses = mpx_basis.size();
302                    StreamSetBuffer * CharClasses = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(numOfCharacterClasses), baseBufferSize);
303                    kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(mpx_basis));
304                    mGrepDriver->makeKernelCall(ccK, {matchCopiedBasisBits}, {CharClasses});
305                    //                kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(mpx_basis), true);
306                    //                mGrepDriver->makeKernelCall(ccK, {mCompressedByteStream}, {CharClasses});
307                    kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[i], externalStreamNames, std::vector<cc::Alphabet *>{mpx.get()});
308                    icgrepInputSets.push_back(CharClasses);
309                    mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
310                    MatchResultsBufs[i] = MatchResults;
311                }
312            } else {
313                StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
314                kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[i], externalStreamNames);
315                mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
316                MatchResultsBufs[i] = MatchResults;
317            }
318        }
319    }
320
321    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
322    if (mREs.size() > 1) {
323        MergedResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
324        kernel::Kernel * streamsMergeK = mGrepDriver->addKernelInstance<kernel::StreamsMerge>(idb, 1, mREs.size());
325        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
326    }
327    StreamSetBuffer * Matches = MergedResults;
328    if (mMoveMatchesToEOL) {
329        StreamSetBuffer * OriginalMatches = Matches;
330        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance<kernel::MatchedLinesKernel>(idb);
331        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
332        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
333    }
334    if (InvertMatchFlag) {
335        kernel::Kernel * invertK = mGrepDriver->addKernelInstance<kernel::InvertMatchesKernel>(idb);
336        StreamSetBuffer * OriginalMatches = Matches;
337        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
338        mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {Matches});
339    }
340    if (MaxCountFlag > 0) {
341        kernel::Kernel * untilK = mGrepDriver->addKernelInstance<kernel::UntilNkernel>(idb);
342        untilK->setInitialArguments({idb->getSize(MaxCountFlag)});
343        StreamSetBuffer * const AllMatches = Matches;
344        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
345        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
346    }
347
348    return std::pair<StreamSetBuffer *, StreamSetBuffer *>(LineBreakStream, Matches);
349};
350
351std::pair<parabix::StreamSetBuffer *, parabix::StreamSetBuffer *> LZ4GrepGenerator::grepPipeline(
352        std::vector<re::RE *> &REs, parabix::StreamSetBuffer *decompressedBasisBits) {
353
354    this->initREs(REs);
355    auto mGrepDriver = &mPxDriver;
356
357
358    auto & idb = mGrepDriver->getBuilder();
359    // TODO: until we automate stream buffer sizing, use this calculation to determine how large our matches buffer needs to be.
360    const unsigned baseBufferSize = this->getInputBufferBlocks();
361    bool MultithreadedSimpleRE = false;
362    bool PropertyKernels = false;
363    bool CC_Multiplexing = false;
364    bool InvertMatchFlag = false;
365    int MaxCountFlag = 0;
366
367
368
369
370    //  Regular Expression Processing and Analysis Phase
371    const auto nREs = mREs.size();
372    bool hasGCB[nREs];
373    bool anyGCB = false;
374
375    for(unsigned i = 0; i < nREs; ++i) {
376        hasGCB[i] = hasGraphemeClusterBoundary(mREs[i]);
377        anyGCB |= hasGCB[i];
378    }
379    StreamSetBuffer * LineBreakStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
380    std::vector<StreamSetBuffer *> MatchResultsBufs(nREs);
381
382    re::RE * prefixRE;
383    re::RE * suffixRE;
384    // For simple regular expressions with a small number of characters, we
385    // can bypass transposition and use the Direct CC compiler.
386//    bool isSimple = (nREs == 1) && (mGrepRecordBreak != GrepRecordBreakKind::Unicode) && (!anyGCB);
387    bool isSimple = false;
388    if (isSimple) {
389        mREs[0] = toUTF8(mREs[0]);
390    }
391    if (isSimple && byteTestsWithinLimit(mREs[0], ByteCClimit)) {
392        std::vector<std::string> externalStreamNames;
393        std::vector<StreamSetBuffer *> icgrepInputSets = {mCompressedByteStream};
394        if (MultithreadedSimpleRE && hasTriCCwithinLimit(mREs[0], ByteCClimit, prefixRE, suffixRE)) {
395            auto CCs = re::collectCCs(prefixRE, &cc::Byte);
396            for (auto cc : CCs) {
397                auto ccName = makeName(cc);
398                mREs[0] = re::replaceCC(mREs[0], cc, ccName);
399                std::string ccNameStr = ccName->getFullName();
400                StreamSetBuffer * ccStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
401                kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, ccNameStr, std::vector<re::CC *>{cc});
402                mGrepDriver->makeKernelCall(ccK, {mCompressedByteStream}, {ccStream});
403                externalStreamNames.push_back(ccNameStr);
404                icgrepInputSets.push_back(ccStream);
405            }
406        }
407        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
408        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ByteGrepKernel>(idb, mREs[0], externalStreamNames);
409        mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
410        MatchResultsBufs[0] = MatchResults;
411        kernel::Kernel * breakK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, "breakCC", std::vector<re::CC *>{mBreakCC});
412        mGrepDriver->makeKernelCall(breakK, {mCompressedByteStream}, {LineBreakStream});
413    } else if (isSimple && hasTriCCwithinLimit(mREs[0], ByteCClimit, prefixRE, suffixRE)) {
414        std::vector<std::string> externalStreamNames;
415        std::vector<StreamSetBuffer *> icgrepInputSets = {mCompressedByteStream};
416        if (MultithreadedSimpleRE) {
417            auto CCs = re::collectCCs(prefixRE, &cc::Byte);
418            for (auto cc : CCs) {
419                auto ccName = makeName(cc);
420                mREs[0] = re::replaceCC(mREs[0], cc, ccName);
421                std::string ccNameStr = ccName->getFullName();
422                StreamSetBuffer * ccStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
423                kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, ccNameStr, std::vector<re::CC *>{cc});
424                mGrepDriver->makeKernelCall(ccK, {mCompressedByteStream}, {ccStream});
425                externalStreamNames.push_back(ccNameStr);
426                icgrepInputSets.push_back(ccStream);
427            }
428        }
429        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
430        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ByteBitGrepKernel>(idb, prefixRE, suffixRE, externalStreamNames);
431        mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
432        MatchResultsBufs[0] = MatchResults;
433        kernel::Kernel * breakK = mGrepDriver->addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(idb, "breakCC", std::vector<re::CC *>{mBreakCC});
434        mGrepDriver->makeKernelCall(breakK, {mCompressedByteStream}, {LineBreakStream});
435    } else {
436        StreamSetBuffer * RequiredStreams = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
437        StreamSetBuffer * UnicodeLB = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
438
439        StreamSetBuffer * LineFeedStream = this->linefeedStreamFromDecompressedBits(decompressedBasisBits);
440
441        kernel::Kernel * requiredStreamsK = mGrepDriver->addKernelInstance<kernel::RequiredStreams_UTF8>(idb);
442        mGrepDriver->makeKernelCall(requiredStreamsK, {decompressedBasisBits, LineFeedStream}, {RequiredStreams, UnicodeLB});
443
444        if (mGrepRecordBreak == GrepRecordBreakKind::LF) {
445            LineBreakStream = LineFeedStream;
446        } else if (mGrepRecordBreak == GrepRecordBreakKind::Null) {
447            kernel::Kernel * breakK = mGrepDriver->addKernelInstance<kernel::ParabixCharacterClassKernelBuilder>(idb, "Null", std::vector<re::CC *>{mBreakCC}, 8);
448            mGrepDriver->makeKernelCall(breakK, {decompressedBasisBits}, {LineBreakStream});
449        } else {
450            LineBreakStream = UnicodeLB;
451        }
452
453        std::map<std::string, StreamSetBuffer *> propertyStream;
454        if (PropertyKernels) {
455            for (auto p : mUnicodeProperties) {
456                auto name = p->getFullName();
457                StreamSetBuffer * s = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
458                propertyStream.emplace(std::make_pair(name, s));
459                kernel::Kernel * propertyK = mGrepDriver->addKernelInstance<kernel::UnicodePropertyKernelBuilder>(idb, p);
460                mGrepDriver->makeKernelCall(propertyK, {decompressedBasisBits}, {s});
461            }
462        }
463        StreamSetBuffer * GCB_stream = nullptr;
464        if (anyGCB) {
465            GCB_stream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
466            kernel::Kernel * gcbK = mGrepDriver->addKernelInstance<kernel::GraphemeClusterBreakKernel>(idb);
467            mGrepDriver->makeKernelCall(gcbK, {decompressedBasisBits, RequiredStreams}, {GCB_stream});
468        }
469
470        for(unsigned i = 0; i < nREs; ++i) {
471            std::vector<std::string> externalStreamNames;
472            std::vector<StreamSetBuffer *> icgrepInputSets = {decompressedBasisBits};
473            if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
474                externalStreamNames.push_back("UTF8_LB");
475                icgrepInputSets.push_back(LineBreakStream);
476                externalStreamNames.push_back("UTF8_nonfinal");
477                icgrepInputSets.push_back(RequiredStreams);
478            }
479            std::set<re::Name *> UnicodeProperties;
480            if (PropertyKernels) {
481                re::gatherUnicodeProperties(mREs[i], UnicodeProperties);
482                for (auto p : UnicodeProperties) {
483                    auto name = p->getFullName();
484                    auto f = propertyStream.find(name);
485                    if (f == propertyStream.end()) report_fatal_error(name + " not found\n");
486                    externalStreamNames.push_back(name);
487                    icgrepInputSets.push_back(f->second);
488                }
489            }
490            if (hasGCB[i]) {
491                externalStreamNames.push_back("\\b{g}");
492                icgrepInputSets.push_back(GCB_stream);
493            }
494            if (CC_Multiplexing) {
495                const auto UnicodeSets = re::collectCCs(mREs[i], &cc::Unicode, std::set<re::Name *>({re::makeZeroWidth("\\b{g}")}));
496                StreamSetBuffer * const MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
497                if (UnicodeSets.size() <= 1) {
498                    kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[i], externalStreamNames);
499                    mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
500                    MatchResultsBufs[i] = MatchResults;
501                } else {
502                    mpx = make_unique<cc::MultiplexedAlphabet>("mpx", UnicodeSets);
503                    mREs[i] = transformCCs(mpx.get(), mREs[i]);
504                    std::vector<re::CC *> mpx_basis = mpx->getMultiplexedCCs();
505                    auto numOfCharacterClasses = mpx_basis.size();
506                    StreamSetBuffer * CharClasses = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(numOfCharacterClasses), baseBufferSize);
507                    kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(mpx_basis));
508                    mGrepDriver->makeKernelCall(ccK, {decompressedBasisBits}, {CharClasses});
509                    //                kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(mpx_basis), true);
510                    //                mGrepDriver->makeKernelCall(ccK, {mCompressedByteStream}, {CharClasses});
511                    kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[i], externalStreamNames, std::vector<cc::Alphabet *>{mpx.get()});
512                    icgrepInputSets.push_back(CharClasses);
513                    mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
514                    MatchResultsBufs[i] = MatchResults;
515                }
516            } else {
517                StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
518                kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[i], externalStreamNames);
519                mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
520                MatchResultsBufs[i] = MatchResults;
521            }
522        }
523    }
524
525    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
526    if (mREs.size() > 1) {
527        MergedResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
528        kernel::Kernel * streamsMergeK = mGrepDriver->addKernelInstance<kernel::StreamsMerge>(idb, 1, mREs.size());
529        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
530    }
531    StreamSetBuffer * Matches = MergedResults;
532    if (mMoveMatchesToEOL) {
533        StreamSetBuffer * OriginalMatches = Matches;
534        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance<kernel::MatchedLinesKernel>(idb);
535        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
536        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
537    }
538    if (InvertMatchFlag) {
539        kernel::Kernel * invertK = mGrepDriver->addKernelInstance<kernel::InvertMatchesKernel>(idb);
540        StreamSetBuffer * OriginalMatches = Matches;
541        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
542        mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {Matches});
543    }
544    if (MaxCountFlag > 0) {
545        kernel::Kernel * untilK = mGrepDriver->addKernelInstance<kernel::UntilNkernel>(idb);
546        untilK->setInitialArguments({idb->getSize(MaxCountFlag)});
547        StreamSetBuffer * const AllMatches = Matches;
548        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
549        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
550    }
551
552    return std::pair<StreamSetBuffer *, StreamSetBuffer *>(LineBreakStream, Matches);
553
554}
555
556void LZ4GrepGenerator::invokeScanMatchGrep(char* fileBuffer, size_t blockStart, size_t blockEnd, bool hasBlockChecksum) {
557    auto main = this->getScanMatchGrepMainFunction();
558    std::ostringstream s;
559    EmitMatch accum("", false, false, s);
560
561    main(fileBuffer, blockStart, blockEnd, hasBlockChecksum, reinterpret_cast<intptr_t>(&accum));
562    llvm::outs() << s.str();
563}
564
565void LZ4GrepGenerator::generateScanMatchGrepPipeline(re::RE* regex) {
566    auto & iBuilder = mPxDriver.getBuilder();
567    this->generateScanMatchMainFunc(iBuilder);
568
569    StreamSetBuffer * const DecompressedByteStream = mPxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), this->getDecompressedBufferBlocks());
570
571    // GeneratePipeline
572    this->generateLoadByteStreamAndBitStream(iBuilder);
573    this->generateExtractAndDepositMarkers(iBuilder);
574
575    auto swizzle = this->generateSwizzleExtractData(iBuilder);
576
577    StreamSetBuffer * depositedSwizzle0 = mPxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getDecompressedBufferBlocks(), 1);
578    StreamSetBuffer * depositedSwizzle1 = mPxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getDecompressedBufferBlocks(), 1);
579
580    Kernel * multiplePdepK = mPxDriver.addKernelInstance<SwizzledMultiplePDEPkernel>(iBuilder, 4, 2);
581    mPxDriver.makeKernelCall(multiplePdepK, {mDepositMarker, swizzle.first, swizzle.second}, {depositedSwizzle0, depositedSwizzle1});
582
583    StreamSetBuffer * matchCopiedSwizzle0 = mPxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
584    StreamSetBuffer * matchCopiedSwizzle1 = mPxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
585
586    Kernel * swizzledMatchCopyK = mPxDriver.addKernelInstance<LZ4SwizzledMatchCopyKernel>(iBuilder, 4, 2, 4);
587    mPxDriver.makeKernelCall(swizzledMatchCopyK, {mMatchOffsetMarker, mM0Marker, mCompressedByteStream, depositedSwizzle0, depositedSwizzle1}, {matchCopiedSwizzle0, matchCopiedSwizzle1});
588
589    // Produce unswizzled bit streams
590    StreamSetBuffer * extractedbits = mPxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), this->getInputBufferBlocks());
591    Kernel * unSwizzleK = mPxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
592    mPxDriver.makeKernelCall(unSwizzleK, {matchCopiedSwizzle0, matchCopiedSwizzle1}, {extractedbits});
593
594    Kernel * p2sK = mPxDriver.addKernelInstance<P2SKernel>(iBuilder);
595    mPxDriver.makeKernelCall(p2sK, {extractedbits}, {DecompressedByteStream});
596
597    StreamSetBuffer * LineBreakStream;
598    StreamSetBuffer * Matches;
599    std::vector<re::RE*> res = {regex};
600    std::tie(LineBreakStream, Matches) = grepPipeline(res, extractedbits);
601
602    kernel::Kernel * scanMatchK = mPxDriver.addKernelInstance<kernel::ScanMatchKernel>(iBuilder);
603    scanMatchK->setInitialArguments({match_accumulator});
604    mPxDriver.makeKernelCall(scanMatchK, {Matches, LineBreakStream, DecompressedByteStream}, {});
605    mPxDriver.LinkFunction(*scanMatchK, "accumulate_match_wrapper", &accumulate_match_wrapper);
606    mPxDriver.LinkFunction(*scanMatchK, "finalize_match_wrapper", &finalize_match_wrapper);
607
608    mPxDriver.generatePipelineIR();
609    mPxDriver.deallocateBuffers();
610
611    iBuilder->CreateRetVoid();
612
613    mPxDriver.finalizeObject();
614}
615
616void LZ4GrepGenerator::generateCountOnlyGrepPipeline(re::RE *regex, bool enableGather) {
617    auto & iBuilder = mPxDriver.getBuilder();
618    this->generateMainFunc(iBuilder);
619
620    // GeneratePipeline
621    this->generateLoadByteStreamAndBitStream(iBuilder);
622    this->generateExtractAndDepositMarkers(iBuilder);
623
624    StreamSetBuffer * const extractedBits = this->generateBitStreamExtractData(iBuilder);
625
626    StreamSetBuffer * depositedBits = mPxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), this->getDecompressedBufferBlocks());
627    Kernel * bitStreamPDEPk = enableGather ? mPxDriver.addKernelInstance<BitStreamGatherPDEPKernel>(iBuilder, 8) : mPxDriver.addKernelInstance<BitStreamPDEPKernel>(iBuilder, 8);
628    mPxDriver.makeKernelCall(bitStreamPDEPk, {mDepositMarker, extractedBits}, {depositedBits});
629
630    StreamSetBuffer * matchCopiedBits = mPxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), this->getInputBufferBlocks());
631    Kernel * bitStreamMatchCopyK = mPxDriver.addKernelInstance<LZ4BitStreamMatchCopyKernel>(iBuilder, 8);
632    mPxDriver.makeKernelCall(bitStreamMatchCopyK, {mMatchOffsetMarker, mM0Marker, mCompressedByteStream, depositedBits}, {matchCopiedBits});
633
634    StreamSetBuffer * LineBreakStream;
635    StreamSetBuffer * Matches;
636    std::vector<re::RE*> res = {regex};
637    if (mEnableMultiplexing) {
638        std::tie(LineBreakStream, Matches) = multiplexingGrepPipeline(res, matchCopiedBits);
639    } else {
640        std::tie(LineBreakStream, Matches) = grepPipeline(res, matchCopiedBits);
641    };
642
643    kernel::Kernel * matchCountK = mPxDriver.addKernelInstance<kernel::PopcountKernel>(iBuilder);
644    mPxDriver.makeKernelCall(matchCountK, {Matches}, {});
645    mPxDriver.generatePipelineIR();
646
647    iBuilder->setKernel(matchCountK);
648    Value * matchedLineCount = iBuilder->getAccumulator("countResult");
649    matchedLineCount = iBuilder->CreateZExt(matchedLineCount, iBuilder->getInt64Ty());
650    iBuilder->CallPrintInt("aaa", matchedLineCount);
651
652    mPxDriver.deallocateBuffers();
653
654    // TODO return matchedLineCount
655//        idb->CreateRet(matchedLineCount);
656
657    iBuilder->CreateRetVoid();
658
659    mPxDriver.finalizeObject();
660}
661
662
663void LZ4GrepGenerator::generateSwizzledCountOnlyGrepPipeline(re::RE *regex) {
664    auto & iBuilder = mPxDriver.getBuilder();
665    this->generateMainFunc(iBuilder);
666
667
668    // GeneratePipeline
669    this->generateLoadByteStreamAndBitStream(iBuilder);
670    this->generateExtractAndDepositMarkers(iBuilder);
671
672    auto swizzle = this->generateSwizzleExtractData(iBuilder);
673
674    StreamSetBuffer * depositedSwizzle0 = mPxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
675    StreamSetBuffer * depositedSwizzle1 = mPxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
676
677    Kernel * multiplePdepK = mPxDriver.addKernelInstance<SwizzledMultiplePDEPkernel>(iBuilder, 4, 2);
678    mPxDriver.makeKernelCall(multiplePdepK, {mDepositMarker, swizzle.first, swizzle.second}, {depositedSwizzle0, depositedSwizzle1});
679
680
681    // split PDEP into 2 kernel will be a little slower in single thread environment
682/*
683    Kernel * pdep1 = mPxDriver.addKernelInstance<PDEPkernel>(iBuilder, 4);
684    mPxDriver.makeKernelCall(pdep1, {mDepositMarker, swizzle.first}, {depositedSwizzle0});
685
686    Kernel * pdep2 = mPxDriver.addKernelInstance<PDEPkernel>(iBuilder, 4);
687    mPxDriver.makeKernelCall(pdep2, {mDepositMarker, swizzle.second}, {depositedSwizzle1});
688*/
689
690    StreamSetBuffer * matchCopiedSwizzle0 = mPxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
691    StreamSetBuffer * matchCopiedSwizzle1 = mPxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), this->getInputBufferBlocks(), 1);
692
693    Kernel * swizzledMatchCopyK = mPxDriver.addKernelInstance<LZ4SwizzledMatchCopyKernel>(iBuilder, 4, 2, 4);
694    mPxDriver.makeKernelCall(swizzledMatchCopyK, {mMatchOffsetMarker, mM0Marker, mCompressedByteStream, depositedSwizzle0, depositedSwizzle1}, {matchCopiedSwizzle0, matchCopiedSwizzle1});
695
696    // Produce unswizzled bit streams
697    StreamSetBuffer * matchCopiedbits = mPxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), this->getInputBufferBlocks());
698    Kernel * unSwizzleK = mPxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
699    mPxDriver.makeKernelCall(unSwizzleK, {matchCopiedSwizzle0, matchCopiedSwizzle1}, {matchCopiedbits});
700
701    StreamSetBuffer * LineBreakStream;
702    StreamSetBuffer * Matches;
703    std::vector<re::RE*> res = {regex};
704    if (mEnableMultiplexing) {
705        std::tie(LineBreakStream, Matches) = multiplexingGrepPipeline(res, matchCopiedbits);
706    } else {
707        std::tie(LineBreakStream, Matches) = grepPipeline(res, matchCopiedbits);
708    };
709
710    kernel::Kernel * matchCountK = mPxDriver.addKernelInstance<kernel::PopcountKernel>(iBuilder);
711    mPxDriver.makeKernelCall(matchCountK, {Matches}, {});
712    mPxDriver.generatePipelineIR();
713
714    iBuilder->setKernel(matchCountK);
715    Value * matchedLineCount = iBuilder->getAccumulator("countResult");
716    matchedLineCount = iBuilder->CreateZExt(matchedLineCount, iBuilder->getInt64Ty());
717    iBuilder->CallPrintInt("aaa", matchedLineCount);
718
719    mPxDriver.deallocateBuffers();
720
721    // TODO return matchedLineCount
722//        idb->CreateRet(matchedLineCount);
723
724    iBuilder->CreateRetVoid();
725
726    mPxDriver.finalizeObject();
727}
728
729ScanMatchGrepMainFunctionType LZ4GrepGenerator::getScanMatchGrepMainFunction() {
730    return reinterpret_cast<ScanMatchGrepMainFunctionType>(mPxDriver.getMain());
731}
732
733void LZ4GrepGenerator::generateScanMatchMainFunc(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
734    Module * M = iBuilder->getModule();
735    Type * const sizeTy = iBuilder->getSizeTy();
736    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
737    Type * const voidTy = iBuilder->getVoidTy();
738    Type * const inputType = iBuilder->getInt8PtrTy();
739    Type * const intAddrTy = iBuilder->getIntAddrTy();
740
741    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, intAddrTy, nullptr));
742    main->setCallingConv(CallingConv::C);
743    Function::arg_iterator args = main->arg_begin();
744    mInputStream = &*(args++);
745    mInputStream->setName("input");
746
747    mHeaderSize = &*(args++);
748    mHeaderSize->setName("mHeaderSize");
749
750    mFileSize = &*(args++);
751    mFileSize->setName("mFileSize");
752
753    mHasBlockChecksum = &*(args++);
754    mHasBlockChecksum->setName("mHasBlockChecksum");
755
756    match_accumulator = &*(args++);
757    match_accumulator->setName("match_accumulator");
758
759    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
760}
Note: See TracBrowser for help on using the repository browser.