Ignore:
Timestamp:
Mar 29, 2017, 2:29:52 PM (2 years ago)
Author:
nmedfort
Message:

Support for stdin. Needs more testing.

File:
1 edited

Legend:

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

    r5307 r5377  
    1010using namespace llvm;
    1111
     12inline static unsigned ceil_log2(const unsigned v) {
     13    assert ("log2(0) is undefined!" && v != 0);
     14    return 32 - __builtin_clz(v - 1);
     15}
     16
    1217namespace kernel {
    13    
    14 void StdInKernel::generateDoSegmentMethod(Value *doFinal, const std::vector<Value *> &producerPos) {
     18
     19void StdInKernel::generateDoSegmentMethod(Value * /* doFinal */, const std::vector<Value *> & /* producerPos */) {
    1520
    1621    BasicBlock * setTermination = CreateBasicBlock("setTermination");
    1722    BasicBlock * stdInExit = CreateBasicBlock("stdInExit");
    18 //    ConstantInt * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
    19     ConstantInt * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
    20     ConstantInt * segmentBytes = iBuilder->getSize(mSegmentBlocks * iBuilder->getBitBlockWidth() * mCodeUnitWidth/8);
    21     ConstantInt * stdin_fileno = iBuilder->getInt32(STDIN_FILENO);
    22     Value * produced = getProducedItemCount("codeUnitBuffer");
    23 //    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(produced, blockItems), itemBytes);
    24 //    Value * bytePtr = getRawItemPointer("codeUnitBuffer", iBuilder->getInt32(0), produced);
    25     Value * bytePtr = getOutputStream("codeUnitBuffer", iBuilder->getInt32(0));
     23    ConstantInt * segmentItems = iBuilder->getSize(mSegmentBlocks * iBuilder->getBitBlockWidth() / mCodeUnitWidth);
     24    ConstantInt * segmentItems2 = iBuilder->getSize(2 * mSegmentBlocks * iBuilder->getBitBlockWidth() / mCodeUnitWidth);
     25    // on the first segment, we buffer twice the data necessary to ensure that we can safely check for a non-LF line break
     26    Value * itemsRead = getProducedItemCount("codeUnitBuffer");
     27    Value * isFirst = iBuilder->CreateICmpEQ(itemsRead, iBuilder->getSize(0));
     28    Value * itemsToRead = iBuilder->CreateSelect(isFirst, segmentItems2, segmentItems);
     29
     30    Value * segmentBytes = reserveItemCount("codeUnitBuffer", itemsToRead);
     31    Value * bytePtr =  getOutputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0));
    2632    bytePtr = iBuilder->CreatePointerCast(bytePtr, iBuilder->getInt8PtrTy());
     33    Value * bytesRead = iBuilder->CreateReadCall(iBuilder->getInt32(STDIN_FILENO), bytePtr, segmentBytes);
     34    itemsRead = iBuilder->CreateAdd(itemsRead, iBuilder->CreateUDiv(bytesRead, iBuilder->getSize(mCodeUnitWidth / 8)));
    2735
    28 
    29    
    30     Value * nRead = iBuilder->CreateReadCall(stdin_fileno, bytePtr, segmentBytes);
    31     Value * bytesRead = iBuilder->CreateSelect(iBuilder->CreateICmpSLT(nRead, iBuilder->getSize(0)), iBuilder->getSize(0), nRead);
    32     produced = iBuilder->CreateAdd(produced, iBuilder->CreateUDiv(bytesRead, itemBytes));
    33     setProducedItemCount("codeUnitBuffer", produced);
    34     Value * lessThanFullSegment = iBuilder->CreateICmpULT(bytesRead, segmentBytes);
    35     iBuilder->CreateCondBr(lessThanFullSegment, setTermination, stdInExit);
     36    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(bytesRead, iBuilder->getSize(0)), setTermination, stdInExit);
    3637    iBuilder->SetInsertPoint(setTermination);
    3738    setTerminationSignal();
    3839    iBuilder->CreateBr(stdInExit);
    39    
    4040    iBuilder->SetInsertPoint(stdInExit);
    4141
    42    
     42    setProducedItemCount("codeUnitBuffer", itemsRead);
    4343}
    4444
     
    6363}
    6464   
    65 void FileSource::generateDoSegmentMethod(Value * doFinal, const std::vector<Value *> & producerPos) {
     65void FileSource::generateDoSegmentMethod(Value * /* doFinal */, const std::vector<Value *> & /* producerPos */) {
    6666
    6767    BasicBlock * closeFile = CreateBasicBlock("closeFile");
     
    7070   
    7171    Value * produced = getProducedItemCount("codeUnitBuffer");
    72     Value * bytePtr = getOutputStream("codeUnitBuffer", iBuilder->getInt32(0));
     72    Value * bytePtr = getOutputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0));
    7373    bytePtr = iBuilder->CreatePointerCast(bytePtr, iBuilder->getInt8PtrTy());
    7474
Note: See TracChangeset for help on using the changeset viewer.