Changeset 5618


Ignore:
Timestamp:
Aug 25, 2017, 11:20:16 PM (3 months ago)
Author:
cameron
Message:

Dynamic buffer tracing; calculations for available items/blocks in reverse mode

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

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/kernels/streamset.cpp

    r5615 r5618  
    140140}
    141141
    142 Value * StreamSetBuffer::getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromPosition) const {
     142Value * StreamSetBuffer::getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromPosition, bool reverse) const {
    143143    if (isa<ArrayType>(mType) && dyn_cast<ArrayType>(mType)->getNumElements() > 1) {
    144144        Constant * stride = iBuilder->getSize(iBuilder->getStride());
    145         return iBuilder->CreateSub(stride, iBuilder->CreateURem(fromPosition, stride));
     145        Value * strideRem = iBuilder->CreateURem(fromPosition, stride);
     146        if (reverse) {
     147            return iBuilder->CreateSelect(iBuilder->CreateICmpEQ(strideRem, iBuilder->getSize(0)), stride, strideRem);
     148        }
     149        else return iBuilder->CreateSub(stride, strideRem);
    146150    } else {
    147151        Constant * bufSize = iBuilder->getSize(mBufferBlocks * iBuilder->getStride());
    148         return iBuilder->CreateSub(bufSize, iBuilder->CreateURem(fromPosition, bufSize, "linearItems"));
    149     }
    150 }
    151 
    152 Value * StreamSetBuffer::getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromBlock) const {
     152        Value * bufRem = iBuilder->CreateURem(fromPosition, bufSize);
     153        if (reverse) {
     154            return iBuilder->CreateSelect(iBuilder->CreateICmpEQ(bufRem, iBuilder->getSize(0)), bufSize, bufRem);
     155        }
     156        else return iBuilder->CreateSub(bufSize, bufRem, "linearItems");
     157    }
     158}
     159
     160Value * StreamSetBuffer::getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromBlock, bool reverse) const {
    153161    Constant * bufBlocks = iBuilder->getSize(mBufferBlocks);
    154     return iBuilder->CreateSub(bufBlocks, iBuilder->CreateURem(fromBlock, bufBlocks), "linearBlocks");
    155 }
    156 
    157 Value * StreamSetBuffer::getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromPosition) const {
    158     return getLinearlyAccessibleItems(iBuilder, self, fromPosition);
    159 }
    160 
    161 Value * StreamSetBuffer::getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromBlock) const {
    162     return getLinearlyAccessibleBlocks(iBuilder, self, fromBlock);
     162    Value * bufRem = iBuilder->CreateURem(fromBlock, bufBlocks);
     163    if (reverse) {
     164        return iBuilder->CreateSelect(iBuilder->CreateICmpEQ(bufRem, iBuilder->getSize(0)), bufBlocks, bufRem);
     165    }
     166    else return iBuilder->CreateSub(bufBlocks, bufRem, "linearBlocks");
     167}
     168
     169Value * StreamSetBuffer::getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromPosition, bool reverse) const {
     170    return getLinearlyAccessibleItems(iBuilder, self, fromPosition, reverse);
     171}
     172
     173Value * StreamSetBuffer::getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromBlock, bool reverse) const {
     174    return getLinearlyAccessibleBlocks(iBuilder, self, fromBlock, reverse);
    163175}
    164176
     
    257269}
    258270
    259 Value * SourceBuffer::getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromPosition) const {
     271Value * SourceBuffer::getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromPosition, bool reverse) const {
     272    if (reverse) report_fatal_error("SourceBuffer cannot be accessed in reverse");
    260273    return iBuilder->CreateSub(getCapacity(iBuilder, self), fromPosition);
    261274}
    262275
    263 Value * SourceBuffer::getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromBlock) const {
     276Value * SourceBuffer::getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromBlock, bool reverse) const {
     277    if (reverse) report_fatal_error("SourceBuffer cannot be accessed in reverse");
    264278    return iBuilder->CreateSub(iBuilder->CreateUDiv(getCapacity(iBuilder, self), iBuilder->getSize(iBuilder->getBitBlockWidth())), fromBlock);
    265279}
     
    292306}
    293307
    294 Value * ExternalBuffer::getLinearlyAccessibleItems(IDISA::IDISA_Builder * const, Value *, Value *) const {
     308Value * ExternalBuffer::getLinearlyAccessibleItems(IDISA::IDISA_Builder * const, Value *, Value *, bool reverse) const {
    295309    report_fatal_error("External buffers: getLinearlyAccessibleItems is not supported.");
    296310}
     
    328342}
    329343
    330 Value * CircularCopybackBuffer::getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromPosition) const {
    331     return iBuilder->CreateAdd(getLinearlyAccessibleItems(iBuilder, self, fromPosition), iBuilder->getSize(mOverflowBlocks * iBuilder->getBitBlockWidth()));
    332 }
    333 
    334 Value * CircularCopybackBuffer::getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromBlock) const {
    335     return iBuilder->CreateAdd(getLinearlyAccessibleBlocks(iBuilder, self, fromBlock), iBuilder->getSize(mOverflowBlocks));
     344Value * CircularCopybackBuffer::getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromPosition, bool reverse) const {
     345    Value * accessibleItems = getLinearlyAccessibleItems(iBuilder, self, fromPosition, reverse);
     346    if (reverse) return accessibleItems;
     347    return iBuilder->CreateAdd(accessibleItems, iBuilder->getSize(mOverflowBlocks * iBuilder->getBitBlockWidth()));
     348}
     349
     350Value * CircularCopybackBuffer::getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromBlock, bool reverse) const {
     351    Value * accessibleBlocks = getLinearlyAccessibleBlocks(iBuilder, self, fromBlock);
     352    if (reverse) return accessibleBlocks;
     353    return iBuilder->CreateAdd(accessibleBlocks, iBuilder->getSize(mOverflowBlocks));
    336354}
    337355
     
    392410}
    393411
    394 Value * SwizzledCopybackBuffer::getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromPosition) const {
    395     return iBuilder->CreateAdd(getLinearlyAccessibleItems(iBuilder, self, fromPosition), iBuilder->getSize(mOverflowBlocks * iBuilder->getBitBlockWidth()));
    396 }
    397 
    398 Value * SwizzledCopybackBuffer::getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromBlock) const {
    399     return iBuilder->CreateAdd(getLinearlyAccessibleBlocks(iBuilder, self, fromBlock), iBuilder->getSize(mOverflowBlocks));
     412Value * SwizzledCopybackBuffer::getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromPosition, bool reverse) const {
     413    Value * accessibleItems = getLinearlyAccessibleItems(iBuilder, self, fromPosition, reverse);
     414    if (reverse) return accessibleItems;
     415    return iBuilder->CreateAdd(accessibleItems, iBuilder->getSize(mOverflowBlocks * iBuilder->getBitBlockWidth()));
     416}
     417
     418Value * SwizzledCopybackBuffer::getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, Value * self, Value * fromBlock, bool reverse) const {
     419    Value * accessibleBlocks = getLinearlyAccessibleBlocks(iBuilder, self, fromBlock);
     420    if (reverse) return accessibleBlocks;
     421    return iBuilder->CreateAdd(accessibleBlocks, iBuilder->getSize(mOverflowBlocks));
    400422}
    401423
     
    554576}
    555577
    556 Value * ExpandableBuffer::getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value *) const {
     578Value * ExpandableBuffer::getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, Value * self, Value *, bool reverse) const {
    557579    report_fatal_error("Expandable buffers: getLinearlyAccessibleItems is not supported.");
    558580}
     
    639661
    640662
    641 Value * DynamicBuffer::getLinearlyAccessibleItems(IDISA::IDISA_Builder * const b, Value * handle, Value * fromPosition) const {
     663Value * DynamicBuffer::getLinearlyAccessibleItems(IDISA::IDISA_Builder * const b, Value * handle, Value * fromPosition, bool reverse) const {
    642664    Constant * blockSize = b->getSize(b->getBitBlockWidth());
    643665    if (isa<ArrayType>(mType) && dyn_cast<ArrayType>(mType)->getNumElements() > 1) {
    644         return b->CreateSub(blockSize, b->CreateURem(fromPosition, blockSize));
     666        Value * blockRem = b->CreateURem(fromPosition, blockSize);
     667        if (reverse) {
     668            return b->CreateSelect(b->CreateICmpEQ(blockRem, b->getSize(0)), blockSize, blockRem);
     669        }
     670        else return b->CreateSub(blockSize, blockRem);
    645671    } else {
    646672        Value * const bufBlocks = b->CreateLoad(b->CreateGEP(handle, {b->getInt32(0), b->getInt32(int(Field::WorkingBlocks))}));
    647673        Value * bufSize = b->CreateMul(bufBlocks, blockSize);
    648         return b->CreateSub(bufSize, b->CreateURem(fromPosition, bufSize, "linearItems"));
    649     }
    650 }
    651 
    652 Value * DynamicBuffer::getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const b, Value * handle, Value * fromBlock) const {
     674        Value * bufRem = b->CreateURem(fromPosition, bufSize);
     675        if (reverse) {
     676            return b->CreateSelect(b->CreateICmpEQ(bufRem, b->getSize(0)), bufSize, bufRem);
     677        }
     678        else return b->CreateSub(bufSize, bufRem, "linearItems");
     679    }
     680}
     681
     682Value * DynamicBuffer::getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const b, Value * handle, Value * fromBlock, bool reverse) const {
    653683    Value * const bufBlocks = b->CreateLoad(b->CreateGEP(handle, {b->getInt32(0), b->getInt32(int(Field::WorkingBlocks))}));
    654     return b->CreateSub(bufBlocks, b->CreateURem(fromBlock, bufBlocks), "linearBlocks");
     684    Value * bufRem = b->CreateURem(fromBlock, bufBlocks);
     685    if (reverse) {
     686        return b->CreateSelect(b->CreateICmpEQ(bufRem, b->getSize(0)), bufBlocks, bufRem);
     687    }
     688    else return b->CreateSub(bufBlocks, bufRem, "linearBlocks");
    655689}
    656690
     
    674708    Type * bufPtrType = bufBasePtrField->getType()->getPointerElementType();
    675709    Value * bufPtr = b->CreatePointerCast(b->CreateCacheAlignedMalloc(bufSize), bufPtrType);
     710    if (codegen::DebugOptionIsSet(codegen::TraceDynamicBuffers)) {
     711        b->CallPrintInt("allocated: ", bufPtr);
     712        b->CallPrintInt("allocated capacity: ", bufSize);
     713    }
    676714    b->CreateStore(bufPtr, bufBasePtrField);
    677715    b->CreateStore(ConstantPointerNull::getNullValue(bufPtrType), b->CreateGEP(handle, {b->getInt32(0), b->getInt32(int(DynamicBuffer::Field::PriorBaseAddress))}));
     
    696734    b->CreateCondBr(priorBufIsNonNull, freePrior, freeCurrent);
    697735    b->SetInsertPoint(freePrior);
    698     //b->CallPrintInt("releasing: ", priorBuf);
     736    if (codegen::DebugOptionIsSet(codegen::TraceDynamicBuffers)) {
     737        b->CallPrintInt("releasing: ", priorBuf);
     738    }
    699739    b->CreateFree(priorBuf);
    700740    b->CreateBr(freeCurrent);
     
    722762   
    723763    Value * oldBufPtr = b->CreateLoad(bufBasePtrField);
    724     Value * const currentWorkingBlocks = b->CreateLoad(workingBlocksField);
    725     //b->CallPrintInt("currentWorkingBlocks: ", currentWorkingBlocks);
     764    Value * currentWorkingBlocks = b->CreateLoad(workingBlocksField);
    726765    Value * workingBytes = b->CreateMul(currentWorkingBlocks, blockBytes);
    727766    Value * const curAllocated = b->CreateLoad(capacityField);
     
    744783    b->CreateCondBr(priorBufIsNonNull, deallocatePrior, allocateNew);
    745784    b->SetInsertPoint(deallocatePrior);
    746     //b->CallPrintInt("deallocating: ", priorBuf);
     785    if (codegen::DebugOptionIsSet(codegen::TraceDynamicBuffers)) {
     786        b->CallPrintInt("deallocating: ", priorBuf);
     787    }
    747788    b->CreateFree(priorBuf);
    748789    b->CreateBr(allocateNew);
     
    750791    b->CreateStore(oldBufPtr, priorBasePtrField);
    751792    Value * newBufPtr = b->CreatePointerCast(b->CreateCacheAlignedMalloc(neededCapacity), bufPtrType);
    752     //b->CallPrintInt("allocated: ", newBufPtr);
    753     //b->CallPrintInt("allocated capacity: ", neededCapacity);
    754 
     793    if (codegen::DebugOptionIsSet(codegen::TraceDynamicBuffers)) {
     794        b->CallPrintInt("re-allocated: ", newBufPtr);
     795        b->CallPrintInt("allocated capacity: ", neededCapacity);
     796    }
    755797    b->CreateStore(newBufPtr, bufBasePtrField);
    756798    createBlockCopy(b, newBufPtr, oldBufPtr, currentWorkingBlocks);
     
    762804    bufPtr->addIncoming(newBufPtr, allocateNew);
    763805    createBlockCopy(b, b->CreateGEP(bufPtr, currentWorkingBlocks), bufPtr, currentWorkingBlocks);
    764     b->CreateStore(b->CreateAdd(currentWorkingBlocks, currentWorkingBlocks), workingBlocksField);
     806    currentWorkingBlocks = b->CreateAdd(currentWorkingBlocks, currentWorkingBlocks);
     807    if (codegen::DebugOptionIsSet(codegen::TraceDynamicBuffers)) {
     808        b->CallPrintInt("currentWorkingBlocks: ", currentWorkingBlocks);
     809    }
     810    b->CreateStore(currentWorkingBlocks, workingBlocksField);
    765811}
    766812
  • icGREP/icgrep-devel/icgrep/kernels/streamset.h

    r5612 r5618  
    8383   
    8484    // The number of items that cam be linearly accessed from a given logical stream position.
    85     virtual llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const;
    86    
    87     virtual llvm::Value * getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const;
     85    virtual llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition, bool reverse = false) const;
     86   
     87    virtual llvm::Value * getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock, bool reverse = false) const;
    8888   
    8989    void createBlockCopy(IDISA::IDISA_Builder * const iBuilder, llvm::Value * targetBlockPtr, llvm::Value * sourceBlockPtr, llvm::Value * blocksToCopy) const;
     
    9191    virtual void createBlockAlignedCopy(IDISA::IDISA_Builder * const iBuilder, llvm::Value * targetBlockPtr, llvm::Value * sourceBlockPtr, llvm::Value * itemsToCopy) const;
    9292
    93     virtual llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const;
    94    
    95     virtual llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const;
     93    virtual llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition, bool reverse = false) const;
     94   
     95    virtual llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock, bool reverse = false) const;
    9696   
    9797    virtual ~StreamSetBuffer() = 0;
     
    158158    llvm::Value * getCapacity(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
    159159   
    160     llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const override;
    161    
    162     llvm::Value * getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const override;
     160    llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition, bool reverse = false) const override;
     161   
     162    llvm::Value * getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock, bool reverse = false) const override;
    163163
    164164    llvm::Type * getStreamSetBlockType() const override;
     
    186186    ExternalBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, llvm::Value * addr, unsigned AddressSpace = 0);
    187187
    188     llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const override;
     188    llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition, bool reverse = false) const override;
    189189
    190190    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
     
    230230    void createCopyBack(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * overflowItems) const;
    231231       
    232     llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const override;
    233    
    234     llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const override;
     232    llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition, bool reverse = false) const override;
     233   
     234    llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock, bool reverse = false) const override;
    235235
    236236    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
     
    252252    void createCopyBack(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * overflowItems) const;
    253253   
    254     llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const override;
    255    
    256     llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const override;
     254    llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition, bool reverse = false) const override;
     255   
     256    llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock, bool reverse = false) const override;
    257257   
    258258    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
     
    279279    llvm::Value * getStreamPackPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, llvm::Value * packIndex, const bool readOnly) const override;
    280280
    281     llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const override;
     281    llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition, bool reverse = false) const override;
    282282
    283283    llvm::Value * getStreamSetCount(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
     
    310310    DynamicBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t initialCapacity, size_t overflowBlocks = 0, unsigned swizzleFactor = 1, unsigned addrSpace = 0);
    311311   
    312     llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const b, llvm::Value * handle, llvm::Value * fromPosition) const override;
    313    
    314     llvm::Value * getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const override;
     312    llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const b, llvm::Value * handle, llvm::Value * fromPosition, bool reverse = false) const override;
     313   
     314    llvm::Value * getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock, bool reverse = false) const override;
    315315
    316316    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & b) override;
  • icGREP/icgrep-devel/icgrep/toolchain/toolchain.cpp

    r5616 r5618  
    2929#endif
    3030                        clEnumVal(SerializeThreads, "Force segment threads to run sequentially."),
     31                        clEnumVal(TraceDynamicBuffers, "Show dynamic buffer allocations and deallocations."),
    3132                        clEnumValEnd), cl::cat(CodeGenOptions));
    3233
  • icGREP/icgrep-devel/icgrep/toolchain/toolchain.h

    r5486 r5618  
    3131    ShowASM,
    3232#endif
    33     SerializeThreads
     33    SerializeThreads,
     34    TraceDynamicBuffers
    3435};
    3536
Note: See TracChangeset for help on using the changeset viewer.