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

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

Start of work to simplify kernel writing. Removed generateDoBlockLogic method.

File size: 32.8 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() const {
44    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
45    Module * m = iBuilder->getModule();
46    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
47    BasicBlock * expand2_3entry = BasicBlock::Create(iBuilder->getContext(), "expand2_3entry", doSegmentFunction, 0);
48    iBuilder->SetInsertPoint(expand2_3entry);
49    BasicBlock * expand_3_4_loop = BasicBlock::Create(iBuilder->getContext(), "expand_3_4_loop", doSegmentFunction, 0);
50    BasicBlock * expand3_4_loop_exit = BasicBlock::Create(iBuilder->getContext(), "expand3_4_loop_exit", doSegmentFunction, 0);
51    BasicBlock * finalStep1 = BasicBlock::Create(iBuilder->getContext(), "finalStep1", doSegmentFunction, 0);
52    BasicBlock * finalStep2 = BasicBlock::Create(iBuilder->getContext(), "finalStep2", doSegmentFunction, 0);
53    BasicBlock * step2load = BasicBlock::Create(iBuilder->getContext(), "step2load", doSegmentFunction, 0);
54    BasicBlock * step2store = BasicBlock::Create(iBuilder->getContext(), "step2store", doSegmentFunction, 0);
55    BasicBlock * finalStep3 = BasicBlock::Create(iBuilder->getContext(), "finalStep3", doSegmentFunction, 0);
56    BasicBlock * step3load = BasicBlock::Create(iBuilder->getContext(), "step3load", doSegmentFunction, 0);
57    BasicBlock * step3store = BasicBlock::Create(iBuilder->getContext(), "step3store", doSegmentFunction, 0);
58    BasicBlock * step3store2 = BasicBlock::Create(iBuilder->getContext(), "step3store2", doSegmentFunction, 0);
59    BasicBlock * itemsDone = BasicBlock::Create(iBuilder->getContext(), "itemsDone", doSegmentFunction, 0);
60    BasicBlock * expand3_4_final = BasicBlock::Create(iBuilder->getContext(), "expand3_4_final", doSegmentFunction, 0);
61    BasicBlock * expand3_4_exit = BasicBlock::Create(iBuilder->getContext(), "expand3_4_exit", doSegmentFunction, 0);
62   
63    // Determine the require shufflevector constants.
64    const unsigned PACK_SIZE = iBuilder->getStride()/8;
65   
66    // Construct a list of indexes in  the form
67    // 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, ...
68    unsigned sourceByteIndex = 0;
69    unsigned expand3_4_index[PACK_SIZE];
70    for (unsigned i = 0; i < PACK_SIZE; i++) {
71        expand3_4_index[i] = sourceByteIndex;
72        if (i % 4 != 2) sourceByteIndex++;
73    }
74    unsigned const expand3_4_offset[4] = {PACK_SIZE, 3*PACK_SIZE/4, PACK_SIZE/2, PACK_SIZE/4};
75    Value * expand_3_4_shuffle[4];
76    for (unsigned j = 0; j < 4; j++) {
77        std::vector<Constant *> Idxs;
78        for (unsigned i = 0; i < PACK_SIZE; i++) {
79            Idxs.push_back(ConstantInt::get(iBuilder->getInt32Ty(), expand3_4_offset[j] + expand3_4_index[i]));
80        }
81        expand_3_4_shuffle[j] = ConstantVector::get(Idxs);
82    }
83    Constant * Const3 = iBuilder->getSize(3);
84    Constant * Const4 = iBuilder->getSize(4);
85    Constant * tripleBlockSize = iBuilder->getSize(3 * iBuilder->getStride());
86    Constant * stride = iBuilder->getSize(iBuilder->getStride());
87    Constant * packSize = iBuilder->getSize(PACK_SIZE);
88    Constant * triplePackSize = iBuilder->getSize(3 * PACK_SIZE); // 3 packs per loop.
89    UndefValue * undefPack = UndefValue::get(iBuilder->fwVectorType(8));
90   
91    const unsigned packAlign = iBuilder->getBitBlockWidth()/8;
92
93    Function::arg_iterator args = doSegmentFunction->arg_begin();
94    Value * self = &*(args++);
95    Value * doFinal = &*(args++);
96    Value * producerPos = &*(args++);
97    Value * processed = getProcessedItemCount(self, "sourceStream");
98    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
99   
100    //
101    // The main loop processes 3 packs of data at a time.  For doFinal
102    // processing, process all the remaining sets of 3 packs, otherwise
103    // process in multiples of 3 full blocks of data.
104    //
105    Value * loopDivisor = iBuilder->CreateSelect(doFinal, triplePackSize, tripleBlockSize);
106    Value * excessItems = iBuilder->CreateURem(itemsAvail, loopDivisor);
107    Value * loopItemsToDo = iBuilder->CreateSub(itemsAvail, excessItems);
108
109    Value * blockNo = getBlockNo(self);
110
111    // A block is made up of 8 packs.  Get the pointer to the first pack (changes the type of the pointer only).
112    Value * sourcePackPtr = getStream(self, "sourceStream", blockNo, iBuilder->getInt32(0), iBuilder->getInt32(0));
113
114    Value * outputGenerated = getProducedItemCount(self, "expandedStream"); // bytes previously generated to output
115    Value * outputBlockNo = iBuilder->CreateUDiv(outputGenerated, stride);
116    Value * outputPackPtr = getStream(self, "expandedStream", outputBlockNo, iBuilder->getInt32(0), iBuilder->getInt32(0));
117
118    Value * hasFullLoop = iBuilder->CreateICmpUGE(loopItemsToDo, triplePackSize);
119
120    iBuilder->CreateCondBr(hasFullLoop, expand_3_4_loop, expand3_4_loop_exit);
121    iBuilder->SetInsertPoint(expand_3_4_loop);
122    PHINode * loopInput_ptr = iBuilder->CreatePHI(sourcePackPtr->getType(), 2);
123    PHINode * loopOutput_ptr = iBuilder->CreatePHI(outputPackPtr->getType(), 2);
124    PHINode * loopItemsRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
125
126    loopInput_ptr->addIncoming(sourcePackPtr, expand2_3entry);
127    loopOutput_ptr->addIncoming(outputPackPtr, expand2_3entry);
128    loopItemsRemain->addIncoming(loopItemsToDo, expand2_3entry);
129
130    // Step 1 of the main loop.
131    Value * pack0 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(loopInput_ptr, packAlign));
132    Value * expand0 = iBuilder->bitCast(iBuilder->CreateShuffleVector(undefPack, pack0, expand_3_4_shuffle[0]));
133    iBuilder->CreateAlignedStore(expand0, loopOutput_ptr, packAlign);
134    // Step 2 of the main loop.
135    Value * inPack1_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(1));
136    Value * outPack1_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(1));
137    Value * pack1 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack1_ptr, packAlign));
138    Value * expand1 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack0, pack1, expand_3_4_shuffle[1]));
139    iBuilder->CreateAlignedStore(expand1, outPack1_ptr, packAlign);
140    // Step 3 of the main loop.
141    Value * inPack2_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(2));
142    Value * outPack2_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(2));
143    Value * pack2 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack2_ptr, packAlign));
144    Value * expand2 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack1, pack2, expand_3_4_shuffle[2]));
145    iBuilder->CreateAlignedStore(expand2, outPack2_ptr, packAlign);
146    Value * outPack3_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(3));
147    Value * expand3 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack2, undefPack, expand_3_4_shuffle[3]));
148    iBuilder->CreateAlignedStore(expand3, outPack3_ptr, packAlign);
149
150    Value * loopNextInputPack = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(3));
151    Value * remainingItems = iBuilder->CreateSub(loopItemsRemain, triplePackSize);
152
153    Value * loopNextOutputPack;
154    loopNextOutputPack = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(4));
155
156    loopInput_ptr->addIncoming(loopNextInputPack, expand_3_4_loop);
157    loopOutput_ptr->addIncoming(loopNextOutputPack, expand_3_4_loop);
158    loopItemsRemain->addIncoming(remainingItems, expand_3_4_loop);
159
160    Value * continueLoop = iBuilder->CreateICmpUGE(remainingItems, triplePackSize);
161    iBuilder->CreateCondBr(continueLoop, expand_3_4_loop, expand3_4_loop_exit);
162   
163    iBuilder->SetInsertPoint(expand3_4_loop_exit);
164    PHINode * loopExitInput_ptr = iBuilder->CreatePHI(sourcePackPtr->getType(), 2);
165    PHINode * loopExitOutput_ptr = iBuilder->CreatePHI(outputPackPtr->getType(), 2);
166    loopExitInput_ptr->addIncoming(sourcePackPtr, expand2_3entry);
167    loopExitOutput_ptr->addIncoming(outputPackPtr, expand2_3entry);
168    loopExitInput_ptr->addIncoming(loopNextInputPack, expand_3_4_loop);
169    loopExitOutput_ptr->addIncoming(loopNextOutputPack, expand_3_4_loop);
170
171    // Update the produced and processed items count based on the loopItemsToDo value.
172    processed = iBuilder->CreateAdd(processed, loopItemsToDo);
173    setProcessedItemCount(self, "sourceStream", processed);
174   
175    setBlockNo(self, iBuilder->CreateUDiv(processed, stride));
176    // We have produced 4 output bytes for every 3 input bytes.
177    Value * totalProduced = iBuilder->CreateMul(iBuilder->CreateUDiv(processed, Const3), Const4);
178    setProducedItemCount(self, "expandedStream", totalProduced);
179   
180    // Except for final segment processing, we are done.
181    iBuilder->CreateCondBr(doFinal, expand3_4_final, expand3_4_exit);
182
183    // Final segment processing.   Less than a triplePack remains.
184    iBuilder->SetInsertPoint(expand3_4_final);
185   
186    // There may be one or two remaining full packs and/or a partial pack.
187    //
188    // We have several cases depending on the number of reumaing items.  Let N = packSize
189    // (a) 0 remaining items: all done
190    // (b) 1..3N/4 remaining items:  do Step1 only, no items or pending data will remain
191    // (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.
192    // (d) N+1 .. 6N/4 remaining items:  do Step 1 and Step 2, no items or pending data will remain.
193    // (e) 6N/4+1 .. 2N remaining items: do Steps 1 and 2, do Step 3 for pending data only, there is no more input.
194    // (f) 2N+1 .. 9N/4 remaining items: do Steps 1 and 2, do Step 3 up to the first write only.
195    // (g) 9N/4+1 .. 3N - 1 remaining items: do Steps 1, 2 and 3.
196    Value * condition_a = iBuilder->CreateICmpEQ(excessItems, ConstantInt::getNullValue(iBuilder->getSizeTy()));
197    iBuilder->CreateCondBr(condition_a, itemsDone, finalStep1);
198    // Final Step1 processing
199    iBuilder->SetInsertPoint(finalStep1);
200    pack0 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(loopExitInput_ptr, packAlign));
201    expand0 = iBuilder->bitCast(iBuilder->CreateShuffleVector(undefPack, pack0, expand_3_4_shuffle[0]));
202    iBuilder->CreateAlignedStore(expand0, loopExitOutput_ptr, packAlign);
203    Value * condition_b = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(3 * PACK_SIZE/4));
204    iBuilder->CreateCondBr(condition_b, itemsDone, finalStep2);
205    // Final Step 2 processing
206    iBuilder->SetInsertPoint(finalStep2);
207    Value * condition_c = iBuilder->CreateICmpULE(excessItems, packSize);
208    iBuilder->CreateCondBr(condition_c, step2store, step2load);
209    iBuilder->SetInsertPoint(step2load);
210    inPack1_ptr = iBuilder->CreateGEP(loopExitInput_ptr, iBuilder->getInt32(1));
211    pack1 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack1_ptr, packAlign));
212    iBuilder->CreateBr(step2store);
213    iBuilder->SetInsertPoint(step2store);
214    PHINode * pack1phi = iBuilder->CreatePHI(iBuilder->fwVectorType(8), 2);
215    pack1phi->addIncoming(undefPack, finalStep2);
216    pack1phi->addIncoming(pack1, step2load);
217    outPack1_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(1));
218    expand1 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack0, pack1phi, expand_3_4_shuffle[1]));
219    iBuilder->CreateAlignedStore(expand1, outPack1_ptr, packAlign);
220    Value * condition_d = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(6 * PACK_SIZE/4));
221    iBuilder->CreateCondBr(condition_d, itemsDone, finalStep3);
222    // Final Step 3
223    iBuilder->SetInsertPoint(finalStep3);
224    Value * condition_e = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(2 * PACK_SIZE));
225    iBuilder->CreateCondBr(condition_e, step3store, step3load);
226    iBuilder->SetInsertPoint(step3load);
227    inPack2_ptr = iBuilder->CreateGEP(loopExitInput_ptr, iBuilder->getInt32(2));
228    pack2 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack2_ptr, packAlign));
229    iBuilder->CreateBr(step3store);
230    iBuilder->SetInsertPoint(step3store);
231    PHINode * pack2phi = iBuilder->CreatePHI(iBuilder->fwVectorType(8), 2);
232    pack2phi->addIncoming(undefPack, finalStep3);
233    pack2phi->addIncoming(pack2, step3load);
234    outPack2_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(2));
235    expand2 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack1phi, pack2phi, expand_3_4_shuffle[2]));
236    iBuilder->CreateAlignedStore(expand2, outPack2_ptr, packAlign);
237    Value * condition_f = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(9 * PACK_SIZE/4));
238    iBuilder->CreateCondBr(condition_f, itemsDone, step3store2);
239    iBuilder->SetInsertPoint(step3store2);
240    outPack3_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(3));
241    expand3 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack2phi, undefPack, expand_3_4_shuffle[3]));
242    iBuilder->CreateAlignedStore(expand3, outPack3_ptr, packAlign);
243    iBuilder->CreateBr(itemsDone);
244    //
245    iBuilder->SetInsertPoint(itemsDone);
246    processed = iBuilder->CreateAdd(processed, excessItems);
247    setProcessedItemCount(self, "sourceStream", processed);
248
249    setBlockNo(self, iBuilder->CreateUDiv(processed, stride));
250    // We have produced 4 output bytes for every 3 input bytes.  If the number of input
251    // bytes is not a multiple of 3, then we have one more output byte for each excess
252    // input byte.
253    totalProduced = iBuilder->CreateAdd(iBuilder->CreateMul(iBuilder->CreateUDiv(processed, Const3), Const4), iBuilder->CreateURem(processed, Const3));
254    setProducedItemCount(self, "expandedStream", totalProduced);
255   
256    iBuilder->CreateBr(expand3_4_exit);
257    iBuilder->SetInsertPoint(expand3_4_exit);
258    iBuilder->CreateRetVoid();
259    iBuilder->restoreIP(savePoint);
260}
261
262
263// Radix 64 determination, converting 3 bytes to 4 6-bit values.
264//
265//  00000000|zyxwvuts|rqpmnlkj|hgfedcba    Original
266//           zy                            bits to move 6 positions right
267//             xwvuts                      bits to move 8 positions left
268//                    rqpm                 bits to move 4 positions right
269//                        nlkj             bits to move 10 positions left
270//                             hqfedc      bits to move 2 positions right
271//                                   ba    bits to move 12 positions left
272//    xwvuts|  nlkjzy|  barqpm|  hgfedc    Target
273void radix64Kernel::generateDoBlockMethod(Function * function, Value * self, Value * blockNo) const {
274    Value * step_right_6 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00C00000));
275    Value * step_left_8 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x003F0000));
276    Value * step_right_4 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x0000F000));
277    Value * step_left_10 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000F00));
278    Value * step_right_2 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x000000FC));
279    Value * step_left_12 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000003));
280    for (unsigned i = 0; i < 8; i++) {
281        Value * expandedStream = getStream(self, "expandedStream", blockNo, iBuilder->getInt32(0), iBuilder->getInt32(i));
282        Value * bytepack = iBuilder->CreateBlockAlignedLoad(expandedStream);
283        Value * right_6_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_6), 6);
284        Value * right_4_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_4), 4);
285        Value * mid = iBuilder->simd_or(right_6_result, right_4_result);
286        Value * right_2_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_2), 2);
287        mid = iBuilder->simd_or(mid, right_2_result);
288        Value * left_8_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_8), 8);
289        mid = iBuilder->simd_or(mid, left_8_result);
290        Value * left_10_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_10), 10);
291        mid = iBuilder->simd_or(mid, left_10_result);
292        Value * left_12_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_12), 12);
293        mid = iBuilder->simd_or(mid, left_12_result);
294        Value * radix64pack = iBuilder->bitCast(mid);
295        Value * radix64stream = getStream(self, "radix64stream",blockNo, iBuilder->getInt32(0), iBuilder->getInt32(i));
296        iBuilder->CreateBlockAlignedStore(radix64pack, radix64stream);
297    }
298    Value * produced = getProducedItemCount(self, "radix64stream");
299    produced = iBuilder->CreateAdd(produced, iBuilder->getSize(iBuilder->getStride()));
300    setProducedItemCount(self, "radix64stream", produced);
301}
302
303void radix64Kernel::generateFinalBlockMethod(Function * function, Value *self, Value * remainingBytes, Value * blockNo) const {
304
305    BasicBlock * entry = iBuilder->GetInsertBlock();
306    BasicBlock * radix64_loop = BasicBlock::Create(iBuilder->getContext(), "radix64_loop", function, 0);
307    BasicBlock * loopExit = BasicBlock::Create(iBuilder->getContext(), "loopExit", function, 0);
308    BasicBlock * handleRemainFirstByte = BasicBlock::Create(iBuilder->getContext(), "handleRemainFirstByte", function, 0);
309    BasicBlock * handleRemainSecondByte = BasicBlock::Create(iBuilder->getContext(), "handleRemainSecondByte", function, 0);
310    BasicBlock * handleNoRemainSecondByte = BasicBlock::Create(iBuilder->getContext(), "handleNoRemainSecondByte", function, 0);
311    BasicBlock * fbExit = BasicBlock::Create(iBuilder->getContext(), "fbExit", function, 0);
312    // Final Block arguments: self, remaining.
313    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
314
315    const unsigned PACK_SIZE = iBuilder->getStride()/8;
316    Constant * packSize = iBuilder->getSize(PACK_SIZE);
317
318    Value * step_right_6 = iBuilder->simd_fill(32, iBuilder->getInt32(0x00C00000));
319    Value * step_left_8 = iBuilder->simd_fill(32, iBuilder->getInt32(0x003F0000));
320    Value * step_right_4 = iBuilder->simd_fill(32, iBuilder->getInt32(0x0000F000));
321    Value * step_left_10 = iBuilder->simd_fill(32, iBuilder->getInt32(0x00000F00));
322    Value * step_right_2 = iBuilder->simd_fill(32, iBuilder->getInt32(0x000000FC));
323    Value * step_left_12 = iBuilder->simd_fill(32, iBuilder->getInt32(0x00000003));
324
325    // Enter the loop only if there is at least one byte remaining to process.
326    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, radix64_loop);
327
328    iBuilder->SetInsertPoint(radix64_loop);
329    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
330    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
331    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
332    loopRemain->addIncoming(remainingBytes, entry);
333
334    Value * expandedStreamLoopPtr = getStream(self, "expandedStream", blockNo, iBuilder->getInt32(0), idx);
335    Value * bytepack = iBuilder->CreateBlockAlignedLoad(expandedStreamLoopPtr);
336    Value * right_6_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_6), 6);
337    Value * right_4_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_4), 4);
338    Value * right_2_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_2), 2);
339    Value * left_8_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_8), 8);
340    Value * left_10_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_10), 10);
341    Value * left_12_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_12), 12);
342
343    Value * mid = right_6_result;
344    mid = iBuilder->simd_or(mid, right_4_result);
345    mid = iBuilder->simd_or(mid, right_2_result);
346    mid = iBuilder->simd_or(mid, left_8_result);
347    mid = iBuilder->simd_or(mid, left_10_result);
348    mid = iBuilder->simd_or(mid, left_12_result);
349    Value * radix64pack = iBuilder->bitCast(mid);
350
351    Value * radix64streamPtr = getStream(self, "radix64stream", blockNo, iBuilder->getInt32(0), idx);
352    iBuilder->CreateBlockAlignedStore(radix64pack, radix64streamPtr);
353
354    Value* nextIdx = iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1));
355    idx->addIncoming(nextIdx, radix64_loop);
356    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
357    loopRemain->addIncoming(remainAfterLoop, radix64_loop);
358
359    Value* continueLoop = iBuilder->CreateICmpULT(remainAfterLoop, packSize);
360    iBuilder->CreateCondBr(continueLoop, radix64_loop, loopExit);
361
362    iBuilder->SetInsertPoint(loopExit);
363    // All base64 data has been computed, but we may need to set one or two '=' padding bytes.
364    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(0)), fbExit, handleRemainFirstByte);
365    iBuilder->SetInsertPoint(handleRemainFirstByte);
366    // At least one padding byte required.
367    Value * i8input_ptr = getStreamView(iBuilder->getInt8PtrTy(), self, "expandedStream", blockNo, iBuilder->getInt32(0));
368    Value * remainOutputStart = iBuilder->CreateSub(remainingBytes, remainMod4);
369
370    Value * firstRemainByte = iBuilder->CreateLoad(i8input_ptr);
371
372    Value * first_move_right_2_mask = ConstantInt::get(iBuilder->getInt8Ty(), 0xFC);
373    Value * first_output_byte = iBuilder->CreateLShr(iBuilder->CreateAnd(firstRemainByte, first_move_right_2_mask), 2);
374
375    Value * first_move_left_4_mask = ConstantInt::get(iBuilder->getInt8Ty(), 0x03);
376    Value * first_move_left_4_byte = iBuilder->CreateShl(iBuilder->CreateAnd(firstRemainByte, first_move_left_4_mask), 4);
377
378
379    Value * i8OutPtr0 = getStreamView(iBuilder->getInt8PtrTy(), self, "radix64stream", blockNo, remainOutputStart);
380
381    iBuilder->CreateStore(first_output_byte, i8OutPtr0);
382
383    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(1)), handleNoRemainSecondByte, handleRemainSecondByte);
384    iBuilder->SetInsertPoint(handleRemainSecondByte);
385
386    Value * secondRemainByte = iBuilder->CreateLoad(iBuilder->CreateGEP(i8input_ptr, iBuilder->getInt32(1)));
387    Value * second_move_right_4_mask = ConstantInt::get(iBuilder->getInt8Ty(), 0xF0);
388    Value * second_move_right_4_byte = iBuilder->CreateLShr(iBuilder->CreateAnd(secondRemainByte, second_move_right_4_mask), 4);
389    Value * second_output_byte = iBuilder->CreateOr(first_move_left_4_byte, second_move_right_4_byte);
390
391    Value * i8OutPtr1 = getStreamView(iBuilder->getInt8PtrTy(), self, "radix64stream", blockNo, iBuilder->CreateAdd(remainOutputStart, iBuilder->getInt64(1)));
392
393    iBuilder->CreateStore(second_output_byte, i8OutPtr1);
394
395    Value * second_move_left_2_mask = ConstantInt::get(iBuilder->getInt8Ty(), 0x0F);
396    Value * second_move_left_2_byte = iBuilder->CreateShl(iBuilder->CreateAnd(secondRemainByte, second_move_left_2_mask), 2);
397
398    Value * i8OutPtr2 = getStreamView(iBuilder->getInt8PtrTy(), self, "radix64stream", blockNo, iBuilder->CreateAdd(remainOutputStart, iBuilder->getInt64(2)));
399
400    iBuilder->CreateStore(second_move_left_2_byte, i8OutPtr2);
401    iBuilder->CreateBr(fbExit);
402
403    iBuilder->SetInsertPoint(handleNoRemainSecondByte);
404
405    i8OutPtr1 = getStreamView(iBuilder->getInt8PtrTy(), self, "radix64stream", blockNo, iBuilder->CreateAdd(remainOutputStart, iBuilder->getInt64(1)));
406
407    iBuilder->CreateStore(first_move_left_4_byte, i8OutPtr1);
408    iBuilder->CreateBr(fbExit);
409
410    iBuilder->SetInsertPoint(fbExit);
411    Value * outputNumberAdd = iBuilder->CreateSelect(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(0)), iBuilder->getSize(0), iBuilder->getSize(1));
412    Value * produced = iBuilder->CreateAdd(getProducedItemCount(self, "radix64stream"), iBuilder->CreateAdd(remainingBytes, outputNumberAdd));
413    setProducedItemCount(self, "radix64stream", produced);
414}
415
416void base64Kernel::generateDoBlockMethod(Function * function, Value * self, Value * blockNo) const {
417    for (unsigned i = 0; i < 8; i++) {
418        Value * radix64stream_ptr = getStream(self, "radix64stream", blockNo, iBuilder->getInt32(0), iBuilder->getInt32(i));
419        Value * bytepack = iBuilder->CreateBlockAlignedLoad(radix64stream_ptr);
420        Value * mask_gt_25 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(25)));
421        Value * mask_gt_51 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(51)));
422        Value * mask_eq_62 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(62)));
423        Value * mask_eq_63 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(63)));
424        // Strategy:
425        // 1. add ord('A') = 65 to all radix64 values, this sets the correct values for entries 0 to 25.
426        // 2. add ord('a') - ord('A') - (26 - 0) = 6 to all values >25, this sets the correct values for entries 0 to 51
427        // 3. subtract ord('a') - ord('0') + (52 - 26) = 75 to all values > 51, this sets the correct values for entries 0 to 61
428        // 4. subtract ord('0') - ord('+') + (62 - 52) = 15 for all values = 62
429        // 4. subtract ord('0') - ord('/') + (63 - 62) = 2 for all values = 63
430        Value * t0_25 = iBuilder->simd_add(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8('A')));
431        Value * t0_51 = iBuilder->simd_add(8, t0_25, iBuilder->simd_and(mask_gt_25, iBuilder->simd_fill(8, iBuilder->getInt8(6))));
432        Value * t0_61 = iBuilder->simd_sub(8, t0_51, iBuilder->simd_and(mask_gt_51, iBuilder->simd_fill(8, iBuilder->getInt8(75))));
433        Value * t0_62 = iBuilder->simd_sub(8, t0_61, iBuilder->simd_and(mask_eq_62, iBuilder->simd_fill(8, iBuilder->getInt8(15))));
434        Value * base64pack = iBuilder->simd_sub(8, t0_62, iBuilder->simd_and(mask_eq_63, iBuilder->simd_fill(8, iBuilder->getInt8(2))));
435        Value * base64stream_ptr = getStream(self, "base64stream", blockNo, iBuilder->getInt32(0), iBuilder->getInt32(i));
436        iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(base64pack), base64stream_ptr);
437    }
438    Value * produced = getProducedItemCount(self, "base64stream");
439    produced = iBuilder->CreateAdd(produced, iBuilder->getSize(iBuilder->getStride()));
440    setProducedItemCount(self, "base64stream", produced);
441}
442
443// Special processing for the base 64 format.   The output must always contain a multiple
444// of 4 bytes.   When the number of radix 64 values is not a multiple of 4
445// number of radix 64 values
446void base64Kernel::generateFinalBlockMethod(Function * function, Value * self, Value * remainingBytes, Value * blockNo) const {
447
448    BasicBlock * entry = iBuilder->GetInsertBlock();
449    BasicBlock * base64_loop = BasicBlock::Create(iBuilder->getContext(), "base64_loop", function, 0);
450    BasicBlock * loopExit = BasicBlock::Create(iBuilder->getContext(), "loopExit", function, 0);
451    BasicBlock * doPadding = BasicBlock::Create(iBuilder->getContext(), "doPadding", function, 0);
452    BasicBlock * doPadding2 = BasicBlock::Create(iBuilder->getContext(), "doPadding2", function, 0);
453    BasicBlock * fbExit = BasicBlock::Create(iBuilder->getContext(), "fbExit", function, 0);
454
455    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
456    Value * padBytes = iBuilder->CreateSub(iBuilder->getSize(4), remainMod4);
457    padBytes = iBuilder->CreateAnd(padBytes, iBuilder->getSize(3));
458
459    Constant * packSize = iBuilder->getSize(iBuilder->getStride() / 8);
460
461    // Enter the loop only if there is at least one byte remaining to process.
462    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, base64_loop);
463
464    iBuilder->SetInsertPoint(base64_loop);
465    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
466    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
467    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
468    loopRemain->addIncoming(remainingBytes, entry);
469    Value * radix64streamPtr = getStream(self, "radix64stream", blockNo, iBuilder->getInt32(0), idx);
470    Value * bytepack = iBuilder->CreateBlockAlignedLoad(radix64streamPtr);
471    Value * mask_gt_25 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(25)));
472    Value * mask_gt_51 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(51)));
473    Value * mask_eq_62 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(62)));
474    Value * mask_eq_63 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(63)));
475    Value * t0_25 = iBuilder->simd_add(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8('A')));
476    Value * t0_51 = iBuilder->simd_add(8, t0_25, iBuilder->simd_and(mask_gt_25, iBuilder->simd_fill(8, iBuilder->getInt8(6))));
477    Value * t0_61 = iBuilder->simd_sub(8, t0_51, iBuilder->simd_and(mask_gt_51, iBuilder->simd_fill(8, iBuilder->getInt8(75))));
478    Value * t0_62 = iBuilder->simd_sub(8, t0_61, iBuilder->simd_and(mask_eq_62, iBuilder->simd_fill(8, iBuilder->getInt8(15))));
479    Value * base64pack = iBuilder->simd_sub(8, t0_62, iBuilder->simd_and(mask_eq_63, iBuilder->simd_fill(8, iBuilder->getInt8(2))));
480    Value * base64streamPtr = getStream(self, "base64stream", blockNo, iBuilder->getInt32(0), idx);
481    iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(base64pack), base64streamPtr);
482    idx->addIncoming(iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1)), base64_loop);
483    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
484    loopRemain->addIncoming(remainAfterLoop, base64_loop);
485
486    Value* continueLoop = iBuilder->CreateICmpULT(remainAfterLoop, packSize);
487    iBuilder->CreateCondBr(continueLoop, base64_loop, loopExit);
488
489    iBuilder->SetInsertPoint(loopExit);
490    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(padBytes, iBuilder->getSize(0)), fbExit, doPadding);
491
492    iBuilder->SetInsertPoint(doPadding);
493    Value * i8output_ptr = getStreamView(iBuilder->getInt8PtrTy(), self, "base64stream", blockNo, iBuilder->getInt32(0));
494    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, remainingBytes));
495    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(3)), fbExit, doPadding2);
496    iBuilder->SetInsertPoint(doPadding2);
497    Value * finalPadPos = iBuilder->CreateAdd(remainingBytes, iBuilder->getSize(1));
498    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, finalPadPos));
499    iBuilder->CreateBr(fbExit);
500    iBuilder->SetInsertPoint(fbExit);
501    Value * produced = iBuilder->CreateAdd(getProducedItemCount(self, "base64stream"), iBuilder->CreateAdd(remainingBytes, padBytes));
502    setProducedItemCount(self, "base64stream", produced);
503}
504
505expand3_4Kernel::expand3_4Kernel(IDISA::IDISA_Builder * iBuilder)
506: SegmentOrientedKernel(iBuilder, "expand3_4",
507              {Binding{iBuilder->getStreamSetTy(1, 8), "sourceStream"}},
508              {Binding{iBuilder->getStreamSetTy(1, 8), "expandedStream"}},
509              {}, {}, {}) {
510    setDoBlockUpdatesProducedItemCountsAttribute(true);
511}
512
513radix64Kernel::radix64Kernel(IDISA::IDISA_Builder * iBuilder)
514: BlockOrientedKernel(iBuilder, "radix64", {Binding{iBuilder->getStreamSetTy(1, 8), "expandedStream"}}, {Binding{iBuilder->getStreamSetTy(1, 8), "radix64stream"}}, {}, {}, {}) {
515    setDoBlockUpdatesProducedItemCountsAttribute(true);
516}
517
518base64Kernel::base64Kernel(IDISA::IDISA_Builder * iBuilder)
519: BlockOrientedKernel(iBuilder, "base64", {Binding{iBuilder->getStreamSetTy(1, 8), "radix64stream"}}, {Binding{iBuilder->getStreamSetTy(1, 8), "base64stream"}}, {}, {}, {}) {
520    setDoBlockUpdatesProducedItemCountsAttribute(true);
521}
522
523}
Note: See TracBrowser for help on using the repository browser.