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

Last change on this file since 5335 was 5317, checked in by cameron, 3 years ago

Simplify stream set access; better naming of access functions

File size: 8.3 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 * 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   
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) PEXT_func = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::x86_bmi_pext_64);
110    else if (PEXT_width == 32) PEXT_func = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::x86_bmi_pext_32);
111    Value * v = iBuilder->fwCast(PEXT_width, strm);
112    Value * output = Constant::getNullValue(v->getType());
113    for (unsigned i = 0; i < iBuilder->getBitBlockWidth()/PEXT_width; i++) {
114        Value * field = iBuilder->CreateExtractElement(v, i);
115        Value * compressed = iBuilder->CreateCall(PEXT_func, {field, masks[i]});
116        output = iBuilder->CreateInsertElement(output, compressed, i);
117    }
118    return output;
119}
120
121// Apply deletion to a set of stream_count input streams to produce a set of output streams.
122// Kernel inputs: stream_count data streams plus one del_mask stream
123// Outputs: the deleted streams, plus a partial sum popcount
124
125void DeleteByPEXTkernel::generateDoBlockMethod() {
126    Value * delMask = loadInputStreamBlock("delMaskSet", iBuilder->getInt32(0));
127    const auto masks = get_PEXT_masks(iBuilder, delMask);
128    for (unsigned j = 0; j < mStreamCount; ++j) {
129        Value * input = loadInputStreamBlock("inputStreamSet", iBuilder->getInt32(j));
130        Value * output = apply_PEXT_deletion(iBuilder, masks, input);
131        storeOutputStreamBlock("outputStreamSet", iBuilder->getInt32(j), output);
132    }
133    Value * delCount = partial_sum_popcount(iBuilder, mDelCountFieldWidth, apply_PEXT_deletion(iBuilder, masks, iBuilder->simd_not(delMask)));
134    storeOutputStreamBlock("deletionCounts", iBuilder->getInt32(0), iBuilder->bitCast(delCount));
135}
136
137void DeleteByPEXTkernel::generateFinalBlockMethod(Value * remainingBytes) {
138    IntegerType * vecTy = iBuilder->getIntNTy(iBuilder->getBitBlockWidth());
139    Value * remaining = iBuilder->CreateZExt(remainingBytes, vecTy);
140    Value * EOF_del = iBuilder->bitCast(iBuilder->CreateShl(Constant::getAllOnesValue(vecTy), remaining));
141    Value * delMask = iBuilder->CreateOr(EOF_del, loadInputStreamBlock("delMaskSet", iBuilder->getInt32(0)));
142    const auto masks = get_PEXT_masks(iBuilder, delMask);
143    for (unsigned j = 0; j < mStreamCount; ++j) {
144        Value * input = loadInputStreamBlock("inputStreamSet", iBuilder->getInt32(j));
145        Value * output = apply_PEXT_deletion(iBuilder, masks, input);
146        storeOutputStreamBlock("outputStreamSet", iBuilder->getInt32(j), output);
147    }
148    Value * delCount = partial_sum_popcount(iBuilder, mDelCountFieldWidth, apply_PEXT_deletion(iBuilder, masks, iBuilder->simd_not(delMask)));
149    storeOutputStreamBlock("deletionCounts", iBuilder->getInt32(0), iBuilder->bitCast(delCount));
150}
151
152DeleteByPEXTkernel::DeleteByPEXTkernel(IDISA::IDISA_Builder * iBuilder, unsigned fw, unsigned streamCount)
153: BlockOrientedKernel(iBuilder, "PEXTdel",
154                      {Binding{iBuilder->getStreamSetTy(streamCount), "inputStreamSet"},
155                          Binding{iBuilder->getStreamSetTy(), "delMaskSet"}},
156                      {Binding{iBuilder->getStreamSetTy(streamCount), "outputStreamSet"},
157                          Binding{iBuilder->getStreamSetTy(), "deletionCounts"}},
158                      {}, {}, {})
159, mDelCountFieldWidth(fw)
160, mStreamCount(streamCount) {
161    mDoBlockUpdatesProducedItemCountsAttribute = false;
162}
163
164}
Note: See TracBrowser for help on using the repository browser.