source: icGREP/icgrep-devel/icgrep/kernels/deletion.cpp @ 5355

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

Swizzled bitstream deletion and -enable-AVX-deletion in u8u16

File size: 14.7 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
6#include "deletion.h"
7#include <IR_Gen/idisa_builder.h>
8#include <llvm/IR/Value.h>
9#include <llvm/IR/Module.h>
10#include <llvm/Support/raw_ostream.h>
11
12using namespace llvm;
13
14namespace kernel {
15
16inline std::vector<Value *> parallel_prefix_deletion_masks(IDISA::IDISA_Builder * iBuilder, const unsigned fw, Value * del_mask) {
17    Value * m = iBuilder->simd_not(del_mask);
18    Value * mk = iBuilder->simd_slli(fw, del_mask, 1);
19    std::vector<Value *> move_masks;
20    for (unsigned shift = 1; shift < fw; shift *= 2) {
21        Value * mp = mk;
22        for (unsigned lookright = 1; lookright < fw; lookright *= 2) {
23            mp = iBuilder->simd_xor(mp, iBuilder->simd_slli(fw, mp, lookright));
24        }
25        Value * mv = iBuilder->simd_and(mp, m);
26        m = iBuilder->simd_or(iBuilder->simd_xor(m, mv), iBuilder->simd_srli(fw, mv, shift));
27        mk = iBuilder->simd_and(mk, iBuilder->simd_not(mp));
28        move_masks.push_back(mv);
29    }
30    return move_masks;
31}
32
33inline Value * apply_parallel_prefix_deletion(IDISA::IDISA_Builder * iBuilder, const unsigned fw, Value * del_mask, const std::vector<Value *> & mv, Value * strm) {
34    Value * s = iBuilder->simd_and(strm, iBuilder->simd_not(del_mask));
35    for (unsigned i = 0; i < mv.size(); i++) {
36        unsigned shift = 1 << i;
37        Value * t = iBuilder->simd_and(s, mv[i]);
38        s = iBuilder->simd_or(iBuilder->simd_xor(s, t), iBuilder->simd_srli(fw, t, shift));
39    }
40    return s;
41}
42
43inline Value * partial_sum_popcount(IDISA::IDISA_Builder * iBuilder, const unsigned fw, Value * mask) {
44    Value * field = iBuilder->simd_popcount(fw, mask);
45    const auto count = iBuilder->getBitBlockWidth() / fw;
46    for (unsigned move = 1; move < count; move *= 2) {
47        field = iBuilder->simd_add(fw, field, iBuilder->mvmd_slli(fw, field, move));
48    }
49    return field;
50}
51
52// Apply deletion to a set of stream_count input streams to produce a set of output streams.
53// Kernel inputs: stream_count data streams plus one del_mask stream
54// Outputs: the deleted streams, plus a partial sum popcount
55
56void DeletionKernel::generateDoBlockMethod() {
57    Value * delMask = loadInputStreamBlock("delMaskSet", iBuilder->getInt32(0));
58    const auto move_masks = parallel_prefix_deletion_masks(iBuilder, mDeletionFieldWidth, delMask);
59    for (unsigned j = 0; j < mStreamCount; ++j) {
60        Value * input = loadInputStreamBlock("inputStreamSet", iBuilder->getInt32(j));
61        Value * output = apply_parallel_prefix_deletion(iBuilder, mDeletionFieldWidth, delMask, move_masks, input);
62        storeOutputStreamBlock("outputStreamSet", iBuilder->getInt32(j), output);
63    }
64    Value * delCount = partial_sum_popcount(iBuilder, mDeletionFieldWidth, iBuilder->simd_not(delMask));
65    storeOutputStreamBlock("deletionCounts", iBuilder->getInt32(0), iBuilder->bitCast(delCount));
66}
67
68void DeletionKernel::generateFinalBlockMethod(Value * remainingBytes) {
69    IntegerType * vecTy = iBuilder->getIntNTy(iBuilder->getBitBlockWidth());
70    Value * remaining = iBuilder->CreateZExt(remainingBytes, vecTy);
71    Value * EOF_del = iBuilder->bitCast(iBuilder->CreateShl(Constant::getAllOnesValue(vecTy), remaining));
72    Value * delMask = iBuilder->CreateOr(EOF_del, loadInputStreamBlock("delMaskSet", iBuilder->getInt32(0)));
73    const auto move_masks = parallel_prefix_deletion_masks(iBuilder, mDeletionFieldWidth, delMask);
74    for (unsigned j = 0; j < mStreamCount; ++j) {
75        Value * input = loadInputStreamBlock("inputStreamSet", iBuilder->getInt32(j));
76        Value * output = apply_parallel_prefix_deletion(iBuilder, mDeletionFieldWidth, delMask, move_masks, input);
77        storeOutputStreamBlock("outputStreamSet", iBuilder->getInt32(j), output);
78    }
79    Value * delCount = partial_sum_popcount(iBuilder, mDeletionFieldWidth, iBuilder->simd_not(delMask));
80    storeOutputStreamBlock("deletionCounts", iBuilder->getInt32(0), iBuilder->bitCast(delCount));
81}
82
83DeletionKernel::DeletionKernel(IDISA::IDISA_Builder * iBuilder, unsigned fw, unsigned streamCount)
84: BlockOrientedKernel(iBuilder, "del",
85              {Binding{iBuilder->getStreamSetTy(streamCount), "inputStreamSet"},
86               Binding{iBuilder->getStreamSetTy(), "delMaskSet"}},
87              {Binding{iBuilder->getStreamSetTy(streamCount), "outputStreamSet"},
88               Binding{iBuilder->getStreamSetTy(), "deletionCounts"}},
89              {}, {}, {})
90, mDeletionFieldWidth(fw)
91, mStreamCount(streamCount) {
92    mDoBlockUpdatesProducedItemCountsAttribute = false;
93}
94
95
96
97const unsigned PEXT_width = 64;
98
99inline std::vector<Value *> get_PEXT_masks(IDISA::IDISA_Builder * iBuilder, Value * del_mask) {
100    Value * m = iBuilder->fwCast(PEXT_width, iBuilder->simd_not(del_mask));
101    std::vector<Value *> masks;
102    for (unsigned i = 0; i < iBuilder->getBitBlockWidth()/PEXT_width; i++) {
103        masks.push_back(iBuilder->CreateExtractElement(m, i));
104    }
105    return masks;
106}
107
108inline Value * apply_PEXT_deletion(IDISA::IDISA_Builder * iBuilder, const std::vector<Value *> & masks, Value * strm) {   
109    Value * PEXT_func = nullptr;
110    if (PEXT_width == 64) {
111        PEXT_func = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::x86_bmi_pext_64);
112    } else if (PEXT_width == 32) {
113        PEXT_func = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::x86_bmi_pext_32);
114    }
115    Value * v = iBuilder->fwCast(PEXT_width, strm);
116    Value * output = Constant::getNullValue(v->getType());
117    for (unsigned i = 0; i < iBuilder->getBitBlockWidth()/PEXT_width; i++) {
118        Value * field = iBuilder->CreateExtractElement(v, i);
119        Value * compressed = iBuilder->CreateCall(PEXT_func, {field, masks[i]});
120        output = iBuilder->CreateInsertElement(output, compressed, i);
121    }
122    return output;
123}
124
125// Apply deletion to a set of stream_count input streams to produce a set of output streams.
126// Kernel inputs: stream_count data streams plus one del_mask stream
127// Outputs: the deleted streams, plus a partial sum popcount
128
129void DeleteByPEXTkernel::generateDoBlockMethod() {
130    Value * delMask = loadInputStreamBlock("delMaskSet", iBuilder->getInt32(0));
131    const auto masks = get_PEXT_masks(iBuilder, delMask);
132    for (unsigned j = 0; j < mStreamCount; ++j) {
133        Value * input = loadInputStreamBlock("inputStreamSet", iBuilder->getInt32(j));
134        Value * output = apply_PEXT_deletion(iBuilder, masks, input);
135        storeOutputStreamBlock("outputStreamSet", iBuilder->getInt32(j), output);
136    }
137    //Value * delCount = partial_sum_popcount(iBuilder, mDelCountFieldWidth, apply_PEXT_deletion(iBuilder, masks, iBuilder->simd_not(delMask)));
138    Value * delCount = iBuilder->simd_popcount(mDelCountFieldWidth, iBuilder->simd_not(delMask));
139    storeOutputStreamBlock("deletionCounts", iBuilder->getInt32(0), iBuilder->bitCast(delCount));
140}
141
142void DeleteByPEXTkernel::generateFinalBlockMethod(Value * remainingBytes) {
143    IntegerType * vecTy = iBuilder->getIntNTy(iBuilder->getBitBlockWidth());
144    Value * remaining = iBuilder->CreateZExt(remainingBytes, vecTy);
145    Value * EOF_del = iBuilder->bitCast(iBuilder->CreateShl(Constant::getAllOnesValue(vecTy), remaining));
146    Value * delMask = iBuilder->CreateOr(EOF_del, loadInputStreamBlock("delMaskSet", iBuilder->getInt32(0)));
147    const auto masks = get_PEXT_masks(iBuilder, delMask);
148    for (unsigned j = 0; j < mStreamCount; ++j) {
149        Value * input = loadInputStreamBlock("inputStreamSet", iBuilder->getInt32(j));
150        Value * output = apply_PEXT_deletion(iBuilder, masks, input);
151        storeOutputStreamBlock("outputStreamSet", iBuilder->getInt32(j), output);
152    }
153    //Value * delCount = partial_sum_popcount(iBuilder, mDelCountFieldWidth, apply_PEXT_deletion(iBuilder, masks, iBuilder->simd_not(delMask)));
154    Value * delCount = iBuilder->simd_popcount(mDelCountFieldWidth, iBuilder->simd_not(delMask));
155    storeOutputStreamBlock("deletionCounts", iBuilder->getInt32(0), iBuilder->bitCast(delCount));
156}
157
158DeleteByPEXTkernel::DeleteByPEXTkernel(IDISA::IDISA_Builder * iBuilder, unsigned fw, unsigned streamCount)
159: BlockOrientedKernel(iBuilder, "PEXTdel",
160                      {Binding{iBuilder->getStreamSetTy(streamCount), "inputStreamSet"},
161                          Binding{iBuilder->getStreamSetTy(), "delMaskSet"}},
162                      {Binding{iBuilder->getStreamSetTy(streamCount), "outputStreamSet"},
163                          Binding{iBuilder->getStreamSetTy(), "deletionCounts"}},
164                      {}, {}, {})
165, mDelCountFieldWidth(fw)
166, mStreamCount(streamCount) {
167    mDoBlockUpdatesProducedItemCountsAttribute = false;
168}
169
170   
171//
172// This kernel performs final stream compression for a set of N bitstreams, given
173// (a) a set of bitstreams partially compressed within K-bit fields and stored
174//     in K-bit swizzled form, and
175// (b) a stream of deletion/extraction counts per K-bit stride.
176//
177// Restrictions:  At present, only K=64 is supported.
178//                At present, N must be an exact multiple of BLOCK_SIZE/K.
179//
180// The kernel always consumes full blocks of input and emits data into the output
181// buffer in swizzles of K items at a time.   Upon completion of a segment,
182// up to K-1 pending output items per stream may be stored in the kernel state.
183//
184// Note: that both input streams and output streams are stored in swizzled form.
185//
186
187   
188
189SwizzledBitstreamCompressByCount::SwizzledBitstreamCompressByCount(IDISA::IDISA_Builder * iBuilder, unsigned bitStreamCount, unsigned fieldWidth)
190    : BlockOrientedKernel(iBuilder, "swizzled_compress", {Binding{iBuilder->getStreamSetTy(), "countsPerStride"}}, {}, {}, {}, {})
191, mBitStreamCount(bitStreamCount)
192    , mFieldWidth(fieldWidth)
193    , mSwizzleFactor(iBuilder->getBitBlockWidth() / fieldWidth)
194    , mSwizzleSetCount((mBitStreamCount + mSwizzleFactor - 1)/mSwizzleFactor) {
195        assert((fieldWidth > 0) && ((fieldWidth & (fieldWidth - 1)) == 0) && "fieldWidth must be a power of 2");
196        assert(mSwizzleFactor > 1 && "fieldWidth must be less than the block width");
197        for (unsigned i = 0; i < mSwizzleSetCount; i++) {
198            mStreamSetInputs.push_back(Binding{iBuilder->getStreamSetTy(mSwizzleFactor, 1), "inputSwizzle" + std::to_string(i)});
199            mStreamSetOutputs.push_back(Binding{iBuilder->getStreamSetTy(mSwizzleFactor, 1), "outputSwizzle" + std::to_string(i), MaxRatio(1)});
200            addScalar(iBuilder->getBitBlockType(), "pendingSwizzleData" + std::to_string(i));
201        }
202        addScalar(iBuilder->getSizeTy(), "pendingOffset");
203}
204
205   
206void SwizzledBitstreamCompressByCount::generateDoBlockMethod() {
207       
208    Value * countStreamPtr = iBuilder->CreateBitCast(getInputStreamBlockPtr("countsPerStride", iBuilder->getInt32(0)), iBuilder->getIntNTy(mFieldWidth)->getPointerTo());
209   
210    // Output is written and committed to the output buffer one swizzle at a time.
211    //
212    Constant * blockOffsetMask = iBuilder->getSize(iBuilder->getBitBlockWidth() - 1);
213    Constant * outputIndexShift = iBuilder->getSize(std::log2(mFieldWidth));
214   
215    Value * outputProduced = getProducedItemCount("outputSwizzle0"); // All output groups have the same count.
216    Value * producedOffset = iBuilder->CreateAnd(outputProduced, blockOffsetMask);
217    Value * outputIndex = iBuilder->CreateLShr(producedOffset, outputIndexShift);
218
219    // There may be pending data in the kernel state, for up to mFieldWidth-1 bits per stream.
220    Value * pendingOffset = getScalarField("pendingOffset");
221    // There is a separate vector of pending data for each swizzle group.
222    std::vector<Value *> pendingData;
223    std::vector<Value *> outputStreamPtr;
224    for (unsigned i = 0; i < mSwizzleSetCount; i++) {
225        pendingData.push_back(getScalarField("pendingSwizzleData" + std::to_string(i)));
226        outputStreamPtr.push_back(getOutputStreamBlockPtr("outputSwizzle" + std::to_string(i), iBuilder->getInt32(0)));
227    }
228   
229    // Generate code for each of the mSwizzleFactor fields making up a block.
230    // We load the count for the field and process all swizzle groups accordingly.
231    for (unsigned i = 0; i < mSwizzleFactor; i++) {
232        Value * newItemCount = iBuilder->CreateLoad(iBuilder->CreateGEP(countStreamPtr, iBuilder->getInt32(i)));
233        Value * pendingSpace = iBuilder->CreateSub(iBuilder->getSize(mFieldWidth), pendingOffset);
234        Value * pendingSpaceFilled = iBuilder->CreateICmpUGE(newItemCount, pendingSpace);
235       
236        // Data from the ith swizzle pack of each group is processed
237        // according to the same newItemCount, pendingSpace, ...
238        for (unsigned j = 0; j < mSwizzleSetCount; j++) {
239            Value * newItems = loadInputStreamBlock("inputSwizzle" + std::to_string(j), iBuilder->getInt32(i));
240            // Combine as many of the new items as possible into the pending group.
241            Value * combinedGroup = iBuilder->CreateOr(pendingData[j], iBuilder->CreateShl(newItems, iBuilder->simd_fill(mFieldWidth, pendingOffset)));
242            // To avoid an unpredictable branch, always store the combined group, whether full or not.
243               
244            iBuilder->CreateBlockAlignedStore(combinedGroup, iBuilder->CreateGEP(outputStreamPtr[j], outputIndex));
245            // Any items in excess of the space available in the current pending group overflow for the next group.
246            Value * overFlowGroup = iBuilder->CreateLShr(newItems, iBuilder->simd_fill(mFieldWidth, pendingSpace));
247            // If we filled the space, then the overflow group becomes the new pending group and the index is updated.
248            pendingData[j] = iBuilder->CreateSelect(pendingSpaceFilled, overFlowGroup, combinedGroup);
249        }
250        outputIndex = iBuilder->CreateSelect(pendingSpaceFilled, iBuilder->CreateAdd(outputIndex, iBuilder->getSize(1)), outputIndex);
251        pendingOffset = iBuilder->CreateAnd(iBuilder->CreateAdd(newItemCount, pendingOffset), iBuilder->getSize(mFieldWidth-1));
252    }
253    setScalarField("pendingOffset", pendingOffset);
254   
255    Value * newlyProduced = iBuilder->CreateSub(iBuilder->CreateShl(outputIndex, outputIndexShift), producedOffset);
256    Value * produced = iBuilder->CreateAdd(outputProduced, newlyProduced);
257    for (unsigned j = 0; j < mSwizzleSetCount; j++) {
258        setScalarField("pendingSwizzleData" + std::to_string(j), pendingData[j]);
259        setProducedItemCount("outputSwizzle" + std::to_string(j), produced);
260    }
261}
262
263void SwizzledBitstreamCompressByCount::generateFinalBlockMethod(Value * remainingBytes) {
264    CreateDoBlockMethodCall();
265    // All the data has been written.  Update the count to include pending data.
266    Value * pendingOffset = getScalarField("pendingOffset");
267    Value * produced = iBuilder->CreateAdd(pendingOffset, getProducedItemCount("outputSwizzle0"));
268    for (unsigned j = 0; j < mSwizzleSetCount; j++) {
269        setProducedItemCount("outputSwizzle" + std::to_string(j), produced);
270    }
271}
272}
Note: See TracBrowser for help on using the repository browser.