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

Last change on this file since 5508 was 5508, checked in by cameron, 2 years ago

Simplified expand3_4 kernel taking advantage of multiblock builder

File size: 16.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 <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   
82    Value * sourcePackPtr = iBuilder->CreateBitCast(sourceStream, iBuilder->getBitBlockType()->getPointerTo());
83    Value * outputPackPtr = iBuilder->CreateBitCast(expandedStream, iBuilder->getBitBlockType()->getPointerTo());
84
85    iBuilder->CreateCondBr(iBuilder->CreateICmpSGT(itemsToDo, iBuilder->getSize(0)), expand_3_4_loop, expand3_4_exit);
86   
87    iBuilder->SetInsertPoint(expand_3_4_loop);
88    PHINode * loopInput_ptr = iBuilder->CreatePHI(sourcePackPtr->getType(), 2);
89    PHINode * loopOutput_ptr = iBuilder->CreatePHI(outputPackPtr->getType(), 2);
90    PHINode * loopItemsRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
91
92    loopInput_ptr->addIncoming(sourcePackPtr, expand2_3entry);
93    loopOutput_ptr->addIncoming(outputPackPtr, expand2_3entry);
94    loopItemsRemain->addIncoming(itemsToDo, expand2_3entry);
95
96
97    // Step 1 of the main loop.
98    Value * pack0 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(loopInput_ptr, packAlign));
99    Value * expand0 = iBuilder->bitCast(iBuilder->CreateShuffleVector(undefPack, pack0, expand_3_4_shuffle[0]));
100    iBuilder->CreateBlockAlignedStore(expand0, loopOutput_ptr);
101    // Step 2 of the main loop.
102    Value * inPack1_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(1));
103    Value * outPack1_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(1));
104    Value * pack1 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack1_ptr, packAlign));
105    Value * expand1 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack0, pack1, expand_3_4_shuffle[1]));
106    iBuilder->CreateBlockAlignedStore(expand1, outPack1_ptr);
107    // Step 3 of the main loop.
108    Value * inPack2_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(2));
109    Value * outPack2_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(2));
110    Value * pack2 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack2_ptr, packAlign));
111    Value * expand2 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack1, pack2, expand_3_4_shuffle[2]));
112    iBuilder->CreateBlockAlignedStore(expand2, outPack2_ptr);
113    Value * outPack3_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(3));
114    Value * expand3 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack2, undefPack, expand_3_4_shuffle[3]));
115    iBuilder->CreateBlockAlignedStore(expand3, outPack3_ptr);
116
117    Value * loopNextInputPack = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(3));
118    Value * remainingItems = iBuilder->CreateSub(loopItemsRemain, triplePackSize);
119
120    Value * loopNextOutputPack;
121    loopNextOutputPack = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(4));
122
123    loopInput_ptr->addIncoming(loopNextInputPack, expand_3_4_loop);
124    loopOutput_ptr->addIncoming(loopNextOutputPack, expand_3_4_loop);
125    loopItemsRemain->addIncoming(remainingItems, expand_3_4_loop);
126
127    Value * continueLoop = iBuilder->CreateICmpSGT(remainingItems, iBuilder->getSize(0));
128    iBuilder->CreateCondBr(continueLoop, expand_3_4_loop, expand3_4_exit);
129   
130    iBuilder->SetInsertPoint(expand3_4_exit);
131    }
132
133
134// Radix 64 determination, converting 3 bytes to 4 6-bit values.
135//
136//  00000000|zyxwvuts|rqpmnlkj|hgfedcba    Original
137//           zy                            bits to move 6 positions right
138//             xwvuts                      bits to move 8 positions left
139//                    rqpm                 bits to move 4 positions right
140//                        nlkj             bits to move 10 positions left
141//                             hqfedc      bits to move 2 positions right
142//                                   ba    bits to move 12 positions left
143//    xwvuts|  nlkjzy|  barqpm|  hgfedc    Target
144inline Value * radix64Kernel::processPackData(const std::unique_ptr<KernelBuilder> & iBuilder, llvm::Value * bytepack) const {
145
146    Value * step_right_6 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00C00000));
147    Value * right_6_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_6), 6);
148
149    Value * step_left_8 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x003F0000));
150    Value * left_8_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_8), 8);
151    Value * mid = iBuilder->simd_or(right_6_result, left_8_result);
152
153    Value * step_right_4 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x0000F000));
154    Value * right_4_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_4), 4);
155    mid = iBuilder->simd_or(mid, right_4_result);
156
157    Value * step_left_10 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000F00));
158    Value * left_10_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_10), 10);
159    mid = iBuilder->simd_or(mid, left_10_result);
160
161    Value * step_right_2 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x000000FC));
162    Value * right_2_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_2), 2);
163    mid = iBuilder->simd_or(mid, right_2_result);
164
165    Value * step_left_12 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000003));
166    Value * left_12_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_12), 12);
167    mid = iBuilder->simd_or(mid, left_12_result);
168
169    return iBuilder->bitCast(mid);
170}
171
172void radix64Kernel::generateDoBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder) {
173    for (unsigned i = 0; i < 8; i++) {
174        Value * bytepack = iBuilder->loadInputStreamPack("expandedStream", iBuilder->getInt32(0), iBuilder->getInt32(i));
175        Value * radix64pack = processPackData(iBuilder, bytepack);
176        iBuilder->storeOutputStreamPack("radix64stream", iBuilder->getInt32(0), iBuilder->getInt32(i), radix64pack);
177    }
178}
179
180void radix64Kernel::generateFinalBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder, Value * remainingBytes) {
181
182    BasicBlock * entry = iBuilder->GetInsertBlock();
183    BasicBlock * radix64_loop = iBuilder->CreateBasicBlock("radix64_loop");
184    BasicBlock * fbExit = iBuilder->CreateBasicBlock("fbExit");
185   
186    const unsigned PACK_SIZE = iBuilder->getStride()/8;
187    Constant * packSize = iBuilder->getSize(PACK_SIZE);
188
189    // Enter the loop only if there is at least one byte remaining to process.
190    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, radix64_loop);
191
192    iBuilder->SetInsertPoint(radix64_loop);
193    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
194    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
195    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
196    loopRemain->addIncoming(remainingBytes, entry);
197
198    Value * bytepack = iBuilder->loadInputStreamPack("expandedStream", iBuilder->getInt32(0), idx);
199    Value * radix64pack = processPackData(iBuilder, bytepack);
200    iBuilder->storeOutputStreamPack("radix64stream", iBuilder->getInt32(0), idx, radix64pack);
201
202    Value* nextIdx = iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1));
203    idx->addIncoming(nextIdx, radix64_loop);
204    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
205    loopRemain->addIncoming(remainAfterLoop, radix64_loop);
206
207    Value* continueLoop = iBuilder->CreateICmpSGT(remainAfterLoop, iBuilder->getSize(0));
208
209    iBuilder->CreateCondBr(continueLoop, radix64_loop, fbExit);
210
211    iBuilder->SetInsertPoint(fbExit);
212}
213
214inline llvm::Value* base64Kernel::processPackData(const std::unique_ptr<KernelBuilder> & iBuilder, llvm::Value* bytepack) const {
215    Value * mask_gt_25 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(25)));
216    Value * mask_gt_51 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(51)));
217    Value * mask_eq_62 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(62)));
218    Value * mask_eq_63 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(63)));
219    // Strategy:
220    // 1. add ord('A') = 65 to all radix64 values, this sets the correct values for entries 0 to 25.
221    // 2. add ord('a') - ord('A') - (26 - 0) = 6 to all values >25, this sets the correct values for entries 0 to 51
222    // 3. subtract ord('a') - ord('0') + (52 - 26) = 75 to all values > 51, this sets the correct values for entries 0 to 61
223    // 4. subtract ord('0') - ord('+') + (62 - 52) = 15 for all values = 62
224    // 4. add ord('/') - ord('0') - (63 - 52) = 3 for all values = 63
225    Value * t0_25 = iBuilder->simd_add(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8('A')));
226    Value * t0_51 = iBuilder->simd_add(8, t0_25, iBuilder->simd_and(mask_gt_25, iBuilder->simd_fill(8, iBuilder->getInt8(6))));
227    Value * t0_61 = iBuilder->simd_sub(8, t0_51, iBuilder->simd_and(mask_gt_51, iBuilder->simd_fill(8, iBuilder->getInt8(75))));
228    Value * t0_62 = iBuilder->simd_sub(8, t0_61, iBuilder->simd_and(mask_eq_62, iBuilder->simd_fill(8, iBuilder->getInt8(15))));
229    return iBuilder->bitCast(iBuilder->simd_sub(8, t0_62, iBuilder->simd_and(mask_eq_63, iBuilder->simd_fill(8, iBuilder->getInt8(12)))));
230}
231
232void base64Kernel::generateDoBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder) {
233    for (unsigned i = 0; i < 8; i++) {
234        Value * bytepack = iBuilder->loadInputStreamPack("radix64stream", iBuilder->getInt32(0), iBuilder->getInt32(i));
235        Value * base64pack = processPackData(iBuilder, bytepack);
236        iBuilder->storeOutputStreamPack("base64stream", iBuilder->getInt32(0), iBuilder->getInt32(i), base64pack);
237    }
238}
239
240// Special processing for the base 64 format.   The output must always contain a multiple
241// of 4 bytes.   When the number of radix 64 values is not a multiple of 4
242// number of radix 64 values
243void base64Kernel::generateFinalBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder, Value * remainingBytes) {
244
245    BasicBlock * entry = iBuilder->GetInsertBlock();
246    BasicBlock * base64_loop = iBuilder->CreateBasicBlock("base64_loop");
247    BasicBlock * loopExit = iBuilder->CreateBasicBlock("loopExit");
248    BasicBlock * doPadding = iBuilder->CreateBasicBlock("doPadding");
249    BasicBlock * doPadding2 = iBuilder->CreateBasicBlock("doPadding2");
250    BasicBlock * fbExit = iBuilder->CreateBasicBlock("fbExit");
251
252    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
253    Value * padBytes = iBuilder->CreateSub(iBuilder->getSize(4), remainMod4);
254    padBytes = iBuilder->CreateAnd(padBytes, iBuilder->getSize(3));
255
256    Constant * packSize = iBuilder->getSize(iBuilder->getStride() / 8);
257
258    // Enter the loop only if there is at least one byte remaining to process.
259    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, base64_loop);
260
261    iBuilder->SetInsertPoint(base64_loop);
262    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
263    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
264    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
265    loopRemain->addIncoming(remainingBytes, entry);
266    Value * bytepack = iBuilder->loadInputStreamPack("radix64stream", iBuilder->getInt32(0), idx);
267    Value * base64pack = processPackData(iBuilder, bytepack);
268    iBuilder->storeOutputStreamPack("base64stream", iBuilder->getInt32(0), idx, base64pack);
269    idx->addIncoming(iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1)), base64_loop);
270    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
271    loopRemain->addIncoming(remainAfterLoop, base64_loop);
272
273    Value* continueLoop = iBuilder->CreateICmpSGT(remainAfterLoop, iBuilder->getSize(0));
274    iBuilder->CreateCondBr(continueLoop, base64_loop, loopExit);
275
276    iBuilder->SetInsertPoint(loopExit);
277    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(padBytes, iBuilder->getSize(0)), fbExit, doPadding);
278
279    iBuilder->SetInsertPoint(doPadding);
280    Value * i8output_ptr = iBuilder->getOutputStreamBlockPtr("base64stream", iBuilder->getInt32(0));
281    i8output_ptr = iBuilder->CreatePointerCast(i8output_ptr, iBuilder->getInt8PtrTy());
282    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, remainingBytes));
283    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(3)), fbExit, doPadding2);
284    iBuilder->SetInsertPoint(doPadding2);
285    Value * finalPadPos = iBuilder->CreateAdd(remainingBytes, iBuilder->getSize(1));
286    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, finalPadPos));
287    iBuilder->CreateBr(fbExit);
288    iBuilder->SetInsertPoint(fbExit);
289}
290
291expand3_4Kernel::expand3_4Kernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
292: MultiBlockKernel("expand3_4",
293            {Binding{iBuilder->getStreamSetTy(1, 8), "sourceStream"}},
294            {Binding{iBuilder->getStreamSetTy(1, 8), "expand34Stream", FixedRatio(4,3)}},
295            {}, {}, {}) {
296    setKernelStride(3 * iBuilder->getBitBlockWidth()/8);
297}
298
299radix64Kernel::radix64Kernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
300: BlockOrientedKernel("radix64",
301            {Binding{iBuilder->getStreamSetTy(1, 8), "expandedStream"}},
302            {Binding{iBuilder->getStreamSetTy(1, 8), "radix64stream"}},
303            {}, {}, {}) {
304}
305
306base64Kernel::base64Kernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder)
307: BlockOrientedKernel("base64",
308            {Binding{iBuilder->getStreamSetTy(1, 8), "radix64stream"}},
309            {Binding{iBuilder->getStreamSetTy(1, 8), "base64stream", RoundUpToMultiple(4)}},
310            {}, {}, {}) {
311}
312
313}
Note: See TracBrowser for help on using the repository browser.