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

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

Parabix driver can take ownership of kernelbuilder instances; uniquify mmap kernel name

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