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

Last change on this file since 5402 was 5398, checked in by nmedfort, 2 years ago

Continued work on processing stdin input. Partial integration of ParabixDriver? methods into icgrep and editd. Object cache does not currently work for recursive REs.

File size: 4.0 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
15void editdScanKernel::generateDoBlockMethod() {
16    auto savePoint = iBuilder->saveIP();
17    Function * scanWordFunction = generateScanWordRoutine(iBuilder->getModule());
18    iBuilder->restoreIP(savePoint);
19
20    const unsigned fieldCount = iBuilder->getBitBlockWidth() / mScanwordBitWidth;
21    Type * T = iBuilder->getIntNTy(mScanwordBitWidth);
22    VectorType * scanwordVectorType =  VectorType::get(T, fieldCount);
23    Value * blockNo = getScalarField("BlockNo");
24    Value * scanwordPos = iBuilder->CreateMul(blockNo, ConstantInt::get(blockNo->getType(), iBuilder->getBitBlockWidth()));
25   
26    std::vector<Value * > matchWordVectors;
27    for(unsigned d = 0; d <= mEditDistance; d++) {
28        Value * matches = loadInputStreamBlock("matchResults", iBuilder->getInt32(d));
29        matchWordVectors.push_back(iBuilder->CreateBitCast(matches, scanwordVectorType));
30    }
31   
32    for(unsigned i = 0; i < fieldCount; ++i) {
33        for(unsigned d = 0; d <= mEditDistance; d++) {
34            Value * matchWord = iBuilder->CreateExtractElement(matchWordVectors[d], ConstantInt::get(T, i));
35            iBuilder->CreateCall(scanWordFunction, {matchWord, iBuilder->getInt32(d), scanwordPos});
36        }
37        scanwordPos = iBuilder->CreateAdd(scanwordPos, ConstantInt::get(T, mScanwordBitWidth));
38    }
39
40    setScalarField("BlockNo", iBuilder->CreateAdd(blockNo, iBuilder->getSize(1)));
41}
42
43Function * editdScanKernel::generateScanWordRoutine(Module * m) const {
44
45    IntegerType * T = iBuilder->getIntNTy(mScanwordBitWidth);
46
47    Function * scanFunc = cast<Function>(m->getOrInsertFunction("scan_word", iBuilder->getVoidTy(), T, iBuilder->getInt32Ty(), T, nullptr));
48    scanFunc->setCallingConv(CallingConv::C);
49    Function::arg_iterator args = scanFunc->arg_begin();
50
51    Value * matchWord = &*(args++);
52    matchWord->setName("matchWord");
53    Value * dist = &*(args++);
54    dist->setName("dist");
55    Value * basePos = &*(args++);
56    basePos->setName("basePos");
57
58    Constant * matchProcessor = m->getOrInsertFunction("wrapped_report_pos", iBuilder->getVoidTy(), T, iBuilder->getInt32Ty(), nullptr);
59
60    BasicBlock * entryBlock = BasicBlock::Create(m->getContext(), "entry", scanFunc, 0);
61
62    BasicBlock * matchesCondBlock = BasicBlock::Create(m->getContext(), "matchesCond", scanFunc, 0);
63    BasicBlock * matchesLoopBlock = BasicBlock::Create(m->getContext(), "matchesLoop", scanFunc, 0);
64    BasicBlock * matchesDoneBlock = BasicBlock::Create(m->getContext(), "matchesDone", scanFunc, 0);
65
66    iBuilder->SetInsertPoint(entryBlock);
67    iBuilder->CreateBr(matchesCondBlock);
68
69    iBuilder->SetInsertPoint(matchesCondBlock);
70    PHINode * matches_phi = iBuilder->CreatePHI(T, 2, "matches");
71    matches_phi->addIncoming(matchWord, entryBlock);
72    Value * have_matches_cond = iBuilder->CreateICmpUGT(matches_phi, ConstantInt::get(T, 0));
73    iBuilder->CreateCondBr(have_matches_cond, matchesLoopBlock, matchesDoneBlock);
74
75    iBuilder->SetInsertPoint(matchesLoopBlock);
76    Value * match_pos = iBuilder->CreateAdd(iBuilder->CreateCountForwardZeroes(matches_phi), basePos);
77    Value * matches_new = iBuilder->CreateAnd(matches_phi, iBuilder->CreateSub(matches_phi, ConstantInt::get(T, 1)));
78    matches_phi->addIncoming(matches_new, matchesLoopBlock);
79    iBuilder->CreateCall(matchProcessor, std::vector<Value *>({match_pos, dist}));
80    iBuilder->CreateBr(matchesCondBlock);
81
82    iBuilder->SetInsertPoint(matchesDoneBlock);
83    iBuilder -> CreateRetVoid();
84
85    return scanFunc;
86
87}
88
89editdScanKernel::editdScanKernel(IDISA::IDISA_Builder * iBuilder, unsigned dist) :
90BlockOrientedKernel(iBuilder, "scanMatch",
91              {Binding{iBuilder->getStreamSetTy(dist + 1), "matchResults"}},
92              {}, {}, {}, {Binding{iBuilder->getSizeTy(), "BlockNo"}}),
93mEditDistance(dist),
94mScanwordBitWidth(iBuilder->getSizeTy()->getBitWidth()) {
95
96}
97
98}
Note: See TracBrowser for help on using the repository browser.