Changeset 4670 for icGREP


Ignore:
Timestamp:
Jul 14, 2015, 6:29:35 PM (4 years ago)
Author:
cameron
Message:

Carry Manager progress

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

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/pablo/carry_data.cpp

    r4595 r4670  
    1414namespace pablo {
    1515 
     16   
     17const unsigned bitsPerPack = 64;
     18
     19
     20void AlignUpwards(unsigned & toAlign, unsigned alignment) {
     21    if ((toAlign & (alignment - 1)) != 0) {
     22        toAlign = (toAlign + alignment) & (alignment - 1);
     23    }
     24}
     25
     26void EnsurePackHasSpace(unsigned & packedTotalBits, unsigned addedBits) {
     27    unsigned bitsInCurrentPack = packedTotalBits % bitsPerPack;
     28    if (bitsInCurrentPack + addedBits > bitsPerPack) {
     29        AlignUpwards(packedTotalBits, bitsPerPack);
     30    }
     31}
     32   
     33   
    1634unsigned PabloBlockCarryData::enumerate(PabloBlock & blk) {
    1735    for (Statement * stmt : blk) {
     
    1937            unsigned shift_amount = adv->getAdvanceAmount();
    2038            if (shift_amount == 1) {
    21                 adv->setLocalAdvanceIndex(unitAdvances);
    22                 unitAdvances++;               
     39                adv->setLocalAdvanceIndex(advance1.entries);
     40                advance1.entries++;               
    2341            }
    2442            else if (shift_amount < LongAdvanceBase) {
    25                 //EnsurePackHasSpace(shortAdvanceTotal, shift_amount);
    26                 //adv->setLocalAdvanceIndex(shortAdvanceTotal);
    27                 adv->setLocalAdvanceIndex(shortAdvances);
    28                 shortAdvances++;
    29                 shortAdvanceTotal += shift_amount;
     43#ifdef PACKING
     44                EnsurePackHasSpace(shortAdvance.allocatedBits, shift_amount);
     45                adv->setLocalAdvanceIndex(shortAdvance.allocatedBits);
     46#else
     47                adv->setLocalAdvanceIndex(shortAdvance.entries);
     48#endif
     49                shortAdvance.entries++;
     50                shortAdvance.allocatedBits += shift_amount;
    3051            }
    3152            else {
    32                 adv->setLocalAdvanceIndex(longAdvanceTotalBlocks);
    33                 longAdvances++;
    34                 longAdvanceTotalBlocks += longAdvanceBufferSize(shift_amount);
     53                adv->setLocalAdvanceIndex(longAdvance.allocatedBitBlocks);
     54                longAdvance.entries++;
     55                longAdvance.allocatedBitBlocks += longAdvanceBufferSize(shift_amount);
    3556            }
    3657        }
    3758        else if (MatchStar * m = dyn_cast<MatchStar>(stmt)) {
    38             m->setLocalCarryIndex(localCarries);
    39             ++localCarries;
     59            m->setLocalCarryIndex(addWithCarry.entries);
     60            ++addWithCarry.entries;
    4061        }
    4162        else if (ScanThru * s = dyn_cast<ScanThru>(stmt)) {
    42             s->setLocalCarryIndex(localCarries);
    43             ++localCarries;
     63            s->setLocalCarryIndex(addWithCarry.entries);
     64            ++addWithCarry.entries;
    4465        }
    4566    }
    46     unsigned localCarryDataIndex = localCarries + unitAdvances + shortAdvances + longAdvanceTotalBlocks;
    47     localCarryDataSize = localCarryDataIndex;
    48     /*
    49     totalCarryDataSize = longAdvanceTotalBlocks * BLOCK_SIZE;
    50     totalCarryDataSize += shortAdvanceTotal;
    51     EnsurePackHasSpace(totalCarryDataSize, localCarries);
    52     totalCarryDataSize += localCarries;
    53     EnsurePackHasSpace(totalCarryDataSize, unitAdvances);
    54     totalCarryDataSize += unitAdvances;
    55      */
     67    longAdvance.frameOffsetinBits = 0;
     68#ifdef PACKING
     69    shortAdvance.frameOffsetinBits = longAdvance.frameOffsetinBits + longAdvance.allocatedBitBlocks * BLOCK_SIZE;
     70    addWithCarry.frameOffsetinBits = shortAdvance.frameOffsetinBits + shortAdvance.allocatedBits;
     71    EnsurePackHasSpace(addWithCarry.frameOffsetinBits, addWithCarry.entries);
     72    advance1.frameOffsetinBits = addWithCarry.frameOffsetinBits + addWithCarry.entries;
     73    EnsurePackHasSpace(advance1.frameOffsetinBits, advance1.entries);
     74    nested.frameOffsetinBits = advance1.frameOffsetinBits + advance1.entries;
     75#else
     76    addWithCarry.frameOffsetinBits = longAdvance.frameOffsetinBits + longAdvance.allocatedBitBlocks * BLOCK_SIZE;
     77    advance1.frameOffsetinBits = addWithCarry.frameOffsetinBits + addWithCarry.entries * BLOCK_SIZE;
     78    shortAdvance.frameOffsetinBits = advance1.frameOffsetinBits + advance1.entries * BLOCK_SIZE;
     79    nested.frameOffsetinBits = shortAdvance.frameOffsetinBits + shortAdvance.entries * BLOCK_SIZE;
     80#endif
     81    unsigned nestedframePosition = nested.frameOffsetinBits;
    5682   
    5783    for (Statement * stmt : blk) {
    5884        if (If * ifStatement = dyn_cast<If>(stmt)) {
    5985            PabloBlockCarryData & nestedBlockData = ifStatement->getBody().carryData;
    60             nestedBlockData.setIfDepth(ifDepth + 1);
    61             nestedBlockData.setBlockCarryDataIndex(blockCarryDataIndex + localCarryDataIndex);
    62             const unsigned ifCarryDataSize = nestedBlockData.enumerate(ifStatement->getBody());
    63             nestedBlockCount++;
    64             //EnsurePackHasSpace(totalCarryDataSize, ifCarryDataSize);
    65             nestedCarryDataSize += ifCarryDataSize;
    66             localCarryDataIndex += ifCarryDataSize;           
     86            nestedBlockData.ifDepth = ifDepth + 1;
     87            const unsigned ifCarryDataBits = nestedBlockData.enumerate(ifStatement->getBody());
     88#ifdef PACKING
     89            EnsurePackHasSpace(nestedframePosition, ifCarryDataBits);
     90#endif
     91            nestedBlockData.framePosition = nestedframePosition;
     92            nestedframePosition += ifCarryDataBits;
     93            if (maxNestingDepth <= nestedBlockData.maxNestingDepth) maxNestingDepth = nestedBlockData.maxNestingDepth + 1;
     94            nested.entries++;
    6795        }
    6896        else if (While * whileStatement = dyn_cast<While>(stmt)) {
    6997            PabloBlockCarryData & nestedBlockData = whileStatement->getBody().carryData;
    70             nestedBlockData.setWhileDepth(whileDepth + 1);
    71             nestedBlockData.setBlockCarryDataIndex(blockCarryDataIndex + localCarryDataIndex);
    72             unsigned whileCarryDataSize = nestedBlockData.enumerate(whileStatement->getBody());
    73             //if (whileStatement->isMultiCarry()) whileCarryDataSize *= whileStatement->getMaxIterations();
    74             nestedBlockCount++;
    75             //EnsurePackHasSpace(totalCarryDataSize, whileCarryDataSize);
    76             nestedCarryDataSize += whileCarryDataSize;
    77             localCarryDataIndex += whileCarryDataSize;
     98            nestedBlockData.whileDepth = whileDepth + 1;
     99            unsigned whileCarryDataBits = nestedBlockData.enumerate(whileStatement->getBody());
     100            //if (whileStatement->isMultiCarry()) whileCarryDataBits *= whileStatement->getMaxIterations();
     101#ifdef PACKING
     102            EnsurePackHasSpace(nestedframePosition, whileCarryDataBits);
     103#endif
     104            nestedBlockData.framePosition = nestedframePosition;
     105            nestedframePosition += whileCarryDataBits;
     106            if (maxNestingDepth <= nestedBlockData.maxNestingDepth) maxNestingDepth = nestedBlockData.maxNestingDepth + 1;
     107            nested.entries++;
    78108        }
    79109    }
    80     totalCarryDataSize = localCarryDataIndex;
    81     if ((ifDepth > 0) && (totalCarryDataSize > 1)) {
     110   
     111   
     112    totalCarryDataBits = nestedframePosition;
     113   
     114    if ((ifDepth > 0) && (totalCarryDataBits > BLOCK_SIZE)) {
    82115        // Need extra space for the summary variable, always the last
    83116        // entry within an if block.
    84         totalCarryDataSize += 1;
     117        AlignUpwards(totalCarryDataBits, BLOCK_SIZE);
     118        summary.frameOffsetinBits = totalCarryDataBits;
     119        summary.allocatedBits = BLOCK_SIZE;
     120        totalCarryDataBits += BLOCK_SIZE;
    85121    }
    86     longAdvanceOffset = 0;
    87     /*
    88     if (totalCarryDataSize > CarryPackSize) {
    89         // Need extra space for the summary variable, always the first
    90         // entry within an if block.
    91         totalCarryDataSize += BLOCK_SIZE;
    92         longAdvanceOffset = BLOCK_SIZE;
     122    else {
     123        summary.frameOffsetinBits = 0;
     124        summary.allocatedBits = totalCarryDataBits;
    93125    }
    94      */
    95     carryOffset = longAdvanceOffset + longAdvanceTotalBlocks;
    96     unitAdvanceOffset = carryOffset + localCarries;
    97     shortAdvanceOffset = unitAdvanceOffset + unitAdvances;
    98 
    99 //     std::cerr << "blockCarryDataIndex = " << blockCarryDataIndex << " nestedBlockCount = " << nestedBlockCount << std::endl;
    100 //     std::cerr << "longAdvanceOffset = " << longAdvanceOffset << " carryOffset = " << carryOffset << std::endl;
    101 //     std::cerr << "unitAdvanceOffset = " << unitAdvanceOffset << " shortAdvanceOffset = " << shortAdvanceOffset << std::endl;
    102 //     std::cerr << "ifDepth = " << ifDepth << " whileDepth = " << whileDepth << std::endl;
    103 //     std::cerr << "totalCarryDataSize = " << totalCarryDataSize << std::endl;
    104     return totalCarryDataSize;
     126    return totalCarryDataBits;
    105127}
    106128
  • icGREP/icgrep-devel/icgrep/pablo/carry_data.h

    r4644 r4670  
    3333class PabloBlockCarryData {
    3434public:
    35     PabloBlockCarryData(): blockCarryDataIndex(0),
    36                            ifDepth(0), whileDepth (0),
    37                            localCarries(0), unitAdvances(0),
    38                            shortAdvances(0), shortAdvanceTotal(0),
    39                            longAdvances(0), longAdvanceTotalBlocks(0),
    40                            nestedBlockCount(0), nestedCarryDataSize(0),
    41                            localCarryDataSize(0), totalCarryDataSize(0),
    42                            carryOffset(0), unitAdvanceOffset(0),
    43                            shortAdvanceOffset(0), longAdvanceOffset(0)
     35    PabloBlockCarryData(): framePosition(0),
     36                           ifDepth(0), whileDepth (0), maxNestingDepth(0),
     37                           longAdvance({0, 0, 0}),
     38                           shortAdvance({0, 0, 0}),
     39                           advance1({0, 0}),
     40                           addWithCarry({0, 0}),
     41                           nested({0, 0, 0}),
     42                           summary({0, 0}),
     43                           totalCarryDataBits(0)
    4444                           {}
    45    
    46    
    47     void setBlockCarryDataIndex (unsigned idx) {blockCarryDataIndex = idx;}
    48    
    49     void setIfDepth (unsigned depth) {ifDepth = depth;}
    50    
    51     void setWhileDepth (unsigned depth) {whileDepth = depth;}
    52    
     45       
    5346    unsigned enumerate(PabloBlock & p);
    5447   
    5548    unsigned getBlockCarryDataIndex()  const {
    56         return blockCarryDataIndex;
     49        return framePosition/BLOCK_SIZE;
    5750    }
    5851   
     
    6457        return whileDepth;
    6558    }
    66    
     59       
    6760    unsigned longAdvanceCarryDataOffset(unsigned advanceIndex)  const {
    68         return blockCarryDataIndex + longAdvanceOffset + advanceIndex;
     61        return longAdvance.frameOffsetinBits / BLOCK_SIZE + advanceIndex;
    6962    }
    7063   
     
    7770    }
    7871   
    79     bool blockHasLongAdvances() const { return longAdvances > 0;}
     72    bool blockHasLongAdvances() const { return longAdvance.entries > 0;}
    8073   
    8174    unsigned shortAdvanceCarryDataOffset(unsigned advanceIndex)  const {
    82         return blockCarryDataIndex + shortAdvanceOffset + advanceIndex;
     75        return shortAdvance.frameOffsetinBits / BLOCK_SIZE + advanceIndex;
    8376    }
    8477   
    8578    unsigned unitAdvanceCarryDataOffset(unsigned advanceIndex)  const {
    86         return blockCarryDataIndex + unitAdvanceOffset + advanceIndex;
     79        return advance1.frameOffsetinBits / BLOCK_SIZE + advanceIndex;
    8780    }
    8881   
    8982    unsigned carryOpCarryDataOffset(unsigned idx)  const {
    90         return blockCarryDataIndex + carryOffset + idx;
     83        return addWithCarry.frameOffsetinBits / BLOCK_SIZE + idx;
    9184    }
    9285   
    93     bool blockHasCarries() const { return totalCarryDataSize > 0;}
     86    unsigned summaryCarryDataIndex()  const {
     87        return summary.frameOffsetinBits / BLOCK_SIZE;
     88    }
    9489   
    95     bool explicitSummaryRequired() const { return totalCarryDataSize > 1;}
     90    unsigned getLocalCarryDataSize () { return nested.frameOffsetinBits / BLOCK_SIZE; }
     91
     92    unsigned getTotalCarryDataSize () { return totalCarryDataBits / BLOCK_SIZE; }
     93   
     94    bool blockHasCarries() const { return totalCarryDataBits > 0;}
     95   
     96    bool explicitSummaryRequired() const { return totalCarryDataBits > BLOCK_SIZE;}
    9697   
    9798    bool summaryNeededInParentBlock() const {return (ifDepth > 0) && blockHasCarries();}
    9899   
    99     unsigned summaryCarryDataIndex()  const {
    100         return blockCarryDataIndex + totalCarryDataSize - 1;
    101     }
     100private:
    102101   
    103     unsigned getLocalCarryDataSize()  const {
    104         return localCarryDataSize;
    105     }
     102    unsigned framePosition;
    106103   
    107     unsigned getTotalCarryDataSize()  const {
    108         return totalCarryDataSize;
    109     }
    110    
    111 private:
    112     unsigned blockCarryDataIndex;
    113104    unsigned ifDepth;
    114105    unsigned whileDepth;
    115     unsigned localCarries;
    116     unsigned unitAdvances;
    117     unsigned shortAdvances;
    118     unsigned shortAdvanceTotal;
    119     unsigned longAdvances;
    120     unsigned longAdvanceTotalBlocks;
    121     unsigned nestedBlockCount;
    122     unsigned nestedCarryDataSize;
    123     unsigned localCarryDataSize;
    124     unsigned totalCarryDataSize;
    125     unsigned carryOffset;
    126     unsigned unitAdvanceOffset;
    127     unsigned shortAdvanceOffset;
    128     unsigned longAdvanceOffset;
     106    unsigned maxNestingDepth;   
     107   
     108    struct {unsigned frameOffsetinBits; unsigned entries; unsigned allocatedBitBlocks;} longAdvance;
     109    struct {unsigned frameOffsetinBits; unsigned entries; unsigned allocatedBits;} shortAdvance;
     110    struct {unsigned frameOffsetinBits; unsigned entries;} advance1;
     111    struct {unsigned frameOffsetinBits; unsigned entries;} addWithCarry;
     112    struct {unsigned frameOffsetinBits; unsigned entries; unsigned allocatedBits;} nested;
     113    struct {unsigned frameOffsetinBits; unsigned allocatedBits;} summary;
     114
     115    unsigned totalCarryDataBits;
    129116   
    130117};
  • icGREP/icgrep-devel/icgrep/pablo/carry_manager.cpp

    r4663 r4670  
    2020 
    2121    mPabloRoot = pb;
     22    mCurrentScope = pb;
     23    mCurrentScopeIndex = 0;
     24   
    2225    mCarryDataPtr = carryPtr;
    23    
    24     PabloBlockCarryData & cd = pb->carryData;
    25     mTotalCarryDataSize = cd.enumerate(*pb) + 1;   // One extra element for the block no.
     26    mCarryInfo = &(pb->carryData);
     27    mTotalCarryDataSize = mCarryInfo->enumerate(*pb)/BLOCK_SIZE + 1;   // One extra element for the block no.
    2628    mBlockNoPtr = mBuilder->CreateBitCast(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(mTotalCarryDataSize - 1)), Type::getInt64PtrTy(mBuilder->getContext()));
    2729    mBlockNo = mBuilder->CreateLoad(mBlockNoPtr);
     
    4244}
    4345
     46/* Entering and leaving blocks. */
     47
     48void CarryManager::enterScope(PabloBlock * blk) {
     49   
     50    mCurrentScope = blk;
     51    mCarryInfo = & (blk->carryData);
     52    mCurrentScopeIndex += mCarryInfo->getBlockCarryDataIndex();
     53    //std::cerr << "enterScope:  mCurrentScopeIndex = " << mCurrentScopeIndex << std::endl;
     54}
     55
     56void CarryManager::leaveScope() {
     57    mCurrentScopeIndex -= mCarryInfo->getBlockCarryDataIndex();
     58    mCurrentScope = mCurrentScope->getParent();
     59    mCarryInfo = & (mCurrentScope->carryData);
     60    //std::cerr << "leaveScope:  mCurrentScopeIndex = " << mCurrentScopeIndex << std::endl;
     61}
     62
     63
    4464
    4565    /* Methods for getting and setting individual carry values. */
    4666   
    47 Value * CarryManager::getCarryOpCarryIn(PabloBlock * blk, int localIndex) {
    48     PabloBlockCarryData & cd = blk->carryData;
    49     if (cd.getWhileDepth() == 0) {
    50        Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(cd.carryOpCarryDataOffset(localIndex)));
    51        mCarryInVector[cd.carryOpCarryDataOffset(localIndex)] = mBuilder->CreateAlignedLoad(packPtr, BLOCK_SIZE/8);
    52     }
    53     return mCarryInVector[cd.carryOpCarryDataOffset(localIndex)];
    54 }
    55 
    56 void CarryManager::setCarryOpCarryOut(PabloBlock * blk, unsigned idx, Value * carry_out) {
    57     PabloBlockCarryData & cd = blk->carryData;
    58     mCarryOutVector[cd.carryOpCarryDataOffset(idx)] = carry_out;
    59     if (cd.getWhileDepth() == 0) {
    60        Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(cd.carryOpCarryDataOffset(idx)));
     67Value * CarryManager::getCarryOpCarryIn(int localIndex) {
     68    if (mCarryInfo->getWhileDepth() == 0) {
     69       Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(mCurrentScopeIndex + mCarryInfo->carryOpCarryDataOffset(localIndex)));
     70       mCarryInVector[mCarryInfo->carryOpCarryDataOffset(localIndex)] = mBuilder->CreateAlignedLoad(packPtr, BLOCK_SIZE/8);
     71    }
     72    return mCarryInVector[mCarryInfo->carryOpCarryDataOffset(localIndex)];
     73}
     74
     75void CarryManager::setCarryOpCarryOut(unsigned localIndex, Value * carry_out) {
     76    unsigned cd_index = mCurrentScopeIndex + mCarryInfo->carryOpCarryDataOffset(localIndex);
     77    mCarryOutVector[cd_index] = carry_out;
     78    if (mCarryInfo->getWhileDepth() == 0) {
     79       Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(cd_index));
    6180       mBuilder->CreateAlignedStore(carry_out, packPtr, BLOCK_SIZE/8);
    6281    }
     
    6483
    6584   
    66 Value * CarryManager::advanceCarryInCarryOut(PabloBlock * blk, int localIndex, int shift_amount, Value * strm) {
     85Value * CarryManager::advanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm) {
    6786    if (shift_amount == 1) {
    68         return unitAdvanceCarryInCarryOut(blk, localIndex, strm);
     87        return unitAdvanceCarryInCarryOut(localIndex, strm);
    6988    }
    7089    else if (shift_amount < LongAdvanceBase) {
    71         return shortAdvanceCarryInCarryOut(blk, localIndex, shift_amount, strm);
     90        return shortAdvanceCarryInCarryOut(localIndex, shift_amount, strm);
    7291    }
    7392    else {
    74         return longAdvanceCarryInCarryOut(blk, localIndex, shift_amount, strm);
    75     }
    76 }
    77 
    78 Value * CarryManager::unitAdvanceCarryInCarryOut(PabloBlock * blk, int localIndex, Value * strm) {
    79     PabloBlockCarryData & cd = blk->carryData;
    80     unsigned carryDataIndex = cd.unitAdvanceCarryDataOffset(localIndex);
     93        return longAdvanceCarryInCarryOut(localIndex, shift_amount, strm);
     94    }
     95}
     96
     97Value * CarryManager::unitAdvanceCarryInCarryOut(int localIndex, Value * strm) {
     98    unsigned carryDataIndex = mCurrentScopeIndex + mCarryInfo->unitAdvanceCarryDataOffset(localIndex);
    8199    mCarryOutVector[carryDataIndex] = strm;
    82     if (cd.getWhileDepth() == 0) {
     100    if (mCarryInfo->getWhileDepth() == 0) {
    83101        Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(carryDataIndex));
    84102        mCarryInVector[carryDataIndex] = mBuilder->CreateAlignedLoad(packPtr, BLOCK_SIZE/8);
     
    102120}
    103121
    104 Value * CarryManager::shortAdvanceCarryInCarryOut(PabloBlock * blk, int localIndex, int shift_amount, Value * strm) {
    105     PabloBlockCarryData & cd = blk->carryData;
    106     unsigned carryDataIndex = cd.shortAdvanceCarryDataOffset(localIndex);
     122Value * CarryManager::shortAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm) {
     123    unsigned carryDataIndex = mCurrentScopeIndex + mCarryInfo->shortAdvanceCarryDataOffset(localIndex);
    107124    mCarryOutVector[carryDataIndex] = strm;
    108     if (cd.getWhileDepth() == 0) {
     125    if (mCarryInfo->getWhileDepth() == 0) {
    109126        Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(carryDataIndex));
    110127        mCarryInVector[carryDataIndex] = mBuilder->CreateAlignedLoad(packPtr, BLOCK_SIZE/8);
     
    138155
    139156   
    140 Value * CarryManager::longAdvanceCarryInCarryOut(PabloBlock * blk, int localIndex, int shift_amount, Value * carry_out) {
    141     PabloBlockCarryData & cd = blk->carryData;
    142     Value * advBaseIndex = mBuilder->getInt64(cd.longAdvanceCarryDataOffset(localIndex));
     157Value * CarryManager::longAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * carry_out) {
     158    unsigned carryDataIndex = mCurrentScopeIndex + mCarryInfo->longAdvanceCarryDataOffset(localIndex);
     159    Value * advBaseIndex = mBuilder->getInt64(carryDataIndex);
    143160    if (shift_amount <= BLOCK_SIZE) {
    144161        // special case using a single buffer entry and the carry_out value.
     
    154171    // We need a buffer of at least two elements for storing the advance data.
    155172    const unsigned block_shift = shift_amount % BLOCK_SIZE;
    156     const unsigned advanceEntries = cd.longAdvanceEntries(shift_amount);
    157     const unsigned bufsize = cd.longAdvanceBufferSize(shift_amount);
     173    const unsigned advanceEntries = mCarryInfo->longAdvanceEntries(shift_amount);
     174    const unsigned bufsize = mCarryInfo->longAdvanceBufferSize(shift_amount);
    158175    Value * indexMask = mBuilder->getInt64(bufsize - 1);  // A mask to implement circular buffer indexing
    159176    Value * loadIndex0 = mBuilder->CreateAdd(mBuilder->CreateAnd(mBuilder->CreateSub(mBlockNo, mBuilder->getInt64(advanceEntries)), indexMask), advBaseIndex);
     
    178195/* Methods for getting and setting carry summary values */
    179196   
    180 bool CarryManager::blockHasCarries(PabloBlock & blk){
    181     PabloBlockCarryData & cd = blk.carryData;
    182     return cd.getTotalCarryDataSize() > 0;
     197bool CarryManager::blockHasCarries(){
     198    return mCarryInfo->blockHasCarries();
    183199}
    184200
    185 Value * CarryManager::getCarrySummaryExpr(PabloBlock & blk) {
    186     PabloBlockCarryData & cd = blk.carryData;
    187     Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(cd.summaryCarryDataIndex()));
     201
     202Value * CarryManager::getCarrySummaryExpr() {
     203    unsigned summary_idx = mCurrentScopeIndex + mCarryInfo->summaryCarryDataIndex();
     204    Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(summary_idx));
    188205    Value * summary_expr = mBuilder->CreateAlignedLoad(packPtr, BLOCK_SIZE/8);
    189206    // If the totalCarryDataSize is 1, then the carry summary is also the pack expr.
    190     if (cd.getTotalCarryDataSize() == 1) {
    191         mCarryInVector[cd.summaryCarryDataIndex()] = summary_expr;
    192     }
     207    mCarryInVector[summary_idx] = summary_expr;
    193208    return summary_expr;
    194209}
    195210
    196 bool CarryManager::summaryNeededInParentBlock(PabloBlock & blk){
    197     PabloBlockCarryData & cd = blk.carryData;
    198     return (cd.getIfDepth() > 0) && (cd.getTotalCarryDataSize() > 0);
     211bool CarryManager::summaryNeededInParentBlock(){
     212    return mCarryInfo->summaryNeededInParentBlock();
    199213}
    200214
    201 void CarryManager::addSummaryPhi(PabloBlock & blk, BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock) {
    202     const PabloBlockCarryData & cd = blk.carryData;
    203     const unsigned carrySummaryIndex = cd.summaryCarryDataIndex();
     215void CarryManager::addSummaryPhi(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock) {
     216    const unsigned carrySummaryIndex = mCurrentScopeIndex + mCarryInfo->summaryCarryDataIndex();
    204217    PHINode * summary_phi = mBuilder->CreatePHI(mBitBlockType, 2, "summary");
    205218    summary_phi->addIncoming(mZeroInitializer, ifEntryBlock);
     
    208221}
    209222
    210 void CarryManager::generateCarryOutSummaryCode(PabloBlock & blk) {
    211    
    212     const PabloBlockCarryData & cd = blk.carryData;
    213     const unsigned baseCarryDataIdx = cd.getBlockCarryDataIndex();
    214     const unsigned carrySummaryIndex = cd.summaryCarryDataIndex();
    215    
    216     if (cd.getTotalCarryDataSize() == 1) {
     223void CarryManager::generateCarryOutSummaryCode() {
     224   
     225    const unsigned carrySummaryIndex = mCurrentScopeIndex + mCarryInfo->summaryCarryDataIndex();
     226   
     227    if (mCarryInfo->getTotalCarryDataSize() == 1) {
    217228        // If totalCarryDataSize == 1, then we have one pack which serves as
    218229        // the summary.   It should already be stored.   
     
    222233    Value * carry_summary = mZeroInitializer;
    223234   
    224     if (cd.blockHasLongAdvances() > 0) { // Force if entry
     235    if (mCarryInfo->blockHasLongAdvances()) { // Force if entry
    225236        carry_summary = mOneInitializer;
    226237    }
    227238    else {
    228         auto localCarryPacks = cd.getLocalCarryDataSize();
     239        auto localCarryPacks = mCarryInfo->getLocalCarryDataSize();
    229240        if (localCarryPacks > 0) {
    230             carry_summary = mCarryOutVector[baseCarryDataIdx];
     241            carry_summary = mCarryOutVector[mCurrentScopeIndex];
    231242            for (auto i = 1; i < localCarryPacks; i++) {
    232243                //carry_summary = mBuilder->CreateOr(carry_summary, mPabloBlock->mCarryOutPack[i]);           
    233                 carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutVector[baseCarryDataIdx+i]);
     244                carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutVector[mCurrentScopeIndex+i]);
    234245            }
    235246        }
    236         for (Statement * stmt : blk) {
     247        for (Statement * stmt : *mCurrentScope) {
    237248            if (If * innerIf = dyn_cast<If>(stmt)) {
    238                 PabloBlock & inner_blk = innerIf->getBody();
    239                 if (inner_blk.carryData.blockHasCarries()) {
    240                   //carry_summary = mBuilder->CreateOr(carry_summary, inner_blk.mCarryOutSummary);
    241                   carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutVector[inner_blk.carryData.summaryCarryDataIndex()]);
     249                PabloBlock * inner_blk = & innerIf->getBody();
     250                enterScope(inner_blk);
     251                if (blockHasCarries()) {
     252                  carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutVector[mCurrentScopeIndex + mCarryInfo->summaryCarryDataIndex()]);
    242253                }
     254                leaveScope();
    243255            }
    244256            else if (While * innerWhile = dyn_cast<While>(stmt)) {
    245                 PabloBlock & inner_blk = innerWhile->getBody();
    246                 if (inner_blk.carryData.blockHasCarries())
    247                   //carry_summary = mBuilder->CreateOr(carry_summary, inner_blk.mCarryOutSummary);
    248                   carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutVector[inner_blk.carryData.summaryCarryDataIndex()]);
     257                PabloBlock * inner_blk = & innerWhile->getBody();
     258                enterScope(inner_blk);
     259                if (blockHasCarries()) {
     260                    carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutVector[mCurrentScopeIndex + mCarryInfo->summaryCarryDataIndex()]);
     261                }
     262                leaveScope();
    249263            }
    250264        }
     
    257271}
    258272
    259 void CarryManager::ensureCarriesLoadedLocal(PabloBlock & blk) {
     273void CarryManager::ensureCarriesLoadedLocal() {
    260274#if 0
    261275    const PabloBlockCarryData & cd = blk.carryData;
     
    272286}
    273287
    274 void CarryManager::ensureCarriesStoredLocal(PabloBlock & blk) {
     288void CarryManager::ensureCarriesStoredLocal() {
    275289#if 0
    276290    const PabloBlockCarryData & cd = blk.carryData;
     
    293307
    294308
    295 void CarryManager::ensureCarriesLoadedRecursive(PabloBlock & whileBlk) {
    296     const PabloBlockCarryData & cd = whileBlk.carryData;
    297     const unsigned baseCarryDataIdx = cd.getBlockCarryDataIndex();
    298     const unsigned totalCarryDataSize = cd.getTotalCarryDataSize();
    299     if (cd.getWhileDepth() == 1) {
    300         for (auto i = baseCarryDataIdx; i < baseCarryDataIdx + totalCarryDataSize; ++i) {
     309
     310void CarryManager::ensureCarriesLoadedRecursive() {
     311    const unsigned totalCarryDataSize = mCarryInfo->getTotalCarryDataSize();
     312    if (mCarryInfo->getWhileDepth() == 1) {
     313        for (auto i = mCurrentScopeIndex; i < mCurrentScopeIndex + totalCarryDataSize; ++i) {
    301314            mCarryInVector[i] = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(i)), BLOCK_SIZE/8, false);
    302315        }
     
    305318
    306319
    307 void CarryManager::initializeCarryDataPhisAtWhileEntry(PabloBlock & whileBlk, BasicBlock * whileEntryBlock) {
    308     const PabloBlockCarryData & cd = whileBlk.carryData;
    309     const unsigned baseCarryDataIdx = cd.getBlockCarryDataIndex();
    310     const unsigned totalCarryDataSize = cd.getTotalCarryDataSize();
    311     for (unsigned index = baseCarryDataIdx; index < baseCarryDataIdx + totalCarryDataSize; ++index) {
     320void CarryManager::initializeCarryDataPhisAtWhileEntry(BasicBlock * whileEntryBlock) {
     321    const unsigned totalCarryDataSize = mCarryInfo->getTotalCarryDataSize();
     322    for (unsigned index = mCurrentScopeIndex; index < mCurrentScopeIndex + totalCarryDataSize; ++index) {
    312323#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
    313324        PHINode * phi_in = mBuilder->CreatePHI(mBitBlockType, 2);
     
    322333
    323334
    324 void CarryManager::extendCarryDataPhisAtWhileBodyFinalBlock(PabloBlock & whileBlk, BasicBlock * whileBodyFinalBlock) {
    325     const PabloBlockCarryData & cd = whileBlk.carryData;
    326     const unsigned baseCarryDataIdx = cd.getBlockCarryDataIndex();
    327     const unsigned totalCarryDataSize = cd.getTotalCarryDataSize();
    328     for (unsigned index = baseCarryDataIdx; index < baseCarryDataIdx + totalCarryDataSize; ++index) {
     335void CarryManager::extendCarryDataPhisAtWhileBodyFinalBlock(BasicBlock * whileBodyFinalBlock) {
     336    const unsigned totalCarryDataSize = mCarryInfo->getTotalCarryDataSize();
     337    for (unsigned index = mCurrentScopeIndex; index < mCurrentScopeIndex + totalCarryDataSize; ++index) {
    329338#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
    330339        mCarryInPhis[index]->addIncoming(mZeroInitializer, whileBodyFinalBlock);
     
    337346}
    338347
    339 void CarryManager::ensureCarriesStoredRecursive(PabloBlock & whileBlk) {
    340     const PabloBlockCarryData & cd = whileBlk.carryData;
    341     const unsigned baseCarryDataIdx = cd.getBlockCarryDataIndex();
    342     const unsigned totalCarryDataSize = cd.getTotalCarryDataSize();
    343     if (cd.getWhileDepth() == 1) {
    344         for (auto i = baseCarryDataIdx; i < baseCarryDataIdx + totalCarryDataSize; ++i) {
     348void CarryManager::ensureCarriesStoredRecursive() {
     349    const unsigned totalCarryDataSize = mCarryInfo->getTotalCarryDataSize();
     350    if (mCarryInfo->getWhileDepth() == 1) {
     351        for (auto i = mCurrentScopeIndex; i < mCurrentScopeIndex + totalCarryDataSize; ++i) {
    345352            Value * storePtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(i));
    346353            mBuilder->CreateAlignedStore(mCarryOutVector[i], storePtr, BLOCK_SIZE/8, false);
  • icGREP/icgrep-devel/icgrep/pablo/carry_manager.h

    r4663 r4670  
    1010#include <llvm/IR/Module.h>
    1111#include <IDISA/idisa_builder.h>
     12#include <codegenstate.h>
    1213
    1314/*
     
    3637    , mZeroInitializer(zero)
    3738    , mOneInitializer(one)
    38     , iBuilder(idb) {
     39    , iBuilder(idb)
     40    , mPabloRoot(nullptr)
     41    , mCurrentScope(nullptr)
     42    , mCarryInfo(nullptr)
     43    , mCurrentScopeIndex(0)
     44    , mCarryDataPtr(nullptr)
     45    , mBlockNoPtr(nullptr)
     46    , mBlockNo(nullptr)
     47    , mTotalCarryDataSize(0)
     48    {
    3949
    4050    }
     
    4656    Value * getBlockNoPtr();
    4757   
     58    /* Entering and leaving scopes. */
     59   
     60    void enterScope(PabloBlock * blk);
     61
     62    void leaveScope();
     63   
    4864    /* Methods for processing individual carry-generating operations. */
    4965   
    50     Value * getCarryOpCarryIn(PabloBlock * blk, int localIndex);
     66    Value * getCarryOpCarryIn(int localIndex);
    5167
    52     void setCarryOpCarryOut(PabloBlock * blk, unsigned idx, Value * carry_out);
     68    void setCarryOpCarryOut(unsigned idx, Value * carry_out);
    5369
    54     Value * advanceCarryInCarryOut(PabloBlock * blk, int localIndex, int shift_amount, Value * strm);
     70    Value * advanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm);
    5571 
    5672    /* Methods for getting and setting carry summary values for If statements */
    5773   
    58     bool blockHasCarries(PabloBlock & blk);
     74    bool blockHasCarries();
    5975   
    60     Value * getCarrySummaryExpr(PabloBlock & blk);
     76    Value * getCarrySummaryExpr();
    6177   
    62     void generateCarryOutSummaryCode(PabloBlock & blk);
     78    void generateCarryOutSummaryCode();
    6379   
    64     bool summaryNeededInParentBlock(PabloBlock & blk);
     80    bool summaryNeededInParentBlock();
    6581   
    66     void addSummaryPhi(PabloBlock & blk, BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock);
     82    void addSummaryPhi(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock);
    6783   
    6884    /* Methods for load/store of carries for non-while blocks. */
    6985   
    70     void ensureCarriesLoadedLocal(PabloBlock & blk);
     86    void ensureCarriesLoadedLocal();
    7187
    72     void ensureCarriesStoredLocal(PabloBlock & blk);
     88    void ensureCarriesStoredLocal();
    7389   
    7490    /* Methods for handling while statements */
    7591   
    76     void ensureCarriesLoadedRecursive(PabloBlock & whileBlk);
     92    void ensureCarriesLoadedRecursive();
    7793
    78     void initializeCarryDataPhisAtWhileEntry(PabloBlock & whileBlk, BasicBlock * whileBodyFinalBlock);
     94    void initializeCarryDataPhisAtWhileEntry(BasicBlock * whileBodyFinalBlock);
    7995
    80     void extendCarryDataPhisAtWhileBodyFinalBlock(PabloBlock & whileBlk, BasicBlock * whileBodyFinalBlock);
     96    void extendCarryDataPhisAtWhileBodyFinalBlock(BasicBlock * whileBodyFinalBlock);
    8197
    82     void ensureCarriesStoredRecursive(PabloBlock & whileBlk);
     98    void ensureCarriesStoredRecursive();
    8399
    84100   
     
    90106    IDISA::IDISA_Builder * iBuilder;
    91107    PabloBlock * mPabloRoot;
     108    PabloBlock * mCurrentScope;
     109    PabloBlockCarryData * mCarryInfo;
     110    unsigned mCurrentScopeIndex;
    92111    Value * mCarryDataPtr;
    93112    Value * mBlockNoPtr;
     
    100119    std::vector<Value *> mCarryOutVector;
    101120
    102     Value * unitAdvanceCarryInCarryOut(PabloBlock * blk, int localIndex, Value * strm);
    103     Value * shortAdvanceCarryInCarryOut(PabloBlock * blk, int localIndex, int shift_amount, Value * strm);
    104     Value * longAdvanceCarryInCarryOut(PabloBlock * blk, int localIndex, int shift_amount, Value * strm);
     121    Value * unitAdvanceCarryInCarryOut(int localIndex, Value * strm);
     122    Value * shortAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm);
     123    Value * longAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm);
    105124   
    106125};
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp

    r4668 r4670  
    385385
    386386void PabloCompiler::compileBlock(PabloBlock & block) {
    387     mCarryManager->ensureCarriesLoadedLocal(block);
    388387    mPabloBlock = & block;
    389388    for (const Statement * statement : block) {
     
    391390    }
    392391    mPabloBlock = block.getParent();
    393     mCarryManager->ensureCarriesStoredLocal(block);
    394392}
    395393
     
    441439   
    442440    Value * if_test_value = compileExpression(ifStatement->getCondition());
    443     if (mCarryManager->blockHasCarries(ifBody)) {
     441   
     442    mCarryManager->enterScope(&ifBody);
     443    if (mCarryManager->blockHasCarries()) {
    444444        // load the summary variable
    445         Value* last_if_pending_data = mCarryManager->getCarrySummaryExpr(ifBody);
     445        Value* last_if_pending_data = mCarryManager->getCarrySummaryExpr();
    446446        mBuilder->CreateCondBr(genBitTest2(if_test_value, last_if_pending_data), ifBodyBlock, ifEndBlock);
    447447
     
    452452    // Entry processing is complete, now handle the body of the if.
    453453    mBuilder->SetInsertPoint(ifBodyBlock);
     454   
    454455   
    455456    ++mIfDepth;
    456457    compileBlock(ifBody);
    457458    --mIfDepth;
    458     if (mCarryManager->blockHasCarries(ifBody)) {
    459         mCarryManager->generateCarryOutSummaryCode(ifBody);
     459    if (mCarryManager->blockHasCarries()) {
     460        mCarryManager->generateCarryOutSummaryCode();
    460461    }
    461462    BasicBlock * ifBodyFinalBlock = mBuilder->GetInsertBlock();
     
    473474    }
    474475    // Create the phi Node for the summary variable, if needed.
    475     if (mCarryManager->summaryNeededInParentBlock(ifBody)) {
    476         mCarryManager->addSummaryPhi(ifBody, ifEntryBlock, ifBodyFinalBlock);
    477     }
     476    if (mCarryManager->summaryNeededInParentBlock()) {
     477        mCarryManager->addSummaryPhi(ifEntryBlock, ifBodyFinalBlock);
     478    }
     479    mCarryManager->leaveScope();
    478480}
    479481
     
    486488    BasicBlock * whileEndBlock = BasicBlock::Create(mMod->getContext(), "while.end", mFunction, 0);
    487489
    488     mCarryManager->ensureCarriesLoadedRecursive(whileBody);
     490    mCarryManager->enterScope(&whileBody);
     491    mCarryManager->ensureCarriesLoadedRecursive();
    489492
    490493    const auto & nextNodes = whileStatement->getVariants();
     
    505508    // (3) Next nodes: (a) values set up before loop, (b) modified values calculated in loop.
    506509
    507     mCarryManager->initializeCarryDataPhisAtWhileEntry(whileBody, whileEntryBlock);
     510    mCarryManager->initializeCarryDataPhisAtWhileEntry(whileEntryBlock);
    508511
    509512    // for any Next nodes in the loop body, initialize to (a) pre-loop value.
     
    525528    BasicBlock * whileBodyFinalBlock = mBuilder->GetInsertBlock();
    526529
    527     mCarryManager->extendCarryDataPhisAtWhileBodyFinalBlock(whileBody, whileBodyFinalBlock);
     530    mCarryManager->extendCarryDataPhisAtWhileBodyFinalBlock(whileBodyFinalBlock);
    528531
    529532    // Terminate the while loop body with a conditional branch back.
     
    543546    --mWhileDepth;
    544547
    545     mCarryManager->ensureCarriesStoredRecursive(whileBody);
     548    mCarryManager->ensureCarriesStoredRecursive();
     549    mCarryManager->leaveScope();
    546550}
    547551
     
    599603        int shift = adv->getAdvanceAmount();
    600604        unsigned advance_index = adv->getLocalAdvanceIndex();
    601         expr = mCarryManager->advanceCarryInCarryOut(mPabloBlock, advance_index, shift, strm_value);
     605        expr = mCarryManager->advanceCarryInCarryOut(advance_index, shift, strm_value);
    602606    }
    603607    else if (const MatchStar * mstar = dyn_cast<MatchStar>(stmt)) {
     
    699703
    700704Value* PabloCompiler::genAddWithCarry(Value* e1, Value* e2, unsigned localIndex) {
    701     Value * carryq_value = mCarryManager->getCarryOpCarryIn(mPabloBlock, localIndex);
     705    Value * carryq_value = mCarryManager->getCarryOpCarryIn(localIndex);
    702706#ifdef USE_TWO_UADD_OVERFLOW
    703707    //This is the ideal implementation, which uses two uadd.with.overflow
     
    756760#endif //USE_TWO_UADD_OVERFLOW
    757761
    758     mCarryManager->setCarryOpCarryOut(mPabloBlock, localIndex, carry_out);
     762    mCarryManager->setCarryOpCarryOut(localIndex, carry_out);
    759763    return sum;
    760764}
Note: See TracChangeset for help on using the changeset viewer.