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

Last change on this file since 5297 was 5297, checked in by nmedfort, 2 years ago

Partial removal of BlockNo?

File size: 5.1 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
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 * delMaskPtr = getInputStream("delMaskSet", iBuilder->getInt32(0));
58    Value * delMask = iBuilder->CreateBlockAlignedLoad(delMaskPtr);
59    const auto move_masks = parallel_prefix_deletion_masks(iBuilder, mDeletionFieldWidth, delMask);
60    for (unsigned j = 0; j < mStreamCount; ++j) {
61        Value * inputStreamPtr = getInputStream("inputStreamSet", iBuilder->getInt32(j));
62        Value * input = iBuilder->CreateBlockAlignedLoad(inputStreamPtr);
63        Value * output = apply_parallel_prefix_deletion(iBuilder, mDeletionFieldWidth, delMask, move_masks, input);
64        Value * outputStreamPtr = getOutputStream("outputStreamSet", iBuilder->getInt32(j));
65        iBuilder->CreateBlockAlignedStore(output, outputStreamPtr);
66    }
67    Value * delCount = partial_sum_popcount(iBuilder, mDeletionFieldWidth, iBuilder->simd_not(delMask));
68    Value * delCountPtr = getOutputStream("deletionCounts", iBuilder->getInt32(0));
69    iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(delCount), delCountPtr);
70    // Stream deletion has only been applied within fields; the actual number of data items has not yet changed.
71    Value * produced = getProducedItemCount("outputStreamSet");
72    produced = iBuilder->CreateAdd(produced, iBuilder->getSize(iBuilder->getStride()));
73    setProducedItemCount("outputStreamSet", produced);
74    setProducedItemCount("deletionCounts", produced);
75}
76
77void DeletionKernel::generateFinalBlockMethod(Value * remainingBytes) {
78    IntegerType * vecTy = iBuilder->getIntNTy(iBuilder->getBitBlockWidth());
79    Value * remaining = iBuilder->CreateZExt(remainingBytes, vecTy);
80    Value * EOF_del = iBuilder->bitCast(iBuilder->CreateShl(Constant::getAllOnesValue(vecTy), remaining));
81    Value * const delmaskPtr = getInputStream("delMaskSet", iBuilder->getInt32(0));
82    Value * const delmaskVal = iBuilder->CreateBlockAlignedLoad(delmaskPtr);
83    iBuilder->CreateBlockAlignedStore(iBuilder->CreateOr(EOF_del, delmaskVal), delmaskPtr);
84    CreateDoBlockMethodCall();
85    // Adjust the produced item count
86    Value * produced = getProducedItemCount("outputStreamSet");
87    produced = iBuilder->CreateSub(produced, iBuilder->getSize(iBuilder->getStride()));
88    produced =  iBuilder->CreateAdd(produced, remainingBytes);
89    setProducedItemCount("outputStreamSet", produced);
90    setProducedItemCount("deletionCounts", produced);
91}
92
93DeletionKernel::DeletionKernel(IDISA::IDISA_Builder * iBuilder, unsigned fw, unsigned streamCount)
94: BlockOrientedKernel(iBuilder, "del",
95              {Binding{iBuilder->getStreamSetTy(streamCount), "inputStreamSet"},
96               Binding{iBuilder->getStreamSetTy(), "delMaskSet"}},
97              {Binding{iBuilder->getStreamSetTy(streamCount), "outputStreamSet"},
98               Binding{iBuilder->getStreamSetTy(), "deletionCounts"}},
99              {}, {}, {})
100, mDeletionFieldWidth(fw)
101, mStreamCount(streamCount) {
102    mDoBlockUpdatesProducedItemCountsAttribute = true;
103}
104
105}
Note: See TracBrowser for help on using the repository browser.