source: icGREP/icgrep-devel/icgrep/u8u16.cpp @ 5436

Last change on this file since 5436 was 5436, checked in by nmedfort, 2 years ago

Continued refactoring work. PabloKernel? now abstract base type with a 'generatePabloMethod' hook to generate Pablo code.

File size: 25.1 KB
RevLine 
[5005]1/*
[5036]2 *  Copyright (c) 2016 International Characters.
[5005]3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
[5267]7#include <IR_Gen/idisa_target.h>                   // for GetIDISA_Builder
8#include <cc/cc_compiler.h>                        // for CC_Compiler
9#include <kernels/deletion.h>                      // for DeletionKernel
[5355]10#include <kernels/swizzle.h>                      // for DeletionKernel
[5429]11#include <kernels/source_kernel.h>
[5267]12#include <kernels/p2s_kernel.h>                    // for P2S16KernelWithCom...
13#include <kernels/s2p_kernel.h>                    // for S2PKernel
14#include <kernels/stdout_kernel.h>                 // for StdOutKernel
15#include <llvm/ExecutionEngine/ExecutionEngine.h>  // for ExecutionEngine
16#include <llvm/IR/Function.h>                      // for Function, Function...
17#include <llvm/IR/Module.h>                        // for Module
18#include <llvm/IR/Verifier.h>                      // for verifyModule
19#include <llvm/Support/CommandLine.h>              // for ParseCommandLineOp...
20#include <llvm/Support/Debug.h>                    // for dbgs
21#include <pablo/pablo_kernel.h>                    // for PabloKernel
22#include <pablo/pablo_toolchain.h>                 // for pablo_function_passes
[5436]23#include <kernels/kernel_builder.h>
[5267]24#include <pablo/pe_zeroes.h>
[5425]25#include <toolchain/toolchain.h>
[5267]26#include "kernels/streamset.h"                     // for CircularBuffer
27#include "llvm/ADT/StringRef.h"                    // for StringRef
28#include "llvm/IR/CallingConv.h"                   // for ::C
29#include "llvm/IR/DerivedTypes.h"                  // for ArrayType, Pointer...
30#include "llvm/IR/LLVMContext.h"                   // for LLVMContext
31#include "llvm/IR/Value.h"                         // for Value
32#include "llvm/Support/Compiler.h"                 // for LLVM_UNLIKELY
33#include <pablo/builder.hpp>                       // for PabloBuilder
[5418]34#include <boost/interprocess/anonymous_shared_memory.hpp>
35#include <boost/interprocess/mapped_region.hpp>
[5005]36#include <iostream>
37
[5267]38using namespace pablo;
39using namespace kernel;
40using namespace parabix;
41using namespace llvm;
[5007]42
[5267]43static cl::OptionCategory u8u16Options("u8u16 Options", "Transcoding control options.");
[5306]44static cl::opt<std::string> inputFile(cl::Positional, cl::desc("<input file>"), cl::Required, cl::cat(u8u16Options));
45static cl::opt<std::string> outputFile(cl::Positional, cl::desc("<output file>"),  cl::Required, cl::cat(u8u16Options));
[5355]46static cl::opt<bool> enableAVXdel("enable-AVX-deletion", cl::desc("Enable AVX2 deletion algorithms."), cl::cat(u8u16Options));
[5191]47static cl::opt<bool> mMapBuffering("mmap-buffering", cl::desc("Enable mmap buffering."), cl::cat(u8u16Options));
48static cl::opt<bool> memAlignBuffering("memalign-buffering", cl::desc("Enable posix_memalign buffering."), cl::cat(u8u16Options));
[5154]49
[5436]50class U8U16Kernel final: public pablo::PabloKernel {
51public:
52    U8U16Kernel(const std::unique_ptr<kernel::KernelBuilder> & b);
53    bool isCachable() const override { return true; }
54    bool moduleIDisSignature() const override { return true; }
55    void generatePabloMethod() override;
56};
[5005]57
[5436]58U8U16Kernel::U8U16Kernel(const std::unique_ptr<kernel::KernelBuilder> & b)
59: PabloKernel(b, "u8u16",
60{Binding{b->getStreamSetTy(8, 1), "u8bit"}},
61{Binding{b->getStreamSetTy(16, 1), "u16bit"}, Binding{b->getStreamSetTy(1, 1), "delMask"}, Binding{b->getStreamSetTy(1, 1), "errMask"}}) {
[5414]62
[5436]63}
[5435]64
[5436]65void U8U16Kernel::generatePabloMethod() {
66
[5005]67    //  input: 8 basis bit streams
[5436]68
69    const auto u8bitSet = getInputStreamVar("u8bit");
70
[5005]71    //  output: 16 u8-indexed streams, + delmask stream + error stream
[5436]72
73    cc::CC_Compiler ccc(this, u8bitSet);
74
[5202]75    PabloBuilder & main = ccc.getBuilder();
76    const auto u8_bits = ccc.getBasisBits();
[5436]77
[5267]78    Zeroes * zeroes = main.createZeroes();
[5202]79
[5005]80    // Outputs
[5202]81    Var * u16_hi[8];
82    for (int i = 0; i < 8; ++i) {
83        u16_hi[i] = main.createVar("u16_hi" + std::to_string(i), zeroes);
84    }
85    Var * u16_lo[8];
86    for (int i = 0; i < 8; ++i) {
87        u16_lo[i] = main.createVar("u16_lo" + std::to_string(i), zeroes);
88    }
89    Var * delmask = main.createVar("delmask", zeroes);
90    Var * error_mask = main.createVar("error_mask", zeroes);
91
92    // The logic for processing non-ASCII bytes will be embedded within an if-hierarchy.
[5005]93    PabloAST * nonASCII = ccc.compileCC(re::makeCC(0x80, 0xFF));
[5436]94
[5005]95    // Builder for the if statement handling all non-ASCII logic
[5202]96    PabloBuilder nAb = PabloBuilder::Create(main);
[5005]97    // Bits 3 through 7 of a 2-byte prefix are data bits, needed to
[5436]98    // produce the UTF-16 code unit data ...,
[5005]99    PabloAST * bit3a1 = nAb.createAdvance(u8_bits[3], 1);
100    PabloAST * bit4a1 = nAb.createAdvance(u8_bits[4], 1);
101    PabloAST * bit5a1 = nAb.createAdvance(u8_bits[5], 1);
102    PabloAST * bit6a1 = nAb.createAdvance(u8_bits[6], 1);
103    PabloAST * bit7a1 = nAb.createAdvance(u8_bits[7], 1);
[5436]104
[5005]105    // Entry condition for 3 or 4 byte sequences: we have a prefix byte in the range 0xE0-0xFF.
106    PabloAST * pfx34 = ccc.compileCC(re::makeCC(0xE0, 0xFF), nAb);
107    // Builder for the if statement handling all logic for 3- and 4-byte sequences.
108    PabloBuilder p34b = PabloBuilder::Create(nAb);
109    // Bits 4 through 7 of a 3-byte prefix are data bits.  They must be moved
110    // to the final position of the 3-byte sequence.
111    PabloAST * bit2a1 = p34b.createAdvance(u8_bits[2], 1);
112    PabloAST * bit4a2 = p34b.createAdvance(bit4a1, 1);
113    PabloAST * bit5a2 = p34b.createAdvance(bit5a1, 1);
114    PabloAST * bit6a2 = p34b.createAdvance(bit6a1, 1);
115    PabloAST * bit7a2 = p34b.createAdvance(bit7a1, 1);
[5202]116
117    Var * const u8scope32 = nAb.createVar("u8scope32", zeroes);
118    Var * const u8scope33 = nAb.createVar("u8scope33", zeroes);
119    Var * const u8scope44 = nAb.createVar("u8scope44", zeroes);
120
[5005]121    //
122    // Logic for 4-byte UTF-8 sequences
123    //
124    // Entry condition  or 4 byte sequences: we have a prefix byte in the range 0xF0-0xFF.
125    PabloAST * pfx4 = ccc.compileCC(re::makeCC(0xF0, 0xFF), p34b);
126    // Builder for the if statement handling all logic for 4-byte sequences only.
127    PabloBuilder p4b = PabloBuilder::Create(p34b);
128    // Illegal 4-byte sequences
129    PabloAST * F0 = ccc.compileCC(re::makeCC(0xF0), p4b);
130    PabloAST * F4 = ccc.compileCC(re::makeCC(0xF4), p4b);
131    PabloAST * F0_err = p4b.createAnd(p4b.createAdvance(F0, 1), ccc.compileCC(re::makeCC(0x80, 0x8F), p4b));
132    PabloAST * F4_err = p4b.createAnd(p4b.createAdvance(F4, 1), ccc.compileCC(re::makeCC(0x90, 0xBF), p4b));
133    PabloAST * F5_FF = ccc.compileCC(re::makeCC(0xF5, 0xFF), p4b);
[5202]134
135    Var * FX_err = p34b.createVar("FX_err", zeroes);
136    p4b.createAssign(FX_err, p4b.createOr(F5_FF, p4b.createOr(F0_err, F4_err)));
[5005]137    //
138    // 4-byte prefixes have a scope that extends over the next 3 bytes.
[5202]139
140    Var * u8scope42 = p34b.createVar("u8scope42", zeroes);
141    Var * u8scope43 = p34b.createVar("u8scope43", zeroes);
142
143    p4b.createAssign(u8scope42, p4b.createAdvance(pfx4, 1));
144    p4b.createAssign(u8scope43, p4b.createAdvance(u8scope42, 1));
145    p4b.createAssign(u8scope44, p4b.createAdvance(u8scope43, 1));
[5005]146    //
[5436]147
[5005]148    //  From the 4-byte sequence 11110abc 10defghi 10jklmno 10pqrstu,
149    //  we must calculate the value abcde - 1 to produce the bit values
150    //  for u16_hi6, hi7, lo0, lo1 at the scope43 position.
[5202]151    Var * s43_lo0 = nAb.createVar("scope43_lo0", zeroes);
152    Var * s43_lo1 = nAb.createVar("scope43_lo1", zeroes);
153    Var * s43_hi6 = nAb.createVar("scope43_hi6", zeroes);
154    Var * s43_hi7 = nAb.createVar("scope43_hi7", zeroes);
155
156    Var * s43_lo2 = main.createVar("scope43_lo2", zeroes);
157    Var * s43_lo3 = main.createVar("scope43_lo3", zeroes);
158    Var * s43_lo4 = main.createVar("scope43_lo4", zeroes);
159    Var * s43_lo5 = main.createVar("scope43_lo5", zeroes);
160    Var * s43_lo6 = main.createVar("scope43_lo6", zeroes);
161    Var * s43_lo7 = main.createVar("scope43_lo7", zeroes);
162
163    p4b.createAssign(s43_lo1, p4b.createAnd(u8scope43, p4b.createNot(bit3a1)));           // e - 1
164    p4b.createAssign(s43_lo0, p4b.createAnd(u8scope43, p4b.createXor(bit2a1, s43_lo1)));  // d - borrow
[5005]165    PabloAST * brw1 = p4b.createAnd(s43_lo1, p4b.createNot(bit2a1));
[5202]166    p4b.createAssign(s43_hi7, p4b.createAnd(u8scope43, p4b.createXor(bit7a2, brw1)));     // c - borrow
[5005]167    PabloAST * brw2 = p4b.createAnd(brw1, p4b.createNot(bit7a2));
[5202]168    p4b.createAssign(s43_hi6, p4b.createAnd(u8scope43, p4b.createXor(bit6a2, brw2)));     // b - borrow
[5005]169    //
[5202]170    p4b.createAssign(s43_lo2, p4b.createAnd(u8scope43, bit4a1));
171    p4b.createAssign(s43_lo3, p4b.createAnd(u8scope43, bit5a1));
172    p4b.createAssign(s43_lo4, p4b.createAnd(u8scope43, bit6a1));
173    p4b.createAssign(s43_lo5, p4b.createAnd(u8scope43, bit7a1));
174    p4b.createAssign(s43_lo6, p4b.createAnd(u8scope43, u8_bits[2]));
175    p4b.createAssign(s43_lo7, p4b.createAnd(u8scope43, u8_bits[3]));
[5005]176    //
177    //
[5202]178    p34b.createIf(pfx4, p4b);
[5005]179    //
180    // Combined logic for 3 and 4 byte sequences
181    //
182    PabloAST * pfx3 = ccc.compileCC(re::makeCC(0xE0, 0xEF), p34b);
183
[5202]184    p34b.createAssign(u8scope32, p34b.createAdvance(pfx3, 1));
185    p34b.createAssign(u8scope33, p34b.createAdvance(u8scope32, 1));
186
[5005]187    // Illegal 3-byte sequences
188    PabloAST * E0 = ccc.compileCC(re::makeCC(0xE0), p34b);
189    PabloAST * ED = ccc.compileCC(re::makeCC(0xED), p34b);
190    PabloAST * E0_err = p34b.createAnd(p34b.createAdvance(E0, 1), ccc.compileCC(re::makeCC(0x80, 0x9F), p34b));
191    PabloAST * ED_err = p34b.createAnd(p34b.createAdvance(ED, 1), ccc.compileCC(re::makeCC(0xA0, 0xBF), p34b));
[5202]192    Var * EX_FX_err = nAb.createVar("EX_FX_err", zeroes);
193
194    p34b.createAssign(EX_FX_err, p34b.createOr(p34b.createOr(E0_err, ED_err), FX_err));
[5005]195    // Two surrogate UTF-16 units are computed at the 3rd and 4th positions of 4-byte sequences.
196    PabloAST * surrogate = p34b.createOr(u8scope43, u8scope44);
[5436]197
[5202]198    Var * p34del = nAb.createVar("p34del", zeroes);
199    p34b.createAssign(p34del, p34b.createOr(u8scope32, u8scope42));
[5005]200
201
202    // The high 5 bits of the UTF-16 code unit are only nonzero for 3 and 4-byte
203    // UTF-8 sequences.
[5202]204    p34b.createAssign(u16_hi[0], p34b.createOr(p34b.createAnd(u8scope33, bit4a2), surrogate));
205    p34b.createAssign(u16_hi[1], p34b.createOr(p34b.createAnd(u8scope33, bit5a2), surrogate));
206    p34b.createAssign(u16_hi[2], p34b.createAnd(u8scope33, bit6a2));
207    p34b.createAssign(u16_hi[3], p34b.createOr(p34b.createAnd(u8scope33, bit7a2), surrogate));
208    p34b.createAssign(u16_hi[4], p34b.createOr(p34b.createAnd(u8scope33, bit2a1), surrogate));
[5436]209
[5005]210    //
[5202]211    nAb.createIf(pfx34, p34b);
[5005]212    //
213    // Combined logic for 2, 3 and 4 byte sequences
214    //
[5202]215
216    Var * u8lastscope = main.createVar("u8lastscope", zeroes);
217
[5005]218    PabloAST * pfx2 = ccc.compileCC(re::makeCC(0xC0, 0xDF), nAb);
219    PabloAST * u8scope22 = nAb.createAdvance(pfx2, 1);
[5202]220    nAb.createAssign(u8lastscope, nAb.createOr(u8scope22, nAb.createOr(u8scope33, u8scope44)));
[5005]221    PabloAST * u8anyscope = nAb.createOr(u8lastscope, p34del);
222
223    PabloAST * C0_C1_err = ccc.compileCC(re::makeCC(0xC0, 0xC1), nAb);
224    PabloAST * scope_suffix_mismatch = nAb.createXor(u8anyscope, ccc.compileCC(re::makeCC(0x80, 0xBF), nAb));
[5202]225    nAb.createAssign(error_mask, nAb.createOr(scope_suffix_mismatch, nAb.createOr(C0_C1_err, EX_FX_err)));
226    nAb.createAssign(delmask, nAb.createOr(p34del, ccc.compileCC(re::makeCC(0xC0, 0xFF), nAb)));
[5436]227
[5005]228    // The low 3 bits of the high byte of the UTF-16 code unit as well as the high bit of the
229    // low byte are only nonzero for 2, 3 and 4 byte sequences.
[5202]230    nAb.createAssign(u16_hi[5], nAb.createOr(nAb.createAnd(u8lastscope, bit3a1), u8scope44));
231    nAb.createAssign(u16_hi[6], nAb.createOr(nAb.createAnd(u8lastscope, bit4a1), s43_hi6));
232    nAb.createAssign(u16_hi[7], nAb.createOr(nAb.createAnd(u8lastscope, bit5a1), s43_hi7));
233    nAb.createAssign(u16_lo[0], nAb.createOr(nAb.createAnd(u8lastscope, bit6a1), s43_lo0));
[5005]234
[5202]235    Var * p234_lo1 = main.createVar("p234_lo1", zeroes);
236
237    nAb.createAssign(p234_lo1, nAb.createOr(nAb.createAnd(u8lastscope, bit7a1), s43_lo1));
238
239    main.createIf(nonASCII, nAb);
[5005]240    //
241    //
242    PabloAST * ASCII = ccc.compileCC(re::makeCC(0x0, 0x7F));
[5202]243    PabloAST * last_byte = main.createOr(ASCII, u8lastscope);
244    main.createAssign(u16_lo[1], main.createOr(main.createAnd(ASCII, u8_bits[1]), p234_lo1));
245    main.createAssign(u16_lo[2], main.createOr(main.createAnd(last_byte, u8_bits[2]), s43_lo2));
246    main.createAssign(u16_lo[3], main.createOr(main.createAnd(last_byte, u8_bits[3]), s43_lo3));
247    main.createAssign(u16_lo[4], main.createOr(main.createAnd(last_byte, u8_bits[4]), s43_lo4));
248    main.createAssign(u16_lo[5], main.createOr(main.createAnd(last_byte, u8_bits[5]), s43_lo5));
249    main.createAssign(u16_lo[6], main.createOr(main.createAnd(last_byte, u8_bits[6]), s43_lo6));
250    main.createAssign(u16_lo[7], main.createOr(main.createAnd(last_byte, u8_bits[7]), s43_lo7));
[5436]251
252    Var * output = getOutputStreamVar("u16bit");
253    Var * delmask_out = getOutputStreamVar("delMask");
254    Var * error_mask_out = getOutputStreamVar("errMask");
255
[5005]256    for (unsigned i = 0; i < 8; i++) {
[5202]257        main.createAssign(main.createExtract(output, i), u16_hi[i]);
[5005]258    }
[5202]259    for (unsigned i = 0; i < 8; i++) {
260        main.createAssign(main.createExtract(output, i + 8), u16_lo[i]);
261    }
[5217]262    main.createAssign(main.createExtract(delmask_out, main.getInteger(0)), delmask);
263    main.createAssign(main.createExtract(error_mask_out,  main.getInteger(0)), error_mask);
[5005]264}
265
[5395]266void u8u16PipelineAVX2Gen(ParabixDriver & pxDriver) {
[5005]267
[5435]268    auto & iBuilder = pxDriver.getBuilder();
[5395]269    Module * mod = iBuilder->getModule();
[5154]270    const unsigned segmentSize = codegen::SegmentSize;
[5301]271    const unsigned bufferSegments = codegen::ThreadNum+1;
[5362]272
[5217]273    assert (iBuilder);
[5154]274
[5254]275    Type * const voidTy = iBuilder->getVoidTy();
276    Type * const bitBlockType = iBuilder->getBitBlockType();
277    Type * const outputType = ArrayType::get(ArrayType::get(bitBlockType, 16), 1)->getPointerTo();
[5418]278
279    Function * const main = cast<Function>(mod->getOrInsertFunction("Main", voidTy, iBuilder->getInt32Ty(), outputType, nullptr));
[5254]280    main->setCallingConv(CallingConv::C);
281    Function::arg_iterator args = main->arg_begin();
[5418]282
283    Value * const fileDecriptor = &*(args++);
284    fileDecriptor->setName("fileDecriptor");
[5254]285    Value * const outputStream = &*(args++);
286    outputStream->setName("outputStream");
287
[5409]288    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main,0));
289   
[5355]290    // File data from mmap
[5429]291    StreamSetBuffer * ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
[5409]292   
[5435]293    Kernel * mmapK = pxDriver.addKernelInstance(make_unique<MMapSourceKernel>(iBuilder, segmentSize));
[5418]294    mmapK->setInitialArguments({fileDecriptor});
[5414]295    pxDriver.makeKernelCall(mmapK, {}, {ByteStream});
[5409]296   
[5355]297    // Transposed bits from s2p
[5409]298    StreamSetBuffer * BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), segmentSize * bufferSegments));
299   
[5435]300    Kernel * s2pk = pxDriver.addKernelInstance(make_unique<S2PKernel>(iBuilder));
[5414]301    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
[5355]302   
303    // Calculate UTF-16 data bits through bitwise logic on u8-indexed streams.
[5409]304    StreamSetBuffer * U8u16Bits = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(16), segmentSize * bufferSegments));
305    StreamSetBuffer * DelMask = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(), segmentSize * bufferSegments));
306    StreamSetBuffer * ErrorMask = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(), segmentSize * bufferSegments));
307   
[5436]308    Kernel * u8u16k = pxDriver.addKernelInstance(make_unique<U8U16Kernel>(iBuilder));
[5414]309    pxDriver.makeKernelCall(u8u16k, {BasisBits}, {U8u16Bits, DelMask, ErrorMask});
[5154]310   
[5355]311    // Apply a deletion algorithm to discard all but the final position of the UTF-8
[5362]312    // sequences for each UTF-16 code unit. Swizzle the results.
[5409]313    StreamSetBuffer * SwizzleFields0 = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(4), segmentSize * bufferSegments));
314    StreamSetBuffer * SwizzleFields1 = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(4), segmentSize * bufferSegments));
315    StreamSetBuffer * SwizzleFields2 = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(4), segmentSize * bufferSegments));
316    StreamSetBuffer * SwizzleFields3 = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(4), segmentSize * bufferSegments));
317    StreamSetBuffer * DeletionCounts = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(), segmentSize * bufferSegments));
318   
[5435]319    Kernel * delK = pxDriver.addKernelInstance(make_unique<DeleteByPEXTkernel>(iBuilder, 64, 16, true));
[5414]320    pxDriver.makeKernelCall(delK, {U8u16Bits, DelMask}, {SwizzleFields0, SwizzleFields1, SwizzleFields2, SwizzleFields3, DeletionCounts});
[5409]321   
[5355]322    //  Produce fully compressed swizzled UTF-16 bit streams
[5409]323    StreamSetBuffer * u16Swizzle0 = pxDriver.addBuffer(make_unique<SwizzledCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), segmentSize * (bufferSegments+2), 1));
324    StreamSetBuffer * u16Swizzle1 = pxDriver.addBuffer(make_unique<SwizzledCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), segmentSize * (bufferSegments+2), 1));
325    StreamSetBuffer * u16Swizzle2 = pxDriver.addBuffer(make_unique<SwizzledCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), segmentSize * (bufferSegments+2), 1));
326    StreamSetBuffer * u16Swizzle3 = pxDriver.addBuffer(make_unique<SwizzledCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), segmentSize * (bufferSegments+2), 1));
327   
[5435]328    Kernel * compressK = pxDriver.addKernelInstance(make_unique<SwizzledBitstreamCompressByCount>(iBuilder, 16));
[5414]329    pxDriver.makeKernelCall(compressK, {DeletionCounts, SwizzleFields0, SwizzleFields1, SwizzleFields2, SwizzleFields3},
[5409]330                           {u16Swizzle0, u16Swizzle1, u16Swizzle2, u16Swizzle3});
331   
[5355]332    // Produce unswizzled UTF-16 bit streams
[5409]333    StreamSetBuffer * u16bits = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(16), segmentSize * bufferSegments));
[5355]334   
[5435]335    Kernel * unSwizzleK = pxDriver.addKernelInstance(make_unique<SwizzleGenerator>(iBuilder, 16, 1, 4));
[5414]336    pxDriver.makeKernelCall(unSwizzleK, {u16Swizzle0, u16Swizzle1, u16Swizzle2, u16Swizzle3}, {u16bits});
[5409]337   
[5435]338    Kernel * p2sk = pxDriver.addKernelInstance(make_unique<P2S16Kernel>(iBuilder));
[5409]339   
[5435]340    Kernel * outK = pxDriver.addKernelInstance(make_unique<FileSink>(iBuilder, 16));
[5409]341    Value * fName = iBuilder->CreatePointerCast(iBuilder->CreateGlobalString(outputFile.c_str()), iBuilder->getInt8PtrTy());
[5414]342    outK->setInitialArguments({fName});
343       
[5409]344    // Different choices for the output buffer depending on chosen option.
345    StreamSetBuffer * U16out = nullptr;
[5355]346    if (mMapBuffering || memAlignBuffering) {
[5429]347        U16out = pxDriver.addExternalBuffer(make_unique<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 16), outputStream));
[5355]348    } else {
[5409]349        U16out = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 16), segmentSize * bufferSegments));
[5355]350    }
[5414]351    pxDriver.makeKernelCall(p2sk, {u16bits}, {U16out});
352    pxDriver.makeKernelCall(outK, {U16out}, {});
[5355]353   
[5395]354    pxDriver.generatePipelineIR();
[5409]355   
[5355]356    iBuilder->CreateRetVoid();
[5409]357   
[5395]358    pxDriver.linkAndFinalize();
[5355]359}
360
[5395]361void u8u16PipelineGen(ParabixDriver & pxDriver) {
[5355]362   
[5435]363    auto & iBuilder = pxDriver.getBuilder();
[5395]364    Module * mod = iBuilder->getModule();
365   
[5355]366    const unsigned segmentSize = codegen::SegmentSize;
367    const unsigned bufferSegments = codegen::ThreadNum+1;
368    Type * const voidTy = iBuilder->getVoidTy();
369    Type * const bitBlockType = iBuilder->getBitBlockType();
370    Type * const outputType = ArrayType::get(ArrayType::get(bitBlockType, 16), 1)->getPointerTo();
371   
[5418]372    Function * const main = cast<Function>(mod->getOrInsertFunction("Main", voidTy, iBuilder->getInt32Ty(), outputType, nullptr));
[5355]373    main->setCallingConv(CallingConv::C);
374    Function::arg_iterator args = main->arg_begin();
375   
[5418]376    Value * const fileDecriptor = &*(args++);
377    fileDecriptor->setName("fileDecriptor");
[5355]378    Value * const outputStream = &*(args++);
379    outputStream->setName("outputStream");
[5418]380
[5409]381    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main,0));
382
383    // File data from mmap
[5429]384    StreamSetBuffer * ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
[5355]385   
[5435]386    Kernel * mmapK = pxDriver.addKernelInstance(make_unique<MMapSourceKernel>(iBuilder, segmentSize));
[5418]387    mmapK->setInitialArguments({fileDecriptor});
[5414]388    pxDriver.makeKernelCall(mmapK, {}, {ByteStream});
[5355]389   
[5409]390    // Transposed bits from s2p
391    StreamSetBuffer * BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), segmentSize * bufferSegments));
[5355]392   
[5435]393    Kernel * s2pk = pxDriver.addKernelInstance(make_unique<S2PKernel>(iBuilder));
[5414]394    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
395   
[5409]396    // Calculate UTF-16 data bits through bitwise logic on u8-indexed streams.
397    StreamSetBuffer * U8u16Bits = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(16), segmentSize * bufferSegments));
398    StreamSetBuffer * DelMask = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(), segmentSize * bufferSegments));
399    StreamSetBuffer * ErrorMask = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(), segmentSize * bufferSegments));
[5355]400   
[5436]401    Kernel * u8u16k = pxDriver.addKernelInstance(make_unique<U8U16Kernel>(iBuilder));
[5414]402    pxDriver.makeKernelCall(u8u16k, {BasisBits}, {U8u16Bits, DelMask, ErrorMask});
[5355]403   
[5409]404    StreamSetBuffer * U16Bits = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(16), segmentSize * bufferSegments));
405   
406    StreamSetBuffer * DeletionCounts = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(), segmentSize * bufferSegments));
407
[5435]408    Kernel * delK = pxDriver.addKernelInstance(make_unique<DeletionKernel>(iBuilder, iBuilder->getBitBlockWidth()/16, 16));
[5414]409    pxDriver.makeKernelCall(delK, {U8u16Bits, DelMask}, {U16Bits, DeletionCounts});
[5355]410   
[5435]411    Kernel * p2sk = pxDriver.addKernelInstance(make_unique<P2S16KernelWithCompressedOutput>(iBuilder));
[5355]412   
[5435]413    Kernel * outK = pxDriver.addKernelInstance(make_unique<FileSink>(iBuilder, 16));
[5409]414    Value * fName = iBuilder->CreatePointerCast(iBuilder->CreateGlobalString(outputFile.c_str()), iBuilder->getInt8PtrTy());
[5414]415    outK->setInitialArguments({fName});
[5355]416   
[5409]417    // Different choices for the output buffer depending on chosen option.
418    StreamSetBuffer * U16out = nullptr;
[5191]419    if (mMapBuffering || memAlignBuffering) {
[5429]420        U16out = pxDriver.addExternalBuffer(make_unique<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 16), outputStream));
[5217]421    } else {
[5409]422        U16out = pxDriver.addBuffer(make_unique<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 16), segmentSize * bufferSegments, 1 /*overflow block*/));
[5191]423    }
[5414]424    pxDriver.makeKernelCall(p2sk, {U16Bits, DeletionCounts}, {U16out});
425    pxDriver.makeKernelCall(outK, {U16out}, {});
[5355]426   
[5395]427    pxDriver.generatePipelineIR();
[5355]428   
[5071]429    iBuilder->CreateRetVoid();
[5401]430
[5395]431    pxDriver.linkAndFinalize();
[5071]432}
433
[5418]434typedef void (*u8u16FunctionType)(uint32_t fd, char * output_data);
[5005]435
[5425]436u8u16FunctionType u8u16CodeGen() {
437    ParabixDriver pxDriver("u8u16");
[5395]438    if (enableAVXdel && AVX2_available() && codegen::BlockSize==256) {
439        u8u16PipelineAVX2Gen(pxDriver);
[5425]440    } else{
[5395]441        u8u16PipelineGen(pxDriver);
442    }
[5425]443    return reinterpret_cast<u8u16FunctionType>(pxDriver.getPointerToMain());
[5005]444}
445
[5418]446size_t file_size(const int fd) {
447    struct stat st;
448    if (LLVM_UNLIKELY(fstat(fd, &st) != 0)) {
449        st.st_size = 0;
450    }
451    return st.st_size;
452}
453
[5007]454void u8u16(u8u16FunctionType fn_ptr, const std::string & fileName) {
[5418]455    const int fd = open(fileName.c_str(), O_RDONLY);
456    if (LLVM_UNLIKELY(fd == -1)) {
457        std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
[5005]458    } else {
[5418]459        const auto fileSize = file_size(fd);
460        if (mMapBuffering) {
461            boost::interprocess::mapped_region outputBuffer(boost::interprocess::anonymous_shared_memory(2 * fileSize));
462            outputBuffer.advise(boost::interprocess::mapped_region::advice_willneed);
463            outputBuffer.advise(boost::interprocess::mapped_region::advice_sequential);
464            fn_ptr(fd, static_cast<char*>(outputBuffer.get_address()));
465        } else if (memAlignBuffering) {
466            char * outputBuffer;
467            const auto r = posix_memalign(reinterpret_cast<void **>(&outputBuffer), 32, 2 * fileSize);
468            if (LLVM_UNLIKELY(r != 0)) {
469                throw std::runtime_error("posix_memalign failed with return code " + std::to_string(r));
470            }
471            fn_ptr(fd, outputBuffer);
472            free(reinterpret_cast<void *>(outputBuffer));
473        } else { /* No external output buffer */
474            fn_ptr(fd, nullptr);
[5234]475        }
[5418]476        close(fd);
[5191]477    }
[5005]478}
479
480int main(int argc, char *argv[]) {
[5373]481    AddParabixVersionPrinter();
[5036]482    cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *>{&u8u16Options, pablo::pablo_toolchain_flags(), codegen::codegen_flags()});
[5005]483    cl::ParseCommandLineOptions(argc, argv);
[5402]484    u8u16(u8u16CodeGen(), inputFile);
[5005]485    return 0;
486}
487
488                       
Note: See TracBrowser for help on using the repository browser.