Ignore:
Timestamp:
Jun 21, 2017, 7:42:55 PM (2 years ago)
Author:
cameron
Message:

Scanmatch kernel using multiblock kernel builder - revised from Dong

Location:
icGREP/icgrep-devel/icgrep/kernels
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/kernels/scanmatchgen.cpp

    r5440 r5526  
    3131}
    3232
    33 void ScanMatchKernel::generateDoBlockMethod(const std::unique_ptr<KernelBuilder> &iBuilder) {
     33void ScanMatchKernel::generateMultiBlockLogic(const std::unique_ptr<KernelBuilder> &iBuilder) {
    3434
    3535    Module * const m = iBuilder->getModule();
    3636    BasicBlock * const entryBlock = iBuilder->GetInsertBlock();
     37    BasicBlock * const initialBlock = iBuilder->CreateBasicBlock("initialBlock");
    3738    BasicBlock * const scanWordIteration = iBuilder->CreateBasicBlock("ScanWordIteration");
    3839    BasicBlock * const matches_test_block = iBuilder->CreateBasicBlock("matches_test_block");
     
    4445    BasicBlock * const return_block = iBuilder->CreateBasicBlock("return_block");
    4546    BasicBlock * const scanWordExit = iBuilder->CreateBasicBlock("ScanWordExit");
     47    BasicBlock * const blocksExit = iBuilder->CreateBasicBlock("blocksExit");
    4648    IntegerType * const sizeTy = iBuilder->getSizeTy();
    4749    const unsigned fieldCount = iBuilder->getBitBlockWidth() / sizeTy->getBitWidth();
    4850    VectorType * const scanwordVectorType =  VectorType::get(sizeTy, fieldCount);
     51    Constant * blockSize = iBuilder->getSize(iBuilder->getBitBlockWidth());
     52    Constant * blockSizeLess1 = iBuilder->getSize(iBuilder->getBitBlockWidth() - 1);
     53
     54    Function::arg_iterator args = mCurrentMethod->arg_begin();
     55    /* self = */ args++;
     56    Value * itemsToDo = &*(args++);
     57    Value * inputStreamAvail = &*(args++);
     58    Value * match_result = &*(args++);
     59    Value * line_break = &*(args++);
     60    Value * input_stream = &*(args);
     61
     62    Value * blocksToDo = iBuilder->CreateUDiv(iBuilder->CreateAdd(itemsToDo, blockSizeLess1), blockSize);
     63   
     64    Value * match_result_ptr = iBuilder->CreateBitCast(match_result, scanwordVectorType->getPointerTo());
     65    Value * line_break_ptr = iBuilder->CreateBitCast(line_break, scanwordVectorType->getPointerTo());
     66
     67    iBuilder->CreateCondBr(iBuilder->CreateICmpUGT(blocksToDo, iBuilder->getSize(0)), initialBlock, blocksExit);
     68
     69    iBuilder->SetInsertPoint(initialBlock);
     70    PHINode * const blockBase = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
     71    blockBase->addIncoming(iBuilder->getSize(0), entryBlock);
    4972    Value * const blockNo = iBuilder->getScalarField("BlockNo");
    5073    Value * const scanwordPos = iBuilder->CreateShl(blockNo, floor_log2(iBuilder->getBitBlockWidth()));
    5174    Value * const lastRecordStart = iBuilder->getProcessedItemCount("InputStream");
    5275    Value * const lastRecordNum = iBuilder->getScalarField("LineNum");
    53 
    54     Value * const matches = iBuilder->CreateBitCast(iBuilder->loadInputStreamBlock("matchResult", iBuilder->getInt32(0)), scanwordVectorType);
    55     Value * const linebreaks = iBuilder->CreateBitCast(iBuilder->loadInputStreamBlock("lineBreak", iBuilder->getInt32(0)), scanwordVectorType);
    56 
    5776    iBuilder->CreateBr(scanWordIteration);
    5877
     
    6180        // while (phiIndex < words per stride)
    6281        PHINode * const phiIndex = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2, "index");
    63         phiIndex->addIncoming(iBuilder->getInt32(0), entryBlock);
     82        phiIndex->addIncoming(iBuilder->getInt32(0), initialBlock);
    6483        PHINode * const phiScanwordPos = iBuilder->CreatePHI(scanwordPos->getType(), 2, "pos");
    65         phiScanwordPos->addIncoming(scanwordPos, entryBlock);
     84        phiScanwordPos->addIncoming(scanwordPos, initialBlock);
    6685        PHINode * const phiLineStart = iBuilder->CreatePHI(lastRecordStart->getType(), 2, "recordstart");
    67         phiLineStart->addIncoming(lastRecordStart, entryBlock);
     86        phiLineStart->addIncoming(lastRecordStart, initialBlock);
    6887        PHINode * const phiLineNum = iBuilder->CreatePHI(lastRecordNum->getType(), 2, "recordnum");
    69         phiLineNum->addIncoming(lastRecordNum, entryBlock);
     88        phiLineNum->addIncoming(lastRecordNum, initialBlock);
     89        Value * const matches = iBuilder->CreateLoad(iBuilder->CreateGEP(match_result_ptr, blockBase));
     90        Value * const linebreaks = iBuilder->CreateLoad(iBuilder->CreateGEP(line_break_ptr, blockBase));
    7091        Value * const matchWord = iBuilder->CreateExtractElement(matches, phiIndex);
    7192        Value * const recordBreaks = iBuilder->CreateExtractElement(linebreaks, phiIndex);
     
    128149
    129150            Function * const matcher = m->getFunction("matcher"); assert (matcher);
    130             auto args = matcher->arg_begin();
    131             Value * const mrn = iBuilder->CreateZExtOrTrunc(matchRecordNum, args->getType());
    132             Value * const mrs = iBuilder->CreateZExtOrTrunc(matchRecordStart, (++args)->getType());
    133             Value * const mre = iBuilder->CreateZExtOrTrunc(matchRecordEnd, (++args)->getType());
     151            auto args_matcher = matcher->arg_begin();
     152            Value * const mrn = iBuilder->CreateZExtOrTrunc(matchRecordNum, args_matcher->getType());
     153            Value * const mrs = iBuilder->CreateZExtOrTrunc(matchRecordStart, (++args_matcher)->getType());
     154            Value * const mre = iBuilder->CreateZExtOrTrunc(matchRecordEnd, (++args_matcher)->getType());
    134155            Value * const inputStream = iBuilder->getRawInputPointer("InputStream", iBuilder->getInt32(0), iBuilder->getInt32(0));
    135             Value * const is = iBuilder->CreatePointerCast(inputStream, (++args)->getType());
     156            Value * const is = iBuilder->CreatePointerCast(inputStream, (++args_matcher)->getType());
    136157            if (mGrepType == GrepType::Normal) {
    137                 Value * const sz = iBuilder->CreateZExtOrTrunc(iBuilder->getBufferedSize("InputStream"), (++args)->getType());
    138                 Value * const fi = iBuilder->CreateZExtOrTrunc(iBuilder->getScalarField("FileIdx"), (++args)->getType());
     158                Value * const sz = iBuilder->CreateZExtOrTrunc(iBuilder->getBufferedSize("InputStream"), (++args_matcher)->getType());
     159                Value * const fi = iBuilder->CreateZExtOrTrunc(iBuilder->getScalarField("FileIdx"), (++args_matcher)->getType());
    139160                iBuilder->CreateCall(matcher, {mrn, mrs, mre, is, sz, fi});
    140161            } else {
     
    187208    iBuilder->setScalarField("LineNum", phiFinalRecordNum);
    188209    iBuilder->setProcessedItemCount("InputStream", phiFinalRecordStart);
     210    Value * blockBaseNext = iBuilder->CreateAdd(blockBase, ConstantInt::get(iBuilder->getSizeTy(), 1));
     211    blockBase->addIncoming(blockBaseNext, scanWordExit);
     212    iBuilder->CreateLikelyCondBr(iBuilder->CreateICmpNE(blockBaseNext, blocksToDo), initialBlock, blocksExit);
     213
     214    iBuilder->SetInsertPoint(blocksExit);
    189215}
    190216
    191217ScanMatchKernel::ScanMatchKernel(const std::unique_ptr<kernel::KernelBuilder> & b, GrepType grepType, const unsigned codeUnitWidth)
    192 : BlockOrientedKernel("scanMatch" + getGrepTypeId(grepType) + std::to_string(codeUnitWidth),
     218: MultiBlockKernel("scanMatch" + getGrepTypeId(grepType) + std::to_string(codeUnitWidth),
    193219    {Binding{b->getStreamSetTy(1, 1), "matchResult"}, Binding{b->getStreamSetTy(1, 1), "lineBreak"}, Binding{b->getStreamSetTy(1, 8), "InputStream", UnknownRate()}},
    194220    {},
  • icGREP/icgrep-devel/icgrep/kernels/scanmatchgen.h

    r5464 r5526  
    1414namespace kernel {
    1515   
    16 class ScanMatchKernel final : public BlockOrientedKernel {
     16class ScanMatchKernel : public MultiBlockKernel {
    1717public:
    18     ScanMatchKernel(const std::unique_ptr<kernel::KernelBuilder> & b, const GrepType grepType, const unsigned codeUnitWidth);
     18        ScanMatchKernel(const std::unique_ptr<kernel::KernelBuilder> & b, const GrepType grepType, const unsigned codeUnitWidth);
    1919    bool isCachable() const override { return true; }
    2020    bool hasSignature() const override { return false; }
    21 protected:
    22     void generateDoBlockMethod(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
    2321private:
    24     const GrepType      mGrepType;
     22        void generateMultiBlockLogic(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
     23        const GrepType      mGrepType;
    2524};
     25
    2626}
    27 
    2827#endif // SCANMATCHGEN_H
Note: See TracChangeset for help on using the changeset viewer.