Ignore:
Timestamp:
Apr 7, 2017, 4:59:04 PM (2 years ago)
Author:
nmedfort
Message:

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:
1 edited

Legend:

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

    r5391 r5398  
    1111using namespace llvm;
    1212
     13inline static unsigned floor_log2(const unsigned v) {
     14    assert ("log2(0) is undefined!" && v != 0);
     15    return 31 - __builtin_clz(v);
     16}
     17
    1318namespace kernel {
    1419
    15 Value * generateForwardZeroesMask(IDISA::IDISA_Builder * iBuilder, Value * bits) {
    16     Value * bits_minus1 = iBuilder->CreateSub(bits, ConstantInt::get(bits->getType(), 1));
    17     return iBuilder->CreateAnd(bits_minus1, iBuilder->CreateNot(bits));
    18 }
    19 
    20 Value * generatePopcount(IDISA::IDISA_Builder * iBuilder, Value * bits) {
    21     Value * ctpopFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::ctpop, bits->getType());
    22     return iBuilder->CreateCall(ctpopFunc, std::vector<Value *>({bits}));
    23 }
    24 
    25 Value * generateCountForwardZeroes(IDISA::IDISA_Builder * iBuilder, Value * bits) {
    26     Value * cttzFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::cttz, bits->getType());
    27     return iBuilder->CreateCall(cttzFunc, std::vector<Value *>({bits, ConstantInt::get(iBuilder->getInt1Ty(), 0)}));
    28 }
    29 
    30 Value * generateCountReverseZeroes(IDISA::IDISA_Builder * iBuilder, Value * bits) {
    31     Value * ctlzFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::ctlz, bits->getType());
    32     return iBuilder->CreateCall(ctlzFunc, std::vector<Value *>({bits, ConstantInt::get(iBuilder->getInt1Ty(), 0)}));
    33 }
    34 
    35 Value * generateResetLowestBit(IDISA::IDISA_Builder * iBuilder, Value * bits) {
    36     Value * bits_minus1 = iBuilder->CreateSub(bits, ConstantInt::get(bits->getType(), 1));
    37     return iBuilder->CreateAnd(bits_minus1, bits);
     20inline std::string getGrepTypeId(const GrepType grepType) {
     21    switch (grepType) {
     22        case GrepType::Normal:
     23            return "N";
     24        case GrepType::NameExpression:
     25            return "E";
     26        case GrepType::PropertyValue:
     27            return "P";
     28        default:
     29            llvm_unreachable("unknown grep type!");
     30    }
    3831}
    3932
     
    5649    VectorType * const scanwordVectorType =  VectorType::get(sizeTy, fieldCount);
    5750    Value * const blockNo = getScalarField("BlockNo");
    58     Value * const scanwordPos = iBuilder->CreateMul(blockNo, ConstantInt::get(blockNo->getType(), iBuilder->getBitBlockWidth()));
    59     Value * const lastRecordStart = getScalarField("LineStart");
     51    Value * const scanwordPos = iBuilder->CreateShl(blockNo, floor_log2(iBuilder->getBitBlockWidth()));
     52    Value * const lastRecordStart = getProcessedItemCount("InputStream");
    6053    Value * const lastRecordNum = getScalarField("LineNum");
    6154    Value * const inputStream = iBuilder->CreatePointerCast(getRawInputPointer("InputStream", iBuilder->getInt32(0), iBuilder->getInt32(0)), codeUnitTy);
    62 
    63     Value * const fileSize = iBuilder->CreateAdd(getProcessedItemCount("InputStream"), getScalarField("PendingBytes"));
    64 
    65     Constant * matchProcessor = nullptr;
    66     Value * fileIdx = nullptr;
    67     switch (mGrepType) {
    68         case GrepType::Normal:
    69             fileIdx = getScalarField("FileIdx");
    70             matchProcessor = m->getOrInsertFunction("wrapped_report_match" + std::to_string(mCodeUnitWidth), iBuilder->getVoidTy(), sizeTy, sizeTy, sizeTy, codeUnitTy, sizeTy, sizeTy, nullptr);
    71             break;
    72         case GrepType::NameExpression:
    73             matchProcessor = m->getOrInsertFunction("insert_codepoints", iBuilder->getVoidTy(), sizeTy, sizeTy, sizeTy, codeUnitTy, nullptr);
    74             break;
    75         case GrepType::PropertyValue:
    76             matchProcessor = m->getOrInsertFunction("insert_property_values", iBuilder->getVoidTy(), sizeTy, sizeTy, sizeTy, codeUnitTy, nullptr);
    77             break;
    78         default: llvm_unreachable("unknown grep type");
    79     }
    8055
    8156    Value * const matches = iBuilder->CreateBitCast(loadInputStreamBlock("matchResult", iBuilder->getInt32(0)), scanwordVectorType);
     
    125100            // The loop body is entered if we have more matches to process.
    126101            iBuilder->SetInsertPoint(processMatchesEntry);
    127             Value * prior_breaks = iBuilder->CreateAnd(generateForwardZeroesMask(iBuilder, phiMatchWord), phiRecordBreaks);
     102            Value * prior_breaks = iBuilder->CreateAnd(makeForwardZeroesMask(phiMatchWord), phiRecordBreaks);
    128103            // Within the loop we have a conditional block that is executed if there are any prior record breaks.
    129104            Value * prior_breaks_cond = iBuilder->CreateICmpNE(prior_breaks, ConstantInt::getNullValue(sizeTy));
     
    132107                // PRIOR_BREAKS_BLOCK
    133108                // If there are prior breaks, we count them and compute the record start position.
    134                 iBuilder->SetInsertPoint(prior_breaks_block);
    135                 Value * matchedRecordNum = iBuilder->CreateAdd(generatePopcount(iBuilder, prior_breaks), phiRecordNum);
    136                 Value * reverseDistance = generateCountReverseZeroes(iBuilder, prior_breaks);
     109                iBuilder->SetInsertPoint(prior_breaks_block);               
     110                Value * matchedRecordNum = iBuilder->CreateAdd(iBuilder->CreatePopcount(prior_breaks), phiRecordNum);
     111                Value * reverseDistance = iBuilder->CreateCountReverseZeroes(prior_breaks);
    137112                Value * width = ConstantInt::get(sizeTy, sizeTy->getBitWidth());
    138113                Value * priorRecordStart = iBuilder->CreateAdd(phiScanwordPos, iBuilder->CreateSub(width, reverseDistance));
     
    153128            phiRecordStart->addIncoming(matchRecordStart, loop_final_block);
    154129
    155             Value * matchRecordEnd = iBuilder->CreateAdd(phiScanwordPos, generateCountForwardZeroes(iBuilder, phiMatchWord));
     130            Value * matchRecordEnd = iBuilder->CreateAdd(phiScanwordPos, iBuilder->CreateCountForwardZeroes(phiMatchWord));
     131            Function * const matcher = m->getFunction("matcher");
     132            assert (matcher);
    156133            switch (mGrepType) {
    157134                case GrepType::Normal:
    158                     iBuilder->CreateCall(matchProcessor, {matchRecordNum, matchRecordStart, matchRecordEnd, inputStream, fileSize, fileIdx});
     135                    iBuilder->CreateCall(matcher, {matchRecordNum, matchRecordStart, matchRecordEnd, inputStream, getBufferedSize("InputStream"), getScalarField("FileIdx")});
    159136                    break;
    160137                case GrepType::NameExpression:
    161138                case GrepType::PropertyValue:
    162                     iBuilder->CreateCall(matchProcessor, {matchRecordNum, matchRecordStart, matchRecordEnd, inputStream});
     139                    iBuilder->CreateCall(matcher, {matchRecordNum, matchRecordStart, matchRecordEnd, inputStream});
    163140                    break;
    164141                default: break;
    165142            }
    166143
    167             Value * remaining_matches = generateResetLowestBit(iBuilder, phiMatchWord);
     144            Value * remaining_matches = resetLowestBit(phiMatchWord);
    168145            phiMatchWord->addIncoming(remaining_matches, loop_final_block);
    169146
     
    181158            // REMAINING_BREAKS_BLOCK: process remaining record breaks after all matches are processed
    182159            iBuilder->SetInsertPoint(remaining_breaks_block);
    183             Value * break_count = generatePopcount(iBuilder, phiRecordBreaks);
     160            Value * break_count = iBuilder->CreatePopcount(phiRecordBreaks);
    184161            Value * final_record_num = iBuilder->CreateAdd(phiRecordNum, break_count);
    185             Value * reverseZeroes = generateCountReverseZeroes(iBuilder, phiRecordBreaks);
     162            Value * reverseZeroes = iBuilder->CreateCountReverseZeroes(phiRecordBreaks);
    186163            Value * pendingLineStart = iBuilder->CreateAdd(phiScanwordPos, iBuilder->CreateSub(width, reverseZeroes));
    187164            iBuilder->CreateBr(return_block);
     
    208185    iBuilder->SetInsertPoint(scanWordExit);
    209186    setScalarField("BlockNo", iBuilder->CreateAdd(blockNo, ConstantInt::get(blockNo->getType(), 1)));
    210     setScalarField("LineStart", phiFinalRecordStart);
    211187    setScalarField("LineNum", phiFinalRecordNum);
    212 }
    213 
    214 void ScanMatchKernel::generateInitMethod() {
    215     setScalarField("PendingBytes", iBuilder->getSize(iBuilder->getBitBlockWidth() + 2));
    216 }
    217 
    218 void ScanMatchKernel::generateFinalBlockMethod(llvm::Value * remainingItems) {
    219     setScalarField("PendingBytes", remainingItems);
    220     CreateDoBlockMethodCall();
     188    setProcessedItemCount("InputStream", phiFinalRecordStart);
     189}
     190
     191inline Value * ScanMatchKernel::makeForwardZeroesMask(Value * const value) const {
     192    return iBuilder->CreateAnd(iBuilder->CreateSub(value, ConstantInt::get(value->getType(), 1)), iBuilder->CreateNot(value));
     193}
     194
     195inline Value * ScanMatchKernel::resetLowestBit(Value * const value) const {
     196    return iBuilder->CreateAnd(iBuilder->CreateSub(value, ConstantInt::get(value->getType(), 1)), value);
    221197}
    222198
    223199ScanMatchKernel::ScanMatchKernel(IDISA::IDISA_Builder * iBuilder, GrepType grepType, const unsigned codeUnitWidth)
    224 : BlockOrientedKernel(iBuilder, "Parabix:scanMatch" + std::to_string(codeUnitWidth),
    225     {Binding{iBuilder->getStreamSetTy(1, 8), "InputStream"}, Binding{iBuilder->getStreamSetTy(1, 1), "matchResult"}, Binding{iBuilder->getStreamSetTy(1, 1), "lineBreak"}},
     200: BlockOrientedKernel(iBuilder, "Parabix:scanMatch" + getGrepTypeId(grepType) + std::to_string(codeUnitWidth),
     201    {Binding{iBuilder->getStreamSetTy(1, 1), "matchResult"}, Binding{iBuilder->getStreamSetTy(1, 1), "lineBreak"}, Binding{iBuilder->getStreamSetTy(1, 8), "InputStream", UnknownRate()}},
    226202    {},
    227203    {Binding{iBuilder->getSizeTy(), "FileIdx"}},
    228204    {},
    229     {Binding{iBuilder->getSizeTy(), "BlockNo"}, Binding{iBuilder->getSizeTy(), "LineStart"}, Binding{iBuilder->getSizeTy(), "LineNum"}, Binding{iBuilder->getSizeTy(), "PendingBytes"}})
     205    {Binding{iBuilder->getSizeTy(), "BlockNo"}, Binding{iBuilder->getSizeTy(), "LineNum"}})
    230206, mGrepType(grepType)
    231207, mCodeUnitWidth(codeUnitWidth) {
    232 }
    233 
    234 }
     208
     209}
     210
     211}
Note: See TracChangeset for help on using the changeset viewer.