Ignore:
Timestamp:
Apr 23, 2017, 3:46:44 PM (2 years ago)
Author:
nmedfort
Message:

Bug fix for mmap-ing 0-length files

File:
1 edited

Legend:

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

    r5419 r5421  
    1313
    1414void MMapSourceKernel::generateInitializeMethod() {
     15    BasicBlock * emptyFile = CreateBasicBlock("EmptyFile");
     16    BasicBlock * nonEmptyFile = CreateBasicBlock("NonEmptyFile");
     17    BasicBlock * exit = CreateBasicBlock("Exit");
     18
    1519    Value * fd = getScalarField("fileDescriptor");
    1620    Value * fileSize = iBuilder->CreateFileSize(fd);
     
    1822        fileSize = iBuilder->CreateUDiv(fileSize, iBuilder->getSize(mCodeUnitWidth / 8));
    1923    }
    20     Value * buffer = iBuilder->CreateFileSourceMMap(fd, fileSize);
     24    Value * const isEmpty = iBuilder->CreateICmpEQ(fileSize, ConstantInt::getNullValue(fileSize->getType()));
     25    iBuilder->CreateUnlikelyCondBr(isEmpty, emptyFile, nonEmptyFile);
     26    // we cannot mmap a 0 length file; just create a 1-page sized fake file buffer for simplicity
     27    iBuilder->SetInsertPoint(emptyFile);
     28    Constant * pageSize = ConstantInt::get(fileSize->getType(), getpagesize());
     29    Value * fakeFileBuffer = iBuilder->CreateAnonymousMMap(pageSize);
     30    iBuilder->CreateBr(exit);
     31
     32    iBuilder->SetInsertPoint(nonEmptyFile);
     33    Value * fileBackedBuffer = iBuilder->CreateFileSourceMMap(fd, fileSize);
     34    iBuilder->CreateBr(exit);
     35
     36    iBuilder->SetInsertPoint(exit);
     37    PHINode * buffer = iBuilder->CreatePHI(fileBackedBuffer->getType(), 2);
     38    buffer->addIncoming(fakeFileBuffer, emptyFile);
     39    buffer->addIncoming(fileBackedBuffer, nonEmptyFile);
     40    PHINode * size = iBuilder->CreatePHI(fileSize->getType(), 2);
     41    size->addIncoming(pageSize, emptyFile);
     42    size->addIncoming(fileSize, nonEmptyFile);
     43
    2144    setBaseAddress("sourceBuffer", buffer);
    22     setBufferedSize("sourceBuffer", fileSize);   
     45    setBufferedSize("sourceBuffer", size);
    2346    setScalarField("readableBuffer", buffer);
    2447    setScalarField("fileSize", fileSize);
    2548    iBuilder->CreateMAdvise(buffer, fileSize, CBuilder::ADVICE_WILLNEED);
     49
    2650}
    2751
     
    6791    iBuilder->SetInsertPoint(produceData);
    6892    ConstantInt * segmentItems = iBuilder->getSize(mSegmentBlocks * iBuilder->getBitBlockWidth());
    69     Value * const fileSize = getBufferedSize("sourceBuffer");
     93    Value * const fileSize = getScalarField("fileSize");
    7094    Value * produced = getProducedItemCount("sourceBuffer");
    7195    produced = iBuilder->CreateAdd(produced, segmentItems);
Note: See TracChangeset for help on using the changeset viewer.