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

Last change on this file since 5267 was 5267, checked in by nmedfort, 3 years ago

Code clean-up. Removed Pablo Call, SetIthBit? and Prototype.

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