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

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

Simplify stream set access; better naming of access functions

File size: 4.3 KB
Line 
1/*
2 *  Copyright (c) 2015 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
6
7#include "editdscan_kernel.h"
8#include <IR_Gen/idisa_builder.h>
9#include <llvm/IR/Module.h>
10
11using namespace llvm;
12
13namespace kernel {
14
15Value * generateCountForwardZeroes(IDISA::IDISA_Builder * iBuilder, Value * bits) {
16    Value * cttzFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::cttz, bits->getType());
17    return iBuilder->CreateCall(cttzFunc, std::vector<Value *>({bits, ConstantInt::get(iBuilder->getInt1Ty(), 0)}));
18}
19
20void editdScanKernel::generateDoBlockMethod() {
21    auto savePoint = iBuilder->saveIP();
22    Function * scanWordFunction = generateScanWordRoutine(iBuilder->getModule());
23    iBuilder->restoreIP(savePoint);
24
25    const unsigned fieldCount = iBuilder->getBitBlockWidth() / mScanwordBitWidth;
26    Type * T = iBuilder->getIntNTy(mScanwordBitWidth);
27    VectorType * scanwordVectorType =  VectorType::get(T, fieldCount);
28    Value * blockNo = getScalarField("BlockNo");
29    Value * scanwordPos = iBuilder->CreateMul(blockNo, ConstantInt::get(blockNo->getType(), iBuilder->getBitBlockWidth()));
30   
31    std::vector<Value * > matchWordVectors;
32    for(unsigned d = 0; d <= mEditDistance; d++) {
33        Value * matches = loadInputStreamBlock("matchResults", iBuilder->getInt32(d));
34        matchWordVectors.push_back(iBuilder->CreateBitCast(matches, scanwordVectorType));
35    }
36   
37    for(unsigned i = 0; i < fieldCount; ++i) {
38        for(unsigned d = 0; d <= mEditDistance; d++) {
39            Value * matchWord = iBuilder->CreateExtractElement(matchWordVectors[d], ConstantInt::get(T, i));
40            iBuilder->CreateCall(scanWordFunction, {matchWord, iBuilder->getInt32(d), scanwordPos});
41        }
42        scanwordPos = iBuilder->CreateAdd(scanwordPos, ConstantInt::get(T, mScanwordBitWidth));
43    }
44
45    setScalarField("BlockNo", iBuilder->CreateAdd(blockNo, iBuilder->getSize(1)));
46}
47
48Function * editdScanKernel::generateScanWordRoutine(Module * m) const {
49
50    IntegerType * T = iBuilder->getIntNTy(mScanwordBitWidth);
51
52    Function * scanFunc = cast<Function>(m->getOrInsertFunction("scan_word", iBuilder->getVoidTy(), T, iBuilder->getInt32Ty(), T, nullptr));
53    scanFunc->setCallingConv(CallingConv::C);
54    Function::arg_iterator args = scanFunc->arg_begin();
55
56    Value * matchWord = &*(args++);
57    matchWord->setName("matchWord");
58    Value * dist = &*(args++);
59    dist->setName("dist");
60    Value * basePos = &*(args++);
61    basePos->setName("basePos");
62
63    Constant * matchProcessor = m->getOrInsertFunction("wrapped_report_pos", iBuilder->getVoidTy(), T, iBuilder->getInt32Ty(), nullptr);
64
65    BasicBlock * entryBlock = BasicBlock::Create(m->getContext(), "entry", scanFunc, 0);
66
67    BasicBlock * matchesCondBlock = BasicBlock::Create(m->getContext(), "matchesCond", scanFunc, 0);
68    BasicBlock * matchesLoopBlock = BasicBlock::Create(m->getContext(), "matchesLoop", scanFunc, 0);
69    BasicBlock * matchesDoneBlock = BasicBlock::Create(m->getContext(), "matchesDone", scanFunc, 0);
70
71    iBuilder->SetInsertPoint(entryBlock);
72    iBuilder->CreateBr(matchesCondBlock);
73
74    iBuilder->SetInsertPoint(matchesCondBlock);
75    PHINode * matches_phi = iBuilder->CreatePHI(T, 2, "matches");
76    matches_phi->addIncoming(matchWord, entryBlock);
77    Value * have_matches_cond = iBuilder->CreateICmpUGT(matches_phi, ConstantInt::get(T, 0));
78    iBuilder->CreateCondBr(have_matches_cond, matchesLoopBlock, matchesDoneBlock);
79
80    iBuilder->SetInsertPoint(matchesLoopBlock);
81    Value * match_pos = iBuilder->CreateAdd(generateCountForwardZeroes(iBuilder, matches_phi), basePos);
82    Value * matches_new = iBuilder->CreateAnd(matches_phi, iBuilder->CreateSub(matches_phi, ConstantInt::get(T, 1)));
83    matches_phi->addIncoming(matches_new, matchesLoopBlock);
84    iBuilder->CreateCall(matchProcessor, std::vector<Value *>({match_pos, dist}));
85    iBuilder->CreateBr(matchesCondBlock);
86
87    iBuilder->SetInsertPoint(matchesDoneBlock);
88    iBuilder -> CreateRetVoid();
89
90    return scanFunc;
91
92}
93
94editdScanKernel::editdScanKernel(IDISA::IDISA_Builder * iBuilder, unsigned dist) :
95BlockOrientedKernel(iBuilder, "scanMatch",
96              {Binding{iBuilder->getStreamSetTy(dist + 1), "matchResults"}},
97              {}, {}, {}, {Binding{iBuilder->getSizeTy(), "BlockNo"}}),
98mEditDistance(dist),
99mScanwordBitWidth(iBuilder->getSizeTy()->getBitWidth()) {
100
101}
102
103}
Note: See TracBrowser for help on using the repository browser.