Ignore:
Timestamp:
Feb 23, 2016, 9:07:05 AM (3 years ago)
Author:
lindanl
Message:

new version using the kernels.

File:
1 edited

Legend:

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

    r4937 r4939  
    44 */
    55
    6 
     6#include "kernel.h"
    77#include "scanmatchgen.h"
    88#include <llvm/IR/Intrinsics.h>
     
    3232    return iBuilder->CreateAnd(bits_minus1, bits);
    3333}
    34 
    35 /*
    36        
    37 void write_matches_ending_in_segment(scanword_t matches, scanword_t lbreaks, ssize_t segmentPos, ssize_t & pendingLineStart, int & lineNum) {
    38     scanword_t remaining_matches = matches;
    39     scanword_t remaining_LBs = lbreaks;
    40     while (remaining_matches != 0) {
    41         // Find all the line marks prior to the match position
    42         scanword_t priorLineMarks = ForwardZeroesMask(remaining_matches) & remaining_LBs;
    43         if (priorLineMarks != 0) {
    44             int matchLineNum = lineNum + popcount(priorLineMarks);
    45             // The last of these line marks prior to the match position is the
    46             // starting positions of the matched line.
    47             matchLineStart = segmentPos + segmentBitWidth - CountReverseZeroes(priorLineMarks);
    48         }
    49         else {
    50             matchLineNum = lineNum;
    51             matchLineStart = pendingLineStart;
    52         }
    53         // The line end is marked by the match posiiton.
    54         matchLineEnd = segmentPos + CountForwardZeroes(remaining_matches);
    55         call write_match(matchLineNum, matchLineStart, matchLineEnd);
    56        
    57         remaining_matches = ResetLowestBit(remaining_matches);
    58         remaining_LBs = remaining_LBs ^ priorLineMarks;
    59         lineNum = matchLineNum;
    60     }
    61     if (remaining_LBs != 0) {
    62         lineNum += popcount(remaining_LBs);
    63         pendingLineStart = segmentPos + segmentBitWidth - CountReverseZeroes(remaining_LBs);
    64     }
    65 }   
    66         */
    67 
    68        
    69        
    70 void generateScanSegmentRoutine(Module * m, IDISA::IDISA_Builder * iBuilder, int segBitWidth) {
     34       
     35       
     36void generateScanWordRoutine(Module * m, IDISA::IDISA_Builder * iBuilder, int scanwordBitWidth, Type * kernelStuctType, bool isNameExpression) {
    7137    LLVMContext & ctxt = m->getContext();
    72     Type * T = iBuilder->getIntNTy(segBitWidth);
     38    Type * T = iBuilder->getIntNTy(scanwordBitWidth);   
     39    Type * S = PointerType::get(iBuilder->getIntNTy(8), 0);
    7340    Type * returnType = StructType::get(ctxt, std::vector<Type *>({T, T}));
    74     FunctionType * functionType = FunctionType::get(returnType, std::vector<Type *>({T, T, T, T, T}), false);
     41    FunctionType * functionType = FunctionType::get(returnType, std::vector<Type *>({kernelStuctType, T, T, T, T, T}), false);
    7542    Function * sFunction;
    7643       
     
    8451    AttributeSet AttrSet = AttributeSet::get(ctxt, Attrs);
    8552   
    86     sFunction = Function::Create(functionType, GlobalValue::ExternalLinkage, "scan_matches_in_segment", m);
     53    sFunction = Function::Create(functionType, GlobalValue::ExternalLinkage, "scan_matches_in_scanword", m);
    8754    sFunction->setCallingConv(CallingConv::C);
    8855    sFunction->setAttributes(AttrSet);
    8956       
    9057    Function::arg_iterator args = sFunction->arg_begin();
     58    Value * this_input_parm = args++;
     59    this_input_parm->setName("this");
    9160    Value * matches_input_parm = args++;
    9261    matches_input_parm->setName("matches");
    9362    Value * record_breaks_input_parm = args++;
    9463    record_breaks_input_parm->setName("breaks");
    95     Value * segmentPos = args++;
    96     segmentPos->setName("segmentPos");
     64    Value * scanwordPos = args++;
     65    scanwordPos->setName("scanwordPos");
    9766    Value * recordStart_input_parm = args++;
    9867    recordStart_input_parm->setName("pendingLineStart");
    9968    Value * recordNum_input_parm = args++;
    10069    recordNum_input_parm->setName("lineNum");
    101    
    102     Constant * matchProcessor = m->getOrInsertFunction("wrapped_report_match", Type::getVoidTy(ctxt), T, T, T, nullptr);
    103 
     70    Constant * matchProcessor;
     71    if(isNameExpression)
     72        matchProcessor = m->getOrInsertFunction("insert_codepoints", Type::getVoidTy(ctxt), T, T, T, S, nullptr);
     73    else
     74        matchProcessor = m->getOrInsertFunction("wrapped_report_match", Type::getVoidTy(ctxt), T, T, T, S, T, S, nullptr);
    10475   
    10576    iBuilder->SetInsertPoint(BasicBlock::Create(ctxt, "entry", sFunction,0));
     
    11687       
    11788    // The match scanner works with a loop involving four variables:
    118     // (a) the bit stream segment of matches marking the ends of selected records,
    119     // (b) the bit stream segment of record_breaks marking the ends of all records,
     89    // (a) the bit stream scanword of matches marking the ends of selected records,
     90    // (b) the bit stream scanword of record_breaks marking the ends of all records,
    12091    // (c) the integer lastRecordNum indicating the number of records processed so far,
    12192    // (d) the index lastRecordStart indicating the file position of the last record.
     
    154125    Value * matchRecordNum = iBuilder->CreateAdd(generatePopcount(iBuilder, prior_breaks), recordNum_phi);
    155126    Value * reverseDistance = generateCountReverseZeroes(iBuilder, prior_breaks);
    156     Value * width = ConstantInt::get(T, segBitWidth);
    157     Value * matchRecordStart = iBuilder->CreateAdd(segmentPos, iBuilder->CreateSub(width, reverseDistance));
     127    Value * width = ConstantInt::get(T, scanwordBitWidth);
     128    Value * matchRecordStart = iBuilder->CreateAdd(scanwordPos, iBuilder->CreateSub(width, reverseDistance));
    158129    iBuilder->CreateBr(loop_final_block);
    159130   
     
    168139    matchRecordStart_phi->addIncoming(recordStart_phi, process_matches_loop_entry);
    169140    matchRecordStart_phi->addIncoming(matchRecordStart, prior_breaks_block);
    170     Value * matchRecordEnd = iBuilder->CreateAdd(segmentPos, generateCountForwardZeroes(iBuilder, matches_phi));
    171     iBuilder->CreateCall(matchProcessor, std::vector<Value *>({matchRecordNum_phi, matchRecordStart_phi, matchRecordEnd}));
     141    Value * matchRecordEnd = iBuilder->CreateAdd(scanwordPos, generateCountForwardZeroes(iBuilder, matches_phi));
     142
     143    Value* filebuf_gep = iBuilder->CreateGEP(this_input_parm, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(7)});
     144    Value* filebufptr = iBuilder->CreateLoad(filebuf_gep, "filebuf");
     145
     146    Value* filesize_gep = iBuilder->CreateGEP(this_input_parm, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(8)});
     147    Value* filesize = iBuilder->CreateLoad(filesize_gep, "filensize");
     148
     149    Value* filename_gep = iBuilder->CreateGEP(this_input_parm, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(9)});
     150    Value* filenameptr = iBuilder->CreateLoad(filename_gep, "filename");
     151
     152    if(isNameExpression)
     153        iBuilder->CreateCall(matchProcessor, std::vector<Value *>({matchRecordNum_phi, matchRecordStart_phi, matchRecordEnd, filebufptr}));
     154    else
     155        iBuilder->CreateCall(matchProcessor, std::vector<Value *>({matchRecordNum_phi, matchRecordStart_phi, matchRecordEnd, filebufptr, filesize, filenameptr}));
    172156    Value * remaining_matches = generateResetLowestBit(iBuilder, matches_phi);
    173157    Value * remaining_breaks = iBuilder->CreateXor(record_breaks_phi, prior_breaks);
     
    190174    Value * final_record_num = iBuilder->CreateAdd(recordNum_phi, break_count);
    191175    Value * reverseZeroes = generateCountReverseZeroes(iBuilder, record_breaks_phi);
    192     Value * pendingLineStart = iBuilder->CreateAdd(segmentPos, iBuilder->CreateSub(width, reverseZeroes));
     176    Value * pendingLineStart = iBuilder->CreateAdd(scanwordPos, iBuilder->CreateSub(width, reverseZeroes));
    193177    iBuilder->CreateBr(return_block);
    194178   
     
    202186    finalRecordStart_phi->addIncoming(pendingLineStart, remaining_breaks_block);
    203187    Value * retVal = UndefValue::get(returnType);
    204     retVal = iBuilder->CreateInsertValue(retVal, finalRecordCount_phi, 0);
    205     retVal = iBuilder->CreateInsertValue(retVal, finalRecordStart_phi, 1);
     188    retVal = iBuilder->CreateInsertValue(retVal, finalRecordStart_phi, 0);
     189    retVal = iBuilder->CreateInsertValue(retVal, finalRecordCount_phi, 1);
    206190    iBuilder->CreateRet(retVal);
    207191   
     
    209193
    210194
    211 void generateScanBitBlockRoutine(Module * m, IDISA::IDISA_Builder * iBuilder, int segBitWidth) {
    212     LLVMContext & ctxt = m->getContext();
    213     Type * B = iBuilder->getBitBlockType();
    214     Type * T = iBuilder->getIntNTy(segBitWidth);
    215     generateScanSegmentRoutine(m, iBuilder, segBitWidth);
    216     int fieldCount = iBuilder->getBitBlockWidth()/segBitWidth;
    217     Type * segmentVectorType =  VectorType::get(T, fieldCount);
    218    
    219    
    220     Type * returnType = StructType::get(ctxt, std::vector<Type *>({T, T}));
    221     FunctionType * functionType = FunctionType::get(returnType, std::vector<Type *>({B, B, T, T, T}), false);
    222     Function * sFunction;
    223    
    224     SmallVector<AttributeSet, 6> Attrs;
    225     Attrs.push_back(AttributeSet::get(ctxt, ~0U, std::vector<Attribute::AttrKind>({ Attribute::NoUnwind, Attribute::UWTable })));
    226     Attrs.push_back(AttributeSet::get(ctxt, 1, std::vector<Attribute::AttrKind>({})));
    227     Attrs.push_back(AttributeSet::get(ctxt, 2, std::vector<Attribute::AttrKind>({})));
    228     Attrs.push_back(AttributeSet::get(ctxt, 3, std::vector<Attribute::AttrKind>({})));
    229     Attrs.push_back(AttributeSet::get(ctxt, 4, std::vector<Attribute::AttrKind>({})));
    230     Attrs.push_back(AttributeSet::get(ctxt, 5, std::vector<Attribute::AttrKind>({})));
    231     AttributeSet AttrSet = AttributeSet::get(ctxt, Attrs);
    232     sFunction = Function::Create(functionType, GlobalValue::ExternalLinkage, "scan_matches_in_bitblock", m);
    233     sFunction->setCallingConv(CallingConv::C);
    234     sFunction->setAttributes(AttrSet);
    235    
    236    
    237    
    238     Function::arg_iterator args = sFunction->arg_begin();
    239     Value * matches_input_parm = args++;
    240     matches_input_parm->setName("matches");
    241     Value * record_breaks_input_parm = args++;
    242     record_breaks_input_parm->setName("breaks");
    243     Value * blockPos = args++;
    244     blockPos->setName("blockPos");
    245     Value * recordStart_input_parm = args++;
    246     recordStart_input_parm->setName("pendingLineStart");
    247     Value * recordNum_input_parm = args++;
    248     recordNum_input_parm->setName("lineNum");
    249    
    250     iBuilder->SetInsertPoint(BasicBlock::Create(ctxt, "entry", sFunction,0));
    251    
    252     Value * matchSegVector = iBuilder->CreateBitCast(matches_input_parm, segmentVectorType);
    253     Value * breakSegVector = iBuilder->CreateBitCast(record_breaks_input_parm, segmentVectorType);
    254     Value * segmentPos = blockPos;
    255     Value * recordStart = recordStart_input_parm;
    256     Value * recordNum = recordNum_input_parm;
    257     Value * segResult = nullptr;
    258     Function * segScanFcn = m->getFunction("scan_matches_in_segment");
    259     for (uint64_t i = 0; i < iBuilder->getBitBlockWidth()/segBitWidth; i++) {
    260         Value * matchSeg = iBuilder->CreateExtractElement(matchSegVector, ConstantInt::get(T, i));
    261         Value * recordBreaksSeg = iBuilder->CreateExtractElement(breakSegVector, ConstantInt::get(T, i));
    262         segResult = iBuilder->CreateCall(segScanFcn, std::vector<Value *>({matchSeg, recordBreaksSeg, segmentPos, recordStart, recordNum}));
    263         segmentPos = iBuilder->CreateAdd(segmentPos, ConstantInt::get(T, segBitWidth));
    264         recordStart = iBuilder->CreateExtractValue(segResult, std::vector<unsigned>({0}));
    265         recordNum = iBuilder->CreateExtractValue(segResult, std::vector<unsigned>({1}));
     195void generateScanMatch(Module * m, IDISA::IDISA_Builder * iBuilder, int scanwordBitWidth, KernelBuilder * kBuilder, bool isNameExpression){
     196   
     197   
     198    Type * T = iBuilder->getIntNTy(scanwordBitWidth);
     199    Type * S = PointerType::get(iBuilder->getIntNTy(8), 0);
     200    int fieldCount = iBuilder->getBitBlockWidth()/scanwordBitWidth;
     201    Type * scanwordVectorType =  VectorType::get(T, fieldCount);
     202
     203    kBuilder->addKernelInputStream(1, "matches");
     204    kBuilder->addKernelInputStream(1, "breaks");
     205    //use index
     206    int blockPosIdx = kBuilder->extendKernelInternalStateType(T);
     207    int lineStartIdx = kBuilder->extendKernelInternalStateType(T);
     208    int lineNumIdx = kBuilder->extendKernelInternalStateType(T);
     209    kBuilder->extendKernelInternalStateType(S);
     210    kBuilder->extendKernelInternalStateType(T);
     211    kBuilder->extendKernelInternalStateType(S);
     212
     213    int segBlocks = kBuilder->getSegmentBlocks();
     214
     215    kBuilder->PrepareDoBlockFunction();
     216
     217    Type * kernelStuctType = PointerType::get(kBuilder->getKernelStructType(), 0);
     218    generateScanWordRoutine(m, iBuilder, scanwordBitWidth, kernelStuctType, isNameExpression);
     219
     220    struct Inputs inputs = kBuilder->openDoBlock();
     221    struct Outputs outputs;   
     222    Value * kernelStuctParam = kBuilder->getKernelStructParam();
     223   
     224    Value * scanwordPos = kBuilder->getKernelInternalState(blockPosIdx);
     225    Value * recordStart = kBuilder->getKernelInternalState(lineStartIdx);
     226    Value * recordNum = kBuilder->getKernelInternalState(lineNumIdx);
     227    Value * wordResult = nullptr;
     228
     229    Function * wordScanFcn = m->getFunction("scan_matches_in_scanword");
     230    for(int j=0; j<segBlocks; j++){
     231        Value * matchWordVector = iBuilder->CreateBitCast(inputs.streams[j][0], scanwordVectorType);
     232        Value * breakWordVector = iBuilder->CreateBitCast(inputs.streams[j][1], scanwordVectorType);
     233        for(int i=0; i<segBlocks*iBuilder->getBitBlockWidth()/scanwordBitWidth; i++){
     234
     235            Value * matchWord = iBuilder->CreateExtractElement(matchWordVector, ConstantInt::get(T, i));
     236            Value * recordBreaksWord = iBuilder->CreateExtractElement(breakWordVector, ConstantInt::get(T, i));
     237            wordResult = iBuilder->CreateCall(wordScanFcn, std::vector<Value *>({kernelStuctParam, matchWord, recordBreaksWord, scanwordPos, recordStart, recordNum}));
     238            scanwordPos = iBuilder->CreateAdd(scanwordPos, ConstantInt::get(T, scanwordBitWidth));
     239            recordStart = iBuilder->CreateExtractValue(wordResult, std::vector<unsigned>({0}));
     240            recordNum = iBuilder->CreateExtractValue(wordResult, std::vector<unsigned>({1}));
     241        }
    266242    }
    267     iBuilder->CreateRet(segResult);
    268 }
    269 
    270 
    271 
    272 
     243
     244    kBuilder->changeKernelInternalState(blockPosIdx, scanwordPos);
     245    kBuilder->changeKernelInternalState(lineStartIdx, recordStart);
     246    kBuilder->changeKernelInternalState(lineNumIdx, recordNum);
     247
     248    kBuilder->closeDoBlock(outputs);
     249
     250    kBuilder->finalizeMethods();
     251}
     252
     253
     254
Note: See TracChangeset for help on using the changeset viewer.