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

Last change on this file since 5594 was 5594, checked in by cameron, 21 months ago

Simplification of MultiBlockKernel? builder: stream set pointers are to the block base position

File size: 17.5 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 <kernels/kernel_builder.h>
8
9using namespace llvm;
10
11namespace kernel {
12
13// This kernel produces an expanded input stream by duplicating every third byte.
14// It is implemented using SIMD shufflevector operations.  With 16-byte registers,
15// a single shufflevector operation produces 16 bytes of output data from the
16// 12 bytes of input data.   With 32-byte registers, 32 bytes of output data are
17// produced from 24 bytes of input data.
18//
19// Using aligned SIMD loads, an inner loop processes three registers full of input
20// data (i.e., three BytePacks) to produce four registers full of output.   This is
21// a 3 step process.
22// Step 1:  Load input_pack0, apply the shuffle operation to produce output_pack0.
23//          At this point 3/4 of the data in input_pack0 has been processed.
24// Step 2:  Load input_pack1, apply a shuffle operation to use the remaining
25//          1/4 of input_pack0 and 1/2 of input_pack1 to produce output_pack1.
26//          At this point 1/2 of the data in input_pack1 has been processed.
27// Step 3:  Load input_pack2, apply a shuffle operation to use the remaining 1/2
28//          of input_pack1 and 1/4 of input_pack2 to produce output_pack2.
29//          Then apply a further shuffle opertaion to use the remaining 3/4 of
30//          input_pack2 to produce output_pack3.
31
32// The MultiBlockLogic is based on a natural stride taking 3 packs at a time.
33// In this case, the output produced is exactly 4 packs or 4 blocks, with no pending
34// data maintained in the kernel state.
35//
36// When processing the final partial stride of data, the kernel performs full
37// triple-pack processing for each full or partial triple-pack remaining,
38// relying on the MultiBlockKernel builder to only copy the correct number
39// of bytes to the actual output stream.
40
41void expand3_4Kernel::generateMultiBlockLogic(const std::unique_ptr<KernelBuilder> & iBuilder) {
42
43    BasicBlock * expand2_3entry = iBuilder->GetInsertBlock();
44    BasicBlock * expand_3_4_loop = iBuilder->CreateBasicBlock("expand_3_4_loop");
45    BasicBlock * expand3_4_exit = iBuilder->CreateBasicBlock("expand3_4_exit");
46   
47    // Determine the require shufflevector constants.
48    const unsigned PACK_SIZE = iBuilder->getBitBlockWidth()/8;
49   
50    // Construct a list of indexes in  the form
51    // 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, ...
52    unsigned sourceByteIndex = 0;
53    unsigned expand3_4_index[PACK_SIZE];
54    for (unsigned i = 0; i < PACK_SIZE; i++) {
55        expand3_4_index[i] = sourceByteIndex;
56        if (i % 4 != 2) sourceByteIndex++;
57    }
58    unsigned const expand3_4_offset[4] = {PACK_SIZE, 3*PACK_SIZE/4, PACK_SIZE/2, PACK_SIZE/4};
59    Value * expand_3_4_shuffle[4];
60    for (unsigned j = 0; j < 4; j++) {
61        std::vector<Constant *> Idxs;
62        for (unsigned i = 0; i < PACK_SIZE; i++) {
63            Idxs.push_back(ConstantInt::get(iBuilder->getInt32Ty(), expand3_4_offset[j] + expand3_4_index[i]));
64        }
65        expand_3_4_shuffle[j] = ConstantVector::get(Idxs);
66    }
67
68    Constant * triplePackSize = iBuilder->getSize(3 * PACK_SIZE); // 3 packs per loop.
69    UndefValue * undefPack = UndefValue::get(iBuilder->fwVectorType(8));
70   
71    const unsigned packAlign = iBuilder->getBitBlockWidth()/8;
72
73    Function::arg_iterator args = mCurrentMethod->arg_begin();
74   
75    /* self = */ args++;
76    Value * itemsToDo = &*(args++);
77    Value * sourceStream = &*(args++);
78    Value * expandedStream = &*(args);
79
80    // The main loop processes 3 packs of data at a time.
81    // The initial pack offsets may be nonzero.
82   
83    Value * sourcePackPtr = iBuilder->CreateBitCast(sourceStream, iBuilder->getBitBlockType()->getPointerTo());
84    Value * offset = iBuilder->CreateURem(iBuilder->getProcessedItemCount("sourceStream"), iBuilder->getSize(iBuilder->getBitBlockWidth()));
85    sourcePackPtr = iBuilder->CreateGEP(sourcePackPtr, iBuilder->CreateUDiv(offset, iBuilder->getSize(PACK_SIZE)));
86    Value * outputPackPtr = iBuilder->CreateBitCast(expandedStream, iBuilder->getBitBlockType()->getPointerTo());
87    offset = iBuilder->CreateURem(iBuilder->getProducedItemCount("expand34Stream"), iBuilder->getSize(iBuilder->getBitBlockWidth()));
88    outputPackPtr = iBuilder->CreateGEP(outputPackPtr, iBuilder->CreateUDiv(offset, iBuilder->getSize(PACK_SIZE)));
89
90    iBuilder->CreateCondBr(iBuilder->CreateICmpSGT(itemsToDo, iBuilder->getSize(0)), expand_3_4_loop, expand3_4_exit);
91   
92    iBuilder->SetInsertPoint(expand_3_4_loop);
93    PHINode * loopInput_ptr = iBuilder->CreatePHI(sourcePackPtr->getType(), 2);
94    PHINode * loopOutput_ptr = iBuilder->CreatePHI(outputPackPtr->getType(), 2);
95    PHINode * loopItemsRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
96
97    loopInput_ptr->addIncoming(sourcePackPtr, expand2_3entry);
98    loopOutput_ptr->addIncoming(outputPackPtr, expand2_3entry);
99    loopItemsRemain->addIncoming(itemsToDo, expand2_3entry);
100
101
102    // Step 1 of the main loop.
103    Value * pack0 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(loopInput_ptr, packAlign));
104    Value * expand0 = iBuilder->bitCast(iBuilder->CreateShuffleVector(undefPack, pack0, expand_3_4_shuffle[0]));
105    iBuilder->CreateBlockAlignedStore(expand0, loopOutput_ptr);
106    // Step 2 of the main loop.
107    Value * inPack1_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(1));
108    Value * outPack1_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(1));
109    Value * pack1 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack1_ptr, packAlign));
110    Value * expand1 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack0, pack1, expand_3_4_shuffle[1]));
111    iBuilder->CreateBlockAlignedStore(expand1, outPack1_ptr);
112    // Step 3 of the main loop.
113    Value * inPack2_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(2));
114    Value * outPack2_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(2));
115    Value * pack2 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack2_ptr, packAlign));
116    Value * expand2 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack1, pack2, expand_3_4_shuffle[2]));
117    iBuilder->CreateBlockAlignedStore(expand2, outPack2_ptr);
118    Value * outPack3_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(3));
119    Value * expand3 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack2, undefPack, expand_3_4_shuffle[3]));
120    iBuilder->CreateBlockAlignedStore(expand3, outPack3_ptr);
121
122    Value * loopNextInputPack = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(3));
123    Value * remainingItems = iBuilder->CreateSub(loopItemsRemain, triplePackSize);
124
125    Value * loopNextOutputPack;
126    loopNextOutputPack = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(4));
127
128    loopInput_ptr->addIncoming(loopNextInputPack, expand_3_4_loop);
129    loopOutput_ptr->addIncoming(loopNextOutputPack, expand_3_4_loop);
130    loopItemsRemain->addIncoming(remainingItems, expand_3_4_loop);
131
132    Value * continueLoop = iBuilder->CreateICmpSGT(remainingItems, iBuilder->getSize(0));
133    iBuilder->CreateCondBr(continueLoop, expand_3_4_loop, expand3_4_exit);
134   
135    iBuilder->SetInsertPoint(expand3_4_exit);
136    }
137
138
139// Radix 64 determination, converting 3 bytes to 4 6-bit values.
140//
141//  00000000|zyxwvuts|rqpmnlkj|hgfedcba    Original
142//           zy                            bits to move 6 positions right
143//             xwvuts                      bits to move 8 positions left
144//                    rqpm                 bits to move 4 positions right
145//                        nlkj             bits to move 10 positions left
146//                             hqfedc      bits to move 2 positions right
147//                                   ba    bits to move 12 positions left
148//    xwvuts|  nlkjzy|  barqpm|  hgfedc    Target
149inline Value * radix64Kernel::processPackData(const std::unique_ptr<KernelBuilder> & iBuilder, llvm::Value * bytepack) const {
150
151    Value * step_right_6 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00C00000));
152    Value * right_6_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_6), 6);
153
154    Value * step_left_8 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x003F0000));
155    Value * left_8_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_8), 8);
156    Value * mid = iBuilder->simd_or(right_6_result, left_8_result);
157
158    Value * step_right_4 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x0000F000));
159    Value * right_4_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_4), 4);
160    mid = iBuilder->simd_or(mid, right_4_result);
161
162    Value * step_left_10 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000F00));
163    Value * left_10_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_10), 10);
164    mid = iBuilder->simd_or(mid, left_10_result);
165
166    Value * step_right_2 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x000000FC));
167    Value * right_2_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_2), 2);
168    mid = iBuilder->simd_or(mid, right_2_result);
169
170    Value * step_left_12 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000003));
171    Value * left_12_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_12), 12);
172    mid = iBuilder->simd_or(mid, left_12_result);
173
174    return iBuilder->bitCast(mid);
175}
176
177void radix64Kernel::generateDoBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder) {
178    for (unsigned i = 0; i < 8; i++) {
179        Value * bytepack = iBuilder->loadInputStreamPack("expandedStream", iBuilder->getInt32(0), iBuilder->getInt32(i));
180        Value * radix64pack = processPackData(iBuilder, bytepack);
181        iBuilder->storeOutputStreamPack("radix64stream", iBuilder->getInt32(0), iBuilder->getInt32(i), radix64pack);
182    }
183}
184
185void radix64Kernel::generateFinalBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder, Value * remainingBytes) {
186
187    BasicBlock * entry = iBuilder->GetInsertBlock();
188    BasicBlock * radix64_loop = iBuilder->CreateBasicBlock("radix64_loop");
189    BasicBlock * fbExit = iBuilder->CreateBasicBlock("fbExit");
190   
191    const unsigned PACK_SIZE = iBuilder->getStride()/8;
192    Constant * packSize = iBuilder->getSize(PACK_SIZE);
193
194    // Enter the loop only if there is at least one byte remaining to process.
195    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, radix64_loop);
196
197    iBuilder->SetInsertPoint(radix64_loop);
198    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
199    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
200    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
201    loopRemain->addIncoming(remainingBytes, entry);
202
203    Value * bytepack = iBuilder->loadInputStreamPack("expandedStream", iBuilder->getInt32(0), idx);
204    Value * radix64pack = processPackData(iBuilder, bytepack);
205    iBuilder->storeOutputStreamPack("radix64stream", iBuilder->getInt32(0), idx, radix64pack);
206
207    Value* nextIdx = iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1));
208    idx->addIncoming(nextIdx, radix64_loop);
209    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
210    loopRemain->addIncoming(remainAfterLoop, radix64_loop);
211
212    Value* continueLoop = iBuilder->CreateICmpSGT(remainAfterLoop, iBuilder->getSize(0));
213
214    iBuilder->CreateCondBr(continueLoop, radix64_loop, fbExit);
215
216    iBuilder->SetInsertPoint(fbExit);
217}
218
219inline llvm::Value* base64Kernel::processPackData(const std::unique_ptr<KernelBuilder> & iBuilder, llvm::Value* bytepack) const {
220    Value * mask_gt_25 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(25)));
221    Value * mask_gt_51 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(51)));
222    Value * mask_eq_62 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(62)));
223    Value * mask_eq_63 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(63)));
224    // Strategy:
225    // 1. add ord('A') = 65 to all radix64 values, this sets the correct values for entries 0 to 25.
226    // 2. add ord('a') - ord('A') - (26 - 0) = 6 to all values >25, this sets the correct values for entries 0 to 51
227    // 3. subtract ord('a') - ord('0') + (52 - 26) = 75 to all values > 51, this sets the correct values for entries 0 to 61
228    // 4. subtract ord('0') - ord('+') + (62 - 52) = 15 for all values = 62
229    // 4. add ord('/') - ord('0') - (63 - 52) = 3 for all values = 63
230    Value * t0_25 = iBuilder->simd_add(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8('A')));
231    Value * t0_51 = iBuilder->simd_add(8, t0_25, iBuilder->simd_and(mask_gt_25, iBuilder->simd_fill(8, iBuilder->getInt8(6))));
232    Value * t0_61 = iBuilder->simd_sub(8, t0_51, iBuilder->simd_and(mask_gt_51, iBuilder->simd_fill(8, iBuilder->getInt8(75))));
233    Value * t0_62 = iBuilder->simd_sub(8, t0_61, iBuilder->simd_and(mask_eq_62, iBuilder->simd_fill(8, iBuilder->getInt8(15))));
234    return iBuilder->bitCast(iBuilder->simd_sub(8, t0_62, iBuilder->simd_and(mask_eq_63, iBuilder->simd_fill(8, iBuilder->getInt8(12)))));
235}
236
237void base64Kernel::generateDoBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder) {
238    for (unsigned i = 0; i < 8; i++) {
239        Value * bytepack = iBuilder->loadInputStreamPack("radix64stream", iBuilder->getInt32(0), iBuilder->getInt32(i));
240        Value * base64pack = processPackData(iBuilder, bytepack);
241        iBuilder->storeOutputStreamPack("base64stream", iBuilder->getInt32(0), iBuilder->getInt32(i), base64pack);
242    }
243}
244
245// Special processing for the base 64 format.   The output must always contain a multiple
246// of 4 bytes.   When the number of radix 64 values is not a multiple of 4
247// number of radix 64 values
248void base64Kernel::generateFinalBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder, Value * remainingBytes) {
249
250    BasicBlock * entry = iBuilder->GetInsertBlock();
251    BasicBlock * base64_loop = iBuilder->CreateBasicBlock("base64_loop");
252    BasicBlock * loopExit = iBuilder->CreateBasicBlock("loopExit");
253    BasicBlock * doPadding = iBuilder->CreateBasicBlock("doPadding");
254    BasicBlock * doPadding2 = iBuilder->CreateBasicBlock("doPadding2");
255    BasicBlock * fbExit = iBuilder->CreateBasicBlock("fbExit");
256
257    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
258    Value * padBytes = iBuilder->CreateSub(iBuilder->getSize(4), remainMod4);
259    padBytes = iBuilder->CreateAnd(padBytes, iBuilder->getSize(3));
260
261    Constant * packSize = iBuilder->getSize(iBuilder->getStride() / 8);
262
263    // Enter the loop only if there is at least one byte remaining to process.
264    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, base64_loop);
265
266    iBuilder->SetInsertPoint(base64_loop);
267    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
268    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
269    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
270    loopRemain->addIncoming(remainingBytes, entry);
271    Value * bytepack = iBuilder->loadInputStreamPack("radix64stream", iBuilder->getInt32(0), idx);
272    Value * base64pack = processPackData(iBuilder, bytepack);
273    iBuilder->storeOutputStreamPack("base64stream", iBuilder->getInt32(0), idx, base64pack);
274    idx->addIncoming(iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1)), base64_loop);
275    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
276    loopRemain->addIncoming(remainAfterLoop, base64_loop);
277
278    Value* continueLoop = iBuilder->CreateICmpSGT(remainAfterLoop, iBuilder->getSize(0));
279    iBuilder->CreateCondBr(continueLoop, base64_loop, loopExit);
280
281    iBuilder->SetInsertPoint(loopExit);
282    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(padBytes, iBuilder->getSize(0)), fbExit, doPadding);
283
284    iBuilder->SetInsertPoint(doPadding);
285    Value * i8output_ptr = iBuilder->getOutputStreamBlockPtr("base64stream", iBuilder->getInt32(0));
286    i8output_ptr = iBuilder->CreatePointerCast(i8output_ptr, iBuilder->getInt8PtrTy());
287    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, remainingBytes));
288    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(3)), fbExit, doPadding2);
289    iBuilder->SetInsertPoint(doPadding2);
290    Value * finalPadPos = iBuilder->CreateAdd(remainingBytes, iBuilder->getSize(1));
291    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, finalPadPos));
292    iBuilder->CreateBr(fbExit);
293    iBuilder->SetInsertPoint(fbExit);
294}
295
296expand3_4Kernel::expand3_4Kernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
297: MultiBlockKernel("expand3_4",
298            {Binding{iBuilder->getStreamSetTy(1, 8), "sourceStream"}},
299            {Binding{iBuilder->getStreamSetTy(1, 8), "expand34Stream", FixedRatio(4,3)}},
300            {}, {}, {}) {
301    setKernelStride(3 * iBuilder->getBitBlockWidth()/8);
302}
303
304radix64Kernel::radix64Kernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
305: BlockOrientedKernel("radix64",
306            {Binding{iBuilder->getStreamSetTy(1, 8), "expandedStream"}},
307            {Binding{iBuilder->getStreamSetTy(1, 8), "radix64stream"}},
308            {}, {}, {}) {
309}
310
311base64Kernel::base64Kernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
312: BlockOrientedKernel("base64",
313            {Binding{iBuilder->getStreamSetTy(1, 8), "radix64stream"}},
314            {Binding{iBuilder->getStreamSetTy(1, 8), "base64stream", RoundUpToMultiple(4)}},
315            {}, {}, {}) {
316}
317
318}
Note: See TracBrowser for help on using the repository browser.