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

Last change on this file since 5260 was 5260, checked in by nmedfort, 9 months ago

Changes working towards simplifying accessing stream elements + some modifications to simplify include / forward declarations within the CodeGen? library.

File size: 35.9 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 "expand3_4.h"
7#include <kernels/kernel.h>
8#include <IR_Gen/idisa_builder.h>
9#include <llvm/IR/Module.h>
10#include <llvm/Support/raw_ostream.h>
11
12using namespace llvm;
13
14namespace kernel {
15
16// This kernel produces an expanded input stream by duplicating every third byte.
17// It is implemented using SIMD shufflevector operations.  With 16-byte registers,
18// a single shufflevector operation produces 16 bytes of output data from the
19// 12 bytes of input data.   With 32-byte registers, 32 bytes of output data are
20// produced from 24 bytes of input data.
21//
22// Using aligned SIMD loads, an inner loop processes three registers full of input
23// data (i.e., three BytePacks) to produce four registers full of output.   This is
24// a 3 step process.
25// Step 1:  Load input_pack0, apply the shuffle operation to produce output_pack0.
26//          At this point 3/4 of the data in input_pack0 has been processed.
27// Step 2:  Load input_pack1, apply a shuffle operation to use the remaining
28//          1/4 of input_pack0 and 1/2 of input_pack1 to produce output_pack1.
29//          At this point 1/2 of the data in input_pack1 has been processed.
30// Step 3:  Load input_pack2, apply a shuffle operation to use the remaining 1/2
31//          of input_pack1 and 1/4 of input_pack2 to produce output_pack2.
32//          Then apply a further shuffle opertaion to use the remaining 3/4 of
33//          input_pack2 to produce output_pack3.
34
35// The doSegment method processes input in terms of tripleBlocks, 3 blocks of input,
36// producing 4 blocks of output.   Unless less than one tripleBlock remains, the
37// doSegment method always processes an integral number of tripleBlocks as a logical
38// segment.  Both input and output buffers are hence maintained at block boundaries,
39// with the input data completely processed for each tripleBlock.
40//
41// The pipeline must guarantee that the doSegment method is called with the
42// a continous buffer for the full segment (number of blocks).
43
44void expand3_4Kernel::generateDoSegmentMethod() const {
45    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
46    Module * m = iBuilder->getModule();
47    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
48    BasicBlock * expand2_3entry = BasicBlock::Create(iBuilder->getContext(), "expand2_3entry", doSegmentFunction, 0);
49    iBuilder->SetInsertPoint(expand2_3entry);
50    BasicBlock * expand_3_4_loop = BasicBlock::Create(iBuilder->getContext(), "expand_3_4_loop", doSegmentFunction, 0);
51    BasicBlock * expand3_4_loop_exit = BasicBlock::Create(iBuilder->getContext(), "expand3_4_loop_exit", doSegmentFunction, 0);
52    BasicBlock * finalStep1 = BasicBlock::Create(iBuilder->getContext(), "finalStep1", doSegmentFunction, 0);
53    BasicBlock * finalStep2 = BasicBlock::Create(iBuilder->getContext(), "finalStep2", doSegmentFunction, 0);
54    BasicBlock * step2load = BasicBlock::Create(iBuilder->getContext(), "step2load", doSegmentFunction, 0);
55    BasicBlock * step2store = BasicBlock::Create(iBuilder->getContext(), "step2store", doSegmentFunction, 0);
56    BasicBlock * finalStep3 = BasicBlock::Create(iBuilder->getContext(), "finalStep3", doSegmentFunction, 0);
57    BasicBlock * step3load = BasicBlock::Create(iBuilder->getContext(), "step3load", doSegmentFunction, 0);
58    BasicBlock * step3store = BasicBlock::Create(iBuilder->getContext(), "step3store", doSegmentFunction, 0);
59    BasicBlock * step3store2 = BasicBlock::Create(iBuilder->getContext(), "step3store2", doSegmentFunction, 0);
60    BasicBlock * itemsDone = BasicBlock::Create(iBuilder->getContext(), "itemsDone", doSegmentFunction, 0);
61    BasicBlock * setTermination = BasicBlock::Create(iBuilder->getContext(), "setTermination", doSegmentFunction, 0);
62    BasicBlock * expand3_4_exit = BasicBlock::Create(iBuilder->getContext(), "expand3_4_exit", doSegmentFunction, 0);
63    BasicBlock * finalExit = BasicBlock::Create(iBuilder->getContext(), "finalExit", doSegmentFunction, 0);
64   
65    // Determine the require shufflevector constants.
66    const unsigned PACK_SIZE = iBuilder->getStride()/8;
67   
68    // Construct a list of indexes in  the form
69    // 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, ...
70    unsigned sourceByteIndex = 0;
71    unsigned expand3_4_index[PACK_SIZE];
72    for (unsigned i = 0; i < PACK_SIZE; i++) {
73        expand3_4_index[i] = sourceByteIndex;
74        if (i % 4 != 2) sourceByteIndex++;
75    }
76    unsigned const expand3_4_offset[4] = {PACK_SIZE, 3*PACK_SIZE/4, PACK_SIZE/2, PACK_SIZE/4};
77    Value * expand_3_4_shuffle[4];
78    for (unsigned j = 0; j < 4; j++) {
79        std::vector<Constant *> Idxs;
80        for (unsigned i = 0; i < PACK_SIZE; i++) {
81            Idxs.push_back(ConstantInt::get(iBuilder->getInt32Ty(), expand3_4_offset[j] + expand3_4_index[i]));
82        }
83        expand_3_4_shuffle[j] = ConstantVector::get(Idxs);
84    }
85    Constant * Const3 = iBuilder->getSize(3);
86    Constant * Const4 = iBuilder->getSize(4);
87    Constant * stride = iBuilder->getSize(iBuilder->getStride());
88    Constant * packSize = iBuilder->getSize(PACK_SIZE);
89    Constant * loopItemCount = iBuilder->getSize(3 * PACK_SIZE); // 3 packs per loop.
90    UndefValue * undefPack = UndefValue::get(iBuilder->fwVectorType(8));
91   
92    const unsigned packAlign = iBuilder->getBitBlockWidth()/8;
93    Function::arg_iterator args = doSegmentFunction->arg_begin();
94    Value * self = &*(args++);
95    Value * blocksToDo = &*(args);
96    Value * streamStructPtr = getStreamSetStructPtr(self, "sourceStream");
97
98    LoadInst * producerPos = iBuilder->CreateAtomicLoadAcquire(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr));
99    Value * processed = getProcessedItemCount(self, "sourceStream");
100    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
101   
102    // Except for the final segment, we always process an integral number of triple blocks.
103    Value * tripleBlocksToDo = iBuilder->CreateMul(blocksToDo, Const3);
104    Constant * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
105    Value * tripleItemMax = iBuilder->CreateMul(tripleBlocksToDo, blockItems);
106
107    Value * lessThanFullSegment = iBuilder->CreateICmpULT(itemsAvail, tripleItemMax);
108    Value * tripleBlockItems = iBuilder->CreateSelect(lessThanFullSegment, itemsAvail, tripleItemMax);
109
110    Value * endSignal = iBuilder->CreateLoad(mStreamSetInputBuffers[0]->getEndOfInputPtr(streamStructPtr));
111    Value * inFinalSegment = iBuilder->CreateAnd(endSignal, lessThanFullSegment);
112    Value * itemsToDo = iBuilder->CreateSelect(inFinalSegment, itemsAvail, tripleBlockItems);
113
114//    iBuilder->CallPrintInt("itemsToDo", itemsToDo);
115
116    Value * blockNo = getScalarField(self, blockNoScalar);
117
118    // A block is made up of 8 packs.  Get the pointer to the first pack (changes the type of the pointer only).
119    Value * sourcePackPtr = getStream(self, "sourceStream", blockNo, iBuilder->getInt32(0), iBuilder->getInt32(0));
120
121    Value * outputGenerated = getProducedItemCount(self, "expandedStream"); // bytes previously generated to output
122    Value * outputBlockNo = iBuilder->CreateUDiv(outputGenerated, stride);
123    Value * outputPackPtr = getStream(self, "expandedStream", outputBlockNo, iBuilder->getInt32(0), iBuilder->getInt32(0));
124
125    Value * hasFullLoop = iBuilder->CreateICmpUGE(itemsToDo, loopItemCount);
126
127
128    iBuilder->CreateCondBr(hasFullLoop, expand_3_4_loop, expand3_4_loop_exit);
129    iBuilder->SetInsertPoint(expand_3_4_loop);
130    PHINode * loopInput_ptr = iBuilder->CreatePHI(sourcePackPtr->getType(), 2);
131    PHINode * loopOutput_ptr = iBuilder->CreatePHI(outputPackPtr->getType(), 2);
132    PHINode * loopItemsRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
133
134    loopInput_ptr->addIncoming(sourcePackPtr, expand2_3entry);
135    loopOutput_ptr->addIncoming(outputPackPtr, expand2_3entry);
136    loopItemsRemain->addIncoming(itemsToDo, expand2_3entry);
137
138    // Step 1 of the main loop.
139    Value * pack0 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(loopInput_ptr, packAlign));
140    Value * expand0 = iBuilder->bitCast(iBuilder->CreateShuffleVector(undefPack, pack0, expand_3_4_shuffle[0]));
141    iBuilder->CreateAlignedStore(expand0, loopOutput_ptr, packAlign);
142    // Step 2 of the main loop.
143    Value * inPack1_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(1));
144    Value * outPack1_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(1));
145    Value * pack1 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack1_ptr, packAlign));
146    Value * expand1 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack0, pack1, expand_3_4_shuffle[1]));
147    iBuilder->CreateAlignedStore(expand1, outPack1_ptr, packAlign);
148    // Step 3 of the main loop.
149    Value * inPack2_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(2));
150    Value * outPack2_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(2));
151    Value * pack2 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack2_ptr, packAlign));
152    Value * expand2 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack1, pack2, expand_3_4_shuffle[2]));
153    iBuilder->CreateAlignedStore(expand2, outPack2_ptr, packAlign);
154    Value * outPack3_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(3));
155    Value * expand3 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack2, undefPack, expand_3_4_shuffle[3]));
156    iBuilder->CreateAlignedStore(expand3, outPack3_ptr, packAlign);
157
158    Value * loopNextInputPack = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(3));
159
160
161
162    Value * remainingItems = iBuilder->CreateSub(loopItemsRemain, loopItemCount);
163
164    Value * loopProcessed = iBuilder->CreateSub(itemsToDo, remainingItems);
165    loopProcessed = iBuilder->CreateMul(iBuilder->CreateUDiv(loopProcessed, iBuilder->getInt64(3)), iBuilder->getInt64(4));
166
167    Value * loopNextOutputPack;
168    loopNextOutputPack = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(4));
169
170    loopInput_ptr->addIncoming(loopNextInputPack, expand_3_4_loop);
171    loopOutput_ptr->addIncoming(loopNextOutputPack, expand_3_4_loop);
172    loopItemsRemain->addIncoming(remainingItems, expand_3_4_loop);
173
174    Value * continueLoop = iBuilder->CreateICmpUGE(remainingItems, loopItemCount);
175    iBuilder->CreateCondBr(continueLoop, expand_3_4_loop, expand3_4_loop_exit);
176
177    // Except for the final segment, the number of items remaining is now 0.
178    // For the final segment, less than loopItemCount items remain.
179    iBuilder->SetInsertPoint(expand3_4_loop_exit);
180    PHINode * loopExitInput_ptr = iBuilder->CreatePHI(sourcePackPtr->getType(), 2);
181    PHINode * loopExitOutput_ptr = iBuilder->CreatePHI(outputPackPtr->getType(), 2);
182    PHINode * loopExitItemsRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
183    loopExitInput_ptr->addIncoming(sourcePackPtr, expand2_3entry);
184    loopExitOutput_ptr->addIncoming(outputPackPtr, expand2_3entry);
185    loopExitItemsRemain->addIncoming(itemsToDo, expand2_3entry);
186    loopExitInput_ptr->addIncoming(loopNextInputPack, expand_3_4_loop);
187    loopExitOutput_ptr->addIncoming(loopNextOutputPack, expand_3_4_loop);
188    loopExitItemsRemain->addIncoming(remainingItems, expand_3_4_loop);
189    // There may be one or two remaining full packs and/or a partial pack.
190    //
191    // We have several cases depending on the number of reumaing items.  Let N = packSize
192    // (a) 0 remaining items: all done
193    // (b) 1..3N/4 remaining items:  do Step1 only, no items or pending data will remain
194    // (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.
195    // (d) N+1 .. 6N/4 remaining items:  do Step 1 and Step 2, no items or pending data will remain.
196    // (e) 6N/4+1 .. 2N remaining items: do Steps 1 and 2, do Step 3 for pending data only, there is no more input.
197    // (f) 2N+1 .. 9N/4 remaining items: do Steps 1 and 2, do Step 3 up to the first write only.
198    // (g) 9N/4+1 .. 3N - 1 remaining items: do Steps 1, 2 and 3.
199    Value * condition_a = iBuilder->CreateICmpEQ(loopExitItemsRemain, ConstantInt::getNullValue(iBuilder->getSizeTy()));
200    iBuilder->CreateCondBr(condition_a, itemsDone, finalStep1);
201    // Final Step1 processing
202    iBuilder->SetInsertPoint(finalStep1);
203    pack0 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(loopExitInput_ptr, packAlign));
204    expand0 = iBuilder->bitCast(iBuilder->CreateShuffleVector(undefPack, pack0, expand_3_4_shuffle[0]));
205    iBuilder->CreateAlignedStore(expand0, loopExitOutput_ptr, packAlign);
206    Value * condition_b = iBuilder->CreateICmpULE(loopExitItemsRemain, iBuilder->getSize(3 * PACK_SIZE/4));
207    iBuilder->CreateCondBr(condition_b, itemsDone, finalStep2);
208    // Final Step 2 processing
209    iBuilder->SetInsertPoint(finalStep2);
210    Value * condition_c = iBuilder->CreateICmpULE(loopExitItemsRemain, packSize);
211    iBuilder->CreateCondBr(condition_c, step2store, step2load);
212    iBuilder->SetInsertPoint(step2load);
213    inPack1_ptr = iBuilder->CreateGEP(loopExitInput_ptr, iBuilder->getInt32(1));
214    pack1 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack1_ptr, packAlign));
215    iBuilder->CreateBr(step2store);
216    iBuilder->SetInsertPoint(step2store);
217    PHINode * pack1phi = iBuilder->CreatePHI(iBuilder->fwVectorType(8), 2);
218    pack1phi->addIncoming(undefPack, finalStep2);
219    pack1phi->addIncoming(pack1, step2load);
220    outPack1_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(1));
221    expand1 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack0, pack1phi, expand_3_4_shuffle[1]));
222    iBuilder->CreateAlignedStore(expand1, outPack1_ptr, packAlign);
223    Value * condition_d = iBuilder->CreateICmpULE(loopExitItemsRemain, iBuilder->getSize(6 * PACK_SIZE/4));
224    iBuilder->CreateCondBr(condition_d, itemsDone, finalStep3);
225    // Final Step 3
226    iBuilder->SetInsertPoint(finalStep3);
227    Value * condition_e = iBuilder->CreateICmpULE(loopExitItemsRemain, iBuilder->getSize(2 * PACK_SIZE));
228    iBuilder->CreateCondBr(condition_e, step3store, step3load);
229    iBuilder->SetInsertPoint(step3load);
230    inPack2_ptr = iBuilder->CreateGEP(loopExitInput_ptr, iBuilder->getInt32(2));
231    pack2 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack2_ptr, packAlign));
232    iBuilder->CreateBr(step3store);
233    iBuilder->SetInsertPoint(step3store);
234    PHINode * pack2phi = iBuilder->CreatePHI(iBuilder->fwVectorType(8), 2);
235    pack2phi->addIncoming(undefPack, finalStep3);
236    pack2phi->addIncoming(pack2, step3load);
237    outPack2_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(2));
238    expand2 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack1phi, pack2phi, expand_3_4_shuffle[2]));
239    iBuilder->CreateAlignedStore(expand2, outPack2_ptr, packAlign);
240    Value * condition_f = iBuilder->CreateICmpULE(loopExitItemsRemain, iBuilder->getSize(9 * PACK_SIZE/4));
241    iBuilder->CreateCondBr(condition_f, itemsDone, step3store2);
242    iBuilder->SetInsertPoint(step3store2);
243    outPack3_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(3));
244    expand3 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack2phi, undefPack, expand_3_4_shuffle[3]));
245    iBuilder->CreateAlignedStore(expand3, outPack3_ptr, packAlign);
246    iBuilder->CreateBr(itemsDone);
247    //
248    iBuilder->SetInsertPoint(itemsDone);
249
250    processed = iBuilder->CreateAdd(processed, itemsToDo);
251    setProcessedItemCount(self, "sourceStream", processed);
252
253    setScalarField(self, blockNoScalar, iBuilder->CreateUDiv(processed, stride));
254    // We have produced 4 output bytes for every 3 input bytes.  If the number of input
255    // bytes is not a multiple of 3, then we have one more output byte for each excess
256    // input byte.
257    Value * totalProduced = iBuilder->CreateAdd(iBuilder->CreateMul(iBuilder->CreateUDiv(processed, Const3), Const4), iBuilder->CreateURem(processed, Const3));
258    setProducedItemCount(self, "expandedStream", totalProduced);
259    Value * ssStructPtr = getStreamSetStructPtr(self, "expandedStream");
260
261    Value * producerPosPtr = mStreamSetOutputBuffers[0]->getProducerPosPtr(ssStructPtr);
262
263    iBuilder->CreateAtomicStoreRelease(totalProduced, producerPosPtr);
264   
265    iBuilder->CreateCondBr(inFinalSegment, setTermination, expand3_4_exit);
266    iBuilder->SetInsertPoint(setTermination);
267#ifndef NDEBUG
268//    iBuilder->CallPrintInt(mKernelName + " termination in segment ", segmentNo);
269#endif
270    setTerminationSignal(self);
271    mStreamSetOutputBuffers[0]->setEndOfInput(ssStructPtr);
272    iBuilder->CreateBr(expand3_4_exit);
273    iBuilder->SetInsertPoint(expand3_4_exit);
274    // Must be the last action, for synchronization.
275    iBuilder->CreateBr(finalExit);
276   
277    iBuilder->SetInsertPoint(finalExit);
278    iBuilder->CreateRetVoid();
279    iBuilder->restoreIP(savePoint);
280}
281
282
283// The doBlock method is deprecated.   But in case it is used, just call doSegment with
284// 1 as the number of blocks to do.
285void expand3_4Kernel::generateDoBlockMethod() const {
286    auto savePoint = iBuilder->saveIP();
287    Module * m = iBuilder->getModule();
288    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
289    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
290    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
291    Value * self = getParameter(doBlockFunction, "self");
292    iBuilder->CreateCall(doSegmentFunction, {self, iBuilder->getSize(1)});
293    iBuilder->CreateRetVoid();
294    iBuilder->restoreIP(savePoint);
295}
296
297// Radix 64 determination, converting 3 bytes to 4 6-bit values.
298//
299//  00000000|zyxwvuts|rqpmnlkj|hgfedcba    Original
300//           zy                            bits to move 6 positions right
301//             xwvuts                      bits to move 8 positions left
302//                    rqpm                 bits to move 4 positions right
303//                        nlkj             bits to move 10 positions left
304//                             hqfedc      bits to move 2 positions right
305//                                   ba    bits to move 12 positions left
306//    xwvuts|  nlkjzy|  barqpm|  hgfedc    Target
307void radix64Kernel::generateDoBlockLogic(Value * self, Value * blockNo) const {
308
309    Value * step_right_6 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00C00000));
310    Value * step_left_8 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x003F0000));
311    Value * step_right_4 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x0000F000));
312    Value * step_left_10 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000F00));
313    Value * step_right_2 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x000000FC));
314    Value * step_left_12 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000003));
315   
316    for (unsigned i = 0; i < 8; i++) {
317        Value * expandedStream = getStream(self, "expandedStream", blockNo, iBuilder->getInt32(0), iBuilder->getInt32(i));
318        Value * bytepack = iBuilder->CreateBlockAlignedLoad(expandedStream);
319
320        Value * right_6_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_6), 6);
321        Value * right_4_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_4), 4);
322        Value * right_2_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_2), 2);
323        Value * left_8_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_8), 8);
324        Value * left_10_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_10), 10);
325        Value * left_12_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_12), 12);
326
327        Value * mid = right_6_result;
328        mid = iBuilder->simd_or(mid, right_4_result);
329        mid = iBuilder->simd_or(mid, right_2_result);
330        mid = iBuilder->simd_or(mid, left_8_result);
331        mid = iBuilder->simd_or(mid, left_10_result);
332        mid = iBuilder->simd_or(mid, left_12_result);
333        Value * radix64pack = iBuilder->bitCast(mid);
334
335        Value * radix64stream = getStream(self, "radix64stream",blockNo, iBuilder->getInt32(0), iBuilder->getInt32(i));
336        iBuilder->CreateBlockAlignedStore(radix64pack, radix64stream);
337    }
338    Value * produced = getProducedItemCount(self, "radix64stream");
339    produced = iBuilder->CreateAdd(produced, iBuilder->getSize(iBuilder->getStride()));
340    setProducedItemCount(self, "radix64stream", produced);   
341}
342
343void radix64Kernel::generateFinalBlockMethod() const {
344    auto savePoint = iBuilder->saveIP();
345    Module * m = iBuilder->getModule();
346    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
347    BasicBlock * radix64_fb_entry = BasicBlock::Create(iBuilder->getContext(), "radix64_fb_entry", finalBlockFunction, 0);
348    iBuilder->SetInsertPoint(radix64_fb_entry);
349    BasicBlock * radix64_loop = BasicBlock::Create(iBuilder->getContext(), "radix64_loop", finalBlockFunction, 0);
350    BasicBlock * loopExit = BasicBlock::Create(iBuilder->getContext(), "loopExit", finalBlockFunction, 0);
351    BasicBlock * handleRemainFirstByte = BasicBlock::Create(iBuilder->getContext(), "handleRemainFirstByte", finalBlockFunction, 0);
352    BasicBlock * handleRemainSecondByte = BasicBlock::Create(iBuilder->getContext(), "handleRemainSecondByte", finalBlockFunction, 0);
353    BasicBlock * handleNoRemainSecondByte = BasicBlock::Create(iBuilder->getContext(), "handleNoRemainSecondByte", finalBlockFunction, 0);
354    BasicBlock * fbExit = BasicBlock::Create(iBuilder->getContext(), "fbExit", finalBlockFunction, 0);
355    // Final Block arguments: self, remaining.
356    Function::arg_iterator args = finalBlockFunction->arg_begin();
357    Value * self = &*(args++);
358    Value * remainingBytes = &*(args++);
359    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
360
361    const unsigned PACK_SIZE = iBuilder->getStride()/8;
362    Constant * packSize = iBuilder->getSize(PACK_SIZE);
363    Value * blockNo = getScalarField(self, blockNoScalar);
364
365    Value * step_right_6 = iBuilder->simd_fill(32, iBuilder->getInt32(0x00C00000));
366    Value * step_left_8 = iBuilder->simd_fill(32, iBuilder->getInt32(0x003F0000));
367    Value * step_right_4 = iBuilder->simd_fill(32, iBuilder->getInt32(0x0000F000));
368    Value * step_left_10 = iBuilder->simd_fill(32, iBuilder->getInt32(0x00000F00));
369    Value * step_right_2 = iBuilder->simd_fill(32, iBuilder->getInt32(0x000000FC));
370    Value * step_left_12 = iBuilder->simd_fill(32, iBuilder->getInt32(0x00000003));
371
372
373    // Enter the loop only if there is at least one byte remaining to process.
374    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, radix64_loop);
375
376    iBuilder->SetInsertPoint(radix64_loop);
377    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
378    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
379    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), radix64_fb_entry);
380    loopRemain->addIncoming(remainingBytes, radix64_fb_entry);
381
382    Value * expandedStreamLoopPtr = getStream(self, "expandedStream", blockNo, iBuilder->getInt32(0), idx);
383    Value * bytepack = iBuilder->CreateBlockAlignedLoad(expandedStreamLoopPtr);
384    Value * right_6_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_6), 6);
385    Value * right_4_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_4), 4);
386    Value * right_2_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_2), 2);
387    Value * left_8_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_8), 8);
388    Value * left_10_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_10), 10);
389    Value * left_12_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_12), 12);
390
391    Value * mid = right_6_result;
392    mid = iBuilder->simd_or(mid, right_4_result);
393    mid = iBuilder->simd_or(mid, right_2_result);
394    mid = iBuilder->simd_or(mid, left_8_result);
395    mid = iBuilder->simd_or(mid, left_10_result);
396    mid = iBuilder->simd_or(mid, left_12_result);
397    Value * radix64pack = iBuilder->bitCast(mid);
398
399    Value * radix64streamPtr = getStream(self, "radix64stream", blockNo, iBuilder->getInt32(0), idx);
400    iBuilder->CreateBlockAlignedStore(radix64pack, radix64streamPtr);
401
402    Value* nextIdx = iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1));
403    idx->addIncoming(nextIdx, radix64_loop);
404    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
405    loopRemain->addIncoming(remainAfterLoop, radix64_loop);
406
407    Value* continueLoop = iBuilder->CreateICmpULT(remainAfterLoop, packSize);
408    iBuilder->CreateCondBr(continueLoop, radix64_loop, loopExit);
409
410    iBuilder->SetInsertPoint(loopExit);
411    // All base64 data has been computed, but we may need to set one or two '=' padding bytes.
412    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(0)), fbExit, handleRemainFirstByte);
413    iBuilder->SetInsertPoint(handleRemainFirstByte);
414    // At least one padding byte required.
415    Value * i8input_ptr = getStreamView(iBuilder->getInt8PtrTy(), self, "expandedStream", blockNo, iBuilder->getInt32(0));
416    Value * remainOutputStart = iBuilder->CreateSub(remainingBytes, remainMod4);
417
418    Value * firstRemainByte = iBuilder->CreateLoad(i8input_ptr);
419
420    Value * first_move_right_2_mask = ConstantInt::get(iBuilder->getInt8Ty(), 0xFC);
421    Value * first_output_byte = iBuilder->CreateLShr(iBuilder->CreateAnd(firstRemainByte, first_move_right_2_mask), 2);
422
423    Value * first_move_left_4_mask = ConstantInt::get(iBuilder->getInt8Ty(), 0x03);
424    Value * first_move_left_4_byte = iBuilder->CreateShl(iBuilder->CreateAnd(firstRemainByte, first_move_left_4_mask), 4);
425
426
427    Value * i8OutPtr0 = getStreamView(iBuilder->getInt8PtrTy(), self, "radix64stream", blockNo, remainOutputStart);
428
429    iBuilder->CreateStore(first_output_byte, i8OutPtr0);
430
431    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(1)), handleNoRemainSecondByte, handleRemainSecondByte);
432    iBuilder->SetInsertPoint(handleRemainSecondByte);
433
434    Value * secondRemainByte = iBuilder->CreateLoad(iBuilder->CreateGEP(i8input_ptr, iBuilder->getInt32(1)));
435    Value * second_move_right_4_mask = ConstantInt::get(iBuilder->getInt8Ty(), 0xF0);
436    Value * second_move_right_4_byte = iBuilder->CreateLShr(iBuilder->CreateAnd(secondRemainByte, second_move_right_4_mask), 4);
437    Value * second_output_byte = iBuilder->CreateOr(first_move_left_4_byte, second_move_right_4_byte);
438
439    Value * i8OutPtr1 = getStreamView(iBuilder->getInt8PtrTy(), self, "radix64stream", blockNo, iBuilder->CreateAdd(remainOutputStart, iBuilder->getInt64(1)));
440
441    iBuilder->CreateStore(second_output_byte, i8OutPtr1);
442
443    Value * second_move_left_2_mask = ConstantInt::get(iBuilder->getInt8Ty(), 0x0F);
444    Value * second_move_left_2_byte = iBuilder->CreateShl(iBuilder->CreateAnd(secondRemainByte, second_move_left_2_mask), 2);
445
446    Value * i8OutPtr2 = getStreamView(iBuilder->getInt8PtrTy(), self, "radix64stream", blockNo, iBuilder->CreateAdd(remainOutputStart, iBuilder->getInt64(2)));
447
448    iBuilder->CreateStore(second_move_left_2_byte, i8OutPtr2);
449    iBuilder->CreateBr(fbExit);
450
451    iBuilder->SetInsertPoint(handleNoRemainSecondByte);
452
453    i8OutPtr1 = getStreamView(iBuilder->getInt8PtrTy(), self, "radix64stream", blockNo, iBuilder->CreateAdd(remainOutputStart, iBuilder->getInt64(1)));
454
455    iBuilder->CreateStore(first_move_left_4_byte, i8OutPtr1);
456    iBuilder->CreateBr(fbExit);
457
458    iBuilder->SetInsertPoint(fbExit);
459    Value * outputNumberAdd = iBuilder->CreateSelect(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(0)), iBuilder->getSize(0), iBuilder->getSize(1));
460    Value * produced = iBuilder->CreateAdd(getProducedItemCount(self, "radix64stream"), iBuilder->CreateAdd(remainingBytes, outputNumberAdd));
461    setProducedItemCount(self, "radix64stream", produced);
462
463    iBuilder->CreateRetVoid();
464    iBuilder->restoreIP(savePoint);
465}
466
467void radix64Kernel::generateDoBlockMethod() const {
468    auto savePoint = iBuilder->saveIP();
469
470    Function * doBlockFunction = iBuilder->getModule()->getFunction(mKernelName + doBlock_suffix);
471
472    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
473
474    Value * self = getParameter(doBlockFunction, "self");
475    Value * blockNo = getScalarField(self, blockNoScalar);
476
477    generateDoBlockLogic(self, blockNo);
478
479    iBuilder->CreateRetVoid();
480    iBuilder->restoreIP(savePoint);
481}
482
483
484void base64Kernel::generateDoBlockLogic(Value * self, Value * blockNo) const {       
485    for (unsigned i = 0; i < 8; i++) {
486        Value * radix64stream_ptr = getStream(self, "radix64stream", blockNo, iBuilder->getInt32(0), iBuilder->getInt32(i));
487        Value * bytepack = iBuilder->CreateBlockAlignedLoad(radix64stream_ptr);
488        Value * mask_gt_25 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(25)));
489        Value * mask_gt_51 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(51)));
490        Value * mask_eq_62 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(62)));
491        Value * mask_eq_63 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(63)));
492        // Strategy:
493        // 1. add ord('A') = 65 to all radix64 values, this sets the correct values for entries 0 to 25.
494        // 2. add ord('a') - ord('A') - (26 - 0) = 6 to all values >25, this sets the correct values for entries 0 to 51
495        // 3. subtract ord('a') - ord('0') + (52 - 26) = 75 to all values > 51, this sets the correct values for entries 0 to 61
496        // 4. subtract ord('0') - ord('+') + (62 - 52) = 15 for all values = 62
497        // 4. subtract ord('0') - ord('/') + (63 - 62) = 2 for all values = 63
498        Value * t0_25 = iBuilder->simd_add(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8('A')));
499        Value * t0_51 = iBuilder->simd_add(8, t0_25, iBuilder->simd_and(mask_gt_25, iBuilder->simd_fill(8, iBuilder->getInt8(6))));
500        Value * t0_61 = iBuilder->simd_sub(8, t0_51, iBuilder->simd_and(mask_gt_51, iBuilder->simd_fill(8, iBuilder->getInt8(75))));
501        Value * t0_62 = iBuilder->simd_sub(8, t0_61, iBuilder->simd_and(mask_eq_62, iBuilder->simd_fill(8, iBuilder->getInt8(15))));
502        Value * base64pack = iBuilder->simd_sub(8, t0_62, iBuilder->simd_and(mask_eq_63, iBuilder->simd_fill(8, iBuilder->getInt8(2))));
503        Value * base64stream_ptr = getStream(self, "base64stream", blockNo, iBuilder->getInt32(0), iBuilder->getInt32(i));
504        iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(base64pack), base64stream_ptr);
505    }
506    Value * produced = getProducedItemCount(self, "base64stream");
507    produced = iBuilder->CreateAdd(produced, iBuilder->getSize(iBuilder->getStride()));
508    setProducedItemCount(self, "base64stream", produced);
509}
510
511
512// Special processing for the base 64 format.   The output must always contain a multiple
513// of 4 bytes.   When the number of radix 64 values is not a multiple of 4
514// number of radix 64 values
515void base64Kernel::generateFinalBlockMethod() const {
516    auto savePoint = iBuilder->saveIP();
517    Module * m = iBuilder->getModule();
518    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
519    BasicBlock * base64_fb_entry = BasicBlock::Create(iBuilder->getContext(), "base64_fb_entry", finalBlockFunction, 0);
520    iBuilder->SetInsertPoint(base64_fb_entry);
521    BasicBlock * base64_loop = BasicBlock::Create(iBuilder->getContext(), "base64_loop", finalBlockFunction, 0);
522    BasicBlock * loopExit = BasicBlock::Create(iBuilder->getContext(), "loopExit", finalBlockFunction, 0);
523    BasicBlock * doPadding = BasicBlock::Create(iBuilder->getContext(), "doPadding", finalBlockFunction, 0);
524    BasicBlock * doPadding2 = BasicBlock::Create(iBuilder->getContext(), "doPadding2", finalBlockFunction, 0);
525    BasicBlock * fbExit = BasicBlock::Create(iBuilder->getContext(), "fbExit", finalBlockFunction, 0);
526    // Final Block arguments: self, remaining.
527    Function::arg_iterator args = finalBlockFunction->arg_begin();
528    Value * self = &*(args++);
529    Value * remainingBytes = &*(args++);
530    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
531    Value * padBytes = iBuilder->CreateSub(iBuilder->getSize(4), remainMod4);
532    padBytes = iBuilder->CreateAnd(padBytes, iBuilder->getSize(3));
533
534    Constant * packSize = iBuilder->getSize(iBuilder->getStride() / 8);
535    Value * blockNo = getScalarField(self, blockNoScalar);
536
537    // Enter the loop only if there is at least one byte remaining to process.
538    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, base64_loop);
539   
540    iBuilder->SetInsertPoint(base64_loop);
541    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
542    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
543    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), base64_fb_entry);
544    loopRemain->addIncoming(remainingBytes, base64_fb_entry);
545    Value * radix64streamPtr = getStream(self, "radix64stream", blockNo, iBuilder->getInt32(0), idx);
546    Value * bytepack = iBuilder->CreateBlockAlignedLoad(radix64streamPtr);
547    Value * mask_gt_25 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(25)));
548    Value * mask_gt_51 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(51)));
549    Value * mask_eq_62 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(62)));
550    Value * mask_eq_63 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(63)));
551    Value * t0_25 = iBuilder->simd_add(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8('A')));
552    Value * t0_51 = iBuilder->simd_add(8, t0_25, iBuilder->simd_and(mask_gt_25, iBuilder->simd_fill(8, iBuilder->getInt8(6))));
553    Value * t0_61 = iBuilder->simd_sub(8, t0_51, iBuilder->simd_and(mask_gt_51, iBuilder->simd_fill(8, iBuilder->getInt8(75))));
554    Value * t0_62 = iBuilder->simd_sub(8, t0_61, iBuilder->simd_and(mask_eq_62, iBuilder->simd_fill(8, iBuilder->getInt8(15))));
555    Value * base64pack = iBuilder->simd_sub(8, t0_62, iBuilder->simd_and(mask_eq_63, iBuilder->simd_fill(8, iBuilder->getInt8(2))));
556    Value * base64streamPtr = getStream(self, "base64stream", blockNo, iBuilder->getInt32(0), idx);
557    iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(base64pack), base64streamPtr);
558    idx->addIncoming(iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1)), base64_loop);
559    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
560    loopRemain->addIncoming(remainAfterLoop, base64_loop);
561
562    Value* continueLoop = iBuilder->CreateICmpULT(remainAfterLoop, packSize);
563    iBuilder->CreateCondBr(continueLoop, base64_loop, loopExit);
564
565    iBuilder->SetInsertPoint(loopExit);
566    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(padBytes, iBuilder->getSize(0)), fbExit, doPadding);
567
568    iBuilder->SetInsertPoint(doPadding);
569    Value * i8output_ptr = getStreamView(iBuilder->getInt8PtrTy(), self, "base64stream", blockNo, iBuilder->getInt32(0));
570    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, remainingBytes));
571    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(3)), fbExit, doPadding2);
572    iBuilder->SetInsertPoint(doPadding2);
573    Value * finalPadPos = iBuilder->CreateAdd(remainingBytes, iBuilder->getSize(1));
574    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, finalPadPos));
575    iBuilder->CreateBr(fbExit);
576    iBuilder->SetInsertPoint(fbExit);
577    Value * produced = iBuilder->CreateAdd(getProducedItemCount(self, "base64stream"), iBuilder->CreateAdd(remainingBytes, padBytes));
578    setProducedItemCount(self, "base64stream", produced);
579    iBuilder->CreateRetVoid();
580    iBuilder->restoreIP(savePoint);
581}
582
583void base64Kernel::generateDoBlockMethod() const {
584    auto savePoint = iBuilder->saveIP();
585
586    Function * doBlockFunction = iBuilder->getModule()->getFunction(mKernelName + doBlock_suffix);
587
588    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
589
590    Value * self = getParameter(doBlockFunction, "self");
591    Value * blockNo = getScalarField(self, blockNoScalar);
592
593    generateDoBlockLogic(self, blockNo);
594
595    iBuilder->CreateRetVoid();
596    iBuilder->restoreIP(savePoint);
597}
598
599}
Note: See TracBrowser for help on using the repository browser.