Changeset 4640


Ignore:
Timestamp:
Jul 5, 2015, 11:16:28 AM (4 years ago)
Author:
cameron
Message:

Pablo compiler refactoring step

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

Legend:

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

    r4628 r4640  
    415415}
    416416
    417 void PabloCompiler::compileBlock(const PabloBlock & blk) {
     417void PabloCompiler::compileBlock(PabloBlock & blk) {
     418    mPabloBlock = & blk;
    418419    for (const Statement * statement : blk) {
    419420        compileStatement(statement);
    420421    }
    421 }
    422 
    423 
     422    mPabloBlock = blk.getParent();
     423}
    424424
    425425
     
    505505}
    506506
    507 // If the following preload is turned off, we have incorrect results with the
    508 // ./icgrep -c '[A-Z]((([a-zA-Z]*a[a-zA-Z]*[ ])*[a-zA-Z]*e[a-zA-Z]*[ ])*[a-zA-Z]*s[a-zA-Z]*[ ])*[.?!]' ../performance/data/howto
    509    
    510 #define PRELOAD_WHILE_CARRIES_AT_TOP_LEVEL 1
    511507//#define SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
     508
     509#define LOAD_WHILE_CARRIES \
     510        if (mWhileDepth == 0) { \
     511            for (auto i = baseCarryDataIdx; i < baseCarryDataIdx + carryDataSize; ++i) { \
     512                mCarryInVector[i] = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(i)), BLOCK_SIZE/8, false); \
     513            } \
     514        }
     515
     516#define INITIALIZE_CARRY_IN_PHIS  \
     517        std::vector<PHINode *> carryInPhis(carryDataSize);  \
     518        for (unsigned index = 0; index < carryDataSize; ++index) {  \
     519            PHINode * phi_in = mBuilder->CreatePHI(mBitBlockType, 2); \
     520            phi_in->addIncoming(mCarryInVector[baseCarryDataIdx + index], whileEntryBlock); \
     521            carryInPhis[index] = phi_in; \
     522        }
     523
     524#define INITIALIZE_CARRY_OUT_ACCUMULATOR_PHIS \
     525        std::vector<PHINode *> carryOutAccumPhis(carryDataSize); \
     526        for (unsigned index = 0; index < carryDataSize; ++index) { \
     527            PHINode * phi_out = mBuilder->CreatePHI(mBitBlockType, 2); \
     528            phi_out->addIncoming(mZeroInitializer, whileEntryBlock); \
     529            carryOutAccumPhis[index] = phi_out; \
     530            mCarryOutVector[baseCarryDataIdx + index] = mZeroInitializer; \
     531        }
     532
     533#define EXTEND_CARRY_IN_PHIS_TO_ZERO_FROM_WHILE_BODY_FINAL_BLOCK \
     534        for (unsigned index = 0; index < carryDataSize; ++index) { \
     535            carryInPhis[index]->addIncoming(mZeroInitializer, whileBodyFinalBlock); \
     536        }
     537
     538#define EXTEND_CARRY_OUT_ACCUMULATOR_PHIS_TO_OR_COMBINE_CARRY_OUT \
     539        for (unsigned index = 0; index < carryDataSize; ++index) { \
     540            PHINode * phi = carryOutAccumPhis[index]; \
     541            Value * carryOut = mBuilder->CreateOr(phi, mCarryOutVector[baseCarryDataIdx + index]); \
     542            phi->addIncoming(carryOut, whileBodyFinalBlock); \
     543            mCarryOutVector[baseCarryDataIdx + index] = carryOut; \
     544        }
     545
     546#define STORE_WHILE_CARRY_DATA \
     547        if (mWhileDepth == 0) { \
     548            for (unsigned index = baseCarryDataIdx; index < baseCarryDataIdx + carryDataSize; ++index) { \
     549                mBuilder->CreateAlignedStore(mCarryOutVector[index], mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(index)), BLOCK_SIZE/8, false); \
     550            } \
     551        }
     552
    512553
    513554void PabloCompiler::compileWhile(const While * whileStatement) {
     
    520561        const PabloBlockCarryData & cd = whileStatement -> getBody().carryData;
    521562        const unsigned baseCarryDataIdx = cd.getBlockCarryDataIndex();
    522 
    523 #ifdef PRELOAD_WHILE_CARRIES_AT_TOP_LEVEL
    524563        const unsigned carryDataSize = cd.getTotalCarryDataSize();
    525         if (mWhileDepth == 0)
    526 #else
    527         const unsigned carryDataSize = cd.getLocalCarryDataSize();
    528 #endif
    529         {
    530             for (auto i = baseCarryDataIdx; i < baseCarryDataIdx + carryDataSize; ++i) {
    531                 mCarryInVector[i] = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(i)), BLOCK_SIZE/8, false);
    532             }
    533         }
     564
     565
     566        LOAD_WHILE_CARRIES
     567
    534568        SmallVector<const Next*, 4> nextNodes;
    535569        SmallVector<PHINode *, 4> nextPhis;
     
    554588
    555589#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
    556         std::vector<PHINode *> carryInPhis(carryDataSize);
    557 #endif
    558         std::vector<PHINode *> carryOutAccumPhis(carryDataSize);
    559    
    560         // Set initial values of phi nodes for loop body using values at while entry.
    561         for (unsigned index = 0; index < carryDataSize; ++index) {
    562 #ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
    563             PHINode * phi_in = mBuilder->CreatePHI(mBitBlockType, 2);
    564             phi_in->addIncoming(mCarryInVector[baseCarryDataIdx + index], whileEntryBlock);
    565             carryInPhis[index] = phi_in;
    566             mCarryInVector[baseCarryDataIdx + index] = phi_in;
    567 #endif
    568             PHINode * phi_out = mBuilder->CreatePHI(mBitBlockType, 2);
    569             phi_out->addIncoming(mZeroInitializer, whileEntryBlock);
    570             carryOutAccumPhis[index] = phi_out;
    571             mCarryOutVector[baseCarryDataIdx + index] = mZeroInitializer;
    572         }
     590        INITIALIZE_CARRY_IN_PHIS
     591#endif
     592
     593        INITIALIZE_CARRY_OUT_ACCUMULATOR_PHIS
    573594   
    574595        // for any Next nodes in the loop body, initialize to (a) pre-loop value.
     
    589610   
    590611        BasicBlock * whileBodyFinalBlock = mBuilder->GetInsertBlock();
    591         // Add the phiNode branches for carry in, carry out nodes.
    592         for (unsigned index = 0; index < carryDataSize; ++index) {
     612
    593613#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
    594             carryInPhis[index]->addIncoming(mZeroInitializer, whileBodyFinalBlock);
    595 #endif
    596             PHINode * phi = carryOutAccumPhis[index];
    597             Value * carryOut = mBuilder->CreateOr(phi, mCarryOutVector[baseCarryDataIdx + index]);
    598             phi->addIncoming(carryOut, whileBodyFinalBlock);
    599             mCarryOutVector[baseCarryDataIdx + index] = carryOut;
    600         }
     614        EXTEND_CARRY_IN_PHIS_TO_ZERO_FROM_WHILE_BODY_FINAL_BLOCK
     615#endif
     616
     617        EXTEND_CARRY_OUT_ACCUMULATOR_PHIS_TO_OR_COMBINE_CARRY_OUT
    601618
    602619        // Terminate the while loop body with a conditional branch back.
     
    620637        --mWhileDepth;
    621638
    622 #ifdef PRELOAD_WHILE_CARRIES_AT_TOP_LEVEL
    623         if (mWhileDepth == 0)
    624 #endif
    625         {
    626             for (unsigned index = baseCarryDataIdx; index < baseCarryDataIdx + carryDataSize; ++index) {
    627                 mBuilder->CreateAlignedStore(mCarryOutVector[index], mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(index)), BLOCK_SIZE/8, false);
    628             }
    629         }
    630 }
     639        STORE_WHILE_CARRY_DATA
     640}
     641
    631642
    632643void PabloCompiler::compileStatement(const Statement * stmt)
     
    713724        int shift = adv->getAdvanceAmount();
    714725        unsigned advance_index = adv->getLocalAdvanceIndex();
    715         Value * expr = genAdvanceWithCarry(strm_value, shift, advance_index, stmt->getParent());
     726        Value * expr = genAdvanceWithCarry(strm_value, shift, advance_index);
    716727        if (DumpTrace) {
    717728            genPrintRegister(stmt->getName()->to_string(), expr);
     
    726737        Value * marker_and_cc = mBuilder->CreateAnd(marker, cc);
    727738        unsigned carry_index = mstar->getLocalCarryIndex();
    728         Value * expr = mBuilder->CreateOr(mBuilder->CreateXor(genAddWithCarry(marker_and_cc, cc, carry_index, stmt->getParent()), cc), marker, "matchstar");
     739        Value * expr = mBuilder->CreateOr(mBuilder->CreateXor(genAddWithCarry(marker_and_cc, cc, carry_index), cc), marker, "matchstar");
    729740        if (DumpTrace) {
    730741            genPrintRegister(stmt->getName()->to_string(), expr);
     
    738749        Value * cc_expr = compileExpression(sthru->getScanThru());
    739750        unsigned carry_index = sthru->getLocalCarryIndex();
    740         Value * expr = mBuilder->CreateAnd(genAddWithCarry(marker_expr, cc_expr, carry_index, stmt->getParent()), genNot(cc_expr), "scanthru");
     751        Value * expr = mBuilder->CreateAnd(genAddWithCarry(marker_expr, cc_expr, carry_index), genNot(cc_expr), "scanthru");
    741752        if (DumpTrace) {
    742753            genPrintRegister(stmt->getName()->to_string(), expr);
     
    827838
    828839
    829 Value* PabloCompiler::genAddWithCarry(Value* e1, Value* e2, unsigned localIndex, const PabloBlock * blk) {
    830     const PabloBlockCarryData & cd = blk->carryData;
     840Value* PabloCompiler::genAddWithCarry(Value* e1, Value* e2, unsigned localIndex) {
     841    const PabloBlockCarryData & cd = mPabloBlock->carryData;
    831842    const unsigned carryIdx = cd.carryOpCarryDataOffset(localIndex);
    832843    Value* carryq_value = genCarryDataLoad(carryIdx);
     
    939950}
    940951
    941 Value* PabloCompiler::genAdvanceWithCarry(Value* strm_value, int shift_amount, unsigned localIndex, const PabloBlock * blk) {
     952Value* PabloCompiler::genAdvanceWithCarry(Value* strm_value, int shift_amount, unsigned localIndex) {
    942953    if (shift_amount >= LongAdvanceBase) {
    943         return genLongAdvanceWithCarry(strm_value, shift_amount, localIndex, blk);
     954        return genLongAdvanceWithCarry(strm_value, shift_amount, localIndex);
    944955    }
    945956    else if (shift_amount == 1) {
    946         return genUnitAdvanceWithCarry(strm_value, localIndex, blk);
    947     }
    948     const PabloBlockCarryData & cd = blk->carryData;
     957        return genUnitAdvanceWithCarry(strm_value, localIndex);
     958    }
     959    const PabloBlockCarryData & cd = mPabloBlock->carryData;
    949960    const auto advanceIndex = cd.shortAdvanceCarryDataOffset(localIndex);
    950961    Value* result_value;
     
    963974}
    964975                   
    965 Value* PabloCompiler::genUnitAdvanceWithCarry(Value* strm_value, unsigned localIndex, const PabloBlock * blk) {
    966     const PabloBlockCarryData & cd = blk->carryData;
     976Value* PabloCompiler::genUnitAdvanceWithCarry(Value* strm_value, unsigned localIndex) {
     977    const PabloBlockCarryData & cd = mPabloBlock->carryData;
    967978    const auto advanceIndex = cd.unitAdvanceCarryDataOffset(localIndex);
    968979    Value* result_value;
     
    9951006// Generate code for long advances >= LongAdvanceBase
    9961007//
    997 Value* PabloCompiler::genLongAdvanceWithCarry(Value* strm_value, int shift_amount, unsigned localIndex, const PabloBlock * blk) {
    998     const PabloBlockCarryData & cd = blk->carryData;
     1008Value* PabloCompiler::genLongAdvanceWithCarry(Value* strm_value, int shift_amount, unsigned localIndex) {
     1009    const PabloBlockCarryData & cd = mPabloBlock->carryData;
    9991010    const unsigned block_shift = shift_amount % BLOCK_SIZE;
    10001011    const unsigned advanceEntries = cd.longAdvanceEntries(shift_amount);
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.h

    r4628 r4640  
    108108
    109109    void genPrintRegister(std::string regName, Value * bitblockValue);
    110     void compileBlock(const PabloBlock & blk);
     110    void compileBlock(PabloBlock & blk);
    111111    void compileStatement(const Statement * stmt);
    112112    void compileIf(const If * ifStmt);
     
    115115    Value* genCarryDataLoad(const unsigned index);
    116116    void   genCarryDataStore(Value* carryOut, const unsigned index);
    117     Value* genAddWithCarry(Value* e1, Value* e2, unsigned localIndex, const PabloBlock * blk);
    118     Value* genAdvanceWithCarry(Value* e1, int shift_amount, unsigned localIndex, const PabloBlock * blk);
    119     Value* genUnitAdvanceWithCarry(Value* e1, unsigned localIndex, const PabloBlock * blk);
    120     Value* genLongAdvanceWithCarry(Value* e1, int shift_amount, unsigned localIndex, const PabloBlock * blk);
     117    Value* genAddWithCarry(Value* e1, Value* e2, unsigned localIndex);
     118    Value* genAdvanceWithCarry(Value* e1, int shift_amount, unsigned localIndex);
     119    Value* genUnitAdvanceWithCarry(Value* e1, unsigned localIndex);
     120    Value* genLongAdvanceWithCarry(Value* e1, int shift_amount, unsigned localIndex);
    121121    Value* genBitBlockAny(Value* test);
    122122    Value* genShiftHighbitToLow(unsigned FieldWidth, Value * op);
     
    153153    PointerType*                        mBasisBitsInputPtr;
    154154
     155    PabloBlock *                        mPabloBlock;
     156   
    155157    Value*                              mCarryDataPtr;
    156158    Value*                              mBlockNo;
  • icGREP/icgrep-devel/icgrep/pablo/ps_if.h

    r4594 r4640  
    3737        return mBody;
    3838    }
    39     inline const PabloBlock & getBody() const {
     39    inline PabloBlock & getBody() const {
    4040        return mBody;
    4141    }
  • icGREP/icgrep-devel/icgrep/pablo/ps_while.h

    r4541 r4640  
    2929        return mBody;
    3030    }
    31     inline const PabloBlock & getBody() const {
     31    inline PabloBlock & getBody() const {
    3232        return mBody;
    3333    }
Note: See TracChangeset for help on using the changeset viewer.