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

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

Kernel infrastructure: move common logic into KernelBuilder? base class; demo linking in wc

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 <kernels/kernel.h>
7#include <kernels/deletion.h>
8#include <IDISA/idisa_builder.h>
9#include <llvm/IR/Value.h>
10
11std::vector<Value *> parallel_prefix_deletion_masks(IDISA::IDISA_Builder * iBuilder, unsigned fw, Value * del_mask) {
12    Value * m = iBuilder->simd_not(del_mask);
13    Value * mk = iBuilder->simd_slli(fw, del_mask, 1);
14    std::vector<Value *> move_masks;
15    for (unsigned shift = 1; shift < fw; shift *= 2) {
16        Value * mp = mk;
17        for (unsigned lookright = 1; lookright < fw; lookright *= 2) {
18            mp = iBuilder->simd_xor(mp, iBuilder->simd_slli(fw, mp, lookright));
19        }
20        Value * mv = iBuilder->simd_and(mp, m);
21        m = iBuilder->simd_or(iBuilder->simd_xor(m, mv), iBuilder->simd_srli(fw, mv, shift));
22        mk = iBuilder->simd_and(mk, iBuilder->simd_not(mp));
23        move_masks.push_back(mv);
24    }
25    return move_masks;
26}
27
28Value * apply_parallel_prefix_deletion(IDISA::IDISA_Builder * iBuilder, unsigned fw, Value * del_mask, std::vector<Value *> mv, Value * strm) {
29    Value * s = iBuilder->simd_and(strm, iBuilder->simd_not(del_mask));
30    for (unsigned i = 0; i < mv.size(); i++) {
31        unsigned shift = 1 << i;
32        Value * t = iBuilder->simd_and(s, mv[i]);
33        s = iBuilder->simd_or(iBuilder->simd_xor(s, t), iBuilder->simd_srli(fw, t, shift));
34    }
35    return s;
36}
37
38Value * partial_sum_popcount(IDISA::IDISA_Builder * iBuilder, unsigned fw, Value * mask) {
39    Value * per_field = iBuilder->simd_popcount(fw, mask);
40    for (unsigned move = 1; move < iBuilder->getBitBlockWidth()/fw; move *= 2) {
41        per_field = iBuilder->simd_add(fw, per_field, iBuilder->mvmd_slli(fw, per_field, move));
42    }
43    return per_field;
44}
45
46// Apply deletion to a set of stream_count input streams to produce a set of output streams.
47// Kernel inputs: stream_count data streams plus one del_mask stream
48// Outputs: the deleted streams, plus a partial sum popcount
49
50
51void deletionKernel::generateDoBlockMethod() {
52    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
53    Module * m = iBuilder->getModule();
54   
55    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
56   
57    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
58   
59    Value * inputStreamBlock = getParameter(doBlockFunction, "inputStreamSet");
60    Value * outputStreamBlock = getParameter(doBlockFunction, "outputStreamSet");
61    Value * delCountBlock = getParameter(doBlockFunction, "deletionCounts");
62   
63    Value * del_mask = iBuilder->CreateBlockAlignedLoad(inputStreamBlock, {iBuilder->getInt32(0), iBuilder->getInt32(mStreamCount)});
64   
65    std::vector<Value *> move_masks = parallel_prefix_deletion_masks(iBuilder, mDeletionFieldWidth, del_mask);
66   
67    for (unsigned j = 0; j < mStreamCount; ++j) {
68        Value * input = iBuilder->CreateBlockAlignedLoad(inputStreamBlock, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
69        Value * output = apply_parallel_prefix_deletion(iBuilder, mDeletionFieldWidth, del_mask, move_masks, input);
70        iBuilder->CreateBlockAlignedStore(output, outputStreamBlock, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
71    }
72    Value * counts = partial_sum_popcount(iBuilder, mDeletionFieldWidth, iBuilder->simd_not(del_mask));
73    iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(counts), delCountBlock, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
74   
75    iBuilder->CreateRetVoid();
76    iBuilder->restoreIP(savePoint);
77}
78
79void deletionKernel::generateFinalBlockMethod() {
80    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
81    Module * m = iBuilder->getModule();
82   
83    unsigned blockSize = iBuilder->getBitBlockWidth();
84    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
85    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
86
87    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", finalBlockFunction, 0));
88    Value * remainingBytes = getParameter(finalBlockFunction, "remainingBytes");
89    Value * inputStreamBlock = getParameter(finalBlockFunction, "inputStreamSet");
90    Value * remaining = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(blockSize));
91    Value * EOF_del = iBuilder->bitCast(iBuilder->CreateShl(Constant::getAllOnesValue(iBuilder->getIntNTy(blockSize)), remaining));
92    Value * const delmaskPtr = iBuilder->CreateGEP(inputStreamBlock, {iBuilder->getInt32(0), iBuilder->getInt32(16)});
93    Value * const delmaskVal = iBuilder->CreateBlockAlignedLoad(delmaskPtr);
94    iBuilder->CreateBlockAlignedStore(iBuilder->CreateOr(EOF_del, delmaskVal), delmaskPtr);
95    Function::arg_iterator args = finalBlockFunction->arg_begin();
96    Value * self = &*(args++);
97    /* Skip "remaining" arg */ args++;
98    std::vector<Value *> doBlockArgs = {self};
99    while (args != finalBlockFunction->arg_end()){
100        doBlockArgs.push_back(&*args++);
101    }
102    iBuilder->CreateCall(doBlockFunction, doBlockArgs);   
103    iBuilder->CreateRetVoid();
104    iBuilder->restoreIP(savePoint);
105}
106
107
Note: See TracBrowser for help on using the repository browser.