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

Last change on this file since 5356 was 5355, checked in by cameron, 2 years ago

Swizzled bitstream deletion and -enable-AVX-deletion in u8u16

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