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

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

Code clean-up. Removed Pablo Call, SetIthBit? and Prototype.

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