Ignore:
Timestamp:
Oct 8, 2016, 7:33:08 PM (3 years ago)
Author:
cameron
Message:

Linear buffers; stdout_kernel; cache-aligned alloca, restructured u8u16, clean-ups

File:
1 edited

Legend:

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

    r5106 r5185  
    2121}
    2222
    23 //  Override the default void type for DoBlock functions.
    24 void stdOutKernel::prepareKernel() {
    25     setDoBlockReturnType(mStreamType);
    26     KernelBuilder::prepareKernel();
    27 }
    28 
    29 
     23// The doBlock method is deprecated.   But incase it is used, just call doSegment with
     24// 1 as the number of blocks to do.
    3025void stdOutKernel::generateDoBlockMethod() {
    3126    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    3227    Module * m = iBuilder->getModule();
     28    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
     29    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
     30    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
     31    Value * self = getParameter(doBlockFunction, "self");
     32    iBuilder->CreateCall(doSegmentFunction, {self, ConstantInt::get(iBuilder->getSizeTy(), 1)});
     33    iBuilder->CreateRetVoid();
     34    iBuilder->restoreIP(savePoint);
     35}
     36           
     37void stdOutKernel::generateDoSegmentMethod() {
     38    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
     39    Module * m = iBuilder->getModule();
    3340    Function * writefn = create_write(m);
    34     Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
     41    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
    3542    Type * i8PtrTy = iBuilder->getInt8PtrTy();
     43   
     44    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doSegmentFunction, 0));
     45    Constant * stride = ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride());
     46    Constant * strideBytes = ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride() * mCodeUnitWidth/8);
     47   
     48    Function::arg_iterator args = doSegmentFunction->arg_begin();
     49    Value * self = &*(args++);
     50    Value * blocksToDo = &*(args);
     51    ////iBuilder->CallPrintInt("blocksToDo", blocksToDo);
     52    Value * segmentNo = getLogicalSegmentNo(self);
     53    Value * streamStructPtr = getStreamSetStructPtr(self, "codeUnitBuffer");
     54    //iBuilder->CallPrintInt("streamStructPtr", iBuilder->CreatePtrToInt(streamStructPtr, iBuilder->getInt64Ty()));
    3655
    37     iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
     56    LoadInst * producerPos = iBuilder->CreateAlignedLoad(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr), sizeof(size_t));
     57    producerPos->setOrdering(AtomicOrdering::Acquire);
     58    //iBuilder->CallPrintInt("producerPos", producerPos);
    3859
    39     Value * self = getParameter(doBlockFunction, "self");
     60    Value * processed = getProcessedItemCount(self);
     61    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
     62    //iBuilder->CallPrintInt("previously processed", processed);
     63    Value * blocksAvail = iBuilder->CreateUDiv(itemsAvail, stride);
     64    //iBuilder->CallPrintInt("blocksAvail", blocksAvail);
     65    /* Adjust the number of full blocks to do, based on the available data, if necessary. */
     66    blocksToDo = iBuilder->CreateSelect(iBuilder->CreateICmpULT(blocksToDo, blocksAvail), blocksToDo, blocksAvail);
    4067    Value * blockNo = getScalarField(self, blockNoScalar);
    41     Value * inputStreamBlock = getStreamSetBlockPtr(self, "inputStreamSet", blockNo);
    42 
    43     Value * bufferPtr = getScalarField(self, "bufferPtr");
    44     Value * bufferFinalBlockPtr = getScalarField(self, "bufferFinalBlockPtr");
    45     //iBuilder->CallPrintInt("bufferPtr", iBuilder->CreatePtrToInt(bufferPtr, iBuilder->getInt64Ty()));
    46     //iBuilder->CallPrintInt("bufferFinalBlockPtr", iBuilder->CreatePtrToInt(bufferFinalBlockPtr, iBuilder->getInt64Ty()));
     68    //iBuilder->CallPrintInt("blockNo", blockNo);
     69    Value * basePtr = getStreamSetBlockPtr(self, "codeUnitBuffer", blockNo);
     70    //iBuilder->CallPrintInt("basePtr", iBuilder->CreatePtrToInt(basePtr, iBuilder->getInt64Ty()));
     71    Value * bytesToDo = iBuilder->CreateMul(blocksToDo, strideBytes);
     72    //iBuilder->CallPrintInt("bytesToDo", bytesToDo);
     73    iBuilder->CreateCall(writefn, std::vector<Value *>({iBuilder->getInt32(1), iBuilder->CreateBitCast(basePtr, i8PtrTy), bytesToDo}));
    4774   
     75    setScalarField(self, blockNoScalar, iBuilder->CreateAdd(blockNo, blocksToDo));
     76    processed = iBuilder->CreateAdd(processed, iBuilder->CreateMul(blocksToDo, stride));
     77    setProcessedItemCount(self, processed);
     78    mStreamSetInputBuffers[0]->setConsumerPos(streamStructPtr, processed);
     79    // Must be the last action, for synchronization.
     80    setLogicalSegmentNo(self, iBuilder->CreateAdd(segmentNo, ConstantInt::get(iBuilder->getSizeTy(), 1)));
    4881   
    49     BasicBlock * flushBlock = BasicBlock::Create(iBuilder->getContext(), "flush", doBlockFunction, 0);
    50     BasicBlock * exitBlock = BasicBlock::Create(iBuilder->getContext(), "exit", doBlockFunction, 0);
    51     Value * inFinal = iBuilder->CreateICmpUGT(bufferPtr, bufferFinalBlockPtr);
    52     iBuilder->CreateCondBr(inFinal, flushBlock, exitBlock);
    53    
    54     iBuilder->SetInsertPoint(flushBlock);
    55     Value * basePtr = getScalarField(self, "bufferBasePtr");
    56     //iBuilder->CallPrintInt("bufferBasePtr", iBuilder->CreatePtrToInt(basePtr, iBuilder->getInt64Ty()));
    57     Value * baseAddress = iBuilder->CreatePtrToInt(basePtr, iBuilder->getInt64Ty());
    58     Value * pointerAddress = iBuilder->CreatePtrToInt(bufferPtr, iBuilder->getInt64Ty());
    59     Value * bytesToFlush = iBuilder->CreateSub(pointerAddress, baseAddress);
    60    
    61     iBuilder->CreateCall(writefn, std::vector<Value *>({iBuilder->getInt32(1), iBuilder->CreateBitCast(basePtr, i8PtrTy), bytesToFlush}));
    62     // Buffer is flushed, return the buffer base pointer for subsequent output to the buffer.
    63     iBuilder->CreateRet(basePtr);
    64 
    65     iBuilder->SetInsertPoint(exitBlock);
    66     iBuilder->CreateRet(bufferPtr);
     82    iBuilder->CreateRetVoid();
    6783    iBuilder->restoreIP(savePoint);
    6884}
     
    7793    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_flush", finalBlockFunction, 0));
    7894    Value * self = getParameter(finalBlockFunction, "self");
    79     Value * bufferPtr = getParameter(finalBlockFunction, "bufferPtr");
    80     Value * basePtr = getScalarField(self, "bufferBasePtr");
    81     // Flush the output.
    82     Value * baseAddress = iBuilder->CreatePtrToInt(basePtr, iBuilder->getInt64Ty());
    83     Value * pointerAddress = iBuilder->CreatePtrToInt(bufferPtr, iBuilder->getInt64Ty());
    84     Value * bytesToFlush = iBuilder->CreateSub(pointerAddress, baseAddress);
     95    Value * streamStructPtr = getStreamSetStructPtr(self, "codeUnitBuffer");
     96    LoadInst * producerPos = iBuilder->CreateAlignedLoad(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr), sizeof(size_t));
     97    producerPos->setOrdering(AtomicOrdering::Acquire);
     98    Value * processed = getProcessedItemCount(self);
     99    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
     100    Value * blockNo = getScalarField(self, blockNoScalar);
     101    Value * basePtr = getStreamSetBlockPtr(self, "codeUnitBuffer", blockNo);
     102    Value * bytesToDo = iBuilder->CreateMul(itemsAvail, ConstantInt::get(iBuilder->getSizeTy(), mCodeUnitWidth/8));
     103
     104    iBuilder->CreateCall(writefn, std::vector<Value *>({iBuilder->getInt32(1), iBuilder->CreateBitCast(basePtr, i8PtrTy), bytesToDo}));
    85105   
    86     iBuilder->CreateCall(writefn, std::vector<Value *>({iBuilder->getInt32(1), iBuilder->CreateBitCast(basePtr, i8PtrTy), bytesToFlush}));
    87     // Buffer is flushed, return the buffer base pointer for subsequent output to the buffer.
    88     iBuilder->CreateRet(basePtr);
     106    setProcessedItemCount(self, producerPos);
     107    mStreamSetInputBuffers[0]->setConsumerPos(streamStructPtr, producerPos);
     108    iBuilder->CreateRetVoid();
    89109    iBuilder->restoreIP(savePoint);
    90110}
Note: See TracChangeset for help on using the changeset viewer.