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

Last change on this file since 5439 was 5436, checked in by nmedfort, 2 years ago

Continued refactoring work. PabloKernel? now abstract base type with a 'generatePabloMethod' hook to generate Pablo code.

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 <llvm/IR/Module.h>
9#include <kernels/kernel_builder.h>
10
11
12using namespace llvm;
13
14namespace kernel {
15
16void CCScanKernel::generateDoBlockMethod() {
17    auto savePoint = iBuilder->saveIP();
18    Function * scanWordFunction = generateScanWordRoutine(iBuilder->getModule());
19    iBuilder->restoreIP(savePoint);
20
21    const unsigned fieldCount = iBuilder->getBitBlockWidth() / mScanwordBitWidth;
22    Type * T = iBuilder->getIntNTy(mScanwordBitWidth);
23    VectorType * scanwordVectorType =  VectorType::get(T, fieldCount);   
24    Value * blockNo = getScalarField("BlockNo");
25    Value * scanwordPos = iBuilder->CreateMul(blockNo, ConstantInt::get(blockNo->getType(), iBuilder->getBitBlockWidth()));
26   
27    std::vector<Value * > matchWordVectors;
28    for(unsigned d = 0; d < mStreamNum; d++) {
29        Value * matches = loadInputStreamBlock("matchResults", iBuilder->getInt32(d));
30        matchWordVectors.push_back(iBuilder->CreateBitCast(matches, scanwordVectorType));
31    }
32   
33    for(unsigned i = 0; i < fieldCount; ++i) {
34        for(unsigned d = 0; d < mStreamNum; d++) {
35            Value * matchWord = iBuilder->CreateExtractElement(matchWordVectors[d], ConstantInt::get(T, i));
36            iBuilder->CreateCall(scanWordFunction, {matchWord, iBuilder->getInt32(d), scanwordPos});
37        }
38        scanwordPos = iBuilder->CreateAdd(scanwordPos, ConstantInt::get(T, mScanwordBitWidth));
39    }   
40    setScalarField("BlockNo", iBuilder->CreateAdd(blockNo, iBuilder->getSize(1)));
41}
42
43Function * CCScanKernel::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
77    Value * cttzFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::cttz, matches_phi->getType());
78    Value * tz = iBuilder->CreateCall(cttzFunc, std::vector<Value *>({matches_phi, ConstantInt::get(iBuilder->getInt1Ty(), 0)}));
79
80    Value * match_pos = iBuilder->CreateAdd(tz, basePos);
81    Value * matches_new = iBuilder->CreateAnd(matches_phi, iBuilder->CreateSub(matches_phi, ConstantInt::get(T, 1)));
82    matches_phi->addIncoming(matches_new, matchesLoopBlock);
83    iBuilder->CreateCall(matchProcessor, std::vector<Value *>({match_pos, dist}));
84    iBuilder->CreateBr(matchesCondBlock);
85
86    iBuilder->SetInsertPoint(matchesDoneBlock);
87    iBuilder -> CreateRetVoid();
88
89    return scanFunc;
90
91}
92
93CCScanKernel::CCScanKernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, unsigned streamNum) :
94BlockOrientedKernel("CCScan",
95              {Binding{iBuilder->getStreamSetTy(streamNum), "matchResults"}},
96              {}, {}, {}, {Binding{iBuilder->getSizeTy(), "BlockNo"}}),
97mStreamNum(streamNum),
98mScanwordBitWidth(iBuilder->getSizeTy()->getBitWidth()) {
99
100}
101
102}
Note: See TracBrowser for help on using the repository browser.