Ignore:
Timestamp:
May 3, 2017, 1:13:04 PM (2 years ago)
Author:
nmedfort
Message:

partial refactoring check in with change for Linda.

File:
1 edited

Legend:

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

    r5400 r5431  
    6262 * @brief initializeCarryData
    6363 ** ------------------------------------------------------------------------------------------------------------- */
    64 void CarryManager::initializeCarryData(PabloKernel * const kernel) {
     64void CarryManager::initializeCarryData() {
    6565
    6666    // Each scope constructs its own CarryData struct, which will be added to the final "carries" struct
     
    8080    // set of carries for that iteration.
    8181
    82     mKernel = kernel;
    83 
    84     mCurrentScope = kernel->getEntryBlock();
     82    iBuilder = mKernel->getBuilder();
     83    assert ("CarryManager does not have a IDISA builder" && iBuilder);
     84
     85    PabloBlock * const entryBlock = mKernel->getEntryBlock();
     86
     87    mCurrentScope = entryBlock;
    8588
    8689    mCarryScopes = 0;
    8790
    88     mCarryMetadata.resize(getScopeCount(kernel->getEntryBlock()));
    89 
    90     Type * const carryStateTy = analyse(kernel->getEntryBlock());
     91    mCarryMetadata.resize(getScopeCount(entryBlock));
     92
     93    Type * const carryStateTy = analyse(entryBlock);
    9194
    9295    mKernel->addScalar(carryStateTy, "carries");
     
    108111    mCarryInfo = &mCarryMetadata[0];
    109112    assert (!mCarryInfo->hasSummary());
     113
     114    iBuilder = mKernel->getBuilder();
     115    assert ("CarryManager does not have a IDISA builder" && iBuilder);
    110116
    111117    mCurrentFrame = mKernel->getScalarFieldPtr("carries");
     
    117123
    118124    assert (mCarrySummaryStack.empty());
    119     mCarrySummaryStack.push_back(Constant::getNullValue(mCarryPackType));
    120 
    121     if (mHasLoop) {
     125    mCarrySummaryStack.push_back(Constant::getNullValue(getCarryPackType()));
     126
     127    if (mHasLoop) {       
    122128        mLoopSelector = mKernel->getScalarField("selector");
    123129        mNextLoopSelector = iBuilder->CreateXor(mLoopSelector, ConstantInt::get(mLoopSelector->getType(), 1));
    124130    }
     131
    125132}
    126133
     
    160167void CarryManager::enterLoopBody(BasicBlock * const entryBlock) {
    161168    if (mCarryInfo->hasSummary()) {
    162         PHINode * phiCarryOutSummary = iBuilder->CreatePHI(mCarryPackType, 2, "summary");
     169        PHINode * phiCarryOutSummary = iBuilder->CreatePHI(getCarryPackType(), 2, "summary");
    163170        assert (!mCarrySummaryStack.empty());
    164171        phiCarryOutSummary->addIncoming(mCarrySummaryStack.back(), entryBlock);
     
    195202        // RESIZE CARRY BLOCK
    196203        iBuilder->SetInsertPoint(resizeCarryState);
    197         const auto BlockWidth = mCarryPackType->getPrimitiveSizeInBits() / 8;
     204        const auto BlockWidth = getCarryPackType()->getPrimitiveSizeInBits() / 8;
    198205        const auto Log2BlockWidth = floor_log2(BlockWidth);
    199206        Constant * const carryStateWidth = ConstantExpr::getIntegerCast(ConstantExpr::getSizeOf(array->getType()->getPointerElementType()), iBuilder->getSizeTy(), false);
     
    230237
    231238        Value * ptr1 = iBuilder->CreateGEP(newSummary, summarySize);
    232         ptr1 = iBuilder->CreatePointerCast(ptr1, mCarryPackType->getPointerTo());
     239        ptr1 = iBuilder->CreatePointerCast(ptr1, getCarryPackType()->getPointerTo());
    233240
    234241        Value * ptr2 = iBuilder->CreateGEP(newSummary, iBuilder->CreateAdd(summarySize, iBuilder->getSize(BlockWidth)));
    235         ptr2 = iBuilder->CreatePointerCast(ptr2, mCarryPackType->getPointerTo());
    236 
    237         newSummary = iBuilder->CreatePointerCast(newSummary, mCarryPackType->getPointerTo());
     242        ptr2 = iBuilder->CreatePointerCast(ptr2, getCarryPackType()->getPointerTo());
     243
     244        newSummary = iBuilder->CreatePointerCast(newSummary, getCarryPackType()->getPointerTo());
    238245        iBuilder->CreateStore(newSummary, summaryPtr);
    239246        Value * const newCapacity = iBuilder->CreateShl(ONE, log2capacity);
     
    258265        Value * initialSummary = iBuilder->CreateAlignedMalloc(initialSummarySize, BlockWidth);
    259266        iBuilder->CreateMemZero(initialSummary, initialSummarySize, BlockWidth);
    260         initialSummary = iBuilder->CreatePointerCast(initialSummary, mCarryPackType->getPointerTo());
     267        initialSummary = iBuilder->CreatePointerCast(initialSummary, getCarryPackType()->getPointerTo());
    261268        iBuilder->CreateStore(initialSummary, summaryPtr);
    262269
     
    333340        carry->addIncoming(carryOutAccumulator, entry);
    334341        // OR the updated carry in summary later for the summaryTest
    335         PHINode * const carryInSummary = iBuilder->CreatePHI(mCarryPackType, 2);
    336         carryInSummary->addIncoming(Constant::getNullValue(mCarryPackType), entry);
     342        PHINode * const carryInSummary = iBuilder->CreatePHI(getCarryPackType(), 2);
     343        carryInSummary->addIncoming(Constant::getNullValue(getCarryPackType()), entry);
    337344
    338345        // half subtractor
     
    362369        iBuilder->SetInsertPoint(resume);
    363370
    364         IntegerType * ty = IntegerType::get(iBuilder->getContext(), mCarryPackType->getPrimitiveSizeInBits());
     371        IntegerType * ty = IntegerType::get(iBuilder->getContext(), getCarryPackType()->getPrimitiveSizeInBits());
    365372        iBuilder->CreateAssert(iBuilder->CreateICmpEQ(iBuilder->CreateBitCast(finalBorrow, ty), ConstantInt::getNullValue(ty)), "finalBorrow != 0");
    366373        iBuilder->CreateAssert(iBuilder->CreateICmpEQ(iBuilder->CreateBitCast(finalCarry, ty), ConstantInt::getNullValue(ty)), "finalCarry != 0");
     
    381388        // If we're returning to the base scope, reset our accumulated summary value.
    382389        if (n == 2) {
    383             carryOut = Constant::getNullValue(mCarryPackType);
     390            carryOut = Constant::getNullValue(getCarryPackType());
    384391        }
    385392        mCarrySummaryStack.back() = carryOut;
     
    412419        }
    413420    }
    414     mCarrySummaryStack.push_back(Constant::getNullValue(mCarryPackType));
     421    mCarrySummaryStack.push_back(Constant::getNullValue(getCarryPackType()));
    415422}
    416423
     
    523530    std::tie(carryOut, result) = iBuilder->bitblock_add_with_carry(e1, e2, carryIn);
    524531    setNextCarryOut(carryOut);
    525     assert (result->getType() == mBitBlockType);
     532    assert (result->getType() == getBitBlockType());
    526533    return result;
    527534}
     
    537544        std::tie(carryOut, result) = iBuilder->bitblock_advance(value, carryIn, shiftAmount);
    538545        setNextCarryOut(carryOut);
    539         assert (result->getType() == mBitBlockType);
     546        assert (result->getType() == getBitBlockType());
    540547        return result;
    541548    } else {
     
    552559    assert (shiftAmount >= LONG_ADVANCE_BREAKPOINT);
    553560
    554     Type * const streamTy = iBuilder->getIntNTy(mBitBlockWidth);
     561    Type * const streamTy = iBuilder->getIntNTy(getBitBlockWidth());
    555562
    556563    if (mIfDepth > 0) {
    557         if (shiftAmount > mBitBlockWidth) {
     564        if (shiftAmount > getBitBlockWidth()) {
    558565            const auto frameIndex = mCurrentFrameIndex++;
    559566            Value * carry = iBuilder->CreateZExt(iBuilder->bitblock_any(value), streamTy);
    560             const unsigned summarySize = ceil_udiv(shiftAmount, mBitBlockWidth * mBitBlockWidth);
     567            const unsigned summarySize = ceil_udiv(shiftAmount, getBitBlockWidth() * getBitBlockWidth());
    561568            for (unsigned i = 0;;++i) {
    562569                Value * const ptr = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(frameIndex), iBuilder->getInt32(i)});
    563570                Value * const prior = iBuilder->CreateBitCast(iBuilder->CreateBlockAlignedLoad(ptr), streamTy);
    564                 Value * const stream = iBuilder->CreateBitCast(iBuilder->CreateOr(iBuilder->CreateShl(prior, 1), carry), mBitBlockType);
     571                Value * const stream = iBuilder->CreateBitCast(iBuilder->CreateOr(iBuilder->CreateShl(prior, 1), carry), getBitBlockType());
    565572                if (LLVM_LIKELY(i == summarySize)) {
    566                     Value * const maskedStream = iBuilder->CreateAnd(stream, iBuilder->bitblock_mask_from(iBuilder->getInt32(summarySize % mBitBlockWidth)));
     573                    Value * const maskedStream = iBuilder->CreateAnd(stream, iBuilder->bitblock_mask_from(iBuilder->getInt32(summarySize % getBitBlockWidth())));
    567574                    addToCarryOutSummary(maskedStream);
    568575                    iBuilder->CreateBlockAlignedStore(maskedStream, ptr);
     
    571578                addToCarryOutSummary(stream);
    572579                iBuilder->CreateBlockAlignedStore(stream, ptr);
    573                 carry = iBuilder->CreateLShr(prior, mBitBlockWidth - 1);
     580                carry = iBuilder->CreateLShr(prior, getBitBlockWidth() - 1);
    574581            }
    575582        } else if (LLVM_LIKELY(mCarryInfo->hasExplicitSummary())) {
     
    579586    const auto frameIndex = mCurrentFrameIndex++;
    580587    // special case using a single buffer entry and the carry_out value.
    581     if (LLVM_LIKELY((shiftAmount < mBitBlockWidth) && (mLoopDepth == 0))) {
     588    if (LLVM_LIKELY((shiftAmount < getBitBlockWidth()) && (mLoopDepth == 0))) {
    582589        Value * const buffer = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(frameIndex), iBuilder->getInt32(0)});
    583         assert (buffer->getType()->getPointerElementType() == mBitBlockType);
     590        assert (buffer->getType()->getPointerElementType() == getBitBlockType());
    584591        Value * carryIn = iBuilder->CreateBlockAlignedLoad(buffer);       
    585592        iBuilder->CreateBlockAlignedStore(value, buffer);
    586593        /* Very special case - no combine */
    587         if (LLVM_UNLIKELY(shiftAmount == mBitBlockWidth)) {
    588             return iBuilder->CreateBitCast(carryIn, mBitBlockType);
    589         }
    590         Value* block0_shr = iBuilder->CreateLShr(iBuilder->CreateBitCast(carryIn, streamTy), mBitBlockWidth - shiftAmount);
     594        if (LLVM_UNLIKELY(shiftAmount == getBitBlockWidth())) {
     595            return iBuilder->CreateBitCast(carryIn, getBitBlockType());
     596        }
     597        Value* block0_shr = iBuilder->CreateLShr(iBuilder->CreateBitCast(carryIn, streamTy), getBitBlockWidth() - shiftAmount);
    591598        Value* block1_shl = iBuilder->CreateShl(iBuilder->CreateBitCast(value, streamTy), shiftAmount);
    592         return iBuilder->CreateBitCast(iBuilder->CreateOr(block1_shl, block0_shr), mBitBlockType);
     599        return iBuilder->CreateBitCast(iBuilder->CreateOr(block1_shl, block0_shr), getBitBlockType());
    593600    } else { //
    594         const unsigned blockShift = shiftAmount % mBitBlockWidth;
    595         const unsigned blocks = ceil_udiv(shiftAmount, mBitBlockWidth);
     601        const unsigned blockShift = shiftAmount % getBitBlockWidth();
     602        const unsigned blocks = ceil_udiv(shiftAmount, getBitBlockWidth());
    596603        // Create a mask to implement circular buffer indexing
    597604        Value * indexMask = iBuilder->getSize(nearest_pow2(blocks + ((mLoopDepth != 0) ? 1 : 0)) - 1);
     
    604611        Value * storeIndex = iBuilder->CreateAnd(blockIndex, indexMask);
    605612        Value * const carryOutPtr = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(frameIndex), storeIndex});
    606         assert (carryIn->getType() == mBitBlockType);
     613        assert (carryIn->getType() == getBitBlockType());
    607614
    608615        // If the long advance is an exact multiple of BitBlockWidth, we simply return the oldest
     
    616623            Value * const carryInPtr2 = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(frameIndex), loadIndex1});
    617624            Value * carry_block1 = iBuilder->CreateBlockAlignedLoad(carryInPtr2);
    618             Value * block0_shr = iBuilder->CreateLShr(iBuilder->CreateBitCast(carryIn, streamTy), mBitBlockWidth - blockShift);
     625            Value * block0_shr = iBuilder->CreateLShr(iBuilder->CreateBitCast(carryIn, streamTy), getBitBlockWidth() - blockShift);
    619626            Value * block1_shl = iBuilder->CreateShl(iBuilder->CreateBitCast(carry_block1, streamTy), blockShift);
    620627            iBuilder->CreateBlockAlignedStore(value, carryOutPtr);
    621             return iBuilder->CreateBitCast(iBuilder->CreateOr(block1_shl, block0_shr), mBitBlockType);
     628            return iBuilder->CreateBitCast(iBuilder->CreateOr(block1_shl, block0_shr), getBitBlockType());
    622629        }
    623630    }
     
    634641        mCarryPackPtr = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(mCurrentFrameIndex), mLoopSelector});
    635642    }
    636     assert (mCarryPackPtr->getType()->getPointerElementType() == mCarryPackType);
     643    assert (mCarryPackPtr->getType()->getPointerElementType() == getCarryPackType());
    637644    Value * const carryIn = iBuilder->CreateBlockAlignedLoad(mCarryPackPtr);
    638645    if (mLoopDepth > 0) {
    639         iBuilder->CreateBlockAlignedStore(Constant::getNullValue(mCarryPackType), mCarryPackPtr);
     646        iBuilder->CreateBlockAlignedStore(Constant::getNullValue(getCarryPackType()), mCarryPackPtr);
    640647    }
    641648    return carryIn;
     
    647654void CarryManager::setNextCarryOut(Value * carryOut) {
    648655    assert (mCurrentFrameIndex < mCurrentFrame->getType()->getPointerElementType()->getStructNumElements());
    649     carryOut = iBuilder->CreateBitCast(carryOut, mCarryPackType);
     656    carryOut = iBuilder->CreateBitCast(carryOut, getCarryPackType());
    650657    if (mCarryInfo->hasSummary()) {
    651658        addToCarryOutSummary(carryOut);
     
    659666    }
    660667    ++mCurrentFrameIndex;
    661     assert (mCarryPackPtr->getType()->getPointerElementType() == mCarryPackType);
     668    assert (mCarryPackPtr->getType()->getPointerElementType() == getCarryPackType());
    662669    iBuilder->CreateBlockAlignedStore(carryOut, mCarryPackPtr);
    663670}
     
    690697    Value * const summary = iBuilder->CreateBlockAlignedLoad(ptr);
    691698    if (mLoopDepth != 0 && mCarryInfo->hasExplicitSummary()) {
    692         iBuilder->CreateBlockAlignedStore(Constant::getNullValue(mCarryPackType), ptr);
     699        iBuilder->CreateBlockAlignedStore(Constant::getNullValue(getCarryPackType()), ptr);
    693700    }
    694701    return summary;
     
    767774    assert (scope != mKernel->getEntryBlock() || mCarryScopes == 0);
    768775    assert (mCarryScopes < mCarryMetadata.size());
    769     assert (mCarryPackType);
     776    assert (getCarryPackType());
    770777
    771778    const unsigned carryScopeIndex = mCarryScopes++;
    772779    const bool nonCarryCollapsingMode = hasIterationSpecificAssignment(scope);
    773     Type * const carryPackType = (loopDepth == 0) ? mCarryPackType : ArrayType::get(mCarryPackType, 2);
     780    Type * const carryPackType = (loopDepth == 0) ? getCarryPackType() : ArrayType::get(getCarryPackType(), 2);
    774781    std::vector<Type *> state;
    775782
     
    779786            Type * type = carryPackType;
    780787            if (LLVM_UNLIKELY(amount >= LONG_ADVANCE_BREAKPOINT)) {
    781                 const unsigned blocks = ceil_udiv(amount, mBitBlockWidth);
    782                 type = ArrayType::get(mBitBlockType, nearest_pow2(blocks + ((loopDepth != 0) ? 1 : 0)));
    783                 if (LLVM_UNLIKELY(ifDepth > 0 && amount > mBitBlockWidth)) {
     788                const unsigned blocks = ceil_udiv(amount, getBitBlockWidth());
     789                type = ArrayType::get(getBitBlockType(), nearest_pow2(blocks + ((loopDepth != 0) ? 1 : 0)));
     790                if (LLVM_UNLIKELY(ifDepth > 0 && amount > getBitBlockWidth())) {
    784791                    // 1 bit will mark the presense of any bit in each block.
    785                     Type * carryType = ArrayType::get(mBitBlockType, ceil_udiv(amount, mBitBlockWidth * mBitBlockWidth));
     792                    Type * carryType = ArrayType::get(getBitBlockType(), ceil_udiv(amount, getBitBlockWidth() * getBitBlockWidth()));
    786793                    state.push_back(carryType);
    787794                }
     
    823830        // carry state pointer, and summary pointer struct.
    824831        if (LLVM_UNLIKELY(nonCarryCollapsingMode)) {
    825             carryState = StructType::get(iBuilder->getSizeTy(), carryState->getPointerTo(), mCarryPackType->getPointerTo(), nullptr);
     832            carryState = StructType::get(iBuilder->getSizeTy(), carryState->getPointerTo(), getCarryPackType()->getPointerTo(), nullptr);
    826833        }
    827834        cd.setNonCollapsingCarryMode(nonCarryCollapsingMode);
     
    831838}
    832839
     840inline unsigned CarryManager::getBitBlockWidth() const {
     841    return iBuilder->getBitBlockWidth();
     842}
     843
     844inline llvm::Type * CarryManager::getCarryPackType() const {
     845    return getBitBlockType();
     846}
     847
     848inline llvm::Type * CarryManager::getBitBlockType() const {
     849    return iBuilder->getBitBlockType();
     850}
     851
    833852/** ------------------------------------------------------------------------------------------------------------- *
    834853 * @brief constructor
    835854 ** ------------------------------------------------------------------------------------------------------------- */
    836 CarryManager::CarryManager(IDISA::IDISA_Builder * idb) noexcept
    837 : iBuilder(idb)
    838 , mKernel(nullptr)
    839 , mBitBlockType(idb->getBitBlockType())
    840 , mBitBlockWidth(idb->getBitBlockWidth())
     855CarryManager::CarryManager(PabloKernel * const kernel) noexcept
     856: mKernel(kernel)
     857, iBuilder(nullptr)
    841858, mCurrentFrame(nullptr)
    842859, mCurrentFrameIndex(0)
    843860, mCurrentScope(nullptr)
    844861, mCarryInfo(nullptr)
    845 , mCarryPackType(mBitBlockType)
    846862, mNextSummaryTest(nullptr)
    847863, mIfDepth(0)
Note: See TracChangeset for help on using the changeset viewer.