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

Last change on this file since 5377 was 5377, checked in by nmedfort, 3 years ago

Support for stdin. Needs more testing.

File size: 7.5 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>
9namespace llvm { class Type; }
10
11using namespace llvm;
12using namespace parabix;
13
14namespace kernel {
15
16// Rather than using doBlock logic to write one block at a time, this custom
17// doSegment method attempts to write the entire segment with a single write call.
18// However, if the segment spans two memory areas (e.g., because of wraparound),
19// then two write calls are made.
20void StdOutKernel::generateDoSegmentMethod(Value *doFinal, const std::vector<Value *> &producerPos) {
21    PointerType * i8PtrTy = iBuilder->getInt8PtrTy();
22
23    Constant * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth() - 1);
24    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth / 8);
25    Value * processed = getProcessedItemCount("codeUnitBuffer");
26    Value * itemsToDo = iBuilder->CreateSub(producerPos[0], processed);
27    // There may be two memory areas if we are at the physical end of a circular buffer.
28    const auto b  = getInputStreamSetBuffer("codeUnitBuffer");
29    Value * wraparound = nullptr;
30    if (isa<CircularBuffer>(b) || isa<CircularCopybackBuffer>(b)) {
31        Value * instance = getStreamSetBufferPtr("codeUnitBuffer");
32        Value * accessible = b->getLinearlyAccessibleItems(instance, processed);
33        wraparound = iBuilder->CreateICmpULT(accessible, itemsToDo);
34        itemsToDo = iBuilder->CreateSelect(wraparound, accessible, itemsToDo);
35    }
36   
37    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateAnd(processed, blockItems), itemBytes);
38    Value * bytePtr = iBuilder->CreatePointerCast(getInputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0)), i8PtrTy);
39    bytePtr = iBuilder->CreateGEP(bytePtr, byteOffset);
40
41    iBuilder->CreateWriteCall(iBuilder->getInt32(1), bytePtr, iBuilder->CreateMul(itemsToDo, itemBytes));
42
43    processed = iBuilder->CreateAdd(processed, itemsToDo);
44    setProcessedItemCount("codeUnitBuffer", processed);
45   
46    // Now we may process the second area (if required).
47    if (isa<CircularBuffer>(b) || isa<CircularCopybackBuffer>(b)) {
48        BasicBlock * wrapAroundWrite = CreateBasicBlock("wrapAroundWrite");
49        BasicBlock * stdoutExit = CreateBasicBlock("stdoutExit");
50        iBuilder->CreateCondBr(wraparound, wrapAroundWrite, stdoutExit);
51        iBuilder->SetInsertPoint(wrapAroundWrite);
52       
53        // Calculate from the updated value of processed;
54        byteOffset = iBuilder->CreateMul(iBuilder->CreateAnd(processed, blockItems), itemBytes);
55        Value * bytePtr = iBuilder->CreatePointerCast(getInputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0)), i8PtrTy);
56        bytePtr = iBuilder->CreateGEP(bytePtr, byteOffset);
57
58        itemsToDo = iBuilder->CreateSub(producerPos[0], processed);
59        iBuilder->CreateWriteCall(iBuilder->getInt32(1), bytePtr, iBuilder->CreateMul(itemsToDo, itemBytes));
60        processed = iBuilder->CreateAdd(processed, itemsToDo);
61        setProcessedItemCount("codeUnitBuffer", producerPos[0]);
62        iBuilder->CreateBr(stdoutExit);
63        iBuilder->SetInsertPoint(stdoutExit);
64    }
65}
66
67StdOutKernel::StdOutKernel(IDISA::IDISA_Builder * iBuilder, unsigned codeUnitWidth)
68: SegmentOrientedKernel(iBuilder, "stdout", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {}, {}, {}, {})
69, mCodeUnitWidth(codeUnitWidth) {
70    setNoTerminateAttribute(true);
71}
72
73void FileSink::generateInitMethod() {
74    BasicBlock * setTerminationOnFailure = CreateBasicBlock("setTerminationOnFailure");
75    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));
79    iBuilder->CreateCondBr(failure, setTerminationOnFailure, fileSinkInitExit);
80    iBuilder->SetInsertPoint(setTerminationOnFailure);
81    setTerminationSignal();
82    iBuilder->CreateBr(fileSinkInitExit);
83    iBuilder->SetInsertPoint(fileSinkInitExit);
84}
85
86void FileSink::generateDoSegmentMethod(Value *doFinal, const std::vector<Value *> &producerPos) {
87
88    PointerType * i8PtrTy = iBuilder->getInt8PtrTy();
89
90    BasicBlock * closeFile = CreateBasicBlock("closeFile");
91    BasicBlock * fileOutExit = CreateBasicBlock("fileOutExit");
92    Constant * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
93    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
94
95    Value * IOstreamPtr = getScalarField("IOstreamPtr");
96    Value * processed = getProcessedItemCount("codeUnitBuffer");
97    Value * itemsToDo = iBuilder->CreateSub(producerPos[0], processed);
98    // There may be two memory areas if we are at the physical end of a circular buffer.
99    const auto b  = getInputStreamSetBuffer("codeUnitBuffer");
100    Value * wraparound = nullptr;
101    if (isa<CircularBuffer>(b) || isa<CircularCopybackBuffer>(b)) {
102        Value * instance = getStreamSetBufferPtr("codeUnitBuffer");
103        Value * accessible = b->getLinearlyAccessibleItems(instance, processed);
104        wraparound = iBuilder->CreateICmpULT(accessible, itemsToDo);
105        itemsToDo = iBuilder->CreateSelect(wraparound, accessible, itemsToDo);
106    }
107   
108    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
109    Value * bytePtr = iBuilder->CreatePointerCast(getInputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0)), i8PtrTy);
110    bytePtr = iBuilder->CreateGEP(bytePtr, byteOffset);
111
112    iBuilder->CreateFWriteCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
113
114   
115    processed = iBuilder->CreateAdd(processed, itemsToDo);
116    setProcessedItemCount("codeUnitBuffer", processed);
117   
118    // Now we may process the second area (if required).
119    if (isa<CircularBuffer>(b) || isa<CircularCopybackBuffer>(b)) {
120        BasicBlock * wrapAroundWrite = CreateBasicBlock("wrapAroundWrite");
121        BasicBlock * checkFinal = CreateBasicBlock("checkFinal");
122        iBuilder->CreateCondBr(wraparound, wrapAroundWrite, checkFinal);
123        iBuilder->SetInsertPoint(wrapAroundWrite);
124       
125        // Calculate from the updated value of processed;
126        byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
127        Value * bytePtr = iBuilder->CreatePointerCast(getInputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0)), i8PtrTy);
128        bytePtr = iBuilder->CreateGEP(bytePtr, byteOffset);
129        itemsToDo = iBuilder->CreateSub(producerPos[0], processed);
130        iBuilder->CreateFWriteCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
131        processed = iBuilder->CreateAdd(processed, itemsToDo);
132        setProcessedItemCount("codeUnitBuffer", producerPos[0]);
133        iBuilder->CreateBr(checkFinal);
134        iBuilder->SetInsertPoint(checkFinal);
135    }
136    iBuilder->CreateCondBr(doFinal, closeFile, fileOutExit);
137
138    iBuilder->SetInsertPoint(closeFile);
139    iBuilder->CreateFCloseCall(IOstreamPtr);
140    iBuilder->CreateBr(fileOutExit);
141
142    iBuilder->SetInsertPoint(fileOutExit);
143}
144
145FileSink::FileSink(IDISA::IDISA_Builder * iBuilder, unsigned codeUnitWidth)
146: SegmentOrientedKernel(iBuilder, "filesink", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {},
147                {Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getFILEptrTy(), "IOstreamPtr"}})
148, mCodeUnitWidth(codeUnitWidth) {
149}
150
151}
152
153
154
155
Note: See TracBrowser for help on using the repository browser.