source: icGREP/icgrep-devel/icgrep/kernels/radix64.cpp @ 5402

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

Moved toolchain and object_cache to kernels directory. Continued work on providing input consumed information.

File size: 26.2 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 */
5#include "radix64.h"
6#include <kernels/streamset.h>
7#include <IR_Gen/idisa_builder.h>
8#include <llvm/IR/Module.h>
9#include <llvm/Support/raw_ostream.h>
10
11using namespace llvm;
12
13namespace kernel {
14
15// This kernel produces an expanded input stream by duplicating every third byte.
16// It is implemented using SIMD shufflevector operations.  With 16-byte registers,
17// a single shufflevector operation produces 16 bytes of output data from the
18// 12 bytes of input data.   With 32-byte registers, 32 bytes of output data are
19// produced from 24 bytes of input data.
20//
21// Using aligned SIMD loads, an inner loop processes three registers full of input
22// data (i.e., three BytePacks) to produce four registers full of output.   This is
23// a 3 step process.
24// Step 1:  Load input_pack0, apply the shuffle operation to produce output_pack0.
25//          At this point 3/4 of the data in input_pack0 has been processed.
26// Step 2:  Load input_pack1, apply a shuffle operation to use the remaining
27//          1/4 of input_pack0 and 1/2 of input_pack1 to produce output_pack1.
28//          At this point 1/2 of the data in input_pack1 has been processed.
29// Step 3:  Load input_pack2, apply a shuffle operation to use the remaining 1/2
30//          of input_pack1 and 1/4 of input_pack2 to produce output_pack2.
31//          Then apply a further shuffle opertaion to use the remaining 3/4 of
32//          input_pack2 to produce output_pack3.
33
34// The doSegment method processes input in terms of tripleBlocks, 3 blocks of input,
35// producing 4 blocks of output.   Unless less than one tripleBlock remains, the
36// doSegment method always processes an integral number of tripleBlocks as a logical
37// segment.  Both input and output buffers are hence maintained at block boundaries,
38// with the input data completely processed for each tripleBlock.
39//
40// The pipeline must guarantee that the doSegment method is called with the
41// a continous buffer for the full segment (number of blocks).
42
43void expand3_4Kernel::generateDoSegmentMethod(Value *doFinal, const std::vector<Value *> &) {
44
45    BasicBlock * expand2_3entry = iBuilder->GetInsertBlock();
46    BasicBlock * expand_3_4_loop = CreateBasicBlock("expand_3_4_loop");
47    BasicBlock * expand3_4_loop_exit = CreateBasicBlock("expand3_4_loop_exit");
48    BasicBlock * finalStep1 = CreateBasicBlock("finalStep1");
49    BasicBlock * finalStep2 = CreateBasicBlock("finalStep2");
50    BasicBlock * step2load = CreateBasicBlock("step2load");
51    BasicBlock * step2store = CreateBasicBlock("step2store");
52    BasicBlock * finalStep3 = CreateBasicBlock("finalStep3");
53    BasicBlock * step3load = CreateBasicBlock("step3load");
54    BasicBlock * step3store = CreateBasicBlock("step3store");
55    BasicBlock * step3store2 = CreateBasicBlock("step3store2");
56    BasicBlock * itemsDone = CreateBasicBlock("itemsDone");
57    BasicBlock * expand3_4_final = CreateBasicBlock("expand3_4_final");
58    BasicBlock * expand3_4_exit = CreateBasicBlock("expand3_4_exit");
59   
60    // Determine the require shufflevector constants.
61    const unsigned PACK_SIZE = iBuilder->getStride()/8;
62   
63    // Construct a list of indexes in  the form
64    // 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, ...
65    unsigned sourceByteIndex = 0;
66    unsigned expand3_4_index[PACK_SIZE];
67    for (unsigned i = 0; i < PACK_SIZE; i++) {
68        expand3_4_index[i] = sourceByteIndex;
69        if (i % 4 != 2) sourceByteIndex++;
70    }
71    unsigned const expand3_4_offset[4] = {PACK_SIZE, 3*PACK_SIZE/4, PACK_SIZE/2, PACK_SIZE/4};
72    Value * expand_3_4_shuffle[4];
73    for (unsigned j = 0; j < 4; j++) {
74        std::vector<Constant *> Idxs;
75        for (unsigned i = 0; i < PACK_SIZE; i++) {
76            Idxs.push_back(ConstantInt::get(iBuilder->getInt32Ty(), expand3_4_offset[j] + expand3_4_index[i]));
77        }
78        expand_3_4_shuffle[j] = ConstantVector::get(Idxs);
79    }
80
81    Constant * tripleBlockSize = iBuilder->getSize(3 * iBuilder->getStride());
82    Constant * packSize = iBuilder->getSize(PACK_SIZE);
83    Constant * triplePackSize = iBuilder->getSize(3 * PACK_SIZE); // 3 packs per loop.
84    UndefValue * undefPack = UndefValue::get(iBuilder->fwVectorType(8));
85   
86    const unsigned packAlign = iBuilder->getBitBlockWidth()/8;
87
88    Value * processed = getProcessedItemCount("sourceStream");
89    Value * available = getAvailableItemCount("sourceStream");
90    Value * itemsAvail = iBuilder->CreateSub(available, processed);
91   
92    //
93    // The main loop processes 3 packs of data at a time.  For doFinal
94    // processing, process all the remaining sets of 3 packs, otherwise
95    // process in multiples of 3 full blocks of data.
96    //
97    Value * loopDivisor = iBuilder->CreateSelect(doFinal, triplePackSize, tripleBlockSize);
98    Value * excessItems = iBuilder->CreateURem(itemsAvail, loopDivisor);
99    Value * loopItemsToDo = iBuilder->CreateSub(itemsAvail, excessItems);
100
101    // A block is made up of 8 packs.  Get the pointer to the first pack (changes the type of the pointer only).
102    Value * sourcePackPtr = getInputStreamPackPtr("sourceStream", iBuilder->getInt32(0), iBuilder->getInt32(0));
103    Value * outputPackPtr = getOutputStreamPackPtr("expandedStream", iBuilder->getInt32(0), iBuilder->getInt32(0));
104
105    Value * hasFullLoop = iBuilder->CreateICmpUGE(loopItemsToDo, triplePackSize);
106
107    iBuilder->CreateCondBr(hasFullLoop, expand_3_4_loop, expand3_4_loop_exit);
108    iBuilder->SetInsertPoint(expand_3_4_loop);
109    PHINode * loopInput_ptr = iBuilder->CreatePHI(sourcePackPtr->getType(), 2);
110    PHINode * loopOutput_ptr = iBuilder->CreatePHI(outputPackPtr->getType(), 2);
111    PHINode * loopItemsRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
112
113    loopInput_ptr->addIncoming(sourcePackPtr, expand2_3entry);
114    loopOutput_ptr->addIncoming(outputPackPtr, expand2_3entry);
115    loopItemsRemain->addIncoming(loopItemsToDo, expand2_3entry);
116
117    // Step 1 of the main loop.
118    Value * pack0 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(loopInput_ptr, packAlign));
119    Value * expand0 = iBuilder->bitCast(iBuilder->CreateShuffleVector(undefPack, pack0, expand_3_4_shuffle[0]));
120    iBuilder->CreateBlockAlignedStore(expand0, loopOutput_ptr);
121    // Step 2 of the main loop.
122    Value * inPack1_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(1));
123    Value * outPack1_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(1));
124    Value * pack1 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack1_ptr, packAlign));
125    Value * expand1 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack0, pack1, expand_3_4_shuffle[1]));
126    iBuilder->CreateBlockAlignedStore(expand1, outPack1_ptr);
127    // Step 3 of the main loop.
128    Value * inPack2_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(2));
129    Value * outPack2_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(2));
130    Value * pack2 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack2_ptr, packAlign));
131    Value * expand2 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack1, pack2, expand_3_4_shuffle[2]));
132    iBuilder->CreateBlockAlignedStore(expand2, outPack2_ptr);
133    Value * outPack3_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(3));
134    Value * expand3 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack2, undefPack, expand_3_4_shuffle[3]));
135    iBuilder->CreateBlockAlignedStore(expand3, outPack3_ptr);
136
137    Value * loopNextInputPack = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(3));
138    Value * remainingItems = iBuilder->CreateSub(loopItemsRemain, triplePackSize);
139
140    Value * loopNextOutputPack;
141    loopNextOutputPack = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(4));
142
143    loopInput_ptr->addIncoming(loopNextInputPack, expand_3_4_loop);
144    loopOutput_ptr->addIncoming(loopNextOutputPack, expand_3_4_loop);
145    loopItemsRemain->addIncoming(remainingItems, expand_3_4_loop);
146
147    Value * continueLoop = iBuilder->CreateICmpUGE(remainingItems, triplePackSize);
148    iBuilder->CreateCondBr(continueLoop, expand_3_4_loop, expand3_4_loop_exit);
149   
150    iBuilder->SetInsertPoint(expand3_4_loop_exit);
151    PHINode * loopExitInput_ptr = iBuilder->CreatePHI(sourcePackPtr->getType(), 2);
152    PHINode * loopExitOutput_ptr = iBuilder->CreatePHI(outputPackPtr->getType(), 2);
153    loopExitInput_ptr->addIncoming(sourcePackPtr, expand2_3entry);
154    loopExitOutput_ptr->addIncoming(outputPackPtr, expand2_3entry);
155    loopExitInput_ptr->addIncoming(loopNextInputPack, expand_3_4_loop);
156    loopExitOutput_ptr->addIncoming(loopNextOutputPack, expand_3_4_loop);
157
158    // Update the processed items count based on the loopItemsToDo value.
159    processed = iBuilder->CreateAdd(processed, loopItemsToDo);
160    setProcessedItemCount("sourceStream", processed);
161
162
163    // Except for final segment processing, we are done.
164    iBuilder->CreateCondBr(doFinal, expand3_4_final, expand3_4_exit);
165
166    // Final segment processing.   Less than a triplePack remains.
167    iBuilder->SetInsertPoint(expand3_4_final);
168   
169    // There may be one or two remaining full packs and/or a partial pack.
170    //
171    // We have several cases depending on the number of reumaing items.  Let N = packSize
172    // (a) 0 remaining items: all done
173    // (b) 1..3N/4 remaining items:  do Step1 only, no items or pending data will remain
174    // (c) 3N/4+1 .. N remaining items:  do Step 1, do Step 2 for pending data from Step 1 only, there is no more input.
175    // (d) N+1 .. 6N/4 remaining items:  do Step 1 and Step 2, no items or pending data will remain.
176    // (e) 6N/4+1 .. 2N remaining items: do Steps 1 and 2, do Step 3 for pending data only, there is no more input.
177    // (f) 2N+1 .. 9N/4 remaining items: do Steps 1 and 2, do Step 3 up to the first write only.
178    // (g) 9N/4+1 .. 3N - 1 remaining items: do Steps 1, 2 and 3.
179    Value * condition_a = iBuilder->CreateICmpEQ(excessItems, ConstantInt::getNullValue(iBuilder->getSizeTy()));
180    iBuilder->CreateCondBr(condition_a, itemsDone, finalStep1);
181    // Final Step1 processing
182    iBuilder->SetInsertPoint(finalStep1);
183    pack0 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(loopExitInput_ptr, packAlign));
184    expand0 = iBuilder->bitCast(iBuilder->CreateShuffleVector(undefPack, pack0, expand_3_4_shuffle[0]));
185    iBuilder->CreateAlignedStore(expand0, loopExitOutput_ptr, packAlign);
186    Value * condition_b = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(3 * PACK_SIZE/4));
187    iBuilder->CreateCondBr(condition_b, itemsDone, finalStep2);
188    // Final Step 2 processing
189    iBuilder->SetInsertPoint(finalStep2);
190    Value * condition_c = iBuilder->CreateICmpULE(excessItems, packSize);
191    iBuilder->CreateCondBr(condition_c, step2store, step2load);
192    iBuilder->SetInsertPoint(step2load);
193    inPack1_ptr = iBuilder->CreateGEP(loopExitInput_ptr, iBuilder->getInt32(1));
194    pack1 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack1_ptr, packAlign));
195    iBuilder->CreateBr(step2store);
196    iBuilder->SetInsertPoint(step2store);
197    PHINode * pack1phi = iBuilder->CreatePHI(iBuilder->fwVectorType(8), 2);
198    pack1phi->addIncoming(undefPack, finalStep2);
199    pack1phi->addIncoming(pack1, step2load);
200    outPack1_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(1));
201    expand1 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack0, pack1phi, expand_3_4_shuffle[1]));
202    iBuilder->CreateAlignedStore(expand1, outPack1_ptr, packAlign);
203    Value * condition_d = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(6 * PACK_SIZE/4));
204    iBuilder->CreateCondBr(condition_d, itemsDone, finalStep3);
205    // Final Step 3
206    iBuilder->SetInsertPoint(finalStep3);
207    Value * condition_e = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(2 * PACK_SIZE));
208    iBuilder->CreateCondBr(condition_e, step3store, step3load);
209    iBuilder->SetInsertPoint(step3load);
210    inPack2_ptr = iBuilder->CreateGEP(loopExitInput_ptr, iBuilder->getInt32(2));
211    pack2 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack2_ptr, packAlign));
212    iBuilder->CreateBr(step3store);
213    iBuilder->SetInsertPoint(step3store);
214    PHINode * pack2phi = iBuilder->CreatePHI(iBuilder->fwVectorType(8), 2);
215    pack2phi->addIncoming(undefPack, finalStep3);
216    pack2phi->addIncoming(pack2, step3load);
217    outPack2_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(2));
218    expand2 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack1phi, pack2phi, expand_3_4_shuffle[2]));
219    iBuilder->CreateAlignedStore(expand2, outPack2_ptr, packAlign);
220    Value * condition_f = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(9 * PACK_SIZE/4));
221    iBuilder->CreateCondBr(condition_f, itemsDone, step3store2);
222    iBuilder->SetInsertPoint(step3store2);
223    outPack3_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(3));
224    expand3 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack2phi, undefPack, expand_3_4_shuffle[3]));
225    iBuilder->CreateAlignedStore(expand3, outPack3_ptr, packAlign);
226    iBuilder->CreateBr(itemsDone);
227    //
228    iBuilder->SetInsertPoint(itemsDone);
229    processed = iBuilder->CreateAdd(processed, excessItems);
230    setProcessedItemCount("sourceStream", processed);
231
232   
233    iBuilder->CreateBr(expand3_4_exit);
234    iBuilder->SetInsertPoint(expand3_4_exit);
235}
236
237
238// Radix 64 determination, converting 3 bytes to 4 6-bit values.
239//
240//  00000000|zyxwvuts|rqpmnlkj|hgfedcba    Original
241//           zy                            bits to move 6 positions right
242//             xwvuts                      bits to move 8 positions left
243//                    rqpm                 bits to move 4 positions right
244//                        nlkj             bits to move 10 positions left
245//                             hqfedc      bits to move 2 positions right
246//                                   ba    bits to move 12 positions left
247//    xwvuts|  nlkjzy|  barqpm|  hgfedc    Target
248inline Value * radix64Kernel::processPackData(llvm::Value * bytepack) const {
249
250    Value * step_right_6 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00C00000));
251    Value * right_6_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_6), 6);
252
253    Value * step_left_8 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x003F0000));
254    Value * left_8_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_8), 8);
255    Value * mid = iBuilder->simd_or(right_6_result, left_8_result);
256
257    Value * step_right_4 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x0000F000));
258    Value * right_4_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_4), 4);
259    mid = iBuilder->simd_or(mid, right_4_result);
260
261    Value * step_left_10 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000F00));
262    Value * left_10_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_10), 10);
263    mid = iBuilder->simd_or(mid, left_10_result);
264
265    Value * step_right_2 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x000000FC));
266    Value * right_2_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_2), 2);
267    mid = iBuilder->simd_or(mid, right_2_result);
268
269    Value * step_left_12 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000003));
270    Value * left_12_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_12), 12);
271    mid = iBuilder->simd_or(mid, left_12_result);
272
273    return iBuilder->bitCast(mid);
274}
275
276void radix64Kernel::generateDoBlockMethod() {
277    for (unsigned i = 0; i < 8; i++) {
278        Value * bytepack = loadInputStreamPack("expandedStream", iBuilder->getInt32(0), iBuilder->getInt32(i));
279        Value * radix64pack = processPackData(bytepack);
280        storeOutputStreamPack("radix64stream", iBuilder->getInt32(0), iBuilder->getInt32(i), radix64pack);
281    }
282}
283
284void radix64Kernel::generateFinalBlockMethod(Value * remainingBytes) {
285
286    BasicBlock * entry = iBuilder->GetInsertBlock();
287    BasicBlock * radix64_loop = CreateBasicBlock("radix64_loop");
288    BasicBlock * fbExit = CreateBasicBlock("fbExit");
289   
290    const unsigned PACK_SIZE = iBuilder->getStride()/8;
291    Constant * packSize = iBuilder->getSize(PACK_SIZE);
292
293    // Enter the loop only if there is at least one byte remaining to process.
294    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, radix64_loop);
295
296    iBuilder->SetInsertPoint(radix64_loop);
297    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
298    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
299    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
300    loopRemain->addIncoming(remainingBytes, entry);
301
302    Value * bytepack = loadInputStreamPack("expandedStream", iBuilder->getInt32(0), idx);
303    Value * radix64pack = processPackData(bytepack);
304    storeOutputStreamPack("radix64stream", iBuilder->getInt32(0), idx, radix64pack);
305
306    Value* nextIdx = iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1));
307    idx->addIncoming(nextIdx, radix64_loop);
308    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
309    loopRemain->addIncoming(remainAfterLoop, radix64_loop);
310
311    Value* continueLoop = iBuilder->CreateICmpSGT(remainAfterLoop, iBuilder->getSize(0));
312
313    iBuilder->CreateCondBr(continueLoop, radix64_loop, fbExit);
314
315    iBuilder->SetInsertPoint(fbExit);
316}
317
318inline llvm::Value* base64Kernel::processPackData(llvm::Value* bytepack) const {
319    Value * mask_gt_25 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(25)));
320    Value * mask_gt_51 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(51)));
321    Value * mask_eq_62 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(62)));
322    Value * mask_eq_63 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(63)));
323    // Strategy:
324    // 1. add ord('A') = 65 to all radix64 values, this sets the correct values for entries 0 to 25.
325    // 2. add ord('a') - ord('A') - (26 - 0) = 6 to all values >25, this sets the correct values for entries 0 to 51
326    // 3. subtract ord('a') - ord('0') + (52 - 26) = 75 to all values > 51, this sets the correct values for entries 0 to 61
327    // 4. subtract ord('0') - ord('+') + (62 - 52) = 15 for all values = 62
328    // 4. add ord('/') - ord('0') - (63 - 52) = 3 for all values = 63
329    Value * t0_25 = iBuilder->simd_add(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8('A')));
330    Value * t0_51 = iBuilder->simd_add(8, t0_25, iBuilder->simd_and(mask_gt_25, iBuilder->simd_fill(8, iBuilder->getInt8(6))));
331    Value * t0_61 = iBuilder->simd_sub(8, t0_51, iBuilder->simd_and(mask_gt_51, iBuilder->simd_fill(8, iBuilder->getInt8(75))));
332    Value * t0_62 = iBuilder->simd_sub(8, t0_61, iBuilder->simd_and(mask_eq_62, iBuilder->simd_fill(8, iBuilder->getInt8(15))));
333    return iBuilder->bitCast(iBuilder->simd_sub(8, t0_62, iBuilder->simd_and(mask_eq_63, iBuilder->simd_fill(8, iBuilder->getInt8(12)))));
334}
335
336void base64Kernel::generateDoBlockMethod() {
337    for (unsigned i = 0; i < 8; i++) {
338        Value * bytepack = loadInputStreamPack("radix64stream", iBuilder->getInt32(0), iBuilder->getInt32(i));
339        Value * base64pack = processPackData(bytepack);
340        storeOutputStreamPack("base64stream", iBuilder->getInt32(0), iBuilder->getInt32(i), base64pack);
341    }
342}
343
344//// Special processing for the base 64 format.   The output must always contain a multiple
345//// of 4 bytes.   When the number of radix 64 values is not a multiple of 4
346//// number of radix 64 values
347//void base64Kernel::generateFinalBlockMethod(Value * remainingBytes) {
348
349//    BasicBlock * entry = iBuilder->GetInsertBlock();
350//    BasicBlock * base64_loop = CreateBasicBlock("base64_loop");
351//    BasicBlock * loopExit = CreateBasicBlock("loopExit");
352//    BasicBlock * doPadding = CreateBasicBlock("doPadding");
353//    BasicBlock * doPadding2 = CreateBasicBlock("doPadding2");
354//    BasicBlock * fbExit = CreateBasicBlock("fbExit");
355
356//    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
357//    Value * padBytes = iBuilder->CreateSub(iBuilder->getSize(4), remainMod4);
358//    padBytes = iBuilder->CreateAnd(padBytes, iBuilder->getSize(3));
359
360//    Constant * packSize = iBuilder->getSize(iBuilder->getStride() / 8);
361
362//    // Enter the loop only if there is at least one byte remaining to process.
363//    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, base64_loop);
364
365//    iBuilder->SetInsertPoint(base64_loop);
366//    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
367//    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
368//    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
369//    loopRemain->addIncoming(remainingBytes, entry);
370//    Value * radix64streamPtr = getInputStream("radix64stream", iBuilder->getInt32(0), idx);
371//    Value * bytepack = iBuilder->CreateBlockAlignedLoad(radix64streamPtr);
372//    Value * base64pack = processPackData(bytepack);
373//    Value * base64streamPtr = getOutputStream("base64stream", iBuilder->getInt32(0), idx);
374
375//    iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(base64pack), base64streamPtr);
376//    idx->addIncoming(iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1)), base64_loop);
377//    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
378//    loopRemain->addIncoming(remainAfterLoop, base64_loop);
379
380//    Value* continueLoop = iBuilder->CreateICmpSGT(remainAfterLoop, iBuilder->getSize(0));
381//    iBuilder->CreateCondBr(continueLoop, base64_loop, loopExit);
382
383//    iBuilder->SetInsertPoint(loopExit);
384//    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(padBytes, iBuilder->getSize(0)), fbExit, doPadding);
385
386//    iBuilder->SetInsertPoint(doPadding);
387
388//    base64streamPtr = getOutputStream("base64stream", iBuilder->getInt32(0), idx);
389//    Value * i8streamPtr = iBuilder->CreatePointerCast(base64streamPtr, iBuilder->getInt8PtrTy());
390//    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8streamPtr, remainingBytes));
391//    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(3)), fbExit, doPadding2);
392//    iBuilder->SetInsertPoint(doPadding2);
393//    Value * finalPadPos = iBuilder->CreateAdd(remainingBytes, iBuilder->getSize(1));
394//    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8streamPtr, finalPadPos));
395//    iBuilder->CreateBr(fbExit);
396//    iBuilder->SetInsertPoint(fbExit);
397//    Value * produced = iBuilder->CreateAdd(getProducedItemCount("base64stream"), iBuilder->CreateAdd(remainingBytes, padBytes));
398//    setProducedItemCount("base64stream", produced);
399//}
400
401// Special processing for the base 64 format.   The output must always contain a multiple
402// of 4 bytes.   When the number of radix 64 values is not a multiple of 4
403// number of radix 64 values
404void base64Kernel::generateFinalBlockMethod(Value * remainingBytes) {
405
406    BasicBlock * entry = iBuilder->GetInsertBlock();
407    BasicBlock * base64_loop = CreateBasicBlock("base64_loop");
408    BasicBlock * loopExit = CreateBasicBlock("loopExit");
409    BasicBlock * doPadding = CreateBasicBlock("doPadding");
410    BasicBlock * doPadding2 = CreateBasicBlock("doPadding2");
411    BasicBlock * fbExit = CreateBasicBlock("fbExit");
412
413    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
414    Value * padBytes = iBuilder->CreateSub(iBuilder->getSize(4), remainMod4);
415    padBytes = iBuilder->CreateAnd(padBytes, iBuilder->getSize(3));
416
417    Constant * packSize = iBuilder->getSize(iBuilder->getStride() / 8);
418
419    // Enter the loop only if there is at least one byte remaining to process.
420    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, base64_loop);
421
422    iBuilder->SetInsertPoint(base64_loop);
423    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
424    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
425    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
426    loopRemain->addIncoming(remainingBytes, entry);
427    Value * bytepack = loadInputStreamPack("radix64stream", iBuilder->getInt32(0), idx);
428    Value * base64pack = processPackData(bytepack);
429    storeOutputStreamPack("base64stream", iBuilder->getInt32(0), idx, base64pack);
430    idx->addIncoming(iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1)), base64_loop);
431    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
432    loopRemain->addIncoming(remainAfterLoop, base64_loop);
433
434    Value* continueLoop = iBuilder->CreateICmpSGT(remainAfterLoop, iBuilder->getSize(0));
435    iBuilder->CreateCondBr(continueLoop, base64_loop, loopExit);
436
437    iBuilder->SetInsertPoint(loopExit);
438    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(padBytes, iBuilder->getSize(0)), fbExit, doPadding);
439
440    iBuilder->SetInsertPoint(doPadding);
441    Value * i8output_ptr = getOutputStreamBlockPtr("base64stream", iBuilder->getInt32(0));
442    i8output_ptr = iBuilder->CreatePointerCast(i8output_ptr, iBuilder->getInt8PtrTy());
443    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, remainingBytes));
444    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(3)), fbExit, doPadding2);
445    iBuilder->SetInsertPoint(doPadding2);
446    Value * finalPadPos = iBuilder->CreateAdd(remainingBytes, iBuilder->getSize(1));
447    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, finalPadPos));
448    iBuilder->CreateBr(fbExit);
449    iBuilder->SetInsertPoint(fbExit);
450}
451
452expand3_4Kernel::expand3_4Kernel(IDISA::IDISA_Builder * iBuilder)
453: SegmentOrientedKernel(iBuilder, "Parabix:expand3_4",
454            {Binding{iBuilder->getStreamSetTy(1, 8), "sourceStream"}},
455            {Binding{iBuilder->getStreamSetTy(1, 8), "expandedStream", FixedRatio(4,3)}},
456            {}, {}, {}) {
457}
458
459radix64Kernel::radix64Kernel(IDISA::IDISA_Builder * iBuilder)
460: BlockOrientedKernel(iBuilder, "Parabix:radix64",
461            {Binding{iBuilder->getStreamSetTy(1, 8), "expandedStream"}},
462            {Binding{iBuilder->getStreamSetTy(1, 8), "radix64stream"}},
463            {}, {}, {}) {
464}
465
466base64Kernel::base64Kernel(IDISA::IDISA_Builder * iBuilder)
467: BlockOrientedKernel(iBuilder, "Parabix:base64",
468            {Binding{iBuilder->getStreamSetTy(1, 8), "radix64stream"}},
469            {Binding{iBuilder->getStreamSetTy(1, 8), "base64stream", RoundUpToMultiple(4)}},
470            {}, {}, {}) {
471}
472
473}
Note: See TracBrowser for help on using the repository browser.