Changeset 5280


Ignore:
Timestamp:
Jan 26, 2017, 6:05:38 PM (10 months ago)
Author:
cameron
Message:

FileSink? kernel

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

Legend:

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

    r5267 r5280  
    242242}
    243243
     244
     245PointerType * CBuilder::getFILEptrTy() {
     246    if (mFILEtype == nullptr) {
     247        mFILEtype = StructType::create(getContext(), "struct._IO_FILE");
     248    }
     249    return mFILEtype->getPointerTo();
     250}
     251
     252Value * CBuilder::CreateFOpenCall(Value * filename, Value * mode) {
     253    Function * fOpenFunc = mMod->getFunction("fopen");
     254    if (fOpenFunc == nullptr) {
     255        fOpenFunc = cast<Function>(mMod->getOrInsertFunction("fopen", getFILEptrTy(), getInt8Ty()->getPointerTo(), getInt8Ty()->getPointerTo(), nullptr));
     256        fOpenFunc->setCallingConv(llvm::CallingConv::C);
     257    }
     258    return CreateCall(fOpenFunc, {filename, mode});
     259}
     260
     261Value * CBuilder::CreateFWriteCall(Value * ptr, Value * size, Value * nitems, Value * stream) {
     262    Function * fWriteFunc = mMod->getFunction("fwrite");
     263    if (fWriteFunc == nullptr) {
     264        fWriteFunc = cast<Function>(mMod->getOrInsertFunction("fwrite", getSizeTy(), getVoidPtrTy(), getSizeTy(), getSizeTy(), getFILEptrTy(), nullptr));
     265        fWriteFunc->setCallingConv(llvm::CallingConv::C);
     266    }
     267    return CreateCall(fWriteFunc, {ptr, size, nitems, stream});
     268}
     269
     270Value * CBuilder::CreateFCloseCall(Value * stream) {
     271    Function * fCloseFunc = mMod->getFunction("fclose");
     272    if (fCloseFunc == nullptr) {
     273        fCloseFunc = cast<Function>(mMod->getOrInsertFunction("fclose", getInt32Ty(), getFILEptrTy(), nullptr));
     274        fCloseFunc->setCallingConv(llvm::CallingConv::C);
     275    }
     276    return CreateCall(fCloseFunc, {stream});
     277}
     278
    244279Value * CBuilder::CreatePThreadCreateCall(Value * thread, Value * attr, Function * start_routine, Value * arg) {
    245280    Function * pthreadCreateFunc = mMod->getFunction("pthread_create");
     
    285320, mMod(m)
    286321, mCacheLineAlignment(CacheLineAlignmentInBytes)
    287 , mSizeType(getIntNTy(GeneralRegisterWidthInBits)) {
    288 }
     322, mSizeType(getIntNTy(GeneralRegisterWidthInBits))
     323, mFILEtype(nullptr) {
     324}
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h

    r5267 r5280  
    3131        mMod = m;
    3232    }
    33        
     33   
    3434    llvm::Value * CreateMalloc(llvm::Type * type, llvm::Value * size);
    35 
     35   
    3636    llvm::Value * CreateAlignedMalloc(llvm::Type * type, llvm::Value * size, const unsigned alignment);
    37 
     37   
    3838    void CreateFree(llvm::Value * const ptr);
    39 
     39   
    4040    void CreateAlignedFree(llvm::Value * const ptr, const bool ptrMayBeNull = false);
    41 
     41   
    4242    llvm::Value * CreateRealloc(llvm::Value * ptr, llvm::Value * size);
    43 
     43   
    4444    void CreateMemZero(llvm::Value * ptr, llvm::Value * size, const unsigned alignment = 1);
    4545
     
    5050    }
    5151   
     52    // stdio.h functions
     53    //
     54    //  Create a call to:  FILE *fopen(const char *filename, const char *mode);
     55    llvm::Value * CreateFOpenCall(llvm::Value * filename, llvm::Value * mode);
     56    //  Create a call to:  size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream));
     57    llvm::Value * CreateFWriteCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
     58    //  Create a call to:  int fclose ( FILE * stream );
     59    llvm::Value * CreateFCloseCall(llvm::Value * stream);
     60   
    5261    llvm::Function * GetPrintf();
    5362
     63    //  Create calls to unistd.h functions.
     64    //
     65    //  Create a call to:  ssize_t write(int fildes, const void *buf, size_t nbyte);
    5466    llvm::Value * CreateWriteCall(llvm::Value * fildes, llvm::Value * buf, llvm::Value * nbyte);
    5567   
     68    //  Posix thread (pthread.h) functions.
     69    //
     70    //  Create a call to:  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
     71    //                    void *(*start_routine)(void*), void *arg);
    5672    llvm::Value * CreatePThreadCreateCall(llvm::Value * thread, llvm::Value * attr, llvm::Function * start_routine, llvm::Value * arg);
    57 
     73   
     74    //  Create a call to:  void pthread_exit(void *value_ptr);
    5875    llvm::Value * CreatePThreadExitCall(llvm::Value * value_ptr);
    59 
     76   
     77    //  Create a call to:  int pthread_join(pthread_t thread, void **value_ptr);
    6078    llvm::Value * CreatePThreadJoinCall(llvm::Value * thread, llvm::Value * value_ptr);
    6179   
     
    7189   
    7290    llvm::PointerType * getVoidPtrTy() const;
     91    llvm::PointerType * getFILEptrTy();
    7392   
    7493    inline unsigned getCacheAlignment() const {
     
    84103    unsigned            mCacheLineAlignment;
    85104    llvm::IntegerType * mSizeType;
     105    llvm::StructType *  mFILEtype;
    86106};
    87107
  • icGREP/icgrep-devel/icgrep/kernels/stdout_kernel.cpp

    r5276 r5280  
    77#include <IR_Gen/idisa_builder.h>
    88#include <kernels/streamset.h>
     9#include <stdio.h>
    910// #include <llvm/IR/Type.h>
    1011namespace llvm { class Type; }
     
    5253}
    5354
     55void FileSink::generateInitMethod() const {
     56    auto savePoint = iBuilder->saveIP();
     57    Module * const m = iBuilder->getModule();
     58    Function * initFunction = m->getFunction(mKernelName + init_suffix);
     59    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "Init_entry", initFunction, 0));   
     60    BasicBlock * setTerminationOnFailure = BasicBlock::Create(iBuilder->getContext(), "setTerminationOnFailure", initFunction, 0);
     61    BasicBlock * fileSinkInitExit = BasicBlock::Create(iBuilder->getContext(), "fileSinkInitExit", initFunction, 0);
     62    Function::arg_iterator args = initFunction->arg_begin();
     63    Value * self = &*(args++);
     64    iBuilder->CreateStore(ConstantAggregateZero::get(mKernelStateType), self);
     65    for (auto binding : mScalarInputs) {
     66        Value * param = &*(args++);
     67        Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), getScalarIndex(binding.name)});
     68        iBuilder->CreateStore(param, ptr);
     69    }
     70    Value * handle = iBuilder->CreateFOpenCall(getScalarField(self, "fileName"), iBuilder->CreateGlobalStringPtr("w"));
     71    setScalarField(self, "IOstreamPtr", handle);
     72    Value * failure = iBuilder->CreateICmpEQ(iBuilder->CreatePtrToInt(handle, iBuilder->getSizeTy()), iBuilder->getSize(0));
     73    iBuilder->CreateCondBr(failure, setTerminationOnFailure, fileSinkInitExit);
     74    iBuilder->SetInsertPoint(setTerminationOnFailure);
     75    setTerminationSignal(self);
     76    iBuilder->CreateBr(fileSinkInitExit);
     77    iBuilder->SetInsertPoint(fileSinkInitExit);
     78    iBuilder->CreateRetVoid();
     79    iBuilder->restoreIP(savePoint);
    5480}
     81   
     82   
     83
     84void FileSink::generateDoSegmentMethod() const {
     85    auto savePoint = iBuilder->saveIP();
     86    Module * m = iBuilder->getModule();
     87    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
     88    Type * i8PtrTy = iBuilder->getInt8PtrTy();
     89   
     90    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doSegmentFunction, 0));
     91    BasicBlock * closeFile = BasicBlock::Create(iBuilder->getContext(), "closeFile", doSegmentFunction, 0);
     92    BasicBlock * fileOutExit = BasicBlock::Create(iBuilder->getContext(), "fileOutExit", doSegmentFunction, 0);
     93    Constant * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
     94    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
     95   
     96    Function::arg_iterator args = doSegmentFunction->arg_begin();
     97    Value * self = &*(args++);
     98    Value * doFinal = &*(args++);
     99    Value * producerPos = &*(args++);
     100    Value * processed = getProcessedItemCount(self, "codeUnitBuffer");
     101    Value * itemsToDo = iBuilder->CreateSub(producerPos, processed);
     102    Value * IOstreamPtr = getScalarField(self, "IOstreamPtr");
     103   
     104    Value * blockNo = getScalarField(self, blockNoScalar);
     105    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
     106    Value * bytePtr = getStreamView(i8PtrTy, self, "codeUnitBuffer", blockNo, byteOffset);
     107    iBuilder->CreateFWriteCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
     108   
     109    processed = iBuilder->CreateAdd(processed, itemsToDo);
     110    setProcessedItemCount(self, "codeUnitBuffer", processed);
     111    setScalarField(self, blockNoScalar, iBuilder->CreateUDiv(processed, blockItems));
     112    iBuilder->CreateCondBr(doFinal, closeFile, fileOutExit);
     113   
     114    iBuilder->SetInsertPoint(closeFile);
     115    iBuilder->CreateFCloseCall(IOstreamPtr);
     116    iBuilder->CreateBr(fileOutExit);
     117   
     118    iBuilder->SetInsertPoint(fileOutExit);
     119    iBuilder->CreateRetVoid();
     120    iBuilder->restoreIP(savePoint);
     121
     122}
     123
     124FileSink::FileSink(IDISA::IDISA_Builder * iBuilder, unsigned codeUnitWidth)
     125: KernelBuilder(iBuilder, "filesink", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {},
     126                {Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getFILEptrTy(), "IOstreamPtr"}})
     127, mCodeUnitWidth(codeUnitWidth) {
     128}
     129
     130}
     131
     132
     133
  • icGREP/icgrep-devel/icgrep/kernels/stdout_kernel.h

    r5267 r5280  
    2525   
    2626};
     27
     28
     29class FileSink : public KernelBuilder {
     30public:
     31   
     32    FileSink(IDISA::IDISA_Builder * iBuilder, unsigned codeUnitWidth);
     33   
     34private:
     35   
     36    void generateInitMethod() const override;
     37    void generateDoSegmentMethod() const override;
     38   
     39private:
     40    const unsigned mCodeUnitWidth;
     41   
     42};
    2743}
    2844
     45
     46
    2947#endif
Note: See TracChangeset for help on using the changeset viewer.