source: icGREP/icgrep-devel/icgrep/kernels/cc_scan_kernel.cpp @ 5385

Last change on this file since 5385 was 5317, checked in by cameron, 3 years ago

Simplify stream set access; better naming of access functions

File size: 4.2 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 "cc_scan_kernel.h"
8#include <IR_Gen/idisa_builder.h>
9#include <llvm/IR/Module.h>
10
11using namespace llvm;
12
13namespace kernel {
14
15void CCScanKernel::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 < mStreamNum; 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 < mStreamNum; 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    setScalarField("BlockNo", iBuilder->CreateAdd(blockNo, iBuilder->getSize(1)));
40}
41
42Function * CCScanKernel::generateScanWordRoutine(Module * m) const {
43
44    IntegerType * T = iBuilder->getIntNTy(mScanwordBitWidth);
45
46    Function * scanFunc = cast<Function>(m->getOrInsertFunction("scan_word", iBuilder->getVoidTy(), T, iBuilder->getInt32Ty(), T, nullptr));
47    scanFunc->setCallingConv(CallingConv::C);
48    Function::arg_iterator args = scanFunc->arg_begin();
49
50    Value * matchWord = &*(args++);
51    matchWord->setName("matchWord");
52    Value * dist = &*(args++);
53    dist->setName("dist");
54    Value * basePos = &*(args++);
55    basePos->setName("basePos");
56
57    Constant * matchProcessor = m->getOrInsertFunction("wrapped_report_pos", iBuilder->getVoidTy(), T, iBuilder->getInt32Ty(), nullptr);
58
59    BasicBlock * entryBlock = BasicBlock::Create(m->getContext(), "entry", scanFunc, 0);
60
61    BasicBlock * matchesCondBlock = BasicBlock::Create(m->getContext(), "matchesCond", scanFunc, 0);
62    BasicBlock * matchesLoopBlock = BasicBlock::Create(m->getContext(), "matchesLoop", scanFunc, 0);
63    BasicBlock * matchesDoneBlock = BasicBlock::Create(m->getContext(), "matchesDone", scanFunc, 0);
64
65    iBuilder->SetInsertPoint(entryBlock);
66    iBuilder->CreateBr(matchesCondBlock);
67
68    iBuilder->SetInsertPoint(matchesCondBlock);
69    PHINode * matches_phi = iBuilder->CreatePHI(T, 2, "matches");
70    matches_phi->addIncoming(matchWord, entryBlock);
71    Value * have_matches_cond = iBuilder->CreateICmpUGT(matches_phi, ConstantInt::get(T, 0));
72    iBuilder->CreateCondBr(have_matches_cond, matchesLoopBlock, matchesDoneBlock);
73
74    iBuilder->SetInsertPoint(matchesLoopBlock);
75
76    Value * cttzFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::cttz, matches_phi->getType());
77    Value * tz = iBuilder->CreateCall(cttzFunc, std::vector<Value *>({matches_phi, ConstantInt::get(iBuilder->getInt1Ty(), 0)}));
78
79    Value * match_pos = iBuilder->CreateAdd(tz, basePos);
80    Value * matches_new = iBuilder->CreateAnd(matches_phi, iBuilder->CreateSub(matches_phi, ConstantInt::get(T, 1)));
81    matches_phi->addIncoming(matches_new, matchesLoopBlock);
82    iBuilder->CreateCall(matchProcessor, std::vector<Value *>({match_pos, dist}));
83    iBuilder->CreateBr(matchesCondBlock);
84
85    iBuilder->SetInsertPoint(matchesDoneBlock);
86    iBuilder -> CreateRetVoid();
87
88    return scanFunc;
89
90}
91
92CCScanKernel::CCScanKernel(IDISA::IDISA_Builder * iBuilder, unsigned streamNum) :
93BlockOrientedKernel(iBuilder, "CCScan",
94              {Binding{iBuilder->getStreamSetTy(streamNum), "matchResults"}},
95              {}, {}, {}, {Binding{iBuilder->getSizeTy(), "BlockNo"}}),
96mStreamNum(streamNum),
97mScanwordBitWidth(iBuilder->getSizeTy()->getBitWidth()) {
98
99}
100
101}
Note: See TracBrowser for help on using the repository browser.