Changeset 5421


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

Bug fix for mmap-ing 0-length files

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

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.cpp

    r5420 r5421  
    230230    ConstantInt * const prot =  ConstantInt::get(intTy, PROT_READ);
    231231    ConstantInt * const flags =  ConstantInt::get(intTy, MAP_PRIVATE);
    232     Constant * const offset = ConstantInt::get(sizeTy, 0);
     232    Constant * const offset = ConstantInt::get(sizeTy, 0);       
    233233    return CreateMMap(ConstantPointerNull::getNullValue(voidPtrTy), size, prot, flags, fd, offset);
    234234}
     
    273273 *      without an underlying file.
    274274 *
    275  * @return Value indicating success (0) or failure (non-zero).
     275 * @return Value indicating success (0) or failure (-1).
    276276 */
    277277Value * CBuilder::CreateMAdvise(Value * addr, Value * length, Advice advice) {
  • 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.