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

Last change on this file since 5357 was 5356, checked in by cameron, 3 years ago

Eliminate obsolete DoBlockUpdatesProducedItemCounts? attribute

File size: 14.6 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}
93
94
95
96const unsigned PEXT_width = 64;
97
98inline std::vector<Value *> get_PEXT_masks(IDISA::IDISA_Builder * iBuilder, Value * del_mask) {
99    Value * m = iBuilder->fwCast(PEXT_width, iBuilder->simd_not(del_mask));
100    std::vector<Value *> masks;
101    for (unsigned i = 0; i < iBuilder->getBitBlockWidth()/PEXT_width; i++) {
102        masks.push_back(iBuilder->CreateExtractElement(m, i));
103    }
104    return masks;
105}
106
107inline Value * apply_PEXT_deletion(IDISA::IDISA_Builder * iBuilder, const std::vector<Value *> & masks, Value * strm) {   
108    Value * PEXT_func = nullptr;
109    if (PEXT_width == 64) {
110        PEXT_func = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::x86_bmi_pext_64);
111    } else if (PEXT_width == 32) {
112        PEXT_func = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::x86_bmi_pext_32);
113    }
114    Value * v = iBuilder->fwCast(PEXT_width, strm);
115    Value * output = Constant::getNullValue(v->getType());
116    for (unsigned i = 0; i < iBuilder->getBitBlockWidth()/PEXT_width; i++) {
117        Value * field = iBuilder->CreateExtractElement(v, i);
118        Value * compressed = iBuilder->CreateCall(PEXT_func, {field, masks[i]});
119        output = iBuilder->CreateInsertElement(output, compressed, i);
120    }
121    return output;
122}
123
124// Apply deletion to a set of stream_count input streams to produce a set of output streams.
125// Kernel inputs: stream_count data streams plus one del_mask stream
126// Outputs: the deleted streams, plus a partial sum popcount
127
128void DeleteByPEXTkernel::generateDoBlockMethod() {
129    Value * delMask = loadInputStreamBlock("delMaskSet", iBuilder->getInt32(0));
130    const auto masks = get_PEXT_masks(iBuilder, delMask);
131    for (unsigned j = 0; j < mStreamCount; ++j) {
132        Value * input = loadInputStreamBlock("inputStreamSet", iBuilder->getInt32(j));
133        Value * output = apply_PEXT_deletion(iBuilder, masks, input);
134        storeOutputStreamBlock("outputStreamSet", iBuilder->getInt32(j), output);
135    }
136    //Value * delCount = partial_sum_popcount(iBuilder, mDelCountFieldWidth, apply_PEXT_deletion(iBuilder, masks, iBuilder->simd_not(delMask)));
137    Value * delCount = iBuilder->simd_popcount(mDelCountFieldWidth, iBuilder->simd_not(delMask));
138    storeOutputStreamBlock("deletionCounts", iBuilder->getInt32(0), iBuilder->bitCast(delCount));
139}
140
141void DeleteByPEXTkernel::generateFinalBlockMethod(Value * remainingBytes) {
142    IntegerType * vecTy = iBuilder->getIntNTy(iBuilder->getBitBlockWidth());
143    Value * remaining = iBuilder->CreateZExt(remainingBytes, vecTy);
144    Value * EOF_del = iBuilder->bitCast(iBuilder->CreateShl(Constant::getAllOnesValue(vecTy), remaining));
145    Value * delMask = iBuilder->CreateOr(EOF_del, loadInputStreamBlock("delMaskSet", iBuilder->getInt32(0)));
146    const auto masks = get_PEXT_masks(iBuilder, delMask);
147    for (unsigned j = 0; j < mStreamCount; ++j) {
148        Value * input = loadInputStreamBlock("inputStreamSet", iBuilder->getInt32(j));
149        Value * output = apply_PEXT_deletion(iBuilder, masks, input);
150        storeOutputStreamBlock("outputStreamSet", iBuilder->getInt32(j), output);
151    }
152    //Value * delCount = partial_sum_popcount(iBuilder, mDelCountFieldWidth, apply_PEXT_deletion(iBuilder, masks, iBuilder->simd_not(delMask)));
153    Value * delCount = iBuilder->simd_popcount(mDelCountFieldWidth, iBuilder->simd_not(delMask));
154    storeOutputStreamBlock("deletionCounts", iBuilder->getInt32(0), iBuilder->bitCast(delCount));
155}
156
157DeleteByPEXTkernel::DeleteByPEXTkernel(IDISA::IDISA_Builder * iBuilder, unsigned fw, unsigned streamCount)
158: BlockOrientedKernel(iBuilder, "PEXTdel",
159                      {Binding{iBuilder->getStreamSetTy(streamCount), "inputStreamSet"},
160                          Binding{iBuilder->getStreamSetTy(), "delMaskSet"}},
161                      {Binding{iBuilder->getStreamSetTy(streamCount), "outputStreamSet"},
162                          Binding{iBuilder->getStreamSetTy(), "deletionCounts"}},
163                      {}, {}, {})
164, mDelCountFieldWidth(fw)
165, mStreamCount(streamCount) {
166}
167
168   
169//
170// This kernel performs final stream compression for a set of N bitstreams, given
171// (a) a set of bitstreams partially compressed within K-bit fields and stored
172//     in K-bit swizzled form, and
173// (b) a stream of deletion/extraction counts per K-bit stride.
174//
175// Restrictions:  At present, only K=64 is supported.
176//                At present, N must be an exact multiple of BLOCK_SIZE/K.
177//
178// The kernel always consumes full blocks of input and emits data into the output
179// buffer in swizzles of K items at a time.   Upon completion of a segment,
180// up to K-1 pending output items per stream may be stored in the kernel state.
181//
182// Note: that both input streams and output streams are stored in swizzled form.
183//
184
185   
186
187SwizzledBitstreamCompressByCount::SwizzledBitstreamCompressByCount(IDISA::IDISA_Builder * iBuilder, unsigned bitStreamCount, unsigned fieldWidth)
188    : BlockOrientedKernel(iBuilder, "swizzled_compress", {Binding{iBuilder->getStreamSetTy(), "countsPerStride"}}, {}, {}, {}, {})
189, mBitStreamCount(bitStreamCount)
190    , mFieldWidth(fieldWidth)
191    , mSwizzleFactor(iBuilder->getBitBlockWidth() / fieldWidth)
192    , mSwizzleSetCount((mBitStreamCount + mSwizzleFactor - 1)/mSwizzleFactor) {
193        assert((fieldWidth > 0) && ((fieldWidth & (fieldWidth - 1)) == 0) && "fieldWidth must be a power of 2");
194        assert(mSwizzleFactor > 1 && "fieldWidth must be less than the block width");
195        for (unsigned i = 0; i < mSwizzleSetCount; i++) {
196            mStreamSetInputs.push_back(Binding{iBuilder->getStreamSetTy(mSwizzleFactor, 1), "inputSwizzle" + std::to_string(i)});
197            mStreamSetOutputs.push_back(Binding{iBuilder->getStreamSetTy(mSwizzleFactor, 1), "outputSwizzle" + std::to_string(i), MaxRatio(1)});
198            addScalar(iBuilder->getBitBlockType(), "pendingSwizzleData" + std::to_string(i));
199        }
200        addScalar(iBuilder->getSizeTy(), "pendingOffset");
201}
202
203   
204void SwizzledBitstreamCompressByCount::generateDoBlockMethod() {
205       
206    Value * countStreamPtr = iBuilder->CreateBitCast(getInputStreamBlockPtr("countsPerStride", iBuilder->getInt32(0)), iBuilder->getIntNTy(mFieldWidth)->getPointerTo());
207   
208    // Output is written and committed to the output buffer one swizzle at a time.
209    //
210    Constant * blockOffsetMask = iBuilder->getSize(iBuilder->getBitBlockWidth() - 1);
211    Constant * outputIndexShift = iBuilder->getSize(std::log2(mFieldWidth));
212   
213    Value * outputProduced = getProducedItemCount("outputSwizzle0"); // All output groups have the same count.
214    Value * producedOffset = iBuilder->CreateAnd(outputProduced, blockOffsetMask);
215    Value * outputIndex = iBuilder->CreateLShr(producedOffset, outputIndexShift);
216
217    // There may be pending data in the kernel state, for up to mFieldWidth-1 bits per stream.
218    Value * pendingOffset = getScalarField("pendingOffset");
219    // There is a separate vector of pending data for each swizzle group.
220    std::vector<Value *> pendingData;
221    std::vector<Value *> outputStreamPtr;
222    for (unsigned i = 0; i < mSwizzleSetCount; i++) {
223        pendingData.push_back(getScalarField("pendingSwizzleData" + std::to_string(i)));
224        outputStreamPtr.push_back(getOutputStreamBlockPtr("outputSwizzle" + std::to_string(i), iBuilder->getInt32(0)));
225    }
226   
227    // Generate code for each of the mSwizzleFactor fields making up a block.
228    // We load the count for the field and process all swizzle groups accordingly.
229    for (unsigned i = 0; i < mSwizzleFactor; i++) {
230        Value * newItemCount = iBuilder->CreateLoad(iBuilder->CreateGEP(countStreamPtr, iBuilder->getInt32(i)));
231        Value * pendingSpace = iBuilder->CreateSub(iBuilder->getSize(mFieldWidth), pendingOffset);
232        Value * pendingSpaceFilled = iBuilder->CreateICmpUGE(newItemCount, pendingSpace);
233       
234        // Data from the ith swizzle pack of each group is processed
235        // according to the same newItemCount, pendingSpace, ...
236        for (unsigned j = 0; j < mSwizzleSetCount; j++) {
237            Value * newItems = loadInputStreamBlock("inputSwizzle" + std::to_string(j), iBuilder->getInt32(i));
238            // Combine as many of the new items as possible into the pending group.
239            Value * combinedGroup = iBuilder->CreateOr(pendingData[j], iBuilder->CreateShl(newItems, iBuilder->simd_fill(mFieldWidth, pendingOffset)));
240            // To avoid an unpredictable branch, always store the combined group, whether full or not.
241               
242            iBuilder->CreateBlockAlignedStore(combinedGroup, iBuilder->CreateGEP(outputStreamPtr[j], outputIndex));
243            // Any items in excess of the space available in the current pending group overflow for the next group.
244            Value * overFlowGroup = iBuilder->CreateLShr(newItems, iBuilder->simd_fill(mFieldWidth, pendingSpace));
245            // If we filled the space, then the overflow group becomes the new pending group and the index is updated.
246            pendingData[j] = iBuilder->CreateSelect(pendingSpaceFilled, overFlowGroup, combinedGroup);
247        }
248        outputIndex = iBuilder->CreateSelect(pendingSpaceFilled, iBuilder->CreateAdd(outputIndex, iBuilder->getSize(1)), outputIndex);
249        pendingOffset = iBuilder->CreateAnd(iBuilder->CreateAdd(newItemCount, pendingOffset), iBuilder->getSize(mFieldWidth-1));
250    }
251    setScalarField("pendingOffset", pendingOffset);
252   
253    Value * newlyProduced = iBuilder->CreateSub(iBuilder->CreateShl(outputIndex, outputIndexShift), producedOffset);
254    Value * produced = iBuilder->CreateAdd(outputProduced, newlyProduced);
255    for (unsigned j = 0; j < mSwizzleSetCount; j++) {
256        setScalarField("pendingSwizzleData" + std::to_string(j), pendingData[j]);
257        setProducedItemCount("outputSwizzle" + std::to_string(j), produced);
258    }
259}
260
261void SwizzledBitstreamCompressByCount::generateFinalBlockMethod(Value * remainingBytes) {
262    CreateDoBlockMethodCall();
263    // All the data has been written.  Update the count to include pending data.
264    Value * pendingOffset = getScalarField("pendingOffset");
265    Value * produced = iBuilder->CreateAdd(pendingOffset, getProducedItemCount("outputSwizzle0"));
266    for (unsigned j = 0; j < mSwizzleSetCount; j++) {
267        setProducedItemCount("outputSwizzle" + std::to_string(j), produced);
268    }
269}
270}
Note: See TracBrowser for help on using the repository browser.