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

Last change on this file since 5353 was 5347, checked in by nmedfort, 3 years ago

Added enable asserts (-ea) command line flag + restructured BlockOrientedKernels? to allow for inlined code.

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) {
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    storeOutputStreamBlock("deletionCounts", iBuilder->getInt32(0), iBuilder->bitCast(delCount));
138}
139
140void DeleteByPEXTkernel::generateFinalBlockMethod(Value * remainingBytes) {
141    IntegerType * vecTy = iBuilder->getIntNTy(iBuilder->getBitBlockWidth());
142    Value * remaining = iBuilder->CreateZExt(remainingBytes, vecTy);
143    Value * EOF_del = iBuilder->bitCast(iBuilder->CreateShl(Constant::getAllOnesValue(vecTy), remaining));
144    Value * delMask = iBuilder->CreateOr(EOF_del, loadInputStreamBlock("delMaskSet", iBuilder->getInt32(0)));
145    const auto masks = get_PEXT_masks(iBuilder, delMask);
146    for (unsigned j = 0; j < mStreamCount; ++j) {
147        Value * input = loadInputStreamBlock("inputStreamSet", iBuilder->getInt32(j));
148        Value * output = apply_PEXT_deletion(iBuilder, masks, input);
149        storeOutputStreamBlock("outputStreamSet", iBuilder->getInt32(j), output);
150    }
151    Value * delCount = partial_sum_popcount(iBuilder, mDelCountFieldWidth, apply_PEXT_deletion(iBuilder, masks, iBuilder->simd_not(delMask)));
152    storeOutputStreamBlock("deletionCounts", iBuilder->getInt32(0), iBuilder->bitCast(delCount));
153}
154
155DeleteByPEXTkernel::DeleteByPEXTkernel(IDISA::IDISA_Builder * iBuilder, unsigned fw, unsigned streamCount)
156: BlockOrientedKernel(iBuilder, "PEXTdel",
157                      {Binding{iBuilder->getStreamSetTy(streamCount), "inputStreamSet"},
158                          Binding{iBuilder->getStreamSetTy(), "delMaskSet"}},
159                      {Binding{iBuilder->getStreamSetTy(streamCount), "outputStreamSet"},
160                          Binding{iBuilder->getStreamSetTy(), "deletionCounts"}},
161                      {}, {}, {})
162, mDelCountFieldWidth(fw)
163, mStreamCount(streamCount) {
164    mDoBlockUpdatesProducedItemCountsAttribute = false;
165}
166
167}
Note: See TracBrowser for help on using the repository browser.