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

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

Continuation of work to simplify Kernel writing

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