Ignore:
Timestamp:
Jun 13, 2016, 11:12:08 AM (3 years ago)
Author:
cameron
Message:

s2p kernel with new infrastructure, includes s2p_FinalBlock

File:
1 edited

Legend:

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

    r5048 r5051  
    66
    77#include <toolchain.h>
     8#include "streamset.h"
     9#include "interface.h"
     10
    811#include "pipeline.h"
    912#include "utf_encoding.h"
     
    1821
    1922#include <llvm/IR/Intrinsics.h>
     23#include "llvm/Support/SourceMgr.h"
     24#include "llvm/IRReader/IRReader.h"
     25#include "llvm/Linker/Linker.h"
    2026
    2127using namespace pablo;
     
    3137
    3238PipelineBuilder::~PipelineBuilder() {
    33     delete mS2PKernel;
    34     delete mICgrepKernel;
    35     delete mScanMatchKernel;
    3639}
    3740
    3841void PipelineBuilder::CreateKernels(PabloFunction * function, bool UTF_16, bool isNameExpression){
    39     mS2PKernel = new KernelBuilder(iBuilder, "s2p", codegen::SegmentSize);
     42    //mS2PKernel = new KernelBuilder(iBuilder, "s2p", codegen::SegmentSize);
    4043    mICgrepKernel = new KernelBuilder(iBuilder, "icgrep", codegen::SegmentSize);
    4144    mScanMatchKernel = new KernelBuilder(iBuilder, "scanMatch", codegen::SegmentSize);
    42     if (UTF_16) {
    43         generateS2P_16Kernel(mMod, iBuilder, mS2PKernel);
    44     }
    45     else {
    46         generateS2PKernel(mMod, iBuilder, mS2PKernel);
    47     }
     45//#define LOAD_S2P_LL
     46#ifdef LOAD_S2P_LL
     47    SMDiagnostic Err;
     48    std::unique_ptr<Module> s2p_Module = parseIRFile("s2p.ll", Err, mMod->getContext());
     49    if (!s2p_Module) {
     50         Err.print("icgrep", errs());
     51         exit(1);
     52    }
     53    Linker L(*mMod);
     54    //s2p_Module->dump();
    4855    generateScanMatch(mMod, iBuilder, 64, mScanMatchKernel, isNameExpression);
     56    L.linkInModule(std::move(s2p_Module));
     57    errs() << "s2p.ll loaded\n";
     58#else
     59    errs() << "A\n";
     60    //mS2PKernel = new KernelBuilder(iBuilder, "s2p", codegen::SegmentSize);
     61    s2pKernel  s2pk(iBuilder);
     62    //s2pk.addKernelDeclarations(mMod);
     63    //mMod->dump();
     64    std::unique_ptr<Module> s2pM = s2pk.createKernelModule();
     65    Linker L(*mMod);
     66    L.linkInModule(std::move(s2pM));
     67    errs() << "b\n";
     68   
     69   
     70    generateScanMatch(mMod, iBuilder, 64, mScanMatchKernel, isNameExpression);
     71#endif
     72   
    4973    pablo_function_passes(function);
    5074    PabloCompiler pablo_compiler(mMod, iBuilder);
     
    6791}
    6892
    69 inline Value * Cal_Count(Instance * icGrepInstance, IDISA::IDISA_Builder * iBuilder) {
    70     Value * match = icGrepInstance->getOutputStream(0, 0);
    71     Value * matches = iBuilder->CreateLoad(match, false, "match");
     93inline Value * Cal_Count(Value * match_ptr, IDISA::IDISA_Builder * iBuilder) {
     94    Value * matches = iBuilder->CreateLoad(match_ptr, false, "match");
    7295    return generatePopcount(iBuilder, matches);
    7396}
    7497
    7598Function * PipelineBuilder::ExecuteKernels(bool CountOnly, bool UTF_16) {
     99
    76100    Type * const int64ty = iBuilder->getInt64Ty();
    77101    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
    78     Type * const inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, (UTF_16 ? 16 : 8))})), 1), 0);
     102    Type * const inputType = PointerType::get(ArrayType::get(ArrayType::get(mBitBlockType, (UTF_16 ? 16 : 8)), 1), 0);
    79103    Type * const resultTy = CountOnly ? int64ty : iBuilder->getVoidTy();
    80104    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", resultTy, inputType, int64ty, int64ty, nullptr));
     
    88112    Value * const fileIdx = &*(args++);
    89113    fileIdx->setName("fileIdx");
     114    errs() << "B\n";
    90115
    91116    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
    92 
    93 
     117    iBuilder->CallPrintInt("bufferSize", bufferSize);
    94118    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
    95119    BasicBlock * segmentCondBlock = nullptr;
     
    103127    BasicBlock * fullBodyBlock = BasicBlock::Create(mMod->getContext(), "fullBody", main, 0);
    104128    BasicBlock * finalBlock = BasicBlock::Create(mMod->getContext(), "final", main, 0);
    105     BasicBlock * finalPartialBlock = BasicBlock::Create(mMod->getContext(), "partial", main, 0);
    106     BasicBlock * finalEmptyBlock = BasicBlock::Create(mMod->getContext(), "empty", main, 0);
    107     BasicBlock * exitBlock = BasicBlock::Create(mMod->getContext(), "exit", main, 0);
    108129
    109130    Value * count = nullptr;
     
    112133        iBuilder->CreateStore(ConstantInt::getNullValue(mBitBlockType), count);
    113134    }
    114 
    115     Instance * s2pInstance = mS2PKernel->instantiate(inputStream);
    116     Instance * icGrepInstance = mICgrepKernel->instantiate(s2pInstance->getOutputStreamBuffer());
     135    errs() << "C\n";
     136
     137    Value * s2pI = make_New(iBuilder,  "s2p", {});
     138    errs() << "D\n";
     139
     140    StreamSetBuffer ByteStream(iBuilder, StreamSetType(1, (UTF_16 ? 16 : 8)), 0);
     141    ByteStream.setStreamSetBuffer(inputStream);
     142   
     143    StreamSetBuffer BasisBits(iBuilder, StreamSetType((UTF_16 ? 16 : 8), 1), codegen::SegmentSize);
     144    Value * basis_bits_ptr = BasisBits.allocateBuffer();
     145   
     146    Value * grepI = make_New(iBuilder,  "icgrep", {});
     147   
     148    StreamSetBuffer MatchResults(iBuilder, StreamSetType(2, 1), codegen::SegmentSize);
     149    Value * match_results_ptr = MatchResults.allocateBuffer();
     150
    117151    Instance * scanMatchInstance = nullptr;
    118152   
    119153    if (!CountOnly) {
    120         scanMatchInstance = mScanMatchKernel->instantiate(icGrepInstance->getOutputStreamBuffer());
     154        scanMatchInstance = mScanMatchKernel->instantiate(match_results_ptr);
    121155        scanMatchInstance->setInternalState("FileBuf", iBuilder->CreateBitCast(inputStream, int8PtrTy));
    122156        scanMatchInstance->setInternalState("FileSize", bufferSize);
     
    124158    }
    125159    Value * initialBufferSize = nullptr;
     160    Value * initialBlockNo = nullptr;
    126161    BasicBlock * initialBlock = nullptr;
    127162
     
    131166        PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
    132167        remainingBytes->addIncoming(bufferSize, entryBlock);
     168        PHINode * blockNo = iBuilder->CreatePHI(int64ty, 2, "blockNo");
     169        blockNo->addIncoming(iBuilder->getInt64(0), entryBlock);
    133170        Constant * const step = ConstantInt::get(int64ty, mBlockSize * segmentSize * (UTF_16 ? 2 : 1));
    134171        Value * segmentCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
     
    136173        iBuilder->SetInsertPoint(segmentBodyBlock);
    137174        for (unsigned i = 0; i < segmentSize; ++i) {
    138             s2pInstance->CreateDoBlockCall();
     175            Value * blkNo = iBuilder->CreateAdd(blockNo, iBuilder->getInt64(i));
     176            make_DoBlock_Call(iBuilder, "s2p", {s2pI, ByteStream.getBlockPointer(blkNo), BasisBits.getBlockPointer(blkNo)});
    139177        }
    140178        for (unsigned i = 0; i < segmentSize; ++i) {
    141             Value * match = (icGrepInstance->getOutputStream(0, 0));
    142             icGrepInstance->CreateDoBlockCall();
    143             Value * temp = iBuilder->CreateLoad(match);
    144             Value * matches = iBuilder->CreateBitCast(temp, iBuilder->getIntNTy(mBlockSize));
    145             Value * popcount_for = generatePopcount(iBuilder, matches);
    146             if(CountOnly){
    147                 Value * temp_count = iBuilder->CreateLoad(count);
    148                 Value * prev_count = iBuilder->CreateBitCast(temp_count, iBuilder->getIntNTy(mBlockSize));
    149                 Value * add_for = iBuilder->CreateAdd(prev_count, popcount_for);
    150                 Value * add = iBuilder->CreateBitCast(add_for, mBitBlockType);
    151                 iBuilder->CreateStore(add, count);
    152             }
     179            Value * blkNo = iBuilder->CreateAdd(blockNo, iBuilder->getInt64(i));
     180            match_results_ptr = MatchResults.getBlockPointer(blkNo);
     181            make_DoBlock_Call(iBuilder, "icgrep", {grepI, BasisBits.getBlockPointer(blkNo), match_results_ptr});
     182            if (CountOnly) {
     183                Value * matchptr = iBuilder->CreateGEP(mBitBlockType, match_results_ptr, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(0)});
     184                Value * temp = iBuilder->CreateLoad(matchptr);
     185                Value * matches = iBuilder->CreateBitCast(temp, iBuilder->getIntNTy(mBlockSize));
     186                Value * popcount_for = generatePopcount(iBuilder, matches);
     187                Value * temp_count = iBuilder->CreateLoad(count);
     188                Value * prev_count = iBuilder->CreateBitCast(temp_count, iBuilder->getIntNTy(mBlockSize));
     189                Value * add_for = iBuilder->CreateAdd(prev_count, popcount_for);
     190                Value * add = iBuilder->CreateBitCast(add_for, mBitBlockType);
     191                iBuilder->CreateStore(add, count);
     192            }
    153193        }
    154194        if (!CountOnly) {
    155195            for (unsigned i = 0; i < segmentSize; ++i) {
    156                 scanMatchInstance->CreateDoBlockCall();
     196                Value * blkNo = iBuilder->CreateAdd(blockNo, iBuilder->getInt64(i));
     197                match_results_ptr = MatchResults.getBlockPointer(blkNo);
     198                make_DoBlock_Call(iBuilder, "scanMatch", {scanMatchInstance->getKernelState(), match_results_ptr});
    157199            }
    158200        }
    159201        remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), segmentBodyBlock);
     202        blockNo->addIncoming(iBuilder->CreateAdd(blockNo, iBuilder->getInt64(segmentSize)), segmentBodyBlock);
    160203        iBuilder->CreateBr(segmentCondBlock);
    161204        initialBufferSize = remainingBytes;
    162205        initialBlock = segmentCondBlock;
     206        initialBlockNo = blockNo;
    163207    } else {
    164208        initialBufferSize = bufferSize;
    165209        initialBlock = entryBlock;
     210        initialBlockNo = iBuilder->getInt64(0);
    166211        iBuilder->CreateBr(fullCondBlock);
    167212    }
     
    170215    PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
    171216    remainingBytes->addIncoming(initialBufferSize, initialBlock);
    172 
     217    PHINode * blockNo = iBuilder->CreatePHI(int64ty, 2, "blockNo");
     218    blockNo->addIncoming(initialBlockNo, initialBlock);
     219   
    173220    Constant * const step = ConstantInt::get(int64ty, mBlockSize * (UTF_16 ? 2 : 1));
    174221    Value * fullCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
     
    176223
    177224    iBuilder->SetInsertPoint(fullBodyBlock);
    178     s2pInstance->CreateDoBlockCall();
    179     icGrepInstance->CreateDoBlockCall();
     225    errs() << "E\n";
     226
     227    make_DoBlock_Call(iBuilder, "s2p", {s2pI, ByteStream.getBlockPointer(blockNo), BasisBits.getBlockPointer(blockNo)});
     228    errs() << "F\n";
     229    make_DoBlock_Call(iBuilder, "icgrep", {grepI, BasisBits.getBlockPointer(blockNo), MatchResults.getBlockPointer(blockNo)});
    180230    if (CountOnly) {
    181         Value * popcount = Cal_Count(icGrepInstance, iBuilder);
     231        Value * matchptr = iBuilder->CreateGEP(mBitBlockType, match_results_ptr, {iBuilder->getInt64(0), iBuilder->getInt32(0)});
     232        Value * popcount = Cal_Count(matchptr, iBuilder);
    182233        Value * temp_count = iBuilder->CreateLoad(count);
    183234        Value * add = iBuilder->CreateAdd(temp_count, popcount);
    184235        iBuilder->CreateStore(add, count);
    185236    } else {
    186         scanMatchInstance->CreateDoBlockCall();
     237        make_DoBlock_Call(iBuilder, "scanMatch", {scanMatchInstance->getKernelState(), MatchResults.getBlockPointer(blockNo)});
    187238    }
    188239
    189240    remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), fullBodyBlock);
     241    blockNo->addIncoming(iBuilder->CreateAdd(blockNo, iBuilder->getInt64(1)), fullBodyBlock);
    190242    iBuilder->CreateBr(fullCondBlock);
    191243
    192244    iBuilder->SetInsertPoint(finalBlock);
    193     Value * emptyBlockCond = iBuilder->CreateICmpEQ(remainingBytes, ConstantInt::get(int64ty, 0));
    194     iBuilder->CreateCondBr(emptyBlockCond, finalEmptyBlock, finalPartialBlock);
    195 
    196 
    197     iBuilder->SetInsertPoint(finalPartialBlock);
    198     s2pInstance->CreateDoBlockCall();
    199     iBuilder->CreateBr(exitBlock);
    200 
    201     iBuilder->SetInsertPoint(finalEmptyBlock);
    202     s2pInstance->clearOutputStreamSet();
    203     iBuilder->CreateBr(exitBlock);
    204 
    205     iBuilder->SetInsertPoint(exitBlock);
     245    basis_bits_ptr = BasisBits.getBlockPointer(blockNo);
     246
     247    make_FinalBlock_Call(iBuilder, "s2p", {s2pI, remainingBytes, ByteStream.getBlockPointer(blockNo), basis_bits_ptr});
     248   
     249    errs() << "G\n";
    206250
    207251    Value * remainingByte = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(mBlockSize));
    208252    Value * remainingUnit = iBuilder->CreateLShr(remainingByte, ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1));
    209253    Value * EOFmark = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), UTF_16 ? remainingUnit : remainingByte);
    210     icGrepInstance->setInternalState("EOFmark", iBuilder->CreateBitCast(EOFmark, mBitBlockType));
    211 
    212     icGrepInstance->CreateDoBlockCall();
     254    //icGrepInstance->setInternalState("EOFmark", iBuilder->CreateBitCast(EOFmark, mBitBlockType));
     255    match_results_ptr = MatchResults.getBlockPointer(blockNo);
     256    make_DoBlock_Call(iBuilder, "icgrep", {grepI, basis_bits_ptr, match_results_ptr});
     257    errs() << "H\n";
    213258    if (CountOnly) {
    214         Value * popcount1 = Cal_Count(icGrepInstance, iBuilder);
     259        Value * matchptr = iBuilder->CreateGEP(mBitBlockType, match_results_ptr, {iBuilder->getInt64(0), iBuilder->getInt32(0)});
     260        Value * popcount1 = Cal_Count(matchptr, iBuilder);
    215261        Value * temp_count1 = iBuilder->CreateLoad(count);
    216262        Value * result = iBuilder->CreateAdd(temp_count1, popcount1);
     
    230276        iBuilder->CreateRet(iBuilder->CreateExtractElement(result, iBuilder->getInt32(0)));
    231277    } else {
    232         scanMatchInstance->CreateDoBlockCall();
     278        make_DoBlock_Call(iBuilder, "scanMatch", {scanMatchInstance->getKernelState(), match_results_ptr});
    233279        iBuilder->CreateRetVoid();
    234280    }
Note: See TracChangeset for help on using the changeset viewer.