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

Last change on this file since 5864 was 5856, checked in by nmedfort, 18 months ago

Revised pipeline structure to better control I/O rates

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