Ignore:
Timestamp:
Mar 31, 2017, 3:05:41 PM (2 years ago)
Author:
nmedfort
Message:

Replaced stdin input stream with mmap'ed buffer and aligned each read call to the page size.

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

Legend:

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

    r5374 r5386  
    1212#include <llvm/IR/MDBuilder.h>
    1313#include <fcntl.h>
     14#include <unistd.h>
     15#include <sys/mman.h>
    1416#include <toolchain.h>
    1517
     
    165167    }
    166168    return CreateCall(aligned_malloc, {CreateZExtOrTrunc(size, intTy)});
     169}
     170
     171Value * CBuilder::CreateAnonymousMMap(Value * size) {
     172    DataLayout DL(mMod);
     173    PointerType * const voidPtrTy = getVoidPtrTy();
     174    IntegerType * const intTy = getIntPtrTy(DL);
     175    IntegerType * const sizeTy = getSizeTy();
     176    Type * const offTy = TypeBuilder<off_t, false>::get(getContext());
     177    Function * fMMap = mMod->getFunction("mmap");
     178    if (LLVM_UNLIKELY(fMMap == nullptr)) {
     179        FunctionType * fty = FunctionType::get(voidPtrTy, {voidPtrTy, sizeTy, intTy, intTy, intTy, offTy}, false);
     180        fMMap = Function::Create(fty, Function::ExternalLinkage, "mmap", mMod);
     181    }
     182    ConstantInt * const prot =  ConstantInt::get(intTy, PROT_READ | PROT_WRITE);
     183    ConstantInt * const flags =  ConstantInt::get(intTy, MAP_PRIVATE | MAP_ANONYMOUS);
     184    ConstantInt * const fd =  ConstantInt::get(intTy, -1);
     185    ConstantInt * const offset = ConstantInt::get(intTy, 0); // getCacheAlignment()
     186    Value * const ptr = CreateCall(fMMap, {Constant::getNullValue(voidPtrTy), size, prot, flags, fd, offset});
     187    CreateAssert(CreateICmpNE(CreatePtrToInt(ptr, getSizeTy()), getSize((size_t)MAP_FAILED)), "CreateAnonymousMMap: mmap failed to allocate memory");
     188    return ptr;
     189}
     190
     191Value * CBuilder::CreateFileSourceMMap(Value * const fd, Value * const size) {
     192    DataLayout DL(mMod);
     193    PointerType * const voidPtrTy = getVoidPtrTy();
     194    IntegerType * const intTy = getIntPtrTy(DL);
     195    IntegerType * const sizeTy = getSizeTy();
     196    Type * const offTy = TypeBuilder<off_t, false>::get(getContext());
     197    Function * fMMap = mMod->getFunction("mmap");
     198    if (LLVM_UNLIKELY(fMMap == nullptr)) {
     199        FunctionType * fty = FunctionType::get(voidPtrTy, {voidPtrTy, sizeTy, intTy, intTy, intTy, offTy}, false);
     200        fMMap = Function::Create(fty, Function::ExternalLinkage, "mmap", mMod);
     201    }
     202    ConstantInt * const prot =  ConstantInt::get(intTy, PROT_READ);
     203    ConstantInt * const flags =  ConstantInt::get(intTy, MAP_PRIVATE);
     204    ConstantInt * const offset = ConstantInt::get(intTy, 0); // getCacheAlignment()
     205    Value * const ptr = CreateCall(fMMap, {Constant::getNullValue(voidPtrTy), size, prot, flags, fd, offset});
     206    CreateAssert(CreateICmpNE(CreatePtrToInt(ptr, getSizeTy()), getSize((size_t)MAP_FAILED)), "CreateFileSourceMMap: mmap failed to allocate memory");
     207    return ptr;
     208}
     209
     210Value * CBuilder::CreateMRemap(Value * addr, Value * oldSize, Value * newSize, const bool mayMove) {
     211    DataLayout DL(mMod);
     212    PointerType * const voidPtrTy = getVoidPtrTy();
     213    IntegerType * const intTy = getIntPtrTy(DL);
     214    IntegerType * const sizeTy = getSizeTy();
     215    Function * fMRemap = mMod->getFunction("mremap");
     216    if (LLVM_UNLIKELY(fMRemap == nullptr)) {
     217        //    void * mremap (void *addr, size_t old_size,
     218        //                   size_t new_size, unsigned long flags);
     219
     220        FunctionType * fty = FunctionType::get(voidPtrTy, {voidPtrTy, sizeTy, sizeTy, intTy}, false);
     221        fMRemap = Function::Create(fty, Function::ExternalLinkage, "mremap", mMod);
     222    }   
     223    addr = CreatePointerCast(addr, voidPtrTy);
     224    CreateAssert(addr, "CreateMRemap: initial addr is null");
     225    Value * ptr = CreateCall(fMRemap, {addr, oldSize, newSize, ConstantInt::get(intTy, mayMove ? MREMAP_MAYMOVE : 0)});
     226    CreateAssert(addr, "CreateMRemap: mremap failed to allocate memory");
     227    return ptr;
     228}
     229
     230Value * CBuilder::CreateMUnmap(Value * addr, Value * size) {
     231    DataLayout DL(mMod);
     232    IntegerType * const intTy = getIntPtrTy(DL);
     233    IntegerType * const sizeTy = getSizeTy();
     234    Function * fMUnmap = mMod->getFunction("munmap");
     235    if (LLVM_UNLIKELY(fMUnmap == nullptr)) {
     236        // int munmap (void *addr, size_t len);
     237
     238        PointerType * const voidPtrTy = getVoidPtrTy();
     239        FunctionType * fty = FunctionType::get(intTy, {voidPtrTy, sizeTy}, false);
     240        fMUnmap = Function::Create(fty, Function::ExternalLinkage, "munmap", mMod);
     241    }
     242    return CreateCall(fMUnmap, {addr, size});
    167243}
    168244
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h

    r5368 r5386  
    1919   
    2020public:
    21    
     21
    2222    CBuilder(llvm::Module * m, const unsigned GeneralRegisterWidthInBits, const bool SupportsIndirectBr, const unsigned CacheLineAlignmentInBytes = 64);
    2323   
     
    5151        return instr;
    5252    }
    53    
     53
    5454    // stdio.h functions
    5555    //
     
    7575    //  Create a call to:  int close(int filedes);
    7676    llvm::Value * CreateCloseCall(llvm::Value * fildes);
     77
     78
     79    llvm::Value * CreateAnonymousMMap(llvm::Value * size);
     80
     81    llvm::Value * CreateFileSourceMMap(llvm::Value * fd, llvm::Value * size);
     82
     83    llvm::Value * CreateMRemap(llvm::Value * addr, llvm::Value * oldSize, llvm::Value * newSize, const bool mayMove = true);
     84
     85    llvm::Value * CreateMUnmap(llvm::Value * addr, llvm::Value * size);
     86
     87
    7788
    7889    //  Posix thread (pthread.h) functions.
Note: See TracChangeset for help on using the changeset viewer.