Changeset 5415 for icGREP


Ignore:
Timestamp:
Apr 19, 2017, 1:41:55 PM (2 years ago)
Author:
cameron
Message:

Speed-up file output using new temporary files; unlinking old files

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

Legend:

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

    r5412 r5415  
    6868    return CreateCall(closeFn, {fildes});
    6969}
     70
     71
     72Value * CBuilder::CreateUnlinkCall(Value * path) {
     73    Function * unlinkFunc = mMod->getFunction("unlink");
     74    if (unlinkFunc == nullptr) {
     75        FunctionType * fty = FunctionType::get(getInt32Ty(), {getInt8PtrTy()}, false);
     76        unlinkFunc = Function::Create(fty, Function::ExternalLinkage, "unlink", mMod);
     77        unlinkFunc->setCallingConv(CallingConv::C);
     78    }
     79    return CreateCall(unlinkFunc, {path});
     80}
     81
     82Value * CBuilder::CreateMkstempCall(Value * ftemplate) {
     83    Function * mkstempFn = mMod->getFunction("mkstemp");
     84    if (mkstempFn == nullptr) {
     85        mkstempFn = cast<Function>(mMod->getOrInsertFunction("mkstemp", getInt32Ty(), getInt8PtrTy(), nullptr));
     86    }
     87    return CreateCall(mkstempFn, {ftemplate});
     88}
     89
     90
     91Value * CBuilder::CreateStrlenCall(Value * str) {
     92    Function * strlenFn = mMod->getFunction("strlen");
     93    if (strlenFn == nullptr) {
     94        strlenFn = cast<Function>(mMod->getOrInsertFunction("strlen", getSizeTy(), getInt8PtrTy(), nullptr));
     95    }
     96    return CreateCall(strlenFn, {str});
     97}
     98
    7099
    71100Function * CBuilder::GetPrintf() {
     
    472501    }
    473502    return CreateCall(fCloseFunc, {stream});
     503}
     504
     505Value * CBuilder::CreateRenameCall(Value * oldName, Value * newName) {
     506    Function * renameFunc = mMod->getFunction("rename");
     507    if (renameFunc == nullptr) {
     508        FunctionType * fty = FunctionType::get(getInt32Ty(), {getInt8PtrTy(), getInt8PtrTy()}, false);
     509        renameFunc = Function::Create(fty, Function::ExternalLinkage, "rename", mMod);
     510        renameFunc->setCallingConv(CallingConv::C);
     511    }
     512    return CreateCall(renameFunc, {oldName, newName});
     513}
     514
     515Value * CBuilder::CreateRemoveCall(Value * path) {
     516    Function * removeFunc = mMod->getFunction("remove");
     517    if (removeFunc == nullptr) {
     518        FunctionType * fty = FunctionType::get(getInt32Ty(), {getInt8PtrTy()}, false);
     519        removeFunc = Function::Create(fty, Function::ExternalLinkage, "remove", mMod);
     520        removeFunc->setCallingConv(CallingConv::C);
     521    }
     522    return CreateCall(removeFunc, {path});
    474523}
    475524
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h

    r5411 r5415  
    6262    //  Create a call to:  int fclose ( FILE * stream );
    6363    llvm::Value * CreateFCloseCall(llvm::Value * stream);
     64    //  Create a call to:  int remove(const char *path);
     65    llvm::Value * CreateRemoveCall(llvm::Value * path);
     66   
     67    //  Create a call to:  int rename(const char *old, const char *new);
     68    llvm::Value * CreateRenameCall(llvm::Value * oldName, llvm::Value * newName);
    6469   
    6570    llvm::Function * GetPrintf();
     
    7580    //  Create a call to:  int close(int filedes);
    7681    llvm::Value * CreateCloseCall(llvm::Value * fildes);
     82    //  Create a call to:  int unlink(const char *path);
     83    llvm::Value * CreateUnlinkCall(llvm::Value * path);
    7784
    78 
     85    //  Create calls to stdlib.h functions.
     86    //
     87    //  Create a call to:  int mkstemp (char *template);
     88    llvm::Value * CreateMkstempCall(llvm::Value * ftemplate);
     89   
     90    //  Create a call to:  size_t strlen(const char *str);
     91    llvm::Value * CreateStrlenCall(llvm::Value * str);
     92   
     93   
     94   
    7995    llvm::Value * CreateAnonymousMMap(llvm::Value * size);
    8096
  • icGREP/icgrep-devel/icgrep/kernels/stdout_kernel.cpp

    r5402 r5415  
    7474    BasicBlock * setTerminationOnFailure = CreateBasicBlock("setTerminationOnFailure");
    7575    BasicBlock * fileSinkInitExit = CreateBasicBlock("fileSinkInitExit");
    76     Value * handle = iBuilder->CreateFOpenCall(getScalarField("fileName"), iBuilder->CreateGlobalStringPtr("w"));
    77     setScalarField("IOstreamPtr", handle);
    78     Value * failure = iBuilder->CreateICmpEQ(iBuilder->CreatePtrToInt(handle, iBuilder->getSizeTy()), iBuilder->getSize(0));
     76    Value * fileName = getScalarField("fileName");
     77    Value * fileNameLength = iBuilder->CreateStrlenCall(fileName);
     78    // Make a temporary file name template with the characters "XXXXXX" appended
     79    // as required by mkstemp.
     80    Constant * suffixPlusNullLength = iBuilder->getSize(7);
     81    Value * tmpFileNamePtr = iBuilder->CreatePointerCast(iBuilder->CreateMalloc(iBuilder->CreateAdd(fileNameLength, suffixPlusNullLength)), iBuilder->getInt8PtrTy());
     82    setScalarField("tmpFileName", tmpFileNamePtr);
     83    iBuilder->CreateMemCpy(tmpFileNamePtr, fileName, fileNameLength, 1);
     84#ifdef BACKUP_OLDFILE
     85    iBuilder->CreateMemCpy(iBuilder->CreateGEP(tmpFileNamePtr, fileNameLength), iBuilder->CreateGlobalStringPtr(".saved"), suffixPlusNullLength, 1);
     86    iBuilder->CreateRenameCall(fileName, tmpFileNamePtr);
     87#else
     88    iBuilder->CreateUnlinkCall(fileName);
     89#endif
     90    iBuilder->CreateMemCpy(iBuilder->CreateGEP(tmpFileNamePtr, fileNameLength), iBuilder->CreateGlobalStringPtr("XXXXXX"), suffixPlusNullLength, 1);
     91    Value * fileDes = iBuilder->CreateMkstempCall(tmpFileNamePtr);
     92    setScalarField("fileDes", fileDes);
     93    Value * failure = iBuilder->CreateICmpEQ(fileDes, iBuilder->getInt32(-1));
    7994    iBuilder->CreateCondBr(failure, setTerminationOnFailure, fileSinkInitExit);
    8095    iBuilder->SetInsertPoint(setTerminationOnFailure);
     
    93108    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
    94109
    95     Value * IOstreamPtr = getScalarField("IOstreamPtr");
     110    Value * fileDes = getScalarField("fileDes");
    96111    Value * available = getAvailableItemCount("codeUnitBuffer");
    97112    Value * processed = getProcessedItemCount("codeUnitBuffer");
     
    110125    Value * bytePtr = iBuilder->CreatePointerCast(getInputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0)), i8PtrTy);
    111126    bytePtr = iBuilder->CreateGEP(bytePtr, byteOffset);
    112 
    113     iBuilder->CreateFWriteCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
    114 
     127    iBuilder->CreateWriteCall(fileDes, bytePtr, iBuilder->CreateMul(itemsToDo, itemBytes));
    115128   
    116129    processed = iBuilder->CreateAdd(processed, itemsToDo);
     
    129142        bytePtr = iBuilder->CreateGEP(bytePtr, byteOffset);
    130143        itemsToDo = iBuilder->CreateSub(available, processed);
    131         iBuilder->CreateFWriteCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
     144        iBuilder->CreateWriteCall(fileDes, bytePtr, iBuilder->CreateMul(itemsToDo, itemBytes));
    132145        processed = iBuilder->CreateAdd(processed, itemsToDo);
    133146        setProcessedItemCount("codeUnitBuffer", available);
     
    138151
    139152    iBuilder->SetInsertPoint(closeFile);
    140     iBuilder->CreateFCloseCall(IOstreamPtr);
     153    iBuilder->CreateCloseCall(fileDes);
     154    Value * newFileNamePtr = getScalarField("fileName");
     155    Value * tmpFileNamePtr = getScalarField("tmpFileName");
     156    iBuilder->CreateRenameCall(tmpFileNamePtr, newFileNamePtr);
     157    iBuilder->CreateFree(tmpFileNamePtr);
     158   
    141159    iBuilder->CreateBr(fileOutExit);
    142160
     
    146164FileSink::FileSink(IDISA::IDISA_Builder * iBuilder, unsigned codeUnitWidth)
    147165: SegmentOrientedKernel(iBuilder, "filesink", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {},
    148                 {Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getFILEptrTy(), "IOstreamPtr"}})
     166                {Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getInt8PtrTy(), "tmpFileName"}, Binding{iBuilder->getInt32Ty(), "fileDes"}})
    149167, mCodeUnitWidth(codeUnitWidth) {
    150168}
Note: See TracChangeset for help on using the changeset viewer.