source: icGREP/icgrep-devel/icgrep/lzparabix/LZParabixGrepGenerator.cpp @ 6133

Last change on this file since 6133 was 6133, checked in by xwa163, 8 months ago
  1. Add sourceCC in multiplexed CC
  2. Remove workaround FakeBasisBits? from ICGrep
  3. Implement Swizzled version of LZParabix
  4. Init checkin for SwizzleByGather? Kernel
File size: 15.7 KB
Line 
1//
2// Created by wxy325 on 2018/6/19.
3//
4
5#include "LZParabixGrepGenerator.h"
6
7
8#include <boost/iostreams/device/mapped_file.hpp>
9
10#include <llvm/Support/PrettyStackTrace.h>
11
12#include <cc/cc_compiler.h>
13
14#include <kernels/cc_kernel.h>
15#include <kernels/s2p_kernel.h>
16#include <kernels/p2s_kernel.h>
17#include <kernels/source_kernel.h>
18#include <kernels/stdout_kernel.h>
19#include <kernels/kernel_builder.h>
20#include <kernels/swizzle.h>
21#include <re/re_toolchain.h>
22
23#include <re/collect_ccs.h>
24#include <re/replaceCC.h>
25#include <re/re_seq.h>
26#include <re/re_cc.h>
27
28#include <UCD/resolve_properties.h>
29#include <kernels/charclasses.h>
30#include <kernels/grep_kernel.h>
31#include <kernels/UCD_property_kernel.h>
32#include <kernels/grapheme_kernel.h>
33#include <kernels/linebreak_kernel.h>
34#include <kernels/streams_merge.h>
35#include <kernels/scanmatchgen.h>
36#include <kernels/until_n.h>
37#include <re/casing.h>
38#include <re/exclude_CC.h>
39#include <re/to_utf8.h>
40#include <re/re_analysis.h>
41#include <re/re_name_resolve.h>
42#include <re/re_name_gather.h>
43#include <re/re_multiplex.h>
44#include <re/re_utility.h>
45#include <re/grapheme_clusters.h>
46#include <re/printer_re.h>
47#include <llvm/Support/raw_ostream.h>
48#include <llvm/Support/Debug.h>
49#include <kernels/fake_stream_generating_kernel.h>
50#include <re/re_alt.h>
51
52namespace re { class CC; }
53
54using namespace llvm;
55using namespace parabix;
56using namespace kernel;
57using namespace grep;
58
59
60LZParabixGrepGenerator::LZParabixGrepGenerator(bool enableMultiplexing): LZParabixGenerator(), mEnableMultiplexing(enableMultiplexing) {
61    mGrepRecordBreak = grep::GrepRecordBreakKind::LF;
62    mMoveMatchesToEOL = true;
63}
64
65void LZParabixGrepGenerator::initREs(std::vector<re::RE *> &REs) {
66    if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
67        mBreakCC = re::makeCC(re::makeCC(0x0A, 0x0D), re::makeCC(re::makeCC(0x85), re::makeCC(0x2028, 0x2029)));
68    } else if (mGrepRecordBreak == GrepRecordBreakKind::Null) {
69        mBreakCC = re::makeByte(0);  // Null
70    } else {
71        mBreakCC = re::makeByte(0x0A); // LF
72    }
73    re::RE * anchorRE = mBreakCC;
74    if (mGrepRecordBreak == GrepRecordBreakKind::Unicode) {
75        re::Name * anchorName = re::makeName("UTF8_LB", re::Name::Type::Unicode);
76        anchorName->setDefinition(re::makeUnicodeBreak());
77        anchorRE = anchorName;
78    }
79
80    mREs = REs;
81    bool allAnchored = true;
82    for(unsigned i = 0; i < mREs.size(); ++i) {
83        if (!hasEndAnchor(mREs[i])) allAnchored = false;
84        mREs[i] = resolveModesAndExternalSymbols(mREs[i]);
85        mREs[i] = re::exclude_CC(mREs[i], mBreakCC);
86        mREs[i] = resolveAnchors(mREs[i], anchorRE);
87        re::gatherUnicodeProperties(mREs[i], mUnicodeProperties);
88        mREs[i] = regular_expression_passes(mREs[i]);
89    }
90    if (allAnchored && (mGrepRecordBreak != GrepRecordBreakKind::Unicode)) mMoveMatchesToEOL = false;
91}
92
93void LZParabixGrepGenerator::generateCountOnlyMainFunc(const std::unique_ptr<kernel::KernelBuilder> &iBuilder) {
94    Module * M = iBuilder->getModule();
95    Type * const int64Ty = iBuilder->getInt64Ty();
96    Type * const sizeTy = iBuilder->getSizeTy();
97    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
98//    Type * const voidTy = iBuilder->getVoidTy();
99    Type * const inputType = iBuilder->getInt8PtrTy();
100
101    Function * const main = cast<Function>(M->getOrInsertFunction("Main", int64Ty, inputType, sizeTy, sizeTy, boolTy, nullptr));
102    main->setCallingConv(CallingConv::C);
103    Function::arg_iterator args = main->arg_begin();
104    mInputStream = &*(args++);
105    mInputStream->setName("input");
106
107    mHeaderSize = &*(args++);
108    mHeaderSize->setName("mHeaderSize");
109
110    mFileSize = &*(args++);
111    mFileSize->setName("mFileSize");
112
113    mHasBlockChecksum = &*(args++);
114    mHasBlockChecksum->setName("mHasBlockChecksum");
115    // TODO for now, we do not handle blockCheckSum
116    mHasBlockChecksum = iBuilder->getInt1(false);
117
118    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
119}
120
121void LZParabixGrepGenerator::generateCountOnlyAioPipeline(re::RE *regex, bool swizzledDecompression ) {
122    auto & iBuilder = mPxDriver.getBuilder();
123    this->generateCountOnlyMainFunc(iBuilder);
124
125    this->generateLoadByteStreamAndBitStream(iBuilder);
126
127
128    StreamSetBuffer * LineBreakStream;
129    StreamSetBuffer * Matches;
130    std::vector<re::RE*> res = {regex};
131    if (mEnableMultiplexing) {
132        std::tie(LineBreakStream, Matches) = multiplexingGrepPipeline(res);
133    } else {
134        std::tie(LineBreakStream, Matches) = grepPipeline(res, swizzledDecompression);
135    }
136
137//    Kernel * outK = mPxDriver.addKernelInstance<FileSink>(iBuilder, 8);
138//    outK->setInitialArguments({iBuilder->GetString("/Users/wxy325/developer/LZ4-sample-files/workspace/lz4d-normal/8k_.txt")});
139//    mPxDriver.makeKernelCall(outK, {decompressedStream}, {});
140
141    kernel::Kernel * matchCountK = mPxDriver.addKernelInstance<kernel::PopcountKernel>(iBuilder);
142    mPxDriver.makeKernelCall(matchCountK, {Matches}, {});
143    mPxDriver.generatePipelineIR();
144
145    iBuilder->setKernel(matchCountK);
146    Value * matchedLineCount = iBuilder->getAccumulator("countResult");
147    matchedLineCount = iBuilder->CreateZExt(matchedLineCount, iBuilder->getInt64Ty());
148
149    mPxDriver.deallocateBuffers();
150
151    iBuilder->CreateRet(matchedLineCount);
152
153    mPxDriver.finalizeObject();
154}
155
156
157std::pair<parabix::StreamSetBuffer *, parabix::StreamSetBuffer *> LZParabixGrepGenerator::multiplexingGrepPipeline(std::vector<re::RE *> &REs) {
158
159    this->initREs(REs);
160    auto mGrepDriver = &mPxDriver;
161
162    auto & idb = mGrepDriver->getBuilder();
163    // TODO: until we automate stream buffer sizing, use this calculation to determine how large our matches buffer needs to be.
164    const unsigned baseBufferSize = this->getInputBufferBlocks(idb);
165    int MaxCountFlag = 0;
166
167    //  Regular Expression Processing and Analysis Phase
168    const auto nREs = mREs.size();
169
170    std::vector<StreamSetBuffer *> MatchResultsBufs(nREs);
171
172
173    std::map<std::string, StreamSetBuffer *> propertyStream;
174
175    std::vector<std::string> externalStreamNames;
176    std::set<re::Name *> UnicodeProperties;
177
178    re::CC* linefeedCC = re::makeCC(0x0A);
179
180    re::Seq* seq = re::makeSeq();
181    seq->push_back(mREs[0]);
182    seq->push_back(std::move(linefeedCC));
183
184    const auto UnicodeSets = re::collectCCs(seq, &cc::Unicode, std::set<re::Name *>({re::makeZeroWidth("\\b{g}")}));
185    StreamSetBuffer * const MatchResults = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize, 1);
186
187    this->generateBlockData(idb);
188    StreamSetBuffer * const LiteralBitStream = this->extractLiteralBitStream(idb);
189
190    bool allCcByteLength = re::isAllCcByteLength(mREs[0]);
191
192    mpx = make_unique<cc::MultiplexedAlphabet>("mpx", UnicodeSets);
193    mREs[0] = transformCCs(mpx.get(), mREs[0]);
194
195    std::vector<re::CC *> mpx_basis = mpx->getMultiplexedCCs();
196    auto numOfCharacterClasses = mpx_basis.size();
197//    llvm::outs() << "numOfCharacterClasses:" << numOfCharacterClasses << "\n";
198    StreamSetBuffer * CharClasses = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(numOfCharacterClasses), baseBufferSize, 1);
199
200    kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(mpx_basis), false, cc::BitNumbering::BigEndian);
201    mGrepDriver->makeKernelCall(ccK, {LiteralBitStream}, {CharClasses});
202
203    StreamSetBuffer * newLineBreak = nullptr;
204
205
206    StreamSetBuffer * decompressedCharClasses = nullptr;
207    StreamSetBuffer * u8NoFinalStream = nullptr;
208    StreamSetBuffer * fakeMatchCopiedBits = nullptr;
209
210    if (allCcByteLength) {
211        auto ret = this->generateBitStreamDecompression(idb, {CharClasses});
212        decompressedCharClasses = ret[0];
213        fakeMatchCopiedBits = mPxDriver.addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(8), this->getInputBufferBlocks(idb), 1);
214        u8NoFinalStream = mPxDriver.addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1), this->getInputBufferBlocks(idb), 1);
215
216        Kernel* fakeStreamGeneratorK = mPxDriver.addKernelInstance<FakeStreamGeneratingKernel>(idb, numOfCharacterClasses, std::vector<unsigned>({8, 1}));
217        mPxDriver.makeKernelCall(fakeStreamGeneratorK, {decompressedCharClasses}, {fakeMatchCopiedBits, u8NoFinalStream});
218    } else {
219        re::RE* nonFinalName = re::makeAlt({re::makeByte(0xC2, 0xF4),
220                                            re::makeSeq({re::makeByte(0xE0, 0xF4), re::makeByte(0x80, 0xBF)}),
221                                            re::makeSeq({re::makeByte(0xF0, 0xF4), re::makeByte(0x80, 0xBF), re::makeByte(0x80, 0xBF)})});
222        StreamSetBuffer * compressedU8NoFinalStream = mPxDriver.addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), this->getInputBufferBlocks(idb));
223        kernel::Kernel * u8NoFinalK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, nonFinalName, externalStreamNames, std::vector<cc::Alphabet *>(), cc::BitNumbering::BigEndian);
224        mGrepDriver->makeKernelCall(u8NoFinalK, {LiteralBitStream}, {compressedU8NoFinalStream});
225
226        auto ret = this->generateBitStreamDecompression(idb, {CharClasses, compressedU8NoFinalStream});
227
228        decompressedCharClasses = ret[0];
229        u8NoFinalStream = ret[1];
230
231        fakeMatchCopiedBits = mPxDriver.addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(8), this->getInputBufferBlocks(idb), 1);
232        Kernel* fakeStreamGeneratorK = mPxDriver.addKernelInstance<FakeStreamGeneratingKernel>(idb, numOfCharacterClasses, 8);
233        mPxDriver.makeKernelCall(fakeStreamGeneratorK, {decompressedCharClasses}, {fakeMatchCopiedBits});
234
235    }
236
237
238    newLineBreak = mPxDriver.addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), this->getInputBufferBlocks(idb));
239    kernel::Kernel * lineFeedGrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, transformCCs(mpx.get(), linefeedCC), externalStreamNames, std::vector<cc::Alphabet *>{mpx.get()}, cc::BitNumbering::BigEndian);
240    mGrepDriver->makeKernelCall(lineFeedGrepK, {fakeMatchCopiedBits, decompressedCharClasses}, {newLineBreak});
241
242    externalStreamNames.push_back("UTF8_nonfinal");
243
244    kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[0], externalStreamNames, std::vector<cc::Alphabet *>{mpx.get()}, cc::BitNumbering::BigEndian);
245    mGrepDriver->makeKernelCall(icgrepK, {fakeMatchCopiedBits, u8NoFinalStream, decompressedCharClasses}, {MatchResults});
246    MatchResultsBufs[0] = MatchResults;
247
248
249    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
250    if (mREs.size() > 1) {
251        MergedResults = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
252        kernel::Kernel * streamsMergeK = mGrepDriver->addKernelInstance<kernel::StreamsMerge>(idb, 1, mREs.size());
253        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
254    }
255    StreamSetBuffer * Matches = MergedResults;
256    if (mMoveMatchesToEOL) {
257        StreamSetBuffer * OriginalMatches = Matches;
258        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance<kernel::MatchedLinesKernel>(idb);
259        Matches = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
260        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, newLineBreak}, {Matches});
261    }
262
263    if (MaxCountFlag > 0) {
264        kernel::Kernel * untilK = mGrepDriver->addKernelInstance<kernel::UntilNkernel>(idb);
265        untilK->setInitialArguments({idb->getSize(MaxCountFlag)});
266        StreamSetBuffer * const AllMatches = Matches;
267        Matches = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
268        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
269    }
270
271    return std::pair<StreamSetBuffer *, StreamSetBuffer *>(newLineBreak, Matches);
272};
273
274
275std::pair<parabix::StreamSetBuffer *, parabix::StreamSetBuffer *>
276LZParabixGrepGenerator::grepPipeline(std::vector<re::RE *> &REs, bool swizzledDecompression) {
277
278    this->initREs(REs);
279    auto mGrepDriver = &mPxDriver;
280
281    auto & idb = mGrepDriver->getBuilder();
282    // TODO: until we automate stream buffer sizing, use this calculation to determine how large our matches buffer needs to be.
283    const unsigned baseBufferSize = this->getInputBufferBlocks(idb);
284    int MaxCountFlag = 0;
285
286    //  Regular Expression Processing and Analysis Phase
287    const auto nREs = mREs.size();
288
289    std::vector<StreamSetBuffer *> MatchResultsBufs(nREs);
290
291
292    this->generateBlockData(idb);
293    StreamSetBuffer * const LiteralBitStream = this->extractLiteralBitStream(idb);
294//    auto compressedLineBreakStream = this->linefeedStreamFromDecompressedBits(LiteralBitStream);
295
296
297    StreamSetBuffer * decompressedBasisBits = nullptr;
298    if (swizzledDecompression) {
299        decompressedBasisBits = this->generateSwizzledBitStreamDecompression(idb, LiteralBitStream);
300    } else {
301        auto ret = this->generateBitStreamDecompression(idb, {LiteralBitStream});
302        decompressedBasisBits = ret[0];
303    }
304
305    StreamSetBuffer * LineBreakStream = this->linefeedStreamFromDecompressedBits(decompressedBasisBits);
306
307    std::map<std::string, StreamSetBuffer *> propertyStream;
308
309    for(unsigned i = 0; i < nREs; ++i) {
310        std::vector<std::string> externalStreamNames;
311        std::vector<StreamSetBuffer *> icgrepInputSets = {decompressedBasisBits};
312
313        std::set<re::Name *> UnicodeProperties;
314
315        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
316        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, mREs[i], externalStreamNames, std::vector<cc::Alphabet *>(), cc::BitNumbering::BigEndian);
317        mGrepDriver->makeKernelCall(icgrepK, icgrepInputSets, {MatchResults});
318        MatchResultsBufs[i] = MatchResults;
319    }
320
321    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
322    if (mREs.size() > 1) {
323        MergedResults = mGrepDriver->addBuffer<StaticBuffer>(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<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
332        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
333    }
334
335    if (MaxCountFlag > 0) {
336        kernel::Kernel * untilK = mGrepDriver->addKernelInstance<kernel::UntilNkernel>(idb);
337        untilK->setInitialArguments({idb->getSize(MaxCountFlag)});
338        StreamSetBuffer * const AllMatches = Matches;
339        Matches = mGrepDriver->addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
340        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
341    }
342
343    return std::pair<StreamSetBuffer *, StreamSetBuffer *>(LineBreakStream, Matches);
344}
345
346parabix::StreamSetBuffer *
347LZParabixGrepGenerator::linefeedStreamFromDecompressedBits(parabix::StreamSetBuffer *decompressedBasisBits) {
348    auto & idb = mPxDriver.getBuilder();
349    const unsigned baseBufferSize = this->getInputBufferBlocks(idb);
350    StreamSetBuffer * LineFeedStream = mPxDriver.addBuffer<StaticBuffer>(idb, idb->getStreamSetTy(1, 1), baseBufferSize);
351    kernel::Kernel * linefeedK = mPxDriver.addKernelInstance<kernel::LineFeedKernelBuilder>(idb, Binding{idb->getStreamSetTy(8), "basis", FixedRate(), Principal()}, cc::BitNumbering::BigEndian);
352    mPxDriver.makeKernelCall(linefeedK, {decompressedBasisBits}, {LineFeedStream});
353    return LineFeedStream;
354}
355
356CountOnlyGrepMainFunctionType LZParabixGrepGenerator::getCountOnlyGrepMainFunction() {
357    return reinterpret_cast<CountOnlyGrepMainFunctionType>(mPxDriver.getMain());
358}
Note: See TracBrowser for help on using the repository browser.