Changeset 4704


Ignore:
Timestamp:
Jul 29, 2015, 10:34:01 AM (4 years ago)
Author:
cameron
Message:

Carry manager fixes, restructuring

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

Legend:

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

    r4700 r4704  
    4949        }
    5050    }
    51     longAdvance.frameOffsetinBits = 0;
     51    longAdvance.frameOffset = 0;
    5252#ifdef PACKING
    53     shortAdvance.frameOffsetinBits = longAdvance.frameOffsetinBits + longAdvance.allocatedBitBlocks * BLOCK_SIZE;
    54     addWithCarry.frameOffsetinBits = shortAdvance.frameOffsetinBits + shortAdvance.allocatedBits;
    55     EnsurePackHasSpace(addWithCarry.frameOffsetinBits, addWithCarry.entries);
    56     advance1.frameOffsetinBits = addWithCarry.frameOffsetinBits + addWithCarry.entries;
    57     EnsurePackHasSpace(advance1.frameOffsetinBits, advance1.entries);
    58     nested.frameOffsetinBits = advance1.frameOffsetinBits + advance1.entries;
     53    shortAdvance.frameOffset = longAdvance.frameOffset + longAdvance.allocatedBitBlocks * BLOCK_SIZE;
     54    addWithCarry.frameOffset = shortAdvance.frameOffset + shortAdvance.allocatedBits;
     55    EnsurePackHasSpace(addWithCarry.frameOffset, addWithCarry.entries);
     56    advance1.frameOffset = addWithCarry.frameOffset + addWithCarry.entries;
     57    EnsurePackHasSpace(advance1.frameOffset, advance1.entries);
     58    nested.frameOffset = advance1.frameOffset + advance1.entries;
    5959#else
    60     shortAdvance.frameOffsetinBits = longAdvance.frameOffsetinBits + longAdvance.allocatedBitBlocks * BLOCK_SIZE;
    61     addWithCarry.frameOffsetinBits = shortAdvance.frameOffsetinBits + shortAdvance.entries * BLOCK_SIZE;
    62     advance1.frameOffsetinBits = addWithCarry.frameOffsetinBits + addWithCarry.entries * BLOCK_SIZE;
    63     nested.frameOffsetinBits = advance1.frameOffsetinBits + advance1.entries * BLOCK_SIZE;
     60    shortAdvance.frameOffset = longAdvance.frameOffset + longAdvance.allocatedBitBlocks;
     61    addWithCarry.frameOffset = shortAdvance.frameOffset + shortAdvance.entries;
     62    advance1.frameOffset = addWithCarry.frameOffset + addWithCarry.entries;
     63    nested.frameOffset = advance1.frameOffset + advance1.entries;
    6464#endif
    6565}
     
    7171    strm << " framePosition: " << framePosition << ", ifDepth: " << ifDepth << ", whileDepth:" << whileDepth << ", maxNestingDepth: " << maxNestingDepth << "\n";
    7272    for (int i = 0; i < totalDepth; i++) strm << "  ";
    73     strm << "longAdvance: offset = " << longAdvance.frameOffsetinBits << ", entries = " << longAdvance.entries << "\n";
     73    strm << "longAdvance: offset = " << longAdvance.frameOffset << ", entries = " << longAdvance.entries << "\n";
    7474    for (int i = 0; i < totalDepth; i++) strm << "  ";
    75     strm << "shortAdvance: offset = " << shortAdvance.frameOffsetinBits << ", entries = " << shortAdvance.entries << "\n";
     75    strm << "shortAdvance: offset = " << shortAdvance.frameOffset << ", entries = " << shortAdvance.entries << "\n";
    7676    for (int i = 0; i < totalDepth; i++) strm << "  ";
    77     strm << "advance1: offset = " << advance1.frameOffsetinBits << ", entries = " << advance1.entries << "\n";
     77    strm << "advance1: offset = " << advance1.frameOffset << ", entries = " << advance1.entries << "\n";
    7878    for (int i = 0; i < totalDepth; i++) strm << "  ";
    79     strm << "addWithCarry: offset = " << addWithCarry.frameOffsetinBits << ", entries = " << addWithCarry.entries << "\n";
     79    strm << "addWithCarry: offset = " << addWithCarry.frameOffset << ", entries = " << addWithCarry.entries << "\n";
    8080    for (int i = 0; i < totalDepth; i++) strm << "  ";
    81     strm << "nested: offset = " << nested.frameOffsetinBits << ", allocatedBits = " << nested.allocatedBits << "\n";
     81    strm << "nested: offset = " << nested.frameOffset << ", allocatedBits = " << nested.allocatedBits << "\n";
    8282    for (int i = 0; i < totalDepth; i++) strm << "  ";
    83     strm << "summary: offset = " << summary.frameOffsetinBits << ", allocatedBits = " << summary.allocatedBits << "\n";
     83    strm << "summary: offset = " << summary.frameOffset << "\n";
    8484    for (int i = 0; i < totalDepth; i++) strm << "  ";
    85     strm << "scopeCarryDataBits = " << scopeCarryDataBits  << "\n";
     85    strm << "scopeCarryDataSize = " << scopeCarryDataSize  << "\n";
    8686    strm.flush();
    8787   
  • icGREP/icgrep-devel/icgrep/pablo/carry_data.h

    r4703 r4704  
    7474                           nested({0, 0, 0}),
    7575                           summary({0, 0}),
    76                            scopeCarryDataBits(0)
     76                           scopeCarryDataSize(0)
    7777    {enumerateLocal();}
    7878       
     
    8383   
    8484    unsigned getFrameIndex()  const {
    85         return framePosition/BLOCK_SIZE;
     85        return framePosition;
    8686    }
    8787   
     
    116116    bool blockHasLongAdvances() const { return longAdvance.entries > 0;}
    117117   
    118     unsigned getLocalCarryPackIndex () { return shortAdvance.frameOffsetinBits / PACK_SIZE; }
     118    unsigned getLocalCarryPackIndex () {
     119#ifdef PACKING
     120        return shortAdvance.frameOffset / PACK_SIZE;
     121#else
     122        return shortAdvance.frameOffset;
     123#endif
     124    }
    119125
    120     unsigned getLocalCarryPackCount () { return fullOrPartialBlocks(nested.frameOffsetinBits - shortAdvance.frameOffsetinBits, PACK_SIZE); }
    121 
    122     unsigned getScopeCarryPackCount () { return fullOrPartialBlocks(scopeCarryDataBits, PACK_SIZE); }
    123    
    124     bool blockHasCarries() const { return scopeCarryDataBits > 0;}
     126    unsigned getLocalCarryPackCount () {
     127#ifdef PACKING
     128        return fullOrPartialBlocks(nested.frameOffset, PACK_SIZE) - shortAdvance.frameOffset / PACK_SIZE;
     129#else
     130        return nested.frameOffset - shortAdvance.frameOffset;
     131#endif
     132    }
    125133   
    126     bool explicitSummaryRequired() const { return (ifDepth > 0) && (scopeCarryDataBits > PACK_SIZE);}
     134    unsigned getScopeCarryPackCount () {
     135#ifdef PACKING
     136        return fullOrPartialBlocks(scopeCarryDataSize, PACK_SIZE);
     137#else
     138        return scopeCarryDataSize;
     139#endif
     140    }
     141   
     142    bool blockHasCarries() const { return scopeCarryDataSize > 0;}
     143   
     144    bool explicitSummaryRequired() const {
     145#ifdef PACKING
     146        return (ifDepth > 0) && (scopeCarryDataSize > PACK_SIZE);
     147#else
     148        return (ifDepth > 0) && (scopeCarryDataSize > 1);
     149#endif
     150    }
    127151   
    128152protected:
     
    136160    unsigned maxNestingDepth;   
    137161   
    138     struct {unsigned frameOffsetinBits; unsigned entries; unsigned allocatedBitBlocks;} longAdvance;
    139     struct {unsigned frameOffsetinBits; unsigned entries; unsigned allocatedBits;} shortAdvance;
    140     struct {unsigned frameOffsetinBits; unsigned entries;} advance1;
    141     struct {unsigned frameOffsetinBits; unsigned entries;} addWithCarry;
    142     struct {unsigned frameOffsetinBits; unsigned entries; unsigned allocatedBits;} nested;
    143     struct {unsigned frameOffsetinBits; unsigned allocatedBits;} summary;
     162    struct {unsigned frameOffset; unsigned entries; unsigned allocatedBitBlocks;} longAdvance;
     163    struct {unsigned frameOffset; unsigned entries; unsigned allocatedBits;} shortAdvance;
     164    struct {unsigned frameOffset; unsigned entries;} advance1;
     165    struct {unsigned frameOffset; unsigned entries;} addWithCarry;
     166    struct {unsigned frameOffset; unsigned entries; unsigned allocatedBits;} nested;
     167    struct {unsigned frameOffset; unsigned allocatedBits;} summary;
    144168
    145     unsigned scopeCarryDataBits;
     169    unsigned scopeCarryDataSize;
    146170   
    147171    llvm::Value * ifEntryPack;
  • icGREP/icgrep-devel/icgrep/pablo/carry_manager.cpp

    r4703 r4704  
    4747    mCarryInfoVector.resize(scopeCount);
    4848   
    49     unsigned totalCarryDataBits = enumerate(pb, 0, 0);
    50     mTotalCarryDataBitBlocks = (totalCarryDataBits + BLOCK_SIZE - 1)/BLOCK_SIZE;
     49    unsigned totalCarryDataSize = enumerate(pb, 0, 0);
     50#ifdef PACKING
     51    mTotalCarryDataBitBlocks = (totalCarryDataSize + BLOCK_SIZE - 1)/BLOCK_SIZE;
     52#else
     53    mTotalCarryDataBitBlocks = totalCarryDataSize;
     54#endif
    5155    // Carry Data area will have one extra bit block to store the block number.
    5256    mBlockNoPtr = mBuilder->CreateBitCast(mBuilder->CreateGEP(carryPtr, mBuilder->getInt64(mTotalCarryDataBitBlocks)), Type::getInt64PtrTy(mBuilder->getContext()));
    5357    mBlockNo = mBuilder->CreateLoad(mBlockNoPtr);
    5458#ifdef PACKING
    55     unsigned totalPackCount = (totalCarryDataBits + PACK_SIZE - 1)/PACK_SIZE;
     59    unsigned totalPackCount = (totalCarryDataSize + PACK_SIZE - 1)/PACK_SIZE;
    5660    mCarryPackPtr.resize(totalPackCount);
    5761    mCarryInPack.resize(totalPackCount);
     
    8993    cd->setIfDepth(ifDepth);
    9094    cd->setWhileDepth(whileDepth);
    91     unsigned nestedOffset = cd->nested.frameOffsetinBits;
     95    unsigned nestedOffset = cd->nested.frameOffset;
    9296 
    9397    for (Statement * stmt : *blk) {
     
    124128    }
    125129   
    126     cd->scopeCarryDataBits = nestedOffset;
     130    cd->scopeCarryDataSize = nestedOffset;
    127131   
    128132    if (cd->explicitSummaryRequired()) {
    129133        // Need extra space for the summary variable, always the last
    130134        // entry within an if block.
    131         cd->scopeCarryDataBits = alignCeiling(cd->scopeCarryDataBits, PACK_SIZE);
    132         cd->summary.frameOffsetinBits = cd->scopeCarryDataBits;
    133         cd->summary.allocatedBits = PACK_SIZE;
    134         cd->scopeCarryDataBits += PACK_SIZE;
     135#ifdef PACKING
     136        cd->scopeCarryDataSize = alignCeiling(cd->scopeCarryDataSize, PACK_SIZE);
     137        cd->summary.frameOffset = cd->scopeCarryDataSize;
     138        cd->scopeCarryDataSize += PACK_SIZE;
     139#else
     140        cd->summary.frameOffset = cd->scopeCarryDataSize;
     141        cd->scopeCarryDataSize++;
     142#endif
    135143    }
    136144    else {
    137         cd->summary.frameOffsetinBits = 0;
    138         cd->summary.allocatedBits = cd->scopeCarryDataBits;
    139     }
    140     return cd->scopeCarryDataBits;
     145        cd->summary.frameOffset = 0;
     146    }
     147    return cd->scopeCarryDataSize;
    141148}
    142149
     
    149156    mCarryInfo = mCarryInfoVector[blk->getScopeIndex()];
    150157    mCurrentFrameIndex += mCarryInfo->getFrameIndex();
    151     //std::cerr << "enterScope:  mCurrentFrameIndex = " << mCurrentFrameIndex << std::endl;
     158    //std::cerr << "enterScope:  blk->getScopeIndex() = " << blk->getScopeIndex() << ", mCurrentFrameIndex = " << mCurrentFrameIndex << std::endl;
    152159}
    153160
     
    174181/* Helper routines */
    175182
    176 unsigned CarryManager::absPosition(unsigned frameOffsetinBits, unsigned relPos) {
    177 #ifdef PACKING
    178     return mCurrentFrameIndex + frameOffsetinBits + relPos;
    179 #else
    180     return mCurrentFrameIndex + frameOffsetinBits/BLOCK_SIZE + relPos;
    181 #endif
     183unsigned CarryManager::absPosition(unsigned frameOffset, unsigned relPos) {
     184    return mCurrentFrameIndex + frameOffset + relPos;
    182185}
    183186
    184187
    185188unsigned CarryManager::carryOpPosition(unsigned localIndex) {
    186     return absPosition(mCarryInfo->addWithCarry.frameOffsetinBits, localIndex);
     189    //std::cerr << "carryOpPosition: addWithCarry.frameOffset = " << mCarryInfo->addWithCarry.frameOffset << ", localIndex = " <<localIndex << std::endl;
     190    return absPosition(mCarryInfo->addWithCarry.frameOffset, localIndex);
    187191}
    188192
    189193unsigned CarryManager::advance1Position(unsigned localIndex) {
    190     return absPosition(mCarryInfo->advance1.frameOffsetinBits, localIndex);
     194    //std::cerr << "unsigned CarryManager::advance1Position: advance1.frameOffset = " << mCarryInfo->advance1.frameOffset << ", localIndex = " <<localIndex << std::endl;
     195    return absPosition(mCarryInfo->advance1.frameOffset, localIndex);
    191196}
    192197
    193198unsigned CarryManager::shortAdvancePosition(unsigned localIndex) {
    194     return absPosition(mCarryInfo->shortAdvance.frameOffsetinBits, localIndex);
     199    return absPosition(mCarryInfo->shortAdvance.frameOffset, localIndex);
    195200}
    196201
    197202unsigned CarryManager::longAdvanceBitBlockPosition(unsigned localIndex) {
    198203#ifdef PACKING
    199     return (mCurrentFrameIndex + mCarryInfo->longAdvance.frameOffsetinBits) / BLOCK_SIZE + localIndex;
    200 #else
    201     return mCurrentFrameIndex + (mCarryInfo->longAdvance.frameOffsetinBits / BLOCK_SIZE) + localIndex;
     204    return (mCurrentFrameIndex + mCarryInfo->longAdvance.frameOffset) / BLOCK_SIZE + localIndex;
     205#else
     206    return mCurrentFrameIndex + mCarryInfo->longAdvance.frameOffset + localIndex;
    202207#endif
    203208}
     
    205210unsigned CarryManager::localBasePack() {
    206211#ifdef PACKING
    207     return (mCurrentFrameIndex + mCarryInfo->shortAdvance.frameOffsetinBits) / PACK_SIZE;
    208 #else
    209     return mCurrentFrameIndex + (mCarryInfo->shortAdvance.frameOffsetinBits / PACK_SIZE);
     212    return (mCurrentFrameIndex + mCarryInfo->shortAdvance.frameOffset) / PACK_SIZE;
     213#else
     214    return mCurrentFrameIndex + mCarryInfo->shortAdvance.frameOffset;
    210215#endif
    211216}
     
    222227
    223228unsigned CarryManager::summaryPosition() {
    224     return absPosition(mCarryInfo->summary.frameOffsetinBits, 0);
     229    return absPosition(mCarryInfo->summary.frameOffset, 0);
    225230}
    226231
    227232unsigned CarryManager::summaryBits() {
    228     return mCarryInfo->summary.allocatedBits;
     233#ifdef PACKING
     234    if (mCarryInfo->scopeCarryDataSize > PACK_SIZE) return PACK_SIZE;
     235    else return mCarryInfo->scopeCarryDataSize;
     236#else
     237    if (mCarryInfo->scopeCarryDataSize > 1) return PACK_SIZE;
     238    else return mCarryInfo->scopeCarryDataSize;
     239#endif
    229240}
    230241
     
    248259    unsigned packIndex = carryBit_lo / PACK_SIZE;
    249260    unsigned carryOffset = carryBit_lo % PACK_SIZE;
    250     unsigned hiLimit = carryBit_lo + carryRangeSize;
    251261   
    252262    Value * carryItem = getCarryPack(packIndex);
    253     if (hiLimit < PACK_SIZE) {
    254        carryItem = mBuilder->CreateAnd(carryItem, mBuilder->getInt64((1 << hiLimit) - 1));
    255     }
    256     if (carryOffset > 0) {
    257        carryItem = mBuilder->CreateLShr(carryItem, mBuilder->getInt64(carryOffset));
    258     }
    259     return carryItem;
    260 }
    261    
     263    if (carryRangeSize == 64) {
     264        assert(carryOffset == 0);
     265        return carryItem;
     266    }
     267    unsigned mask = (1 << carryRangeSize) - 1;
     268    return mBuilder->CreateAnd(carryItem, mBuilder->getInt64(mask << carryOffset));
     269}
     270   
     271Value * CarryManager::getCarryBits(unsigned carryBitPos, unsigned carryBitCount) {
     272   
     273    unsigned carryOffset = carryBitPos % PACK_SIZE;
     274
     275    Value * carryRange = getCarryRange(carryBitPos, carryBitCount);
     276    if (carryOffset == 0) return carryRange;
     277    return mBuilder->CreateLShr(carryRange, carryOffset);
     278}
     279
    262280Value * CarryManager::getCarryBit(unsigned carryBitPos) {
    263     return getCarryRange(carryBitPos, 1);
    264 }
    265    
     281    return getCarryBits(carryBitPos, 1);
     282}
     283
    266284/*  NOTE: In the following the mCarryOutPack is an accumulator.
    267285    It must be created at the appropriate outer level.  */
     
    374392    }   
    375393    setCarryBits(posn - offset, shift_amount, field);
    376     Value* carry_longint = mBuilder->CreateZExt(getCarryRange(posn, shift_amount), mBuilder->getIntNTy(BLOCK_SIZE));
     394    Value* carry_longint = mBuilder->CreateZExt(getCarryBits(posn, shift_amount), mBuilder->getIntNTy(BLOCK_SIZE));
    377395    Value* strm_longint = mBuilder->CreateBitCast(strm, mBuilder->getIntNTy(BLOCK_SIZE));
    378396    Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, shift_amount), carry_longint);
     
    459477    unsigned summary_posn = summaryPosition();
    460478#ifdef PACKING
    461     return getCarryRange(summary_posn, summaryBits());
     479    return mBuilder->CreateBitCast(mBuilder->CreateZExt(getCarryRange(summary_posn, summaryBits()), mBuilder->getIntNTy(BLOCK_SIZE)), mBitBlockType);
    462480#else
    463481    return getCarryPack(summary_posn);
     
    465483}
    466484
    467     void CarryManager::initializeCarryDataAtIfEntry() {
    468         if (mCarryOutPack[scopeBasePack()] == nullptr) {
    469             mCarryInfo->ifEntryPack = mZeroInitializer;
    470         }
    471         else {
    472             mCarryInfo->ifEntryPack = mCarryOutPack[scopeBasePack()];
    473         }
    474     }
    475    
    476     void CarryManager::buildCarryDataPhisAfterIfBody(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock) {
    477         unsigned const ifScopeCarryBits = mCarryInfo->scopeCarryDataBits;
    478         if (ifScopeCarryBits == 0) {
    479             // No carry data, therefore no phi nodes.
    480             return;
    481         }
    482         if (ifScopeCarryBits < PACK_SIZE) {
    483             unsigned const ifPackIndex = scopeBasePack();
    484             PHINode * ifPack_phi = mBuilder->CreatePHI(mCarryPackType, 2, "ifPack");
    485             ifPack_phi->addIncoming(mCarryInfo->ifEntryPack, ifEntryBlock);
    486             ifPack_phi->addIncoming(mCarryOutPack[ifPackIndex], ifBodyFinalBlock);
    487             mCarryOutPack[ifPackIndex] = ifPack_phi;
    488         }
    489         else if ((ifScopeCarryBits > PACK_SIZE) && (mCarryInfo->getIfDepth() > 1)) {
    490             const unsigned summaryPackIndex = summaryPosition();
    491             PHINode * summary_phi = mBuilder->CreatePHI(mCarryPackType, 2, "summary");
    492             summary_phi->addIncoming(mZeroInitializer, ifEntryBlock);
    493             summary_phi->addIncoming(mCarryOutPack[summaryPackIndex], ifBodyFinalBlock);
    494             mCarryOutPack[summaryPackIndex] = summary_phi;
    495         }
    496     }
     485void CarryManager::initializeCarryDataAtIfEntry() {
     486    if (mCarryOutPack[scopeBasePack()] == nullptr) {
     487        mCarryInfo->ifEntryPack = mZeroInitializer;
     488    }
     489    else {
     490        mCarryInfo->ifEntryPack = mCarryOutPack[scopeBasePack()];
     491    }
     492}
     493   
     494void CarryManager::buildCarryDataPhisAfterIfBody(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock) {
     495    unsigned const ifScopeCarrySize = mCarryInfo->scopeCarryDataSize;
     496    if (ifScopeCarrySize == 0) {
     497        // No carry data, therefore no phi nodes.
     498        return;
     499    }
     500#ifdef PACKING
     501    if (ifScopeCarrySize < PACK_SIZE) {
     502        unsigned const ifPackIndex = scopeBasePack();
     503        PHINode * ifPack_phi = mBuilder->CreatePHI(mCarryPackType, 2, "ifPack");
     504        ifPack_phi->addIncoming(mCarryInfo->ifEntryPack, ifEntryBlock);
     505        ifPack_phi->addIncoming(mCarryOutPack[ifPackIndex], ifBodyFinalBlock);
     506        mCarryOutPack[ifPackIndex] = ifPack_phi;
     507    }
     508#endif
     509    if (mCarryInfo->explicitSummaryRequired()) {
     510        const unsigned summaryPackIndex = summaryPosition();
     511        PHINode * summary_phi = mBuilder->CreatePHI(mCarryPackType, 2, "summary");
     512        summary_phi->addIncoming(mZeroInitializer, ifEntryBlock);
     513        summary_phi->addIncoming(mCarryOutPack[summaryPackIndex], ifBodyFinalBlock);
     514        mCarryOutPack[summaryPackIndex] = summary_phi;
     515    }
     516}
    497517   
    498518void CarryManager::addSummaryPhiIfNeeded(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock) {
  • icGREP/icgrep-devel/icgrep/pablo/carry_manager.h

    r4703 r4704  
    138138   
    139139    Value * getCarryRange(unsigned carryBit_lo, unsigned carryRangeSize);     
     140    Value * getCarryBits(unsigned carryBitPos, unsigned bits);
    140141    Value * getCarryBit(unsigned carryBitPos);
    141142    void setCarryBits(unsigned carryBit_lo, unsigned carryRangeSize, Value * bits);
Note: See TracChangeset for help on using the changeset viewer.