Ignore:
Timestamp:
Mar 10, 2016, 4:10:02 PM (3 years ago)
Author:
nmedfort
Message:

Some fixes for threading and kernel builder.

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

Legend:

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

    r4959 r4968  
    77#include <pablo/function.h>
    88#include <IDISA/idisa_builder.h>
     9#include <llvm/Support/CommandLine.h>
    910
    1011using namespace llvm;
    1112using namespace pablo;
     13
     14static cl::opt<unsigned> SegmentSize("segment-size", cl::desc("Segment Size"), cl::value_desc("LLVM IR file"), cl::init(1));
    1215
    1316inline bool isPowerOfTwo(const unsigned x) {
     
    2225, mBitBlockType(b->getBitBlockType())
    2326, mBlockSize(b->getBitBlockWidth())
    24 , mBlocksPerSegment(1)
     27, mBlocksPerSegment(SegmentSize)
    2528, mCircularBufferModulo(1)
    2629, mSegmentIndex(0)
    2730, mStartIndex(0) {
     31    assert (mBlocksPerSegment > 0);
    2832    addInternalStateType(b->getInt64Ty());
    2933    addInternalStateType(b->getInt64Ty());
     
    3236}
    3337
    34 unsigned KernelBuilder::addInternalStateType(Type * type){
    35     unsigned idx = mStates.size();
     38/** ------------------------------------------------------------------------------------------------------------- *
     39 * @brief addInternalStateType
     40 ** ------------------------------------------------------------------------------------------------------------- */
     41unsigned KernelBuilder::addInternalStateType(Type * const type) {
     42    assert (type);
     43    const unsigned index = mStates.size();
    3644    mStates.push_back(type);
    37     return idx;
    38 }
    39 void KernelBuilder::addOutputStream(const unsigned fields){
    40     if (fields == 1){
    41         mOutputStreams.push_back(mBitBlockType);
    42     }
    43     else {
    44         mOutputStreams.push_back(ArrayType::get(mBitBlockType, fields));
    45     }
    46 
    47 }
    48 void KernelBuilder::addOutputAccum(Type * t){
    49     mOutputAccums.push_back(t);
    50 
    51 }
    52 void KernelBuilder::addInputStream(const unsigned fields, std::string name){
     45    return index;
     46}
     47
     48/** ------------------------------------------------------------------------------------------------------------- *
     49 * @brief addOutputStream
     50 ** ------------------------------------------------------------------------------------------------------------- */
     51void KernelBuilder::addOutputStream(const unsigned fields) {
     52    assert (fields > 0);
     53    mOutputStreams.push_back((fields == 1) ? mBitBlockType : ArrayType::get(mBitBlockType, fields));
     54}
     55
     56/** ------------------------------------------------------------------------------------------------------------- *
     57 * @brief addOutputAccum
     58 ** ------------------------------------------------------------------------------------------------------------- */
     59void KernelBuilder::addOutputAccum(Type * const type) {
     60    assert (type);
     61    mOutputAccums.push_back(type);
     62}
     63
     64/** ------------------------------------------------------------------------------------------------------------- *
     65 * @brief addInputStream
     66 ** ------------------------------------------------------------------------------------------------------------- */
     67void KernelBuilder::addInputStream(const unsigned fields, std::string name) {
     68    assert (fields > 0);
    5369    if (name.empty())
    5470        mInputStreamNames.push_back(mKernelName + "_inputstream_" + std::to_string(mInputStreams.size()));
     
    6278    }
    6379}
    64 void KernelBuilder::addInputScalar(Type * t, std::string name){
     80
     81/** ------------------------------------------------------------------------------------------------------------- *
     82 * @brief addInputScalar
     83 ** ------------------------------------------------------------------------------------------------------------- */
     84void KernelBuilder::addInputScalar(Type * const type, std::string name) {
    6585    if (name.empty())
    6686        mInputScalarNames.push_back(mKernelName + "_inputscalar_" + std::to_string(mInputScalars.size()));
     
    6888        mInputScalarNames.push_back(name);
    6989
    70     mInputScalars.push_back(t);
     90    mInputScalars.push_back(type);
    7191}
    7292
     
    7999    }
    80100    const unsigned capacity = mBlocksPerSegment + mCircularBufferModulo - 1;
    81     mInputStreamType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), mInputStreams), capacity), 0);
     101
     102    mInputStreamType = PointerType::get(StructType::get(mMod->getContext(), mInputStreams), 0);
    82103    mInputScalarType = PointerType::get(StructType::get(mMod->getContext(), mInputScalars), 0);
    83104    Type * outputStreamType = ArrayType::get(StructType::get(mMod->getContext(), mOutputStreams), capacity);
    84105    Type * outputAccumType = StructType::get(mMod->getContext(), mOutputAccums);
    85     Type * stateType = StructType::create(mMod->getContext(), mStates, mKernelName);
    86     mKernelStructType = StructType::create(mMod->getContext(),std::vector<Type *>({stateType, outputStreamType, outputAccumType}), "KernelStruct_"+ mKernelName);
     106    Type * internalStateType = StructType::create(mMod->getContext(), mStates, mKernelName);
     107    mKernelStructType = StructType::create(mMod->getContext(),std::vector<Type *>({internalStateType, outputStreamType, outputAccumType}), "KernelStruct_"+ mKernelName);
    87108
    88109    FunctionType * functionType = FunctionType::get(Type::getVoidTy(mMod->getContext()),
     
    110131 ** ------------------------------------------------------------------------------------------------------------- */
    111132void KernelBuilder::finalize() {
    112     Type * const int64Ty = iBuilder->getInt64Ty();
    113 
    114133    // Finish the actual function
    115134    if (mCircularBufferModulo > 1) {
    116135        Value * startIdx = getInternalState(mStartIndex);
    117         Value * value = iBuilder->CreateAdd(iBuilder->CreateBlockAlignedLoad(startIdx), iBuilder->getInt32(1));
     136        Value * value = iBuilder->CreateBlockAlignedLoad(startIdx);
     137        value = iBuilder->CreateAdd(value, iBuilder->getInt32(1));
    118138        iBuilder->CreateBlockAlignedStore(value, startIdx);
    119139    }
     
    131151    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", initializer, 0));
    132152
     153    Type * const int64Ty = iBuilder->getInt64Ty(); // TODO: should call getIntPtrTy() instead but we don't have the data layout here.
    133154    for (unsigned i = 0; i < mStates.size(); ++i) {
    134155        Value * const gep = getInternalState(i);
     
    136157        if (type->isIntegerTy() || type->isArrayTy() || type->isVectorTy()) {
    137158            setInternalState(i, Constant::getNullValue(type));
    138         } else {
     159        } else {           
    139160            Value * gep_next = iBuilder->CreateGEP(gep, iBuilder->getInt32(1));
    140161            Value * get_int = iBuilder->CreatePtrToInt(gep, int64Ty);
     
    160181    seg_size_param->setName("seg_size");
    161182    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", mConstructor, 0));
    162     iBuilder->CreateStore(block_size_param, getInternalState(0));
    163     iBuilder->CreateStore(seg_size_param, getInternalState(1));
     183    setInternalState(0, block_size_param);
     184    setInternalState(1, seg_size_param);
    164185    iBuilder->CreateCall(initializer, mKernelParam);
    165186    iBuilder->CreateRetVoid();
     
    173194    iBuilder->CreateCall3(mConstructor, mKernelStruct,
    174195        ConstantInt::get(iBuilder->getIntNTy(64), mBlockSize),
    175         ConstantInt::get(iBuilder->getIntNTy(64), (mBlocksPerSegment + mCircularBufferModulo) * mBlockSize));
     196        ConstantInt::get(iBuilder->getIntNTy(64), (mBlocksPerSegment + mCircularBufferModulo - 1) * mBlockSize));
    176197    return mKernelStruct;
    177 
    178 }
    179 
     198}
     199
     200/** ------------------------------------------------------------------------------------------------------------- *
     201 * @brief getInputStream
     202 ** ------------------------------------------------------------------------------------------------------------- */
    180203Value * KernelBuilder::getInputStream(const unsigned index, const unsigned streamOffset) {
    181     Value * const indices[] = {iBuilder->getInt32(0), getOffset(streamOffset), iBuilder->getInt32(index)};
     204    Value * const indices[] = {getOffset(streamOffset), iBuilder->getInt32(index)};
    182205    return iBuilder->CreateGEP(mInputParam, indices);
    183206}
    184207
     208/** ------------------------------------------------------------------------------------------------------------- *
     209 * @brief getInputScalar
     210 ** ------------------------------------------------------------------------------------------------------------- */
     211Value * KernelBuilder::getInputScalar(const unsigned index) {
     212    throw std::runtime_error("currently not supported!");
     213}
     214
     215/** ------------------------------------------------------------------------------------------------------------- *
     216 * @brief getKernelState
     217 ** ------------------------------------------------------------------------------------------------------------- */
    185218Value * KernelBuilder::getKernelState(const unsigned index, const unsigned streamOffset) {
    186219    Value * const indices[] = {iBuilder->getInt32(0), iBuilder->getInt32(0), getOffset(streamOffset), iBuilder->getInt32(index)};
     
    188221}
    189222
     223/** ------------------------------------------------------------------------------------------------------------- *
     224 * @brief getOutputStream
     225 ** ------------------------------------------------------------------------------------------------------------- */
    190226Value * KernelBuilder::getOutputStream(const unsigned index, const unsigned streamOffset) {
    191227    Value * const indices[] = {iBuilder->getInt32(0), iBuilder->getInt32(1), getOffset(streamOffset), iBuilder->getInt32(index)};
     
    193229}
    194230
    195 Value * KernelBuilder::getOutputScalar(const unsigned index, const unsigned streamOffset) {
    196     Value * const indices[] = {iBuilder->getInt32(0), iBuilder->getInt32(2), getOffset(streamOffset), iBuilder->getInt32(index)};
    197     return iBuilder->CreateGEP(mKernelParam, indices);
    198 }
    199 
     231/** ------------------------------------------------------------------------------------------------------------- *
     232 * @brief getOutputScalar
     233 ** ------------------------------------------------------------------------------------------------------------- */
     234Value * 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 ** ------------------------------------------------------------------------------------------------------------- */
    200243Value * KernelBuilder::getInternalState(const unsigned index){
    201244    Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(index)};
     
    203246}
    204247
     248/** ------------------------------------------------------------------------------------------------------------- *
     249 * @brief setInternalState
     250 ** ------------------------------------------------------------------------------------------------------------- */
    205251void KernelBuilder::setInternalState(const unsigned index, Value * const value) {
    206     iBuilder->CreateBlockAlignedStore(value, getInternalState(index));
    207 }
    208 
    209 void KernelBuilder::generateInitCall(){
     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/** ------------------------------------------------------------------------------------------------------------- *
     262 * @brief generateDoBlockCall
     263 ** ------------------------------------------------------------------------------------------------------------- */
     264void KernelBuilder::generateInitCall() {
     265    assert (mInitFunction && mKernelStruct);
    210266    iBuilder->CreateCall(mInitFunction, mKernelStruct);
    211267}
    212268
    213 void KernelBuilder::generateDoBlockCall(Value * inputStreams){
    214     iBuilder->CreateCall2(mFunction, mKernelStruct, inputStreams);
     269/** ------------------------------------------------------------------------------------------------------------- *
     270 * @brief generateDoBlockCall
     271 ** ------------------------------------------------------------------------------------------------------------- */
     272void KernelBuilder::generateDoBlockCall(Value * inputStreams) {
     273    assert (mFunction && mKernelStruct);
     274    iBuilder->CreateCall2(mFunction, mKernelStruct, iBuilder->CreatePointerCast(inputStreams, mInputStreamType));
    215275}
    216276
     
    218278 * @brief offset
    219279 *
    220  * Compute the index of the given offset value.
    221  ** ------------------------------------------------------------------------------------------------------------- */
    222 Value * KernelBuilder::getOffset(const unsigned offset) {
    223     Value * index = iBuilder->getInt32(mSegmentIndex + offset);
     280 * Compute the stream index of the given offset value.
     281 ** ------------------------------------------------------------------------------------------------------------- */
     282Value * KernelBuilder::getOffset(const unsigned value) {
     283    const unsigned adjustedOffset = (mSegmentIndex + value);
     284    Value * offset = iBuilder->getInt32(adjustedOffset);
    224285    if (mStartIndex) {
    225         index = iBuilder->CreateAdd(iBuilder->CreateBlockAlignedLoad(getInternalState(mStartIndex)), index);
    226         const unsigned capacity = (mBlocksPerSegment + mCircularBufferModulo);
    227         if (isPowerOfTwo(capacity)) {
    228             index = iBuilder->CreateAnd(index, ConstantInt::get(index->getType(), capacity - 1));
     286        Value * index = iBuilder->CreateBlockAlignedLoad(getInternalState(mStartIndex));
     287        if (adjustedOffset) {
     288            index = iBuilder->CreateAdd(index, offset);
     289        }
     290        const unsigned bufferSize = (mBlocksPerSegment + mCircularBufferModulo - 1); assert (bufferSize > 1);
     291        if (isPowerOfTwo(bufferSize)) {
     292            index = iBuilder->CreateAnd(index, ConstantInt::get(index->getType(), bufferSize - 1));
    229293        } else {
    230             index = iBuilder->CreateURem(index, ConstantInt::get(index->getType(), capacity));
     294            index = iBuilder->CreateURem(index, ConstantInt::get(index->getType(), bufferSize));
    231295        }
    232296        // TODO: generate branch / phi node when it's sufficiently unlikely that we'll wrap around.
    233     }
    234     return index;
    235 }
    236 
     297        offset = index;
     298    }
     299    return offset;
     300}
     301
     302/** ------------------------------------------------------------------------------------------------------------- *
     303 * @brief setLongestLookaheadAmount
     304 ** ------------------------------------------------------------------------------------------------------------- */
     305void KernelBuilder::setLongestLookaheadAmount(const unsigned bits) {
     306    const unsigned blockWidth = iBuilder->getBitBlockWidth();
     307    const unsigned lookaheadBlocks = (bits + blockWidth - 1) / blockWidth;
     308    mCircularBufferModulo = (lookaheadBlocks + 1);
     309}
  • icGREP/icgrep-devel/icgrep/kernels/kernel.h

    r4959 r4968  
    3737    KernelBuilder(std::string name, llvm::Module * m, IDISA::IDISA_Builder * b);
    3838
    39     unsigned addInternalStateType(llvm::Type * type);
     39    unsigned addInternalStateType(llvm::Type * const type);
    4040    void addOutputStream(const unsigned fields);
    41     void addOutputAccum(llvm::Type * t);
     41    void addOutputAccum(llvm::Type * const type);
    4242    void addInputStream(const unsigned fields, std::string name);
    43     void addInputScalar(llvm::Type * t, std::string name);
     43    void addInputScalar(llvm::Type * const type, std::string name);
    4444
    4545    llvm::Function * prepareFunction();
     
    4949
    5050    llvm::Value * getInputStream(const unsigned index, const unsigned streamOffset = 0);
     51    llvm::Value * getInputScalar(const unsigned index);
    5152    llvm::Value * getKernelState(const unsigned index, const unsigned streamOffset = 0);
    5253    llvm::Value * getOutputStream(const unsigned index, const unsigned streamOffset = 0);
    53     llvm::Value * getOutputScalar(const unsigned index, const unsigned streamOffset = 0);
     54    llvm::Value * getOutputScalar(const unsigned index);
    5455
    5556    void finalize();
     
    6465    llvm::Value * getKernelStructParam() const;
    6566
    66     void setCircularBufferSize(const unsigned blocks);
     67    void setLongestLookaheadAmount(const unsigned bits);
    6768    void setBlocksPerSegment(const unsigned blocks);
    6869
     
    7273protected:
    7374
    74     llvm::Value * getOffset(const unsigned offset);
     75    llvm::Value * getOffset(const unsigned value);
    7576
    7677private:
     
    118119}
    119120
    120 inline void KernelBuilder::setCircularBufferSize(const unsigned blocks) {
    121     mCircularBufferModulo = blocks;
    122 }
    123 
    124121inline void KernelBuilder::setBlocksPerSegment(const unsigned blocks) {
    125122    mBlocksPerSegment = blocks;
  • icGREP/icgrep-devel/icgrep/kernels/pipeline.cpp

    r4960 r4968  
    9090    Value * scanMatchKernelStruct = mScanMatchKernel->generateKernelInstance();
    9191
     92
    9293    Value * gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileBufIdx)});
    93     Value* filebuf = iBuilder->CreateBitCast(input_param, S);
     94    Value * filebuf = iBuilder->CreateBitCast(input_param, S);
    9495    iBuilder->CreateStore(filebuf, gep);
     96
    9597    gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileSizeIdx)});
    9698    iBuilder->CreateStore(buffersize_param, gep);
     99
     100
    97101    gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileNameIdx)});
    98102    iBuilder->CreateStore(filename_param, gep);
  • icGREP/icgrep-devel/icgrep/kernels/symboltablepipeline.cpp

    r4959 r4968  
    3636, mFileSizeIdx(8)
    3737, mFileNameIdx(9)
     38, mLongestLookahead(0)
    3839, mBitBlockType(b->getBitBlockType())
    3940, mBlockSize(b->getBitBlockWidth()){
     
    8182
    8283/** ------------------------------------------------------------------------------------------------------------- *
    83  * @brief generateLookaheadFunction
     84 * @brief generateSortingFunction
    8485 ** ------------------------------------------------------------------------------------------------------------- */
    85 PabloFunction * SymbolTableBuilder::generateLookaheadFunction(const PabloFunction * const leading, const std::vector<unsigned> & endpoints) {
    86     PabloFunction * const function = PabloFunction::Create("lookahead", leading->getNumOfResults(), leading->getNumOfResults());
     86PabloFunction * SymbolTableBuilder::generateSortingFunction(const PabloFunction * const leading, const std::vector<unsigned> & endpoints) {
     87    PabloFunction * const function = PabloFunction::Create("sorting", leading->getNumOfResults(), leading->getNumOfResults() * 2);
    8788    PabloBlock * const entry = function->getEntryBlock();
    8889    function->setParameter(0, entry->createVar("S"));
     
    9192        function->setParameter(i, entry->createVar("M" + std::to_string(i - 2)));
    9293    }
    93     function->setResult(0, entry->createAssign("S", function->getParameter(0)));
    94     function->setResult(1, entry->createAssign("E", function->getParameter(1)));
     94    PabloAST * R = function->getParameter(0);
     95    PabloAST * const E = entry->createNot(function->getParameter(1));
    9596    unsigned i = 1;
    9697    unsigned lowerbound = 0;
     
    9899        PabloAST * const M = function->getParameter(i + 1);
    99100        PabloAST * const L = entry->createLookahead(M, endpoint);
    100         function->setResult(i + 1, entry->createAssign("L" + std::to_string(i), L));
     101        PabloAST * S = entry->createAnd(L, R);
     102        Assign * Si = entry->createAssign("S_" + std::to_string(i), S);
     103        R = entry->createXor(R, S);
     104        PabloAST * F = entry->createScanThru(R, E);
     105        Assign * Ei = entry->createAssign("E_" + std::to_string(i), F);
     106        function->setResult(i * 2, Si);
     107        function->setResult(i * 2 + 1, Ei);
    101108        ++i;
    102109        lowerbound = endpoint;
    103110    }
    104     return function;
    105 }
    106 
    107 /** ------------------------------------------------------------------------------------------------------------- *
    108  * @brief generateSortingFunction
    109  ** ------------------------------------------------------------------------------------------------------------- */
    110 PabloFunction * SymbolTableBuilder::generateSortingFunction(const PabloFunction * const lookahead) {
    111     PabloFunction * const function = PabloFunction::Create("sorting", lookahead->getNumOfResults(), 0);
    112     PabloBlock * const entry = function->getEntryBlock();
    113     function->setParameter(0, entry->createVar("S"));
    114     function->setParameter(1, entry->createVar("E"));
    115     for (unsigned i = 2; i < lookahead->getNumOfResults(); ++i) {
    116         function->setParameter(i, entry->createVar("L" + std::to_string(i - 1)));
    117     }
    118 
    119     PabloAST * R = function->getParameter(0);
    120     PabloAST * const E = entry->createNot(function->getParameter(1));
    121     for (unsigned i = 2; i < lookahead->getNumOfResults(); ++i) {
    122         PabloAST * const L = function->getParameter(i);
    123         PabloAST * S = entry->createAnd(L, R);
    124         R = entry->createXor(R, S);
    125         PabloBlock * const block = PabloBlock::Create(entry);
    126         PabloAST * F = block->createScanThru(R, E);
    127         block->createCall(Prototype::Create("length_group_" + std::to_string(i - 1), 2, 0, nullptr), std::vector<PabloAST *>{S, F});
    128         entry->createIf(S, {}, block);
    129     }
    130     PabloBlock * const block = PabloBlock::Create(entry);
    131     PabloAST * F = block->createScanThru(R, E);
    132     block->createCall(Prototype::Create("unknown_length_group", 2, 0, nullptr), std::vector<PabloAST *>{R, F});
    133     entry->createIf(R, {}, block);
    134 
     111    Assign * Si = entry->createAssign("S_n", R);
     112    PabloAST * F = entry->createScanThru(R, E);
     113    Assign * Ei = entry->createAssign("E_n", F);
     114    function->setResult(i * 2, Si);
     115    function->setResult(i * 2 + 1, Ei);
     116    mLongestLookahead = lowerbound;
    135117    return function;
    136118}
     
    148130    endpoints.push_back(16);
    149131
    150     mS2PKernel = new KernelBuilder("s2p", mMod, iBuilder);
    151     mLeadingKernel = new KernelBuilder("leading", mMod, iBuilder);
    152     mLookaheadKernel = new KernelBuilder("lookahead", mMod, iBuilder);
    153     mSortingKernel = new KernelBuilder("sorting", mMod, iBuilder);
    154 
    155     generateS2PKernel(mMod, iBuilder, mS2PKernel);
    156 
    157132    PabloCompiler pablo_compiler(mMod, iBuilder);
    158133
     
    163138    PabloPrinter::print(*leading, out);
    164139
    165 //    out << "\n\nLOOKAHEAD:\n";
    166 //    PabloFunction * const lookahead = generateLookaheadFunction(leading, endpoints);
    167 //    PabloPrinter::print(*lookahead, out);
    168 
    169 //    out << "\n\nSORTING:\n";
    170 //    PabloFunction * const sorting = generateSortingFunction(lookahead);
    171 //    PabloPrinter::print(*sorting, out);
     140    out << "\n\nSORTING:\n";
     141    PabloFunction * const sorting = generateSortingFunction(leading, endpoints);
     142    PabloPrinter::print(*sorting, out);
    172143
    173144    out.flush();
     145
     146    mS2PKernel = new KernelBuilder("s2p", mMod, iBuilder);
     147    mLeadingKernel = new KernelBuilder("leading", mMod, iBuilder);
     148    mSortingKernel = new KernelBuilder("sorting", mMod, iBuilder);
     149
     150    mLeadingKernel->setLongestLookaheadAmount(mLongestLookahead);
     151    mSortingKernel->setLongestLookaheadAmount(mLongestLookahead);
     152
     153    generateS2PKernel(mMod, iBuilder, mS2PKernel);
    174154
    175155    pablo_compiler.setKernel(mLeadingKernel);
    176156    pablo_compiler.compile(leading);
    177 //    pablo_compiler.setKernel(mLookaheadKernel);
    178 //    pablo_compiler.compile(lookahead);
    179 //    pablo_compiler.setKernel(mSortingKernel);
    180 //    pablo_compiler.compile(sorting);
     157    pablo_compiler.setKernel(mSortingKernel);
     158    pablo_compiler.compile(sorting);
    181159
    182160    delete leading;
    183 //    delete lookahead;
    184 //    delete sorting;
     161    delete sorting;
     162
    185163    releaseSlabAllocatorMemory();
    186 
    187164}
    188165
    189166void SymbolTableBuilder::ExecuteKernels(){
    190167
    191 //    Type * T = iBuilder->getIntNTy(64);
    192 //    Type * S = PointerType::get(iBuilder->getIntNTy(8), 0);
    193 //    Type * inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, 8)})), 1), 0);
    194 //    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", Type::getVoidTy(mMod->getContext()), inputType, T, S, T, nullptr));
    195 //    main->setCallingConv(CallingConv::C);
    196 //    Function::arg_iterator args = main->arg_begin();
    197 
    198 //    Value* input_param = args++;
    199 //    input_param->setName("input");
    200 //    Value* buffersize_param = args++;
    201 //    buffersize_param->setName("buffersize");
    202 //    Value* filename_param = args++;
    203 //    filename_param->setName("filename");
    204 //    Value* finalLineUnterminated_param = args++;
    205 //    finalLineUnterminated_param->setName("finalLineUnterminated");
    206 
    207 //    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
    208 
    209 //    BasicBlock * entry_block = iBuilder->GetInsertBlock();
     168    Type * T = iBuilder->getIntNTy(64);
     169    Type * S = PointerType::get(iBuilder->getIntNTy(8), 0);
     170    Type * inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, 8)})), 1), 0);
     171    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", Type::getVoidTy(mMod->getContext()), inputType, T, nullptr));
     172    main->setCallingConv(CallingConv::C);
     173    Function::arg_iterator args = main->arg_begin();
     174
     175    Value * const input_param = args++;
     176    input_param->setName("input");
     177
     178    Value * const bufferSize = args++;
     179    bufferSize->setName("buffersize");
     180
     181    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
     182
     183    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
     184
     185    BasicBlock * leadingTestBlock = BasicBlock::Create(mMod->getContext(), "ltb", main, 0);
     186    BasicBlock * leadingBodyBlock = BasicBlock::Create(mMod->getContext(), "lbb", main, 0);
     187    BasicBlock * leadingExitBlock = BasicBlock::Create(mMod->getContext(), "leb", main, 0);
     188
     189    BasicBlock * regularTestBlock = BasicBlock::Create(mMod->getContext(), "rtb", main, 0);
     190    BasicBlock * regularBodyBlock = BasicBlock::Create(mMod->getContext(), "rbb", main, 0);
     191    BasicBlock * regularExitBlock = BasicBlock::Create(mMod->getContext(), "reb", main, 0);
     192
    210193//    BasicBlock * pipeline_test_block = BasicBlock::Create(mMod->getContext(), "pipeline_test_block", main, 0);
    211194//    BasicBlock * pipeline_do_block = BasicBlock::Create(mMod->getContext(), "pipeline_do_block", main, 0);
     
    217200//    BasicBlock * pipeline_return_block = BasicBlock::Create(mMod->getContext(), "pipeline_return_block", main, 0);
    218201
    219 //    Value * s2pKernelStruct = mS2PKernel->generateKernelInstance();
    220 //    Value * icGrepKernelStruct = mICgrepKernel->generateKernelInstance();
    221 //    Value * scanMatchKernelStruct = mScanMatchKernel->generateKernelInstance();
    222 
    223 //    Value * gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileBufIdx)});
     202    Value * s2pKernelStruct = mS2PKernel->generateKernelInstance();
     203    Value * leadingKernelStruct = mLeadingKernel->generateKernelInstance();
     204    Value * sortingKernelStruct = mSortingKernel->generateKernelInstance();
     205
     206    Value * basis_bits = iBuilder->CreateGEP(s2pKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
     207    Value * leadingData = iBuilder->CreateGEP(leadingKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
     208
     209
     210    const unsigned leadingBlocks = (mLongestLookahead + iBuilder->getBitBlockWidth() - 1) / iBuilder->getBitBlockWidth();
     211
     212    // If the buffer size is smaller than our largest length group, only check up to the buffer size.
     213    Value * safetyCheck = iBuilder->CreateICmpSLT(bufferSize, iBuilder->getInt64(leadingBlocks * iBuilder->getBitBlockWidth()));
     214    iBuilder->CreateCondBr(safetyCheck, regularExitBlock, leadingTestBlock);
     215
     216    // Now process the leading blocks ...
     217    iBuilder->SetInsertPoint(leadingTestBlock);
     218    PHINode * remainingBytes = iBuilder->CreatePHI(T, 2, "remainingBytes");
     219    PHINode * leadingOffset = iBuilder->CreatePHI(T, 2, "blockIndex");
     220    remainingBytes->addIncoming(bufferSize, entryBlock);
     221    leadingOffset->addIncoming(iBuilder->getInt64(0), entryBlock);
     222    Value * remainingLeadingBlocksCond = iBuilder->CreateICmpULT(leadingOffset, iBuilder->getInt64(leadingBlocks));
     223    iBuilder->CreateCondBr(remainingLeadingBlocksCond, leadingBodyBlock, leadingExitBlock);
     224    iBuilder->SetInsertPoint(leadingBodyBlock);
     225    Value * gep = iBuilder->CreateGEP(input_param, leadingOffset);
     226    mS2PKernel->generateDoBlockCall(gep);
     227    mLeadingKernel->generateDoBlockCall(basis_bits);
     228    leadingOffset->addIncoming(iBuilder->CreateAdd(leadingOffset, iBuilder->getInt64(1)), leadingBodyBlock);
     229    remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, iBuilder->getInt64(mBlockSize)), leadingBodyBlock);
     230    iBuilder->CreateBr(leadingTestBlock);
     231    iBuilder->SetInsertPoint(leadingExitBlock);
     232
     233    // Now process the leading blocks ...
     234    iBuilder->CreateBr(regularTestBlock);
     235    iBuilder->SetInsertPoint(regularTestBlock);
     236    PHINode * remainingBytes2 = iBuilder->CreatePHI(T, 2, "remainingBytes");
     237    PHINode * leadingOffset2 = iBuilder->CreatePHI(T, 2, "blockIndex");
     238    remainingBytes2->addIncoming(remainingBytes, leadingExitBlock);
     239    leadingOffset2->addIncoming(leadingOffset, leadingExitBlock);
     240    Value * remainingBytesCond = iBuilder->CreateICmpUGE(remainingBytes2, iBuilder->getInt64(mBlockSize));
     241    iBuilder->CreateCondBr(remainingBytesCond, regularBodyBlock, regularExitBlock);
     242    iBuilder->SetInsertPoint(regularBodyBlock);
     243    Value * gep2 = iBuilder->CreateGEP(input_param, leadingOffset2);
     244    mS2PKernel->generateDoBlockCall(gep2);
     245    mLeadingKernel->generateDoBlockCall(basis_bits);
     246    leadingOffset2->addIncoming(iBuilder->CreateAdd(leadingOffset2, iBuilder->getInt64(1)), regularBodyBlock);
     247    remainingBytes2->addIncoming(iBuilder->CreateSub(remainingBytes2, iBuilder->getInt64(mBlockSize)), regularBodyBlock);
     248    mSortingKernel->generateDoBlockCall(leadingData);
     249    iBuilder->CreateBr(regularTestBlock);
     250    iBuilder->SetInsertPoint(regularExitBlock);
     251
     252
     253
     254//    Value * gep = iBuilder->CreateGEP(sortingKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileBufIdx)});
    224255//    Value* filebuf = iBuilder->CreateBitCast(input_param, S);
    225256//    iBuilder->CreateStore(filebuf, gep);
    226257
    227 //    gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileSizeIdx)});
     258//    gep = iBuilder->CreateGEP(sortingKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileSizeIdx)});
    228259//    iBuilder->CreateStore(buffersize_param, gep);
    229260
    230 //    gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileNameIdx)});
     261//    gep = iBuilder->CreateGEP(sortingKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileNameIdx)});
    231262//    iBuilder->CreateStore(filename_param, gep);
    232263
    233264//    Value * basis_bits = iBuilder->CreateGEP(s2pKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
    234 //    Value * results = iBuilder->CreateGEP(icGrepKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
     265//    Value * results = iBuilder->CreateGEP(leadingKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
    235266
    236267//    iBuilder->CreateBr(pipeline_test_block);
     
    301332//    mICgrepKernel->generateDoBlockCall(basis_bits);
    302333//    mScanMatchKernel->generateDoBlockCall(results);
    303 //    iBuilder->CreateRetVoid();
    304 
    305 }
     334    iBuilder->CreateRetVoid();
     335
     336
     337    mMod->dump();
     338}
     339
     340SymbolTableBuilder::~SymbolTableBuilder() {
     341    delete mS2PKernel;
     342    delete mLeadingKernel;
     343    delete mSortingKernel;
     344}
  • icGREP/icgrep-devel/icgrep/kernels/symboltablepipeline.h

    r4959 r4968  
    3232public:
    3333    SymbolTableBuilder(Module * m, IDISA::IDISA_Builder * b);
     34    ~SymbolTableBuilder();
    3435    void createKernels();
    3536    void ExecuteKernels();
     
    3738
    3839    pablo::PabloFunction * generateLeadingFunction(const std::vector<unsigned> & endpoints);
    39     pablo::PabloFunction * generateLookaheadFunction(const pablo::PabloFunction * const leading, const std::vector<unsigned> & endpoints);
    40     pablo::PabloFunction * generateSortingFunction(const pablo::PabloFunction * const lookahead);
     40    pablo::PabloFunction * generateSortingFunction(const pablo::PabloFunction * const leading, const std::vector<unsigned> & endpoints);
     41
     42    void generateLLVMParser();
    4143
    4244private:
     
    4547    KernelBuilder *                     mS2PKernel;
    4648    KernelBuilder *                     mLeadingKernel;
    47     KernelBuilder *                     mLookaheadKernel;
    4849    KernelBuilder *                     mSortingKernel;
     50    unsigned                            mLongestLookahead;
    4951    int                                 mFileBufIdx;
    5052    int                                 mFileSizeIdx;
Note: See TracChangeset for help on using the changeset viewer.