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

Last change on this file since 5377 was 5356, checked in by cameron, 2 years ago

Eliminate obsolete DoBlockUpdatesProducedItemCounts? attribute

File size: 4.4 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::generateDoBlockMethod() {
26    auto savePoint = iBuilder->saveIP();
27
28    Type * const int32ty = iBuilder->getInt32Ty();
29    Type * const int8ty = iBuilder->getInt8Ty();
30
31    Value * pattStartPtr = getScalarField("pattStream");
32    Value * strideCarryArr = getScalarField("strideCarry");
33
34    unsigned carryIdx = 0;
35
36    std::vector<std::vector<Value *>> e(mPatternLen + 1, std::vector<Value *>(mEditDistance + 1));
37    std::vector<std::vector<Value *>> adv(mPatternLen, std::vector<Value *>(mEditDistance + 1));
38    std::vector<std::vector<int>> calculated(mPatternLen, std::vector<int>(mEditDistance + 1, 0));
39    Value * pattPos = iBuilder->getInt32(0);
40    Value * pattPtr = iBuilder->CreateGEP(pattStartPtr, pattPos);
41    Value * pattCh = iBuilder->CreateLoad(pattPtr);
42    Value * pattIdx = iBuilder->CreateAnd(iBuilder->CreateLShr(pattCh, 1), ConstantInt::get(int8ty, 3));
43    Value * pattStream = loadInputStreamBlock("CCStream", iBuilder->CreateZExt(pattIdx, int32ty));
44    pattPos = iBuilder->CreateAdd(pattPos, ConstantInt::get(int32ty, 1));
45
46    e[0][0] = pattStream;
47    for(unsigned j = 1; j <= mEditDistance; j++){
48      e[0][j] = iBuilder->allOnes();
49    }
50
51    for(unsigned i = 1; i < mPatternLen; i++){
52        pattPtr = iBuilder->CreateGEP(pattStartPtr, pattPos);
53        pattCh = iBuilder->CreateLoad(pattPtr);
54        pattIdx = iBuilder->CreateAnd(iBuilder->CreateLShr(pattCh, 1), ConstantInt::get(int8ty, 3));
55        Value * pattStream = loadInputStreamBlock("CCStream", iBuilder->CreateZExt(pattIdx, int32ty));
56
57        bitblock_advance_ci_co(e[i-1][0], 1, strideCarryArr, carryIdx++, adv, calculated, i-1, 0);
58        e[i][0] = iBuilder->CreateAnd(adv[i-1][0], pattStream); 
59        for(unsigned j = 1; j<= mEditDistance; j++){
60            bitblock_advance_ci_co(e[i-1][j], 1, strideCarryArr, carryIdx++, adv, calculated, i-1, j);
61            bitblock_advance_ci_co(e[i-1][j-1], 1, strideCarryArr, carryIdx++, adv, calculated, i-1, j-1);
62            bitblock_advance_ci_co(e[i][j-1], 1, strideCarryArr, carryIdx++, adv, calculated, i, j-1);
63            Value * tmp1 = iBuilder->CreateAnd(adv[i-1][j], pattStream);
64            Value * tmp2 = iBuilder->CreateAnd(adv[i-1][j-1], iBuilder->CreateNot(pattStream));
65            Value * tmp3 = iBuilder->CreateOr(adv[i][j-1], e[i-1][j-1]);
66            e[i][j] = iBuilder->CreateOr(iBuilder->CreateOr(tmp1, tmp2), tmp3);
67
68        }
69        pattPos = iBuilder->CreateAdd(pattPos, ConstantInt::get(int32ty, 1));
70    }
71   
72    storeOutputStreamBlock("ResultStream", iBuilder->getInt32(0), e[mPatternLen-1][0]);
73    for(unsigned j = 1; j<= mEditDistance; j++){
74        storeOutputStreamBlock("ResultStream", iBuilder->getInt32(j), iBuilder->CreateAnd(e[mPatternLen-1][j], iBuilder->CreateNot(e[mPatternLen-1][j-1])));
75    }
76       
77    iBuilder->CreateRetVoid();
78    iBuilder->restoreIP(savePoint);
79}
80
81void editdCPUKernel::generateFinalBlockMethod(Value * remainingBytes) {
82    setScalarField("EOFmask", iBuilder->bitblock_mask_from(remainingBytes));
83    CreateDoBlockMethodCall();
84}
85
86editdCPUKernel::editdCPUKernel(IDISA::IDISA_Builder * b, unsigned dist, unsigned pattLen) :
87BlockOrientedKernel(b, "editd_cpu",
88             {Binding{b->getStreamSetTy(4), "CCStream"}},
89             {Binding{b->getStreamSetTy(dist + 1), "ResultStream"}},
90             {Binding{PointerType::get(b->getInt8Ty(), 1), "pattStream"},
91             Binding{PointerType::get(ArrayType::get(b->getBitBlockType(), pattLen * (dist + 1) * 4), 0), "strideCarry"}},
92             {},
93             {Binding{b->getBitBlockType(), "EOFmask"}}),
94mEditDistance(dist),
95mPatternLen(pattLen){
96}
97
98}
99
100
Note: See TracBrowser for help on using the repository browser.