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

Last change on this file since 5260 was 5260, checked in by nmedfort, 9 months ago

Changes working towards simplifying accessing stream elements + some modifications to simplify include / forward declarations within the CodeGen? library.

File size: 6.6 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    /* Adjust the produced item count */
44    Value * produced = getProducedItemCount(self, "ResultStream");
45    produced = iBuilder->CreateSub(produced, iBuilder->getSize(iBuilder->getStride()));
46    setProducedItemCount(self, "ResultStream", iBuilder->CreateAdd(produced, remaining));
47    iBuilder->CreateRetVoid();
48    iBuilder->restoreIP(savePoint);
49}
50   
51void editdCPUKernel::generateDoBlockMethod() const {
52    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
53    Module * m = iBuilder->getModule(); 
54
55    Type * const int32ty = iBuilder->getInt32Ty();
56    Type * const int8ty = iBuilder->getInt8Ty();
57
58    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
59       
60    BasicBlock * entryBlock = BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0);
61   
62    iBuilder->SetInsertPoint(entryBlock);
63
64    Value * kernelStuctParam = getParameter(doBlockFunction, "self");
65    Value * pattStartPtr = getScalarField(kernelStuctParam, "pattStream");
66    Value * stideCarryArr = getScalarField(kernelStuctParam, "srideCarry");
67    Value * blockNo = getScalarField(kernelStuctParam, blockNoScalar);
68   
69    unsigned carryIdx = 0;
70
71    std::vector<std::vector<Value *>> e(mPatternLen+1, std::vector<Value *>(mEditDistance+1));
72    std::vector<std::vector<Value *>> adv(mPatternLen, std::vector<Value *>(mEditDistance+1));
73    std::vector<std::vector<int>> calculated(mPatternLen, std::vector<int>(mEditDistance + 1, 0));
74    Value * pattPos = iBuilder->getInt32(0);
75    Value * pattPtr = iBuilder->CreateGEP(pattStartPtr, pattPos);
76    Value * pattCh = iBuilder->CreateLoad(pattPtr);
77    Value * pattIdx = iBuilder->CreateAnd(iBuilder->CreateLShr(pattCh, 1), ConstantInt::get(int8ty, 3));
78    Value * pattStreamPtr = getStream(kernelStuctParam, "CCStream", blockNo, iBuilder->CreateZExt(pattIdx, int32ty));
79    Value * pattStream = iBuilder->CreateLoad(pattStreamPtr);
80    pattPos = iBuilder->CreateAdd(pattPos, ConstantInt::get(int32ty, 1));
81
82    e[0][0] = pattStream;
83    for(unsigned j = 1; j <= mEditDistance; j++){
84      e[0][j] = iBuilder->allOnes();
85    }
86
87    for(unsigned i = 1; i < mPatternLen; i++){
88        pattPtr = iBuilder->CreateGEP(pattStartPtr, pattPos);
89        pattCh = iBuilder->CreateLoad(pattPtr);
90        pattIdx = iBuilder->CreateAnd(iBuilder->CreateLShr(pattCh, 1), ConstantInt::get(int8ty, 3));
91        pattStreamPtr = getStream(kernelStuctParam, "CCStream", blockNo, iBuilder->CreateZExt(pattIdx, int32ty));
92        pattStream = iBuilder->CreateLoad(pattStreamPtr);
93
94        bitblock_advance_ci_co(e[i-1][0], 1, stideCarryArr, carryIdx++, adv, calculated, i-1, 0);
95        e[i][0] = iBuilder->CreateAnd(adv[i-1][0], pattStream); 
96        for(unsigned j = 1; j<= mEditDistance; j++){
97            bitblock_advance_ci_co(e[i-1][j], 1, stideCarryArr, carryIdx++, adv, calculated, i-1, j);
98            bitblock_advance_ci_co(e[i-1][j-1], 1, stideCarryArr, carryIdx++, adv, calculated, i-1, j-1);
99            bitblock_advance_ci_co(e[i][j-1], 1, stideCarryArr, carryIdx++, adv, calculated, i, j-1);
100            Value * tmp1 = iBuilder->CreateAnd(adv[i-1][j], pattStream);
101            Value * tmp2 = iBuilder->CreateAnd(adv[i-1][j-1], iBuilder->CreateNot(pattStream));
102            Value * tmp3 = iBuilder->CreateOr(adv[i][j-1], e[i-1][j-1]);
103            e[i][j] = iBuilder->CreateOr(iBuilder->CreateOr(tmp1, tmp2), tmp3);
104
105        }
106        pattPos = iBuilder->CreateAdd(pattPos, ConstantInt::get(int32ty, 1));
107    }
108   
109    Value * ptr = getStream(kernelStuctParam, "ResultStream", blockNo, iBuilder->getInt32(0));
110    iBuilder->CreateStore(e[mPatternLen - 1][0], ptr);
111    for(unsigned j = 1; j<= mEditDistance; j++){
112        ptr = getStream(kernelStuctParam, "ResultStream", blockNo, iBuilder->getInt32(j));
113        iBuilder->CreateStore(iBuilder->CreateAnd(e[mPatternLen-1][j], iBuilder->CreateNot(e[mPatternLen-1][j-1])), ptr);
114    }
115
116    Value * produced = getProducedItemCount(kernelStuctParam, "ResultStream");
117    produced = iBuilder->CreateAdd(produced, iBuilder->getSize(iBuilder->getStride()));
118    setProducedItemCount(kernelStuctParam, "ResultStream", produced); 
119       
120    iBuilder->CreateRetVoid();
121    iBuilder->restoreIP(savePoint);
122}
123
124editdCPUKernel::editdCPUKernel(IDISA::IDISA_Builder * b, unsigned dist, unsigned pattLen) :
125KernelBuilder(b, "editd_cpu",
126             {Binding{b->getStreamSetTy(4), "CCStream"}},
127             {Binding{b->getStreamSetTy(dist + 1), "ResultStream"}},
128             {Binding{PointerType::get(b->getInt8Ty(), 1), "pattStream"},
129             Binding{PointerType::get(ArrayType::get(b->getBitBlockType(), pattLen * (dist + 1) * 4), 0), "srideCarry"}},
130             {},
131             {Binding{b->getBitBlockType(), "EOFmask"}}),
132mEditDistance(dist),
133mPatternLen(pattLen){
134
135}
136
137}
138
139
Note: See TracBrowser for help on using the repository browser.