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

Last change on this file since 5223 was 5220, checked in by cameron, 3 years ago

Automate instance creation

File size: 4.4 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 <llvm/IR/Intrinsics.h>
9#include <IDISA/idisa_builder.h>
10#include <llvm/Support/raw_os_ostream.h>
11#include <iostream>
12
13using namespace llvm;
14
15namespace kernel {
16
17Value * generateCountForwardZeroes(IDISA::IDISA_Builder * iBuilder, Value * bits) {
18    Value * cttzFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::cttz, bits->getType());
19    return iBuilder->CreateCall(cttzFunc, std::vector<Value *>({bits, ConstantInt::get(iBuilder->getInt1Ty(), 0)}));
20}
21
22void editdScanKernel::generateDoBlockMethod() {
23    auto savePoint = iBuilder->saveIP();
24    Module * m = iBuilder->getModule();
25    Function * scanWordFunction = generateScanWordRoutine(m);
26    const unsigned fieldCount = iBuilder->getBitBlockWidth() / mScanwordBitWidth;
27    Type * T = iBuilder->getIntNTy(mScanwordBitWidth);
28    Type * scanwordVectorType =  VectorType::get(T, fieldCount);
29
30    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
31
32    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
33    Value * kernelStuctParam = getParameter(doBlockFunction, "self");
34    Value * blockNo = getScalarField(kernelStuctParam, blockNoScalar);
35    Value * scanwordPos = iBuilder->CreateMul(blockNo, ConstantInt::get(blockNo->getType(), iBuilder->getBitBlockWidth()));
36   
37    Value * matchResultsPtr = getStreamSetBlockPtr(kernelStuctParam, "matchResults", blockNo);
38
39    std::vector<Value * > matchWordVectors;
40    for(unsigned d = 0; d <= mEditDistance; d++){
41        Value * matches = iBuilder->CreateBlockAlignedLoad(matchResultsPtr, {iBuilder->getInt32(0), iBuilder->getInt32(d)});
42        matchWordVectors.push_back(iBuilder->CreateBitCast(matches, scanwordVectorType));
43    }
44   
45    for(unsigned i = 0; i < fieldCount; ++i){       
46        for(unsigned d = 0; d <= mEditDistance; d++){
47            Value * matchWord = iBuilder->CreateExtractElement(matchWordVectors[d], ConstantInt::get(T, i));
48            iBuilder->CreateCall(scanWordFunction, {matchWord, iBuilder->getInt32(d), scanwordPos});
49        }
50        scanwordPos = iBuilder->CreateAdd(scanwordPos, ConstantInt::get(T, mScanwordBitWidth));
51
52    }
53    iBuilder -> CreateRetVoid();
54    iBuilder->restoreIP(savePoint);
55}
56
57Function * editdScanKernel::generateScanWordRoutine(Module * m) {
58
59    Type * T = iBuilder->getIntNTy(mScanwordBitWidth);
60    LLVMContext & ctxt = m->getContext();
61
62    Function * scanFunc = cast<Function>(m->getOrInsertFunction("scan_word", Type::getVoidTy(ctxt), T, iBuilder->getInt32Ty(), T, nullptr));
63    scanFunc->setCallingConv(CallingConv::C);
64    Function::arg_iterator args = scanFunc->arg_begin();
65
66    Value * matchWord = &*(args++);
67    matchWord->setName("matchWord");
68    Value * dist = &*(args++);
69    dist->setName("dist");
70    Value * basePos = &*(args++);
71    basePos->setName("basePos");
72
73    Constant * matchProcessor = m->getOrInsertFunction("wrapped_report_pos", Type::getVoidTy(ctxt), T, iBuilder->getInt32Ty(), nullptr);
74
75    BasicBlock * entryBlock = BasicBlock::Create(m->getContext(), "entry", scanFunc, 0);
76
77    BasicBlock * matchesCondBlock = BasicBlock::Create(m->getContext(), "matchesCond", scanFunc, 0);
78    BasicBlock * matchesLoopBlock = BasicBlock::Create(m->getContext(), "matchesLoop", scanFunc, 0);
79    BasicBlock * matchesDoneBlock = BasicBlock::Create(m->getContext(), "matchesDone", scanFunc, 0);
80
81    iBuilder->SetInsertPoint(entryBlock);
82    iBuilder->CreateBr(matchesCondBlock);
83
84    iBuilder->SetInsertPoint(matchesCondBlock);
85    PHINode * matches_phi = iBuilder->CreatePHI(T, 2, "matches");
86    matches_phi->addIncoming(matchWord, entryBlock);
87    Value * have_matches_cond = iBuilder->CreateICmpUGT(matches_phi, ConstantInt::get(T, 0));
88    iBuilder->CreateCondBr(have_matches_cond, matchesLoopBlock, matchesDoneBlock);
89
90    iBuilder->SetInsertPoint(matchesLoopBlock);
91    Value * match_pos = iBuilder->CreateAdd(generateCountForwardZeroes(iBuilder, matches_phi), basePos);
92    Value * matches_new = iBuilder->CreateAnd(matches_phi, iBuilder->CreateSub(matches_phi, ConstantInt::get(T, 1)));
93    matches_phi->addIncoming(matches_new, matchesLoopBlock);
94    iBuilder->CreateCall(matchProcessor, std::vector<Value *>({match_pos, dist}));
95    iBuilder->CreateBr(matchesCondBlock);
96
97    iBuilder->SetInsertPoint(matchesDoneBlock);
98    iBuilder -> CreateRetVoid();
99
100    return scanFunc;
101
102}
103
104}
Note: See TracBrowser for help on using the repository browser.