Ignore:
Timestamp:
Mar 22, 2018, 7:26:40 AM (13 months ago)
Author:
cameron
Message:

mmap - command line flag + overridden for stdin, devices, files of size 0 (incl virtual files such as /proc/cpuinfo)

File:
1 edited

Legend:

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

    r5915 r5927  
    11/*
    2  *  Copyright (c) 2017 International Characters.
     2 *  Copyright (c) 2018 International Characters.
    33 *  This software is licensed to the public under the Open Software License 3.0.
    44 */
     
    4343    PointerType * const codeUnitPtrTy = kb->getIntNTy(codeUnitWidth)->getPointerTo();
    4444    Value * const fileBuffer = kb->CreatePointerCast(kb->CreateFileSourceMMap(fd, fileSize), codeUnitPtrTy);
    45     kb->setScalarField("buffer", fileBuffer);   
    46     kb->setBaseAddress("sourceBuffer", fileBuffer);   
     45    kb->setScalarField("buffer", fileBuffer);
     46    kb->setBaseAddress("sourceBuffer", fileBuffer);
    4747    kb->CreateMAdvise(fileBuffer, fileSize, CBuilder::ADVICE_WILLNEED);
    4848    if (codeUnitWidth > 8) {
     
    237237    BasicBlock * finalizeMMap = kb->CreateBasicBlock("finalizeMMap");
    238238    BasicBlock * finalizeDone = kb->CreateBasicBlock("finalizeDone");
    239     // if the fileDescriptor is 0, the file is stdin, use readSource kernel logic, otherwise use mmap logic.
    240     kb->CreateCondBr(kb->CreateICmpEQ(kb->getScalarField("fileDescriptor"), kb->getInt32(STDIN_FILENO)), finalizeRead, finalizeMMap);
     239    kb->CreateCondBr(kb->CreateTrunc(kb->getScalarField("useMMap"), kb->getInt1Ty()), finalizeMMap, finalizeRead);
     240    kb->SetInsertPoint(finalizeMMap);
     241    MMapSourceKernel::unmapSourceBuffer(kb);
     242    kb->CreateBr(finalizeDone);
    241243    kb->SetInsertPoint(finalizeRead);
    242244    ReadSourceKernel::freeBuffer(kb);
    243245    kb->CreateBr(finalizeDone);
    244     kb->SetInsertPoint(finalizeMMap);
    245     MMapSourceKernel::unmapSourceBuffer(kb);
    246     kb->CreateBr(finalizeDone);
    247246    kb->SetInsertPoint(finalizeDone);
    248247}
     
    250249void FDSourceKernel::generateInitializeMethod(const std::unique_ptr<KernelBuilder> & kb) {
    251250    BasicBlock * initializeRead = kb->CreateBasicBlock("initializeRead");
     251    BasicBlock * tryMMap = kb->CreateBasicBlock("tryMMap");
    252252    BasicBlock * initializeMMap = kb->CreateBasicBlock("initializeMMap");
    253253    BasicBlock * initializeDone = kb->CreateBasicBlock("initializeDone");
    254     // if the fileDescriptor is 0, the file is stdin, use readSource kernel logic, otherwise use MMap logic.
    255     kb->CreateCondBr(kb->CreateICmpEQ(kb->getScalarField("fileDescriptor"), kb->getInt32(STDIN_FILENO)), initializeRead, initializeMMap);
    256     kb->SetInsertPoint(initializeRead);
    257     ReadSourceKernel::generateInitializeMethod(mCodeUnitWidth, getStride(), kb);
    258     kb->CreateBr(initializeDone);
     254    // The source will use MMapSource or readSoure kernel logic depending on the useMMap
     255    // parameter, possibly overridden.
     256    Value * useMMap = kb->CreateTrunc(kb->getScalarField("useMMap"), kb->getInt1Ty());
     257    // if the fileDescriptor is 0, the file is stdin, use readSource kernel logic.
     258    Value * fd = kb->getScalarField("fileDescriptor");
     259    useMMap = kb->CreateAnd(useMMap, kb->CreateICmpNE(fd, kb->getInt32(STDIN_FILENO)));
     260    kb->CreateCondBr(useMMap, tryMMap, initializeRead);
     261   
     262    kb->SetInsertPoint(tryMMap);
     263    // If the fileSize is 0, we may have a virtual file such as /proc/cpuinfo
     264    Value * fileSize = kb->CreateZExtOrTrunc(kb->CreateCall(mFileSizeFunction, fd), kb->getSizeTy());
     265    useMMap = kb->CreateICmpNE(fileSize, kb->getSize(0));
     266    kb->CreateCondBr(useMMap, initializeMMap, initializeRead);
    259267    kb->SetInsertPoint(initializeMMap);
    260268    MMapSourceKernel::generateInitializeMethod(mFileSizeFunction, mCodeUnitWidth, kb);
     269    kb->CreateBr(initializeDone);
     270
     271    kb->SetInsertPoint(initializeRead);
     272    // Ensure that readSource logic is used throughout.
     273    kb->setScalarField("useMMap", kb->getInt8(0));
     274    ReadSourceKernel::generateInitializeMethod(mCodeUnitWidth, getStride(), kb);
    261275    kb->CreateBr(initializeDone);
    262276    kb->SetInsertPoint(initializeDone);
     
    267281    BasicBlock * DoSegmentMMap = kb->CreateBasicBlock("DoSegmentMMap");
    268282    BasicBlock * DoSegmentDone = kb->CreateBasicBlock("DoSegmentDone");
    269     // if the fileDescriptor is 0, the file is stdin, use readSource kernel logic, otherwise use MMap logic.
    270     kb->CreateCondBr(kb->CreateICmpEQ(kb->getScalarField("fileDescriptor"), kb->getInt32(STDIN_FILENO)), DoSegmentRead, DoSegmentMMap);
     283    kb->CreateCondBr(kb->CreateTrunc(kb->getScalarField("useMMap"), kb->getInt1Ty()), DoSegmentMMap, DoSegmentRead);
     284    kb->SetInsertPoint(DoSegmentMMap);
     285    MMapSourceKernel::generateDoSegmentMethod(mCodeUnitWidth, kb);
     286    kb->CreateBr(DoSegmentDone);
    271287    kb->SetInsertPoint(DoSegmentRead);
    272288    ReadSourceKernel::generateDoSegmentMethod(mCodeUnitWidth, getStride(), kb);
    273289    kb->CreateBr(DoSegmentDone);
    274     kb->SetInsertPoint(DoSegmentMMap);
    275     MMapSourceKernel::generateDoSegmentMethod(mCodeUnitWidth, kb);
    276     kb->CreateBr(DoSegmentDone);
    277290    kb->SetInsertPoint(DoSegmentDone);
    278291}
    279 
    280292
    281293/// MEMORY SOURCE KERNEL
     
    324336, {}
    325337, {Binding{kb->getStreamSetTy(1, codeUnitWidth), "sourceBuffer", FixedRate(), Deferred()}}
    326 , {Binding{kb->getInt32Ty(), "fileDescriptor"}}
     338, {Binding{kb->getInt8Ty(), "useMMap"}, Binding{kb->getInt32Ty(), "fileDescriptor"}}
    327339, {}
    328340, {Binding{kb->getIntNTy(codeUnitWidth)->getPointerTo(), "buffer"}, Binding{kb->getSizeTy(), "fileSize"}})
Note: See TracChangeset for help on using the changeset viewer.