source: icGREP/icgrep-devel/icgrep/editd/editd_cpu_kernel.cpp @ 5261

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

Move responsibility for ProducedItemCount? into doSegment unless overridden

File size: 6.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#include "editd_cpu_kernel.h"
6#include <kernels/kernel.h>
7#include <IR_Gen/idisa_builder.h>
8#include <llvm/IR/Module.h>
9#include <llvm/Support/raw_ostream.h>
10#include <iostream>
11
12using namespace llvm;
13
14namespace kernel {
15
16void editdCPUKernel::bitblock_advance_ci_co(Value * val, unsigned shift, Value * stideCarryArr, unsigned carryIdx, std::vector<std::vector<Value *>> & adv, std::vector<std::vector<int>> & calculated, int i, int j) const {
17    if (calculated[i][j] == 0) {
18        Value * ptr = iBuilder->CreateGEP(stideCarryArr, {iBuilder->getInt32(0), iBuilder->getInt32(carryIdx)});
19        Value * ci = iBuilder->CreateLoad(ptr);
20        std::pair<Value *, Value *> rslt = iBuilder->bitblock_advance(val, ci, shift);
21        iBuilder->CreateStore(std::get<0>(rslt), ptr);
22        adv[i][j] = std::get<1>(rslt);
23        calculated[i][j] = 1;
24    }
25}
26
27void editdCPUKernel::generateFinalBlockMethod() const {
28    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
29    Module * m = iBuilder->getModule();
30    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
31    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
32    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_entry", finalBlockFunction, 0));
33    // Final Block arguments: self, remaining, then the standard DoBlock args.
34    Function::arg_iterator args = finalBlockFunction->arg_begin();
35    Value * self = &*(args++);
36    Value * remaining = &*(args++);
37    std::vector<Value *> doBlockArgs = {self};
38    while (args != finalBlockFunction->arg_end()){
39        doBlockArgs.push_back(&*args++);
40    }
41    setScalarField(self, "EOFmask", iBuilder->bitblock_mask_from(remaining));
42    iBuilder->CreateCall(doBlockFunction, doBlockArgs);
43    iBuilder->CreateRetVoid();
44    iBuilder->restoreIP(savePoint);
45}
46   
47void editdCPUKernel::generateDoBlockMethod() const {
48    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
49    Module * m = iBuilder->getModule(); 
50
51    Type * const int32ty = iBuilder->getInt32Ty();
52    Type * const int8ty = iBuilder->getInt8Ty();
53
54    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
55       
56    BasicBlock * entryBlock = BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0);
57   
58    iBuilder->SetInsertPoint(entryBlock);
59
60    Value * kernelStuctParam = getParameter(doBlockFunction, "self");
61    Value * pattStartPtr = getScalarField(kernelStuctParam, "pattStream");
62    Value * stideCarryArr = getScalarField(kernelStuctParam, "srideCarry");
63    Value * blockNo = getScalarField(kernelStuctParam, blockNoScalar);
64   
65    unsigned carryIdx = 0;
66
67    std::vector<std::vector<Value *>> e(mPatternLen+1, std::vector<Value *>(mEditDistance+1));
68    std::vector<std::vector<Value *>> adv(mPatternLen, std::vector<Value *>(mEditDistance+1));
69    std::vector<std::vector<int>> calculated(mPatternLen, std::vector<int>(mEditDistance + 1, 0));
70    Value * pattPos = iBuilder->getInt32(0);
71    Value * pattPtr = iBuilder->CreateGEP(pattStartPtr, pattPos);
72    Value * pattCh = iBuilder->CreateLoad(pattPtr);
73    Value * pattIdx = iBuilder->CreateAnd(iBuilder->CreateLShr(pattCh, 1), ConstantInt::get(int8ty, 3));
74    Value * pattStreamPtr = getStream(kernelStuctParam, "CCStream", blockNo, iBuilder->CreateZExt(pattIdx, int32ty));
75    Value * pattStream = iBuilder->CreateLoad(pattStreamPtr);
76    pattPos = iBuilder->CreateAdd(pattPos, ConstantInt::get(int32ty, 1));
77
78    e[0][0] = pattStream;
79    for(unsigned j = 1; j <= mEditDistance; j++){
80      e[0][j] = iBuilder->allOnes();
81    }
82
83    for(unsigned i = 1; i < mPatternLen; i++){
84        pattPtr = iBuilder->CreateGEP(pattStartPtr, pattPos);
85        pattCh = iBuilder->CreateLoad(pattPtr);
86        pattIdx = iBuilder->CreateAnd(iBuilder->CreateLShr(pattCh, 1), ConstantInt::get(int8ty, 3));
87        pattStreamPtr = getStream(kernelStuctParam, "CCStream", blockNo, iBuilder->CreateZExt(pattIdx, int32ty));
88        pattStream = iBuilder->CreateLoad(pattStreamPtr);
89
90        bitblock_advance_ci_co(e[i-1][0], 1, stideCarryArr, carryIdx++, adv, calculated, i-1, 0);
91        e[i][0] = iBuilder->CreateAnd(adv[i-1][0], pattStream); 
92        for(unsigned j = 1; j<= mEditDistance; j++){
93            bitblock_advance_ci_co(e[i-1][j], 1, stideCarryArr, carryIdx++, adv, calculated, i-1, j);
94            bitblock_advance_ci_co(e[i-1][j-1], 1, stideCarryArr, carryIdx++, adv, calculated, i-1, j-1);
95            bitblock_advance_ci_co(e[i][j-1], 1, stideCarryArr, carryIdx++, adv, calculated, i, j-1);
96            Value * tmp1 = iBuilder->CreateAnd(adv[i-1][j], pattStream);
97            Value * tmp2 = iBuilder->CreateAnd(adv[i-1][j-1], iBuilder->CreateNot(pattStream));
98            Value * tmp3 = iBuilder->CreateOr(adv[i][j-1], e[i-1][j-1]);
99            e[i][j] = iBuilder->CreateOr(iBuilder->CreateOr(tmp1, tmp2), tmp3);
100
101        }
102        pattPos = iBuilder->CreateAdd(pattPos, ConstantInt::get(int32ty, 1));
103    }
104   
105    Value * ptr = getStream(kernelStuctParam, "ResultStream", blockNo, iBuilder->getInt32(0));
106    iBuilder->CreateStore(e[mPatternLen - 1][0], ptr);
107    for(unsigned j = 1; j<= mEditDistance; j++){
108        ptr = getStream(kernelStuctParam, "ResultStream", blockNo, iBuilder->getInt32(j));
109        iBuilder->CreateStore(iBuilder->CreateAnd(e[mPatternLen-1][j], iBuilder->CreateNot(e[mPatternLen-1][j-1])), ptr);
110    }
111       
112    iBuilder->CreateRetVoid();
113    iBuilder->restoreIP(savePoint);
114}
115
116editdCPUKernel::editdCPUKernel(IDISA::IDISA_Builder * b, unsigned dist, unsigned pattLen) :
117KernelBuilder(b, "editd_cpu",
118             {Binding{b->getStreamSetTy(4), "CCStream"}},
119             {Binding{b->getStreamSetTy(dist + 1), "ResultStream"}},
120             {Binding{PointerType::get(b->getInt8Ty(), 1), "pattStream"},
121             Binding{PointerType::get(ArrayType::get(b->getBitBlockType(), pattLen * (dist + 1) * 4), 0), "srideCarry"}},
122             {},
123             {Binding{b->getBitBlockType(), "EOFmask"}}),
124mEditDistance(dist),
125mPatternLen(pattLen){
126setDoBlockUpdatesProducedItemCountsAttribute(false);
127}
128
129}
130
131
Note: See TracBrowser for help on using the repository browser.