Changeset 4708


Ignore:
Timestamp:
Jul 30, 2015, 12:30:21 PM (4 years ago)
Author:
cameron
Message:

Move add-with-carry into carry manager

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

Legend:

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

    r4707 r4708  
    256256}
    257257
     258   
    258259/* maskSelectBitRange selects the bits of a pack from lo_bit through
    259  lo_bit + bitCount - 1, setting all other bits to zero.  */
    260 
     260   lo_bit + bitCount - 1, setting all other bits to zero.  */
     261   
    261262Value * CarryManager::maskSelectBitRange(Value * pack, unsigned lo_bit, unsigned bitCount) {
    262263    if (bitCount == PACK_SIZE) {
     
    267268    return mBuilder->CreateAnd(pack, mBuilder->getInt64(mask << lo_bit));
    268269}
    269 
     270   
    270271Value * CarryManager::getCarryInBits(unsigned carryBitPos, unsigned carryBitCount) {
    271272    unsigned packIndex = carryBitPos / PACK_SIZE;
     
    277278
    278279void CarryManager::extractAndSaveCarryOutBits(Value * bitblock, unsigned carryBit_pos, unsigned carryBitCount) {
    279    
    280280    unsigned packIndex = carryBit_pos / PACK_SIZE;
    281281    unsigned packOffset = carryBit_pos % PACK_SIZE;
     
    301301}
    302302   
    303     /*  NOTE: In the following the mCarryOutPack is an accumulator.
    304     It must be created at the appropriate outer level.  */
    305 void CarryManager::setCarryBits(unsigned carryBit_lo, unsigned carryRangeSize, Value * bits) {
    306    
    307     unsigned packIndex = carryBit_lo / PACK_SIZE;
    308     unsigned carryOffset = carryBit_lo % PACK_SIZE;
    309     if (carryOffset > 0) {
    310         bits = mBuilder->CreateShl(bits, mBuilder->getInt64(carryOffset));
    311     }
    312     if (mCarryOutPack[packIndex] == nullptr) {
    313         mCarryOutPack[packIndex] = bits;
    314         //std::cerr << "setCarryBits/initial , pack = " << packIndex << ", offset = " << carryOffset << ", count = " << carryRangeSize << std::endl;
    315     }
    316     else {
    317         //std::cerr << "setCarryBits/combine , pack = " << packIndex << ", offset = " << carryOffset << ", count = " << carryRangeSize << std::endl;
    318         mCarryOutPack[packIndex] = mBuilder->CreateOr(mCarryOutPack[packIndex], bits);
    319     }
    320 }
    321    
    322303   
    323304/* Methods for getting and setting individual carry values. */
     
    336317    unsigned posn = carryOpPosition(localIndex);
    337318#ifdef PACKING
    338     Value * field = iBuilder->mvmd_extract(PACK_SIZE, carry_out_strm, 0);
    339     //setCarryBits(posn, mBuilder->CreateLShr(field, mBuilder->getInt64(PACK_SIZE - 1)));
    340     setCarryBits(posn, 1, field);
    341 #else
    342     mCarryOutPack[posn] = carry_out_strm;
     319    extractAndSaveCarryOutBits(carry_out_strm, posn, 1);
     320#else
     321    Value * carry_bit = mBuilder->CreateLShr(mBuilder->CreateBitCast(carry_out_strm, mBuilder->getIntNTy(BLOCK_SIZE)), 127);
     322    mCarryOutPack[posn] = mBuilder->CreateBitCast(carry_bit, mBitBlockType);
    343323    if (mCarryInfo->getWhileDepth() == 0) {
    344324        storeCarryPack(posn);
     
    347327}
    348328
    349    
     329Value* CarryManager::genShiftLeft64(Value* e) {
     330    Value* i128_val = mBuilder->CreateBitCast(e, mBuilder->getIntNTy(BLOCK_SIZE));
     331    return mBuilder->CreateBitCast(mBuilder->CreateShl(i128_val, 64), mBitBlockType);
     332}
     333
     334Value * CarryManager::addCarryInCarryOut(int localIndex, Value* e1, Value* e2) {
     335    Value * carryq_value = getCarryOpCarryIn(localIndex);
     336    #if (BLOCK_SIZE == 128)
     337    //calculate carry through logical ops
     338    Value* carrygen = mBuilder->CreateAnd(e1, e2, "carrygen");
     339    Value* carryprop = mBuilder->CreateOr(e1, e2, "carryprop");
     340    Value* digitsum = mBuilder->CreateAdd(e1, e2, "digitsum");
     341    Value* partial = mBuilder->CreateAdd(digitsum, carryq_value, "partial");
     342    Value* digitcarry = mBuilder->CreateOr(carrygen, mBuilder->CreateAnd(carryprop, mBuilder->CreateNot(partial)));
     343    Value* mid_carry_in = genShiftLeft64(mBuilder->CreateLShr(digitcarry, 63));
     344    Value* sum = mBuilder->CreateAdd(partial, mBuilder->CreateBitCast(mid_carry_in, mBitBlockType), "sum");
     345    Value* carry_out_strm = mBuilder->CreateOr(carrygen, mBuilder->CreateAnd(carryprop, mBuilder->CreateNot(sum)));
     346
     347    #else
     348    //BLOCK_SIZE == 256, there is no other implementation
     349    static_assert(false, "Add with carry for 256-bit bitblock requires USE_UADD_OVERFLOW");
     350    #endif         
     351    setCarryOpCarryOut(localIndex, carry_out_strm);
     352    return sum;
     353}
     354
     355
    350356Value * CarryManager::advanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm) {
    351357    if (shift_amount == 1) {
  • icGREP/icgrep-devel/icgrep/pablo/carry_manager.h

    r4707 r4708  
    141141    Value * maskSelectBitRange(Value * pack, unsigned lo_bit, unsigned bitCount);     
    142142    Value * getCarryInBits(unsigned carryBitPos, unsigned bits);
    143     void setCarryBits(unsigned carryBit_lo, unsigned carryRangeSize, Value * bits);
    144143    void extractAndSaveCarryOutBits(Value * strm, unsigned carryBit_lo, unsigned carryBitCount);
    145144    Value * pack2bitblock(Value * pack);
     145    Value* genShiftLeft64(Value* e);
     146
    146147
    147148    unsigned absPosition(unsigned frameOffsetinBits, unsigned relPos);
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp

    r4703 r4708  
    365365}
    366366
    367 Value * PabloCompiler::genBitTest2(Value * e1, Value * e2) {
    368     Type * t1 = e1->getType();
    369     Type * t2 = e2->getType();
    370     if (t1 == mBitBlockType) {
    371         if (t2 == mBitBlockType) {
    372             return iBuilder.bitblock_any(mBuilder->CreateOr(e1, e2));
     367    Value * PabloCompiler::genBitTest2(Value * e1, Value * e2) {
     368        Type * t1 = e1->getType();
     369        Type * t2 = e2->getType();
     370        if (t1 == mBitBlockType) {
     371            if (t2 == mBitBlockType) {
     372                return iBuilder.bitblock_any(mBuilder->CreateOr(e1, e2));
     373            }
     374            else {
     375                Value * m1 = mBuilder->CreateZExt(iBuilder.hsimd_signmask(16, e1), t2);
     376                return mBuilder->CreateICmpNE(mBuilder->CreateOr(m1, e2), ConstantInt::get(t2, 0));
     377            }
     378        }
     379        else if (t2 == mBitBlockType) {
     380            Value * m2 = mBuilder->CreateZExt(iBuilder.hsimd_signmask(16, e2), t1);
     381            return mBuilder->CreateICmpNE(mBuilder->CreateOr(e1, m2), ConstantInt::get(t1, 0));
    373382        }
    374383        else {
    375             Value * m1 = mBuilder->CreateZExt(iBuilder.hsimd_signmask(16, e1), t2);
    376             return mBuilder->CreateICmpNE(mBuilder->CreateOr(m1, e2), ConstantInt::get(t2, 0));
    377         }
    378     }
    379     else if (t2 == mBitBlockType) {
    380         Value * m2 = mBuilder->CreateZExt(iBuilder.hsimd_signmask(16, e2), t1);
    381         return mBuilder->CreateICmpNE(mBuilder->CreateOr(e1, m2), ConstantInt::get(t1, 0));
    382     }
    383     else {
    384         return mBuilder->CreateICmpNE(mBuilder->CreateOr(e1, e2), ConstantInt::get(t1, 0));
    385     }
    386 }
    387 
    388 void PabloCompiler::compileIf(const If * ifStatement) {       
     384            return mBuilder->CreateICmpNE(mBuilder->CreateOr(e1, e2), ConstantInt::get(t1, 0));
     385        }
     386    }
     387   
     388    void PabloCompiler::compileIf(const If * ifStatement) {       
    389389    //
    390390    //  The If-ElseZero stmt:
     
    446446    // Create the phi Node for the summary variable, if needed.
    447447    mCarryManager->buildCarryDataPhisAfterIfBody(ifEntryBlock, ifBodyFinalBlock);
    448     //mCarryManager->addSummaryPhiIfNeeded(ifEntryBlock, ifBodyFinalBlock);
    449448    mCarryManager->leaveScope();
    450449}
     
    610609        Value * marker_and_cc = mBuilder->CreateAnd(marker, cc);
    611610        unsigned carry_index = mstar->getLocalCarryIndex();
    612         expr = mBuilder->CreateOr(mBuilder->CreateXor(genAddWithCarry(marker_and_cc, cc, carry_index), cc), marker, "matchstar");
     611        Value * sum = mCarryManager->addCarryInCarryOut(carry_index, marker_and_cc, cc);
     612        expr = mBuilder->CreateOr(mBuilder->CreateXor(sum, cc), marker, "matchstar");
    613613    }
    614614    else if (const ScanThru * sthru = dyn_cast<ScanThru>(stmt)) {
     
    616616        Value * cc_expr = compileExpression(sthru->getScanThru());
    617617        unsigned carry_index = sthru->getLocalCarryIndex();
    618         expr = mBuilder->CreateAnd(genAddWithCarry(marker_expr, cc_expr, carry_index), genNot(cc_expr), "scanthru");
     618        Value * sum = mCarryManager->addCarryInCarryOut(carry_index, marker_expr, cc_expr);
     619        expr = mBuilder->CreateAnd(sum, genNot(cc_expr), "scanthru");
    619620    }
    620621    else {
Note: See TracChangeset for help on using the changeset viewer.