Changeset 4970


Ignore:
Timestamp:
Mar 11, 2016, 4:44:53 PM (4 years ago)
Author:
nmedfort
Message:

Added ability to name internal state types; removed unnecessary predefined states. Some progress towards supporting segment size > 1

Location:
icGREP/icgrep-devel/icgrep
Files:
10 edited

Legend:

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

    r4968 r4970  
    1212using namespace pablo;
    1313
    14 static cl::opt<unsigned> SegmentSize("segment-size", cl::desc("Segment Size"), cl::value_desc("LLVM IR file"), cl::init(1));
     14static cl::opt<unsigned> SegmentSize("segment-size", cl::desc("Segment Size"), cl::value_desc("positive integer"), cl::init(1));
    1515
    1616inline bool isPowerOfTwo(const unsigned x) {
     
    2828, mCircularBufferModulo(1)
    2929, mSegmentIndex(0)
    30 , mStartIndex(0) {
     30, mBlockIndex(0) {
    3131    assert (mBlocksPerSegment > 0);
    32     addInternalStateType(b->getInt64Ty());
    33     addInternalStateType(b->getInt64Ty());
    34     addInternalStateType(b->getInt64Ty());
    35     addInternalStateType(b->getInt64Ty());
    36 }
    37 
    38 /** ------------------------------------------------------------------------------------------------------------- *
    39  * @brief addInternalStateType
    40  ** ------------------------------------------------------------------------------------------------------------- */
    41 unsigned KernelBuilder::addInternalStateType(Type * const type) {
     32    mBlockIndex = addInternalState(b->getInt64Ty(), "BlockNo");
     33}
     34
     35/** ------------------------------------------------------------------------------------------------------------- *
     36 * @brief addInternalState
     37 ** ------------------------------------------------------------------------------------------------------------- */
     38unsigned KernelBuilder::addInternalState(Type * const type) {
    4239    assert (type);
    4340    const unsigned index = mStates.size();
     
    4643}
    4744
     45unsigned KernelBuilder::addInternalState(llvm::Type * const type, std::string name) {
     46    if (LLVM_UNLIKELY(mStateNameMap.count(name) != 0)) {
     47        throw std::runtime_error("Kernel already contains internal state " + name);
     48    }
     49    const unsigned index = addInternalState(type);
     50    mStateNameMap.emplace(name, index);
     51    return index;
     52}
     53
    4854/** ------------------------------------------------------------------------------------------------------------- *
    4955 * @brief addOutputStream
     
    5561
    5662/** ------------------------------------------------------------------------------------------------------------- *
    57  * @brief addOutputAccum
    58  ** ------------------------------------------------------------------------------------------------------------- */
    59 void KernelBuilder::addOutputAccum(Type * const type) {
     63 * @brief addOutputScalar
     64 ** ------------------------------------------------------------------------------------------------------------- */
     65void KernelBuilder::addOutputScalar(Type * const type) {
    6066    assert (type);
    61     mOutputAccums.push_back(type);
     67    mOutputScalar.push_back(type);
    6268}
    6369
     
    6672 ** ------------------------------------------------------------------------------------------------------------- */
    6773void KernelBuilder::addInputStream(const unsigned fields, std::string name) {
    68     assert (fields > 0);
    69     if (name.empty())
    70         mInputStreamNames.push_back(mKernelName + "_inputstream_" + std::to_string(mInputStreams.size()));
    71     else
    72         mInputStreamNames.push_back(name);
    73 
     74    assert (fields > 0 && !name.empty());
     75    mInputStreamNames.push_back(name);
    7476    if (fields == 1){
    7577        mInputStreams.push_back(mBitBlockType);
     
    7981}
    8082
     83void KernelBuilder::addInputStream(const unsigned fields) {
     84    addInputStream(fields, std::move(mKernelName + "_inputstream_" + std::to_string(mInputStreams.size())));
     85}
     86
     87
     88/** ------------------------------------------------------------------------------------------------------------- *
     89 * @brief getInputStream
     90 ** ------------------------------------------------------------------------------------------------------------- */
     91Value * KernelBuilder::getInputStream(const unsigned index, const unsigned streamOffset) {
     92    Value * const indices[] = {getOffset(streamOffset), iBuilder->getInt32(index)};
     93    return iBuilder->CreateGEP(mInputParam, indices);
     94}
     95
     96/** ------------------------------------------------------------------------------------------------------------- *
     97 * @brief getInputScalar
     98 ** ------------------------------------------------------------------------------------------------------------- */
     99Value * KernelBuilder::getInputScalar(const unsigned) {
     100    throw std::runtime_error("currently not supported!");
     101}
     102
     103/** ------------------------------------------------------------------------------------------------------------- *
     104 * @brief getOutputStream
     105 ** ------------------------------------------------------------------------------------------------------------- */
     106Value * KernelBuilder::getOutputStream(const unsigned index, const unsigned streamOffset) {
     107    Value * const indices[] = {iBuilder->getInt32(0), iBuilder->getInt32(1), getOffset(streamOffset), iBuilder->getInt32(index)};
     108    return iBuilder->CreateGEP(mKernelParam, indices);
     109}
     110
     111/** ------------------------------------------------------------------------------------------------------------- *
     112 * @brief getOutputScalar
     113 ** ------------------------------------------------------------------------------------------------------------- */
     114Value * KernelBuilder::getOutputScalar(const unsigned) {
     115    throw std::runtime_error("currently not supported!");
     116}
     117
     118/** ------------------------------------------------------------------------------------------------------------- *
     119 * @brief getInternalState
     120 ** ------------------------------------------------------------------------------------------------------------- */
     121Value * KernelBuilder::getInternalState(const unsigned index, Value * const inputStruct) {
     122    Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(index)};
     123    return iBuilder->CreateGEP(inputStruct ? inputStruct : mKernelParam, indices);
     124}
     125
     126Value * KernelBuilder::getInternalState(const std::string & name, Value * const inputStruct) {
     127    const auto f = mStateNameMap.find(name);
     128    if (LLVM_UNLIKELY(f == mStateNameMap.end())) {
     129        throw std::runtime_error("Kernel does not contain internal state " + name);
     130    }
     131    return getInternalState(f->second, inputStruct);
     132}
     133
     134/** ------------------------------------------------------------------------------------------------------------- *
     135 * @brief setInternalState
     136 ** ------------------------------------------------------------------------------------------------------------- */
     137void KernelBuilder::setInternalState(const unsigned index, Value * const value) {
     138    Value * ptr = getInternalState(index);
     139    assert (ptr->getType()->getPointerElementType() == value->getType());
     140    if (value->getType() == iBuilder->getBitBlockType()) {
     141        iBuilder->CreateBlockAlignedStore(value, ptr);
     142    } else {
     143        iBuilder->CreateStore(value, ptr);
     144    }
     145}
     146
    81147/** ------------------------------------------------------------------------------------------------------------- *
    82148 * @brief addInputScalar
    83149 ** ------------------------------------------------------------------------------------------------------------- */
    84150void KernelBuilder::addInputScalar(Type * const type, std::string name) {
    85     if (name.empty())
    86         mInputScalarNames.push_back(mKernelName + "_inputscalar_" + std::to_string(mInputScalars.size()));
    87     else
    88         mInputScalarNames.push_back(name);
    89 
     151    assert (type && !name.empty());
     152    mInputScalarNames.push_back(name);
    90153    mInputScalars.push_back(type);
     154}
     155
     156void KernelBuilder::addInputScalar(Type * const type) {
     157    addInputScalar(type, std::move(mKernelName + "_inputscalar_" + std::to_string(mInputScalars.size())));
    91158}
    92159
     
    96163Function * KernelBuilder::prepareFunction() {   
    97164    if (mCircularBufferModulo > 1) {
    98         mStartIndex = addInternalStateType(iBuilder->getInt32Ty());
     165        mBlockIndex = addInternalState(iBuilder->getInt32Ty());
    99166    }
    100167    const unsigned capacity = mBlocksPerSegment + mCircularBufferModulo - 1;
     
    103170    mInputScalarType = PointerType::get(StructType::get(mMod->getContext(), mInputScalars), 0);
    104171    Type * outputStreamType = ArrayType::get(StructType::get(mMod->getContext(), mOutputStreams), capacity);
    105     Type * outputAccumType = StructType::get(mMod->getContext(), mOutputAccums);
     172    Type * outputAccumType = StructType::get(mMod->getContext(), mOutputScalar);
    106173    Type * internalStateType = StructType::create(mMod->getContext(), mStates, mKernelName);
    107174    mKernelStructType = StructType::create(mMod->getContext(),std::vector<Type *>({internalStateType, outputStreamType, outputAccumType}), "KernelStruct_"+ mKernelName);
     
    116183    mKernelParam = args++;
    117184    mKernelParam->setName("this");
    118 
    119185    mInputParam = args++;
    120186    mInputParam->setName("input_stream");
     
    131197 ** ------------------------------------------------------------------------------------------------------------- */
    132198void KernelBuilder::finalize() {
     199
    133200    // Finish the actual function
    134     if (mCircularBufferModulo > 1) {
    135         Value * startIdx = getInternalState(mStartIndex);
    136         Value * value = iBuilder->CreateBlockAlignedLoad(startIdx);
    137         value = iBuilder->CreateAdd(value, iBuilder->getInt32(1));
    138         iBuilder->CreateBlockAlignedStore(value, startIdx);
    139     }
     201    Value * startIdx = getInternalState(mBlockIndex);
     202    Value * value = iBuilder->CreateBlockAlignedLoad(startIdx);
     203    value = iBuilder->CreateAdd(value, ConstantInt::get(value->getType(), 1));
     204    iBuilder->CreateBlockAlignedStore(value, startIdx);
    140205    iBuilder->CreateRetVoid();
    141 
    142206
    143207    // Generate the zero initializer
     
    145209    initializer->setCallingConv(CallingConv::C);
    146210    Function::arg_iterator args = initializer->arg_begin();
    147 
    148211    mKernelParam = args++;
    149212    mKernelParam->setName("this");
     
    176239    mKernelParam->setName("this");
    177240
    178     Value* block_size_param = args++;
    179     block_size_param->setName("block_size");
    180     Value* seg_size_param = args++;
    181     seg_size_param->setName("seg_size");
    182241    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", mConstructor, 0));
    183     setInternalState(0, block_size_param);
    184     setInternalState(1, seg_size_param);
    185242    iBuilder->CreateCall(initializer, mKernelParam);
    186243    iBuilder->CreateRetVoid();
     
    199256
    200257/** ------------------------------------------------------------------------------------------------------------- *
    201  * @brief getInputStream
    202  ** ------------------------------------------------------------------------------------------------------------- */
    203 Value * KernelBuilder::getInputStream(const unsigned index, const unsigned streamOffset) {
    204     Value * const indices[] = {getOffset(streamOffset), iBuilder->getInt32(index)};
    205     return iBuilder->CreateGEP(mInputParam, indices);
    206 }
    207 
    208 /** ------------------------------------------------------------------------------------------------------------- *
    209  * @brief getInputScalar
    210  ** ------------------------------------------------------------------------------------------------------------- */
    211 Value * KernelBuilder::getInputScalar(const unsigned index) {
    212     throw std::runtime_error("currently not supported!");
    213 }
    214 
    215 /** ------------------------------------------------------------------------------------------------------------- *
    216  * @brief getKernelState
    217  ** ------------------------------------------------------------------------------------------------------------- */
    218 Value * KernelBuilder::getKernelState(const unsigned index, const unsigned streamOffset) {
    219     Value * const indices[] = {iBuilder->getInt32(0), iBuilder->getInt32(0), getOffset(streamOffset), iBuilder->getInt32(index)};
    220     return iBuilder->CreateGEP(mKernelParam, indices);
    221 }
    222 
    223 /** ------------------------------------------------------------------------------------------------------------- *
    224  * @brief getOutputStream
    225  ** ------------------------------------------------------------------------------------------------------------- */
    226 Value * KernelBuilder::getOutputStream(const unsigned index, const unsigned streamOffset) {
    227     Value * const indices[] = {iBuilder->getInt32(0), iBuilder->getInt32(1), getOffset(streamOffset), iBuilder->getInt32(index)};
    228     return iBuilder->CreateGEP(mKernelParam, indices);
    229 }
    230 
    231 /** ------------------------------------------------------------------------------------------------------------- *
    232  * @brief getOutputScalar
    233  ** ------------------------------------------------------------------------------------------------------------- */
    234 Value * KernelBuilder::getOutputScalar(const unsigned index) {
    235 //    Value * const indices[] = {iBuilder->getInt32(0), iBuilder->getInt32(2), getOffset(0), iBuilder->getInt32(index)};
    236 //    return iBuilder->CreateGEP(mKernelParam, indices);
    237     throw std::runtime_error("currently not supported!");
    238 }
    239 
    240 /** ------------------------------------------------------------------------------------------------------------- *
    241  * @brief getInternalState
    242  ** ------------------------------------------------------------------------------------------------------------- */
    243 Value * KernelBuilder::getInternalState(const unsigned index){
    244     Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(index)};
    245     return iBuilder->CreateGEP(mKernelParam, indices);
    246 }
    247 
    248 /** ------------------------------------------------------------------------------------------------------------- *
    249  * @brief setInternalState
    250  ** ------------------------------------------------------------------------------------------------------------- */
    251 void KernelBuilder::setInternalState(const unsigned index, Value * const value) {
    252     Value * ptr = getInternalState(index);
    253     assert (ptr->getType()->getPointerElementType() == value->getType());
    254     if (value->getType() == iBuilder->getBitBlockType()) {
    255         iBuilder->CreateBlockAlignedStore(value, ptr);
    256     } else {
    257         iBuilder->CreateStore(value, ptr);
    258     }
    259 }
    260 
    261 /** ------------------------------------------------------------------------------------------------------------- *
    262258 * @brief generateDoBlockCall
    263259 ** ------------------------------------------------------------------------------------------------------------- */
     
    283279    const unsigned adjustedOffset = (mSegmentIndex + value);
    284280    Value * offset = iBuilder->getInt32(adjustedOffset);
    285     if (mStartIndex) {
    286         Value * index = iBuilder->CreateBlockAlignedLoad(getInternalState(mStartIndex));
     281    if (mBlockIndex) {
     282        Value * index = iBuilder->CreateBlockAlignedLoad(getInternalState(mBlockIndex));
    287283        if (adjustedOffset) {
    288284            index = iBuilder->CreateAdd(index, offset);
  • icGREP/icgrep-devel/icgrep/kernels/kernel.h

    r4968 r4970  
    88#include <string>
    99#include <vector>
     10#include <boost/container/flat_map.hpp>
    1011
    1112namespace llvm {
     
    3233
    3334class KernelBuilder {
    34 
     35    using NameMap = boost::container::flat_map<std::string, unsigned>;
    3536public:
    3637    // sets name & sets internal state to the kernel superclass state
    3738    KernelBuilder(std::string name, llvm::Module * m, IDISA::IDISA_Builder * b);
    3839
    39     unsigned addInternalStateType(llvm::Type * const type);
     40    unsigned addInternalState(llvm::Type * const type);
     41    unsigned addInternalState(llvm::Type * const type, std::string name);
     42
     43    void addInputStream(const unsigned fields);
     44    void addInputStream(const unsigned fields, std::string name);
     45
     46    void addInputScalar(llvm::Type * const type);
     47    void addInputScalar(llvm::Type * const type, std::string name);
     48
     49
    4050    void addOutputStream(const unsigned fields);
    41     void addOutputAccum(llvm::Type * const type);
    42     void addInputStream(const unsigned fields, std::string name);
    43     void addInputScalar(llvm::Type * const type, std::string name);
     51    void addOutputScalar(llvm::Type * const type);
    4452
    4553    llvm::Function * prepareFunction();
    4654
    4755    void increment();
    48     void incrementCircularBuffer();
    4956
    5057    llvm::Value * getInputStream(const unsigned index, const unsigned streamOffset = 0);
     58
    5159    llvm::Value * getInputScalar(const unsigned index);
    52     llvm::Value * getKernelState(const unsigned index, const unsigned streamOffset = 0);
     60
     61    llvm::Value * getInternalState(const std::string & name, llvm::Value * const inputStruct = nullptr);
     62
     63    llvm::Value * getInternalState(const unsigned index, llvm::Value * const inputStruct = nullptr);
     64
    5365    llvm::Value * getOutputStream(const unsigned index, const unsigned streamOffset = 0);
     66
    5467    llvm::Value * getOutputScalar(const unsigned index);
    5568
     
    6982
    7083    void setInternalState(const unsigned index, llvm::Value * const value);
    71     llvm::Value * getInternalState(const unsigned index);
     84
     85    llvm::Value * getBlockIndexScalar();
    7286
    7387protected:
     
    8498    std::vector<llvm::Type *>           mOutputStreams;
    8599    std::vector<llvm::Type *>           mInputScalars;
    86     std::vector<llvm::Type *>           mOutputAccums;
     100    std::vector<llvm::Type *>           mOutputScalar;
    87101    std::vector<std::string>            mInputStreamNames;
    88102    std::vector<std::string>            mInputScalarNames;
     
    100114    llvm::Value *                       mKernelParam;
    101115    unsigned                            mSegmentIndex;
    102     unsigned                            mStartIndex;
     116    unsigned                            mBlockIndex;
     117
     118
     119    NameMap                             mStateNameMap;
     120
    103121};
    104122
     
    127145}
    128146
     147inline llvm::Value * KernelBuilder::getBlockIndexScalar() {
     148    return getInternalState(mBlockIndex);
     149}
     150
    129151#endif // KERNEL_H
  • icGREP/icgrep-devel/icgrep/kernels/pipeline.cpp

    r4968 r4970  
    1919: mMod(m)
    2020, iBuilder(b)
    21 , mFileBufIdx(7)
    22 , mFileSizeIdx(8)
    23 , mFileNameIdx(9)
    2421, mBitBlockType(b->getBitBlockType())
    2522, mBlockSize(b->getBitBlockWidth()){
     
    3936
    4037    generateS2PKernel(mMod, iBuilder, mS2PKernel);
     38
    4139    generateScanMatch(mMod, iBuilder, 64, mScanMatchKernel, isNameExpression);
    4240
     
    5755}
    5856
    59 void PipelineBuilder::ExecuteKernels(){
     57void PipelineBuilder::ExecuteKernels() {
    6058    Type * T = iBuilder->getIntNTy(64);   
    6159    Type * S = PointerType::get(iBuilder->getIntNTy(8), 0);
     
    9189
    9290
    93     Value * gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileBufIdx)});
     91    Value * gep = mScanMatchKernel->getInternalState("FileBuf", scanMatchKernelStruct);
    9492    Value * filebuf = iBuilder->CreateBitCast(input_param, S);
    9593    iBuilder->CreateStore(filebuf, gep);
    9694
    97     gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileSizeIdx)});
     95
     96    gep = mScanMatchKernel->getInternalState("FileSize", scanMatchKernelStruct);
    9897    iBuilder->CreateStore(buffersize_param, gep);
    9998
    10099
    101     gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileNameIdx)});
     100    gep = mScanMatchKernel->getInternalState("FileName", scanMatchKernelStruct);
    102101    iBuilder->CreateStore(filename_param, gep);
    103102
     
    113112    blkNo_phi->addIncoming(iBuilder->getInt64(0), entry_block);
    114113
    115     Value * final_block_cond = iBuilder->CreateICmpSLT(remaining_phi, ConstantInt::get(T, mBlockSize));
     114    Constant * step = ConstantInt::get(T, mBlockSize * mS2PKernel->getSegmentBlocks());
     115
     116    Value * final_block_cond = iBuilder->CreateICmpSLT(remaining_phi, step);
    116117    iBuilder->CreateCondBr(final_block_cond, pipeline_final_block, pipeline_do_block);
    117118
     
    126127    mScanMatchKernel->generateDoBlockCall(results);
    127128
    128     Value * update_remaining = iBuilder->CreateSub(remaining_phi, iBuilder->getInt64(mBlockSize));
     129    Value * update_remaining = iBuilder->CreateSub(remaining_phi, step);
    129130    remaining_phi->addIncoming(update_remaining, pipeline_do_block);
    130131    iBuilder->CreateBr(pipeline_test_block);
  • icGREP/icgrep-devel/icgrep/kernels/pipeline.h

    r4959 r4970  
    4343    KernelBuilder *                     mICgrepKernel;   
    4444    KernelBuilder *                     mScanMatchKernel;
    45     int                                 mFileBufIdx;
    46     int                                 mFileSizeIdx;
    47     int                                 mFileNameIdx;
    4845    Type*                               mBitBlockType;
    4946    int                                 mBlockSize;
  • icGREP/icgrep-devel/icgrep/kernels/scanmatchgen.cpp

    r4959 r4970  
    3737}
    3838
    39 Function * generateScanWordRoutine(Module * m, IDISA::IDISA_Builder * iBuilder, unsigned scanwordBitWidth, Type * kernelStuctType, bool isNameExpression) {
     39Function * generateScanWordRoutine(Module * m, IDISA::IDISA_Builder * iBuilder, unsigned scanwordBitWidth, KernelBuilder * const kBuilder, bool isNameExpression) {
    4040
    4141    Function * function = m->getFunction("scan_matches_in_scanword");
     
    4848    Type * S = PointerType::get(iBuilder->getIntNTy(8), 0);
    4949    Type * returnType = StructType::get(ctxt, std::vector<Type *>({T, T}));
    50     FunctionType * functionType = FunctionType::get(returnType, std::vector<Type *>({kernelStuctType, T, T, T, T, T}), false);
     50    FunctionType * functionType = FunctionType::get(returnType, std::vector<Type *>({PointerType::get(kBuilder->getKernelStructType(), 0), T, T, T, T, T}), false);
    5151
    5252    SmallVector<AttributeSet, 6> Attrs;
     
    7777    Value * recordNum_input_parm = args++;
    7878    recordNum_input_parm->setName("lineNum");
     79
    7980    Constant * matchProcessor;
    80     if(isNameExpression)
     81    if (isNameExpression) {
    8182        matchProcessor = m->getOrInsertFunction("insert_codepoints", Type::getVoidTy(ctxt), T, T, T, S, nullptr);
    82     else
     83    } else {
    8384        matchProcessor = m->getOrInsertFunction("wrapped_report_match", Type::getVoidTy(ctxt), T, T, T, S, T, S, nullptr);
    84 
     85    }
    8586    iBuilder->SetInsertPoint(BasicBlock::Create(ctxt, "entry", function,0));
    8687
     
    147148    matchRecordNum_phi->addIncoming(matchRecordNum, prior_breaks_block);
    148149    matchRecordStart_phi->addIncoming(recordStart_phi, process_matches_loop_entry);
    149     matchRecordStart_phi->addIncoming(matchRecordStart, prior_breaks_block);
     150    matchRecordStart_phi->addIncoming(matchRecordStart, prior_breaks_block);   
    150151    Value * matchRecordEnd = iBuilder->CreateAdd(scanwordPos, generateCountForwardZeroes(iBuilder, matches_phi));
    151152
    152     Value* filebuf_gep = iBuilder->CreateGEP(this_input_parm, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(7)});
     153    Value* filebuf_gep = kBuilder->getInternalState("FileBuf", this_input_parm);
    153154    Value* filebufptr = iBuilder->CreateLoad(filebuf_gep, "filebuf");
    154155
    155     Value* filesize_gep = iBuilder->CreateGEP(this_input_parm, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(8)});
    156     Value* filesize = iBuilder->CreateLoad(filesize_gep, "filensize");
    157 
    158     Value* filename_gep = iBuilder->CreateGEP(this_input_parm, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(9)});
    159     Value* filenameptr = iBuilder->CreateLoad(filename_gep, "filename");
    160 
    161     if(isNameExpression)
     156    if (isNameExpression) {
    162157        iBuilder->CreateCall(matchProcessor, std::vector<Value *>({matchRecordNum_phi, matchRecordStart_phi, matchRecordEnd, filebufptr}));
    163     else
     158    } else {
     159        Value * filesize_gep = kBuilder->getInternalState("FileSize", this_input_parm);
     160        Value * filesize = iBuilder->CreateLoad(filesize_gep, "filesize");
     161
     162        Value * filename_gep = kBuilder->getInternalState("FileName", this_input_parm);
     163        Value * filenameptr = iBuilder->CreateLoad(filename_gep, "filename");
     164
    164165        iBuilder->CreateCall(matchProcessor, std::vector<Value *>({matchRecordNum_phi, matchRecordStart_phi, matchRecordEnd, filebufptr, filesize, filenameptr}));
     166    }
     167
    165168    Value * remaining_matches = generateResetLowestBit(iBuilder, matches_phi);
    166169    Value * remaining_breaks = iBuilder->CreateXor(record_breaks_phi, prior_breaks);
     
    203206
    204207
    205 void generateScanMatch(Module * m, IDISA::IDISA_Builder * iBuilder, unsigned scanWordBitWidth, KernelBuilder * kBuilder, bool isNameExpression){
     208void generateScanMatch(Module * m, IDISA::IDISA_Builder * iBuilder, unsigned scanWordBitWidth, KernelBuilder * kBuilder, bool isNameExpression) {
    206209
    207210
     
    214217    kBuilder->addInputStream(1, "breaks");
    215218    //use index
    216     unsigned blockPosIdx = kBuilder->addInternalStateType(T);
    217     unsigned lineStartIdx = kBuilder->addInternalStateType(T);
    218     unsigned lineNumIdx = kBuilder->addInternalStateType(T);
    219     kBuilder->addInternalStateType(S);
    220     kBuilder->addInternalStateType(T);
    221     kBuilder->addInternalStateType(S);
     219    const unsigned lineStart = kBuilder->addInternalState(T, "LineStart");
     220    const unsigned lineNum = kBuilder->addInternalState(T, "LineNum");
     221    kBuilder->addInternalState(S, "FileBuf");
     222    kBuilder->addInternalState(T, "FileSize");
     223    kBuilder->addInternalState(S, "FileName");
    222224
    223225    Function * function = kBuilder->prepareFunction();
    224226
    225     Type * kernelStuctType = PointerType::get(kBuilder->getKernelStructType(), 0);
    226 
    227     Function * scanWordFunction = generateScanWordRoutine(m, iBuilder, scanWordBitWidth, kernelStuctType, isNameExpression);
     227    // Type * kernelStuctType = PointerType::get(kBuilder->getKernelStructType(), 0);
     228
     229    Function * scanWordFunction = generateScanWordRoutine(m, iBuilder, scanWordBitWidth, kBuilder, isNameExpression);
    228230
    229231    iBuilder->SetInsertPoint(&function->getEntryBlock());
     
    231233    Value * kernelStuctParam = kBuilder->getKernelStructParam();
    232234
    233     Value * scanwordPos = iBuilder->CreateBlockAlignedLoad(kBuilder->getInternalState(blockPosIdx));
    234     Value * recordStart = iBuilder->CreateBlockAlignedLoad(kBuilder->getInternalState(lineStartIdx));
    235     Value * recordNum = iBuilder->CreateBlockAlignedLoad(kBuilder->getInternalState(lineNumIdx));
     235    Value * scanwordPos = iBuilder->CreateBlockAlignedLoad(kBuilder->getInternalState("BlockNo"));
     236    scanwordPos = iBuilder->CreateMul(scanwordPos, ConstantInt::get(scanwordPos->getType(), iBuilder->getBitBlockWidth()));
     237
     238    Value * recordStart = iBuilder->CreateBlockAlignedLoad(kBuilder->getInternalState(lineStart));
     239    Value * recordNum = iBuilder->CreateBlockAlignedLoad(kBuilder->getInternalState(lineNum));
     240
    236241    Value * wordResult = nullptr;
    237242
    238243    const unsigned segmentBlocks = kBuilder->getSegmentBlocks();
    239244    const unsigned scanWordBlocks =  segmentBlocks * fieldCount;
    240 
    241245    for(unsigned j = 0; j < segmentBlocks; ++j) {
    242 
    243246        Value * matchWordVector = iBuilder->CreateBitCast(iBuilder->CreateBlockAlignedLoad(kBuilder->getInputStream(0)), scanwordVectorType);
    244247        Value * breakWordVector = iBuilder->CreateBitCast(iBuilder->CreateBlockAlignedLoad(kBuilder->getInputStream(1)), scanwordVectorType);
    245 
    246248        for(unsigned i = 0; i < scanWordBlocks; ++i){
    247249            Value * matchWord = iBuilder->CreateExtractElement(matchWordVector, ConstantInt::get(T, i));
     
    254256        kBuilder->increment();
    255257    }
    256 
    257     kBuilder->setInternalState(blockPosIdx, scanwordPos);
    258     kBuilder->setInternalState(lineStartIdx, recordStart);
    259     kBuilder->setInternalState(lineNumIdx, recordNum);
    260 
     258    kBuilder->setInternalState(lineStart, recordStart);
     259    kBuilder->setInternalState(lineNum, recordNum);
    261260    kBuilder->finalize();
    262261}
  • icGREP/icgrep-devel/icgrep/kernels/scanmatchgen.h

    r4959 r4970  
    55#ifndef SCANMATCHGEN_H
    66#define SCANMATCHGEN_H
     7
     8#include <tuple>
    79
    810class KernelBuilder;
  • icGREP/icgrep-devel/icgrep/pablo/carry_manager.cpp

    r4968 r4970  
    2828    const unsigned totalCarryDataSize = std::max<unsigned>(enumerate(mRootScope, 0, 0), 1);
    2929    mCarryPackPtr.resize(totalCarryDataSize, nullptr);
    30     mCarryInPack.resize(totalCarryDataSize, nullptr);
     30    mCarryInPack.resize(totalCarryDataSize);
    3131    mCarryOutPack.resize(totalCarryDataSize, nullptr);
    3232    mTotalCarryDataBitBlocks = totalCarryDataSize;
    3333    ArrayType* cdArrayTy = ArrayType::get(mBitBlockType, mTotalCarryDataBitBlocks);
    34     mCdArrayIdx = kBuilder->addInternalStateType(cdArrayTy);
     34    mCdArrayIdx = kBuilder->addInternalState(cdArrayTy);
    3535    if (mPabloCountCount > 0) {
    3636        ArrayType* pcArrayTy = ArrayType::get(iBuilder->getIntNTy(64), mPabloCountCount);
    37         mPcArrayIdx = kBuilder->addInternalStateType(pcArrayTy);
     37        mPcArrayIdx = kBuilder->addInternalState(pcArrayTy);
     38    }
     39    mKernelBuilder = kBuilder;
     40}
     41
     42/** ------------------------------------------------------------------------------------------------------------- *
     43 * @brief reset
     44 ** ------------------------------------------------------------------------------------------------------------- */
     45void CarryManager::reset() {
     46    Value * cdArrayPtr = mKernelBuilder->getInternalState(mCdArrayIdx);
     47    mCarryPackBasePtr = iBuilder->CreateBitCast(cdArrayPtr, PointerType::get(mCarryPackType, 0));
     48    mCarryBitBlockPtr = iBuilder->CreateBitCast(cdArrayPtr, PointerType::get(mBitBlockType, 0));
     49    if (mPabloCountCount > 0) {
     50        Value * pcArrayPtr = mKernelBuilder->getInternalState(mPcArrayIdx);
     51        mPopcountBasePtr = iBuilder->CreateBitCast(pcArrayPtr, Type::getInt64PtrTy(iBuilder->getContext()));
    3852    }
    3953    mCurrentScope = mRootScope;
     
    4155    mCarryInfo = mCarryInfoVector[0];
    4256    mCarryOutPack[summaryPack()] = Constant::getNullValue(mCarryPackType);
    43 }
    44 
    45 /** ------------------------------------------------------------------------------------------------------------- *
    46  * @brief initialize_setPtrs
    47  ** ------------------------------------------------------------------------------------------------------------- */
    48 void CarryManager::initialize_setPtrs(KernelBuilder * kBuilder) {
    49     Value * cdArrayPtr = kBuilder->getInternalState(mCdArrayIdx);
    50     mCarryPackBasePtr = iBuilder->CreateBitCast(cdArrayPtr, PointerType::get(mCarryPackType, 0));
    51     mCarryBitBlockPtr = iBuilder->CreateBitCast(cdArrayPtr, PointerType::get(mBitBlockType, 0));
    52     if (mPabloCountCount > 0) {
    53         Value * pcArrayPtr = kBuilder->getInternalState(mPcArrayIdx);
    54         mPopcountBasePtr = iBuilder->CreateBitCast(pcArrayPtr, Type::getInt64PtrTy(iBuilder->getContext()));
    55     }
    56     setBlockNo(kBuilder);
    57     mCurrentScope = mRootScope;
    58     mCurrentFrameIndex = 0;
    59     mCarryInfo = mCarryInfoVector[0];
    60     mCarryOutPack[summaryPack()] = Constant::getNullValue(mCarryPackType);
    61 }
    62 
    63 /** ------------------------------------------------------------------------------------------------------------- *
    64  * @brief setBlockNo
    65  ** ------------------------------------------------------------------------------------------------------------- */
    66 void CarryManager::setBlockNo(KernelBuilder * kBuilder) {
    67     mBlockNo = iBuilder->CreateUDiv(iBuilder->CreateBlockAlignedLoad(kBuilder->getInternalState(mFilePosIdx)), iBuilder->getInt64(mBitBlockWidth));
     57    assert (mCarrySummary.empty());
     58    std::fill(mCarryInPack.begin(), mCarryInPack.end(), nullptr);
    6859}
    6960
     
    205196    const unsigned bufsize = mCarryInfo->longAdvanceBufferSize(shiftAmount);
    206197    Value * indexMask = iBuilder->getInt64(bufsize - 1);  // A mask to implement circular buffer indexing
    207     Value * loadIndex0 = iBuilder->CreateAdd(iBuilder->CreateAnd(iBuilder->CreateSub(mBlockNo, iBuilder->getInt64(advanceEntries)), indexMask), advBaseIndex);
    208     Value * storeIndex = iBuilder->CreateAdd(iBuilder->CreateAnd(mBlockNo, indexMask), advBaseIndex);
     198    Value * blockIndex = iBuilder->CreateBlockAlignedLoad(mKernelBuilder->getBlockIndexScalar());
     199    Value * loadIndex0 = iBuilder->CreateAdd(iBuilder->CreateAnd(iBuilder->CreateSub(blockIndex, iBuilder->getInt64(advanceEntries)), indexMask), advBaseIndex);
     200    Value * storeIndex = iBuilder->CreateAdd(iBuilder->CreateAnd(blockIndex, indexMask), advBaseIndex);
    209201    Value * carry_block0 = iBuilder->CreateBlockAlignedLoad(iBuilder->CreateGEP(mCarryBitBlockPtr, loadIndex0));
    210202    // If the long advance is an exact multiple of mBITBLOCK_WIDTH, we simply return the oldest
     
    215207    }
    216208    // Otherwise we need to combine data from the two oldest blocks.
    217     Value * loadIndex1 = iBuilder->CreateAdd(iBuilder->CreateAnd(iBuilder->CreateSub(mBlockNo, iBuilder->getInt64(advanceEntries-1)), indexMask), advBaseIndex);
     209    Value * loadIndex1 = iBuilder->CreateAdd(iBuilder->CreateAnd(iBuilder->CreateSub(blockIndex, iBuilder->getInt64(advanceEntries-1)), indexMask), advBaseIndex);
    218210    Value * carry_block1 = iBuilder->CreateBlockAlignedLoad(iBuilder->CreateGEP(mCarryBitBlockPtr, loadIndex1));
    219211    Value* block0_shr = iBuilder->CreateLShr(iBuilder->CreateBitCast(carry_block0, iBuilder->getIntNTy(mBitBlockWidth)), mBitBlockWidth - block_shift);
  • icGREP/icgrep-devel/icgrep/pablo/carry_manager.h

    r4959 r4970  
    1313#include <pablo/carry_data.h>
    1414#include <llvm/IR/Constants.h>
    15 #include "../kernels/kernel.h"
     15#include <kernels/kernel.h>
    1616
    1717/*
     
    4848    , mCarryBitBlockPtr(nullptr)
    4949    , mPopcountBasePtr(nullptr)
    50     , mBlockNo(nullptr)
     50    , mKernelBuilder(nullptr)
    5151    , mPabloCountCount(0)
    5252    , mTotalCarryDataBitBlocks(0)
     
    6161    void initialize(PabloFunction * const function, KernelBuilder * const kBuilder);
    6262
    63     void initialize_setPtrs(KernelBuilder * const kBuilder);
     63    void reset();
    6464
    65     void setBlockNo(KernelBuilder * kBuilder);
    66     Value * getBlockNo() const;
    67    
    6865    unsigned enumerate(PabloBlock * blk, unsigned ifDepth, unsigned whileDepth);
    6966         
     
    143140    Value * mCarryBitBlockPtr;
    144141    Value * mPopcountBasePtr;
    145     Value * mBlockNo;
     142    KernelBuilder * mKernelBuilder;
    146143    unsigned mPabloCountCount; // Number of Pablo "Count" operations
    147144    unsigned mTotalCarryDataBitBlocks;
     
    163160}
    164161
    165 inline Value * CarryManager::getBlockNo() const {
    166     return mBlockNo;
    167 }
    168 
    169162}
    170163
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp

    r4968 r4970  
    7373, mPabloFunction(nullptr)
    7474, mPabloBlock(nullptr)
    75 , mKBuilder(nullptr)
     75, mKernelBuilder(nullptr)
    7676, mWhileDepth(0)
    7777, mIfDepth(0)
    7878, mFunction(nullptr)
    79 , mMaxWhileDepth(0)
    80 , mFilePosIdx(2) {
     79, mMaxWhileDepth(0) {
    8180   
    8281}
    8382
    8483void PabloCompiler::setKernel(KernelBuilder * kBuilder){
    85     mKBuilder = kBuilder;   
     84    mKernelBuilder = kBuilder;
    8685}
    8786
     
    124123
    125124    for (unsigned i = 0; i < function->getNumOfParameters(); ++i) {
    126         mKBuilder->addInputStream(1, function->getParameter(i)->getName()->to_string());
     125        mKernelBuilder->addInputStream(1, function->getParameter(i)->getName()->to_string());
    127126    }
    128127    for (unsigned i = 0; i < function->getNumOfResults(); ++i) {
    129         mKBuilder->addOutputStream(1);
    130     }
    131 
    132     mCarryManager->initialize(function, mKBuilder);
    133 
    134     mKBuilder->prepareFunction();
    135 
    136     mFunction = mKBuilder->getDoBlockFunction();
    137 
    138     mCarryManager->initialize_setPtrs(mKBuilder);
    139 
    140     for(unsigned i = 0; i < mKBuilder->getSegmentBlocks(); i++){
     128        mKernelBuilder->addOutputStream(1);
     129    }
     130
     131    mCarryManager->initialize(function, mKernelBuilder);
     132
     133    mKernelBuilder->prepareFunction();
     134
     135    mFunction = mKernelBuilder->getDoBlockFunction();
     136
     137    for(unsigned i = 0; i < mKernelBuilder->getSegmentBlocks(); ++i){
     138
     139        mCarryManager->reset();
    141140
    142141        for (unsigned j = 0; j < function->getNumOfParameters(); ++j) {
    143             mMarkerMap.insert(std::make_pair(function->getParameter(j), mKBuilder->getInputStream(j)));
     142            mMarkerMap.insert(std::make_pair(function->getParameter(j), mKernelBuilder->getInputStream(j)));
    144143        }
    145144
    146145        compileBlock(function->getEntryBlock());
    147 
    148         Value * filePos = mKBuilder->getInternalState(mFilePosIdx);
    149         filePos = iBuilder->CreateBlockAlignedLoad(filePos);
    150         filePos = iBuilder->CreateAdd(filePos, iBuilder->getInt64(iBuilder->getBitBlockWidth()));
    151         mKBuilder->setInternalState(mFilePosIdx, filePos);
    152 
    153         mCarryManager->setBlockNo(mKBuilder);
    154146
    155147        for (unsigned j = 0; j < function->getNumOfResults(); ++j) {
     
    161153                result = f->second;
    162154            }
    163             iBuilder->CreateBlockAlignedStore(result, mKBuilder->getOutputStream(j));
     155            iBuilder->CreateBlockAlignedStore(result, mKernelBuilder->getOutputStream(j));
    164156        }
    165157
    166158        mMarkerMap.clear();
    167159
    168         mKBuilder->increment();
     160        mKernelBuilder->increment();
    169161    }   
    170162
    171     mKBuilder->finalize();
     163    mKernelBuilder->finalize();
    172164}
    173165
     
    178170    Examine(function.getEntryBlock());
    179171}
    180 
    181172
    182173void PabloCompiler::Examine(PabloBlock * block) {
     
    192183}
    193184
    194 void PabloCompiler::compileBlock(PabloBlock * block) {
     185void PabloCompiler::compileBlock(const PabloBlock * const block) {
    195186    mPabloBlock = block;
    196187    for (const Statement * statement : *block) {
     
    436427        const unsigned offset = l->getAmount() / iBuilder->getBitBlockWidth();
    437428        const unsigned shift = (l->getAmount() % iBuilder->getBitBlockWidth());
    438         Value * const b0 = iBuilder->CreateBitCast(iBuilder->CreateBlockAlignedLoad(mKBuilder->getInputStream(offset), index), streamType);
    439         Value * const b1 = iBuilder->CreateBitCast(iBuilder->CreateBlockAlignedLoad(mKBuilder->getInputStream(offset + 1), index), streamType);
     429        Value * const b0 = iBuilder->CreateBitCast(iBuilder->CreateBlockAlignedLoad(mKernelBuilder->getInputStream(offset), index), streamType);
     430        Value * const b1 = iBuilder->CreateBitCast(iBuilder->CreateBlockAlignedLoad(mKernelBuilder->getInputStream(offset + 1), index), streamType);
    440431        Value * result = iBuilder->CreateOr(iBuilder->CreateLShr(b0, shift), iBuilder->CreateShl(b1, iBuilder->getBitBlockWidth() - shift), "lookahead");
    441432        expr = iBuilder->CreateBitCast(result, iBuilder->getBitBlockType());
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.h

    r4959 r4970  
    5858    void Examine(PabloBlock * block);
    5959
    60     void compileBlock(PabloBlock * block);
     60    void compileBlock(const PabloBlock * const block);
    6161    void compileStatement(const Statement * stmt);
    6262    void compileIf(const If * ifStmt);
     
    7373    CarryManager *                      mCarryManager;
    7474
    75     PabloFunction *                     mPabloFunction;
    76     PabloBlock *                        mPabloBlock;
     75    const PabloFunction *               mPabloFunction;
     76    const PabloBlock *                  mPabloBlock;
    7777
    78     KernelBuilder *                     mKBuilder;
     78    KernelBuilder *                     mKernelBuilder;
    7979
    8080    unsigned                            mWhileDepth;
     
    8484
    8585    unsigned                            mMaxWhileDepth;
    86     int                                 mFilePosIdx;
    8786};
    8887
Note: See TracChangeset for help on using the changeset viewer.