source: icGREP/icgrep-devel/icgrep/kernels/stdin_kernel.cpp @ 5335

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

Continued work on eliminating BlockNo?

File size: 4.6 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5#include <kernels/stdin_kernel.h>
6#include <llvm/IR/Module.h>
7#include <kernels/kernel.h>
8#include <IR_Gen/idisa_builder.h>
9
10using namespace llvm;
11
12namespace kernel {
13   
14void StdInKernel::generateDoSegmentMethod(Value *doFinal, const std::vector<Value *> &producerPos) {
15
16    BasicBlock * setTermination = CreateBasicBlock("setTermination");
17    BasicBlock * stdInExit = CreateBasicBlock("stdInExit");
18//    ConstantInt * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
19    ConstantInt * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
20    ConstantInt * segmentBytes = iBuilder->getSize(mSegmentBlocks * iBuilder->getBitBlockWidth() * mCodeUnitWidth/8);
21    ConstantInt * stdin_fileno = iBuilder->getInt32(STDIN_FILENO);
22    Value * produced = getProducedItemCount("codeUnitBuffer");
23//    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(produced, blockItems), itemBytes);
24//    Value * bytePtr = getRawItemPointer("codeUnitBuffer", iBuilder->getInt32(0), produced);
25    Value * bytePtr = getOutputStream("codeUnitBuffer", iBuilder->getInt32(0));
26    bytePtr = iBuilder->CreatePointerCast(bytePtr, iBuilder->getInt8PtrTy());
27
28
29   
30    Value * nRead = iBuilder->CreateReadCall(stdin_fileno, bytePtr, segmentBytes);
31    Value * bytesRead = iBuilder->CreateSelect(iBuilder->CreateICmpSLT(nRead, iBuilder->getSize(0)), iBuilder->getSize(0), nRead);
32    produced = iBuilder->CreateAdd(produced, iBuilder->CreateUDiv(bytesRead, itemBytes));
33    setProducedItemCount("codeUnitBuffer", produced);
34    Value * lessThanFullSegment = iBuilder->CreateICmpULT(bytesRead, segmentBytes);
35    iBuilder->CreateCondBr(lessThanFullSegment, setTermination, stdInExit);
36    iBuilder->SetInsertPoint(setTermination);
37    setTerminationSignal();
38    iBuilder->CreateBr(stdInExit);
39   
40    iBuilder->SetInsertPoint(stdInExit);
41
42   
43}
44
45StdInKernel::StdInKernel(IDISA::IDISA_Builder * iBuilder, unsigned blocksPerSegment, unsigned codeUnitWidth)
46: SegmentOrientedKernel(iBuilder, "stdin_source", {}, {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {}, {}, {})
47, mSegmentBlocks(blocksPerSegment)
48, mCodeUnitWidth(codeUnitWidth) {
49   
50}
51
52void FileSource::generateInitMethod() {
53    BasicBlock * setTerminationOnFailure = CreateBasicBlock("setTerminationOnFailure");
54    BasicBlock * fileSourceInitExit = CreateBasicBlock("fileSourceInitExit");
55    Value * handle = iBuilder->CreateFOpenCall(getScalarField("fileName"), iBuilder->CreateGlobalStringPtr("r"));
56    setScalarField("IOstreamPtr", handle);
57    Value * failure = iBuilder->CreateICmpEQ(iBuilder->CreatePtrToInt(handle, iBuilder->getSizeTy()), iBuilder->getSize(0));
58    iBuilder->CreateCondBr(failure, setTerminationOnFailure, fileSourceInitExit);
59    iBuilder->SetInsertPoint(setTerminationOnFailure);
60    setTerminationSignal();
61    iBuilder->CreateBr(fileSourceInitExit);
62    iBuilder->SetInsertPoint(fileSourceInitExit);
63}
64   
65void FileSource::generateDoSegmentMethod(Value * doFinal, const std::vector<Value *> & producerPos) {
66
67    BasicBlock * closeFile = CreateBasicBlock("closeFile");
68    BasicBlock * fileSourceExit = CreateBasicBlock("fileSourceExit");
69    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
70   
71    Value * produced = getProducedItemCount("codeUnitBuffer");
72    Value * bytePtr = getOutputStream("codeUnitBuffer", iBuilder->getInt32(0));
73    bytePtr = iBuilder->CreatePointerCast(bytePtr, iBuilder->getInt8PtrTy());
74
75    Value * IOstreamPtr = getScalarField("IOstreamPtr");
76    Value * itemsToDo = iBuilder->getSize(mSegmentBlocks * iBuilder->getBitBlockWidth());
77    Value * nRead = iBuilder->CreateFReadCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
78    produced = iBuilder->CreateAdd(produced, nRead);
79    setProducedItemCount("codeUnitBuffer", produced);
80    Value * lessThanFullSegment = iBuilder->CreateICmpULT(nRead, itemsToDo);
81    iBuilder->CreateCondBr(lessThanFullSegment, closeFile, fileSourceExit);
82
83    iBuilder->SetInsertPoint(closeFile);
84    iBuilder->CreateFCloseCall(IOstreamPtr);
85    setTerminationSignal();
86    iBuilder->CreateBr(fileSourceExit);
87   
88    iBuilder->SetInsertPoint(fileSourceExit);
89   
90}
91   
92FileSource::FileSource(IDISA::IDISA_Builder * iBuilder, unsigned blocksPerSegment, unsigned codeUnitWidth)
93: SegmentOrientedKernel(iBuilder, "filesink", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {},
94                {Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getFILEptrTy(), "IOstreamPtr"}})
95, mSegmentBlocks(blocksPerSegment)
96, mCodeUnitWidth(codeUnitWidth) {
97}
98
99}
Note: See TracBrowser for help on using the repository browser.