Ignore:
Timestamp:
Nov 18, 2016, 1:46:55 PM (2 years ago)
Author:
nmedfort
Message:

Merged PabloFunction? and PabloKernel? classes. Updated projects where necessary.

File:
1 edited

Legend:

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

    r5204 r5217  
    1414enum SS_struct_index {iProducer_pos = 0, iConsumer_pos = 1, iEnd_of_input = 2, iBuffer_ptr = 3};
    1515
    16 llvm::PointerType * StreamSetBuffer::getStreamBufferPointerType() {
    17     return PointerType::get(mStreamSetType, mAddrSpace);
    18 }
    19 
    20 llvm::PointerType * StreamSetBuffer::getStreamSetStructPointerType() {
    21     return PointerType::get(mStreamSetStructType, 0);
    22 }
    23 
    2416llvm::Value * StreamSetBuffer::getProducerPosPtr(Value * bufferStructPtr) {
    2517    return iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)});
    2618}
    2719
    28 void StreamSetBuffer::setProducerPos(Value * bufferStructPtr, llvm::Value * pos){
    29     iBuilder->CreateStore(pos, iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
     20void StreamSetBuffer::setProducerPos(Value * bufferStructPtr, llvm::Value * pos) {
     21    iBuilder->CreateStore(pos, getProducerPosPtr(bufferStructPtr));
    3022}
    3123
     
    3426}
    3527
    36 void StreamSetBuffer::setConsumerPos(Value * bufferStructPtr, Value * pos){
    37     iBuilder->CreateStore(pos, iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
     28void StreamSetBuffer::setConsumerPos(Value * bufferStructPtr, Value * pos) {
     29    iBuilder->CreateStore(pos, getConsumerPosPtr(bufferStructPtr));
    3830}
    3931
    40 llvm::Value * StreamSetBuffer::hasEndOfInputPtr(Value * bufferStructPtr) {
     32llvm::Value * StreamSetBuffer::getEndOfInputPtr(Value * bufferStructPtr) {
    4133    return iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)});
    4234}
    4335
    44 void StreamSetBuffer::setEndOfInput(Value * bufferStructPtr){
    45     iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt1Ty(), 1), iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
     36void StreamSetBuffer::setEndOfInput(Value * bufferStructPtr) {
     37    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt1Ty(), 1), getEndOfInputPtr(bufferStructPtr));
    4638}
    4739
    48 llvm::Value * StreamSetBuffer::getStreamSetStructPtr(){
    49     return mStreamSetStructPtr;
    50 }
    51 
    52 llvm::Value * StreamSetBuffer::allocateBuffer() {
     40void StreamSetBuffer::allocateBuffer() {
    5341    Type * const size_ty = iBuilder->getSizeTy();
    5442    Type * const int1ty = iBuilder->getInt1Ty();
    5543    mStreamSetBufferPtr = iBuilder->CreateCacheAlignedAlloca(mStreamSetType, ConstantInt::get(iBuilder->getSizeTy(), mBufferBlocks));
    5644    mStreamSetStructPtr = iBuilder->CreateCacheAlignedAlloca(mStreamSetStructType);
    57 
    5845    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
    5946    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
    6047    iBuilder->CreateStore(ConstantInt::get(int1ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
    6148    iBuilder->CreateStore(mStreamSetBufferPtr, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)}));
    62    
    63     return mStreamSetBufferPtr;
    64 }
    65 
    66 llvm::Value * StreamSetBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
    67     Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
    68     return iBuilder->CreateGEP(iBuilder->CreateLoad(handle), {blockNo});
    6949}
    7050
    7151// Single Block Buffer
    7252// For a single block buffer, the block pointer is always the buffer base pointer.
    73 llvm::Value * SingleBlockBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
     53llvm::Value * SingleBlockBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value *) {
    7454    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
    7555    return iBuilder->CreateLoad(handle);
     
    10989}
    11090
    111 llvm::Value * ExternalFileBuffer::allocateBuffer() {
     91void ExternalFileBuffer::allocateBuffer() {
    11292    throw std::runtime_error("External buffers cannot be allocated.");
    11393}
     
    11595llvm::Value * ExternalFileBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
    11696    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
    117     return iBuilder->CreateGEP(iBuilder->CreateLoad(handle), {blockNo});
     97    Value * bufPtr = iBuilder->CreateLoad(handle);
     98    return iBuilder->CreateGEP(bufPtr, blockNo);
    11899}
    119100
     
    121102
    122103llvm::Value * CircularBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
     104    assert (blockNo->getType()->isIntegerTy());
     105
    123106    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
    124     // Circular access is based on blockNo mod mBufferBlocks.  For power of 2 buffer sizes (required), we
    125     // use bitwise masking to efficiently compute the mod function  (blockNo & (mBufferBlocks - 1)
    126107    Value * bufPtr = iBuilder->CreateLoad(handle);
    127     //iBuilder->CallPrintInt("CircularBuffer bufPtr", iBuilder->CreatePtrToInt(bufPtr, iBuilder->getSizeTy()));
    128     return iBuilder->CreateGEP(bufPtr, {iBuilder->CreateAnd(blockNo, ConstantInt::get(iBuilder->getSizeTy(), mBufferBlocks-1))});
     108    Value * offset = nullptr;
     109    if (mBufferBlocks == 1) {
     110        offset = ConstantInt::getNullValue(iBuilder->getSizeTy());
     111    } else if ((mBufferBlocks & (mBufferBlocks - 1)) == 0) { // is power of 2
     112        offset = iBuilder->CreateAnd(blockNo, ConstantInt::get(blockNo->getType(), mBufferBlocks - 1));
     113    } else {
     114        offset = iBuilder->CreateURem(blockNo, ConstantInt::get(blockNo->getType(), mBufferBlocks));
     115    }
     116    return iBuilder->CreateGEP(bufPtr, offset);
    129117}
    130118
     
    136124    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
    137125    Value * bufPtr = iBuilder->CreateLoad(handle);
    138     //iBuilder->CallPrintInt("LinearBuffer bufPtr", iBuilder->CreatePtrToInt(bufPtr, iBuilder->getSizeTy()));
    139126    return iBuilder->CreateGEP(bufPtr, {iBuilder->CreateSub(blockNo, consumerBlock)});
    140127}
     
    144131    Type * const i8 = iBuilder->getInt8Ty();
    145132    Type * const i32 = iBuilder->getInt32Ty();
    146     Type * const i8_ptr = PointerType::get(i8, mAddrSpace);
     133    Type * const i8_ptr = i8->getPointerTo(mAddrSpace);
     134    IntegerType * const sizeTy = iBuilder->getSizeTy();
    147135
     136    Module * const M = iBuilder->getModule();
    148137
    149     Module * M = iBuilder->getModule();
     138    Function * const memmoveFunc = cast<Function>(M->getOrInsertFunction("llvm.memmove.p0i8.p0i8.i" + std::to_string(sizeTy->getBitWidth()),
     139                                                                  iBuilder->getVoidTy(), i8_ptr, i8_ptr, sizeTy, i32, i1, nullptr));
     140    Function * const current = iBuilder->GetInsertBlock()->getParent();
     141    BasicBlock * const copyBackBody = BasicBlock::Create(M->getContext(), "copy_back", current, 0);
     142    BasicBlock * const setConsumerPosExit = BasicBlock::Create(M->getContext(), "setConsumerPos_done", current, 0);
     143    Constant * const blockWidth = ConstantInt::get(sizeTy, iBuilder->getStride());
    150144
    151     IntegerType * sizeTy = iBuilder->getSizeTy();
     145    Constant * const one = ConstantInt::get(sizeTy, 1);
    152146
    153     Function * memmoveFunc = cast<Function>(M->getOrInsertFunction("llvm.memmove.p0i8.p0i8.i" + std::to_string(sizeTy->getBitWidth()),
    154                                                                   iBuilder->getVoidTy(), i8_ptr, i8_ptr, sizeTy, i32, i1, nullptr));
    155     Function * current = iBuilder->GetInsertBlock()->getParent();
    156     BasicBlock * copyBackBody = BasicBlock::Create(M->getContext(), "copy_back", current, 0);
    157     BasicBlock * setConsumerPosExit = BasicBlock::Create(M->getContext(), "setConsumerPos_done", current, 0);
    158     Constant * blockWidth = ConstantInt::get(sizeTy, iBuilder->getStride());
    159     Constant * one = ConstantInt::get(sizeTy, 1);
    160     Value * consumerPos_ptr = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)});
    161     Value * consumerPos = iBuilder->CreateLoad(consumerPos_ptr);
    162     Value * consumerBlock = iBuilder->CreateUDiv(consumerPos, blockWidth);
     147    Value * const consumerPosPtr = getConsumerPosPtr(bufferStructPtr);
     148    Value * const consumerPos = iBuilder->CreateLoad(consumerPosPtr);
     149
    163150    // Ensure that the new consumer position is no less than the current position.
    164151    new_consumer_pos = iBuilder->CreateSelect(iBuilder->CreateICmpULT(new_consumer_pos, consumerPos), consumerPos, new_consumer_pos);
    165152    Value * producerPos = iBuilder->CreateLoad(iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
     153
    166154    // Ensure that the new consumer position is no greater than the current producer position.
    167155    Value * new_pos_lt_producer_pos = iBuilder->CreateICmpULT(new_consumer_pos, producerPos);
    168156    new_consumer_pos = iBuilder->CreateSelect(new_pos_lt_producer_pos, new_consumer_pos, producerPos);
     157
    169158    // Now, the new_consumer_pos is at most = to the producer_pos; if =, we're done.
    170159    iBuilder->CreateCondBr(new_pos_lt_producer_pos, copyBackBody, setConsumerPosExit);
     
    172161   
    173162    Value * new_consumer_block = iBuilder->CreateUDiv(new_consumer_pos, blockWidth);
    174    
    175163    Value * lastProducerBlock = iBuilder->CreateUDiv(iBuilder->CreateSub(producerPos, one), blockWidth);
    176     //iBuilder->CallPrintInt("new_consumer_block", new_consumer_block);
    177     //iBuilder->CallPrintInt("lastProducerBlock", lastProducerBlock);
    178 
    179164    Value * copyBlocks = iBuilder->CreateAdd(iBuilder->CreateSub(lastProducerBlock, new_consumer_block), one);
    180165
     
    182167
    183168    Constant * blockBytes = ConstantInt::get(sizeTy, dl.getTypeAllocSize(mStreamSetType) * iBuilder->getStride());
     169
    184170    Value * copyLength = iBuilder->CreateMul(copyBlocks, blockBytes);
    185     //iBuilder->CallPrintInt("memmove copyLength", copyLength);
     171
    186172    // Must copy back one full block for each of the streams in the stream set.
    187173    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
    188174    Value * bufferPtr = iBuilder->CreateLoad(handle);
    189     //iBuilder->CallPrintInt("memmove bufferPtr", iBuilder->CreatePtrToInt(bufferPtr, sizeTy));
    190 
     175    Value * const consumerBlock = iBuilder->CreateUDiv(consumerPos, blockWidth);
    191176    Value * copyFrom = iBuilder->CreateGEP(bufferPtr, {iBuilder->CreateSub(new_consumer_block, consumerBlock)});
    192     //iBuilder->CallPrintInt("memmove copyFrom", iBuilder->CreatePtrToInt(copyFrom, sizeTy));
    193     Value * alignment = ConstantInt::get(iBuilder->getInt32Ty(), iBuilder->getBitBlockWidth()/8);
     177    Value * alignment = ConstantInt::get(iBuilder->getInt32Ty(), iBuilder->getBitBlockWidth() / 8);
    194178   
    195179    iBuilder->CreateCall(memmoveFunc, {iBuilder->CreateBitCast(bufferPtr, i8_ptr), iBuilder->CreateBitCast(copyFrom, i8_ptr), copyLength, alignment, ConstantInt::getNullValue(i1)});
     
    197181    // Copy back done, store the new consumer position.
    198182    iBuilder->SetInsertPoint(setConsumerPosExit);
    199     iBuilder->CreateStore(new_consumer_pos, consumerPos_ptr);
     183
     184    iBuilder->CreateStore(new_consumer_pos, consumerPosPtr);
    200185}   
Note: See TracChangeset for help on using the changeset viewer.