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

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

Moved toolchain and object_cache to kernels directory. Continued work on providing input consumed information.

File size: 7.6 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 * available = getAvailableItemCount("codeUnitBuffer");
97    Value * processed = getProcessedItemCount("codeUnitBuffer");
98    Value * itemsToDo = iBuilder->CreateSub(available, processed);
99    // There may be two memory areas if we are at the physical end of a circular buffer.
100    const auto b  = getInputStreamSetBuffer("codeUnitBuffer");
101    Value * wraparound = nullptr;
102    if (isa<CircularBuffer>(b) || isa<CircularCopybackBuffer>(b)) {
103        Value * instance = getStreamSetBufferPtr("codeUnitBuffer");
104        Value * accessible = b->getLinearlyAccessibleItems(instance, processed);
105        wraparound = iBuilder->CreateICmpULT(accessible, itemsToDo);
106        itemsToDo = iBuilder->CreateSelect(wraparound, accessible, itemsToDo);
107    }
108   
109    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
110    Value * bytePtr = iBuilder->CreatePointerCast(getInputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0)), i8PtrTy);
111    bytePtr = iBuilder->CreateGEP(bytePtr, byteOffset);
112
113    iBuilder->CreateFWriteCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
114
115   
116    processed = iBuilder->CreateAdd(processed, itemsToDo);
117    setProcessedItemCount("codeUnitBuffer", processed);
118   
119    // Now we may process the second area (if required).
120    if (isa<CircularBuffer>(b) || isa<CircularCopybackBuffer>(b)) {
121        BasicBlock * wrapAroundWrite = CreateBasicBlock("wrapAroundWrite");
122        BasicBlock * checkFinal = CreateBasicBlock("checkFinal");
123        iBuilder->CreateCondBr(wraparound, wrapAroundWrite, checkFinal);
124        iBuilder->SetInsertPoint(wrapAroundWrite);
125       
126        // Calculate from the updated value of processed;
127        byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
128        Value * bytePtr = iBuilder->CreatePointerCast(getInputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0)), i8PtrTy);
129        bytePtr = iBuilder->CreateGEP(bytePtr, byteOffset);
130        itemsToDo = iBuilder->CreateSub(available, processed);
131        iBuilder->CreateFWriteCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
132        processed = iBuilder->CreateAdd(processed, itemsToDo);
133        setProcessedItemCount("codeUnitBuffer", available);
134        iBuilder->CreateBr(checkFinal);
135        iBuilder->SetInsertPoint(checkFinal);
136    }
137    iBuilder->CreateCondBr(doFinal, closeFile, fileOutExit);
138
139    iBuilder->SetInsertPoint(closeFile);
140    iBuilder->CreateFCloseCall(IOstreamPtr);
141    iBuilder->CreateBr(fileOutExit);
142
143    iBuilder->SetInsertPoint(fileOutExit);
144}
145
146FileSink::FileSink(IDISA::IDISA_Builder * iBuilder, unsigned codeUnitWidth)
147: SegmentOrientedKernel(iBuilder, "filesink", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {},
148                {Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getFILEptrTy(), "IOstreamPtr"}})
149, mCodeUnitWidth(codeUnitWidth) {
150}
151
152}
153
154
155
156
Note: See TracBrowser for help on using the repository browser.