source: icGREP/icgrep-devel/icgrep/kernels/stdout_kernel.cpp @ 5298

Last change on this file since 5298 was 5292, checked in by nmedfort, 2 years ago

Removed 'function' and 'self' parameters from generateXXXMethod() functions.

File size: 4.1 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5#include "stdout_kernel.h"
6#include <llvm/IR/Module.h>
7#include <IR_Gen/idisa_builder.h>
8#include <kernels/streamset.h>
9#include <stdio.h>
10// #include <llvm/IR/Type.h>
11namespace llvm { class Type; }
12
13using namespace llvm;
14
15namespace kernel {
16           
17// Rather than using doBlock logic to write one block at a time, this custom
18// doSegment method, writes the entire segment with a single write call.
19void StdOutKernel::generateDoSegmentMethod(Value *doFinal, const std::vector<Value *> &producerPos) {
20
21    PointerType * i8PtrTy = iBuilder->getInt8PtrTy();
22
23    Constant * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
24    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
25   
26    Value * processed = getProcessedItemCount("codeUnitBuffer");
27    Value * itemsToDo = iBuilder->CreateSub(producerPos[0], processed);
28   
29    Value * blockNo = getBlockNo();
30    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
31    Value * bytePtr = getStreamView(i8PtrTy, "codeUnitBuffer", blockNo, byteOffset);
32    iBuilder->CreateWriteCall(iBuilder->getInt32(1), bytePtr, iBuilder->CreateMul(itemsToDo, itemBytes));
33
34    processed = iBuilder->CreateAdd(processed, itemsToDo);
35    setProcessedItemCount("codeUnitBuffer", processed);
36    setBlockNo(iBuilder->CreateUDiv(processed, blockItems));
37
38}
39
40StdOutKernel::StdOutKernel(IDISA::IDISA_Builder * iBuilder, unsigned codeUnitWidth)
41: SegmentOrientedKernel(iBuilder, "stdout", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {}, {}, {}, {})
42, mCodeUnitWidth(codeUnitWidth) {
43    setNoTerminateAttribute(true);
44}
45
46void FileSink::generateInitMethod() {
47    BasicBlock * setTerminationOnFailure = CreateBasicBlock("setTerminationOnFailure");
48    BasicBlock * fileSinkInitExit = CreateBasicBlock("fileSinkInitExit");
49    Value * handle = iBuilder->CreateFOpenCall(getScalarField("fileName"), iBuilder->CreateGlobalStringPtr("w"));
50    setScalarField("IOstreamPtr", handle);
51    Value * failure = iBuilder->CreateICmpEQ(iBuilder->CreatePtrToInt(handle, iBuilder->getSizeTy()), iBuilder->getSize(0));
52    iBuilder->CreateCondBr(failure, setTerminationOnFailure, fileSinkInitExit);
53    iBuilder->SetInsertPoint(setTerminationOnFailure);
54    setTerminationSignal();
55    iBuilder->CreateBr(fileSinkInitExit);
56    iBuilder->SetInsertPoint(fileSinkInitExit);
57}
58
59void FileSink::generateDoSegmentMethod(Value *doFinal, const std::vector<Value *> &producerPos) {
60    PointerType * i8PtrTy = iBuilder->getInt8PtrTy();
61   
62    BasicBlock * closeFile = CreateBasicBlock("closeFile");
63    BasicBlock * fileOutExit = CreateBasicBlock("fileOutExit");
64    Constant * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
65    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
66   
67    Value * processed = getProcessedItemCount("codeUnitBuffer");
68    Value * itemsToDo = iBuilder->CreateSub(producerPos[0], processed);
69    Value * IOstreamPtr = getScalarField("IOstreamPtr");
70   
71    Value * blockNo = getBlockNo();
72    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
73    Value * bytePtr = getStreamView(i8PtrTy, "codeUnitBuffer", blockNo, byteOffset);
74    iBuilder->CreateFWriteCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
75   
76    processed = iBuilder->CreateAdd(processed, itemsToDo);
77    setProcessedItemCount("codeUnitBuffer", processed);
78    setBlockNo(iBuilder->CreateUDiv(processed, blockItems));
79    iBuilder->CreateCondBr(doFinal, closeFile, fileOutExit);
80   
81    iBuilder->SetInsertPoint(closeFile);
82    iBuilder->CreateFCloseCall(IOstreamPtr);
83    iBuilder->CreateBr(fileOutExit);
84   
85    iBuilder->SetInsertPoint(fileOutExit);
86}
87
88FileSink::FileSink(IDISA::IDISA_Builder * iBuilder, unsigned codeUnitWidth)
89: SegmentOrientedKernel(iBuilder, "filesink", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {},
90                {Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getFILEptrTy(), "IOstreamPtr"}})
91, mCodeUnitWidth(codeUnitWidth) {
92}
93
94}
95
96
97
Note: See TracBrowser for help on using the repository browser.