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

FileSink? kernel

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

Legend:

Unmodified
Added
Removed
  • 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.