source: icGREP/icgrep-devel/icgrep/kernels/stdin_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.8 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    Type * i8PtrTy = iBuilder->getInt8PtrTy();
17   
18    BasicBlock * setTermination = CreateBasicBlock("setTermination");
19    BasicBlock * stdInExit = CreateBasicBlock("stdInExit");
20    ConstantInt * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
21    ConstantInt * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
22    ConstantInt * segmentBytes = iBuilder->getSize(mSegmentBlocks * iBuilder->getBitBlockWidth() * mCodeUnitWidth/8);
23    ConstantInt * stdin_fileno = iBuilder->getInt32(STDIN_FILENO);
24    Value * produced = getProducedItemCount("codeUnitBuffer");
25    Value * blockNo = iBuilder->CreateUDiv(produced, blockItems);
26    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(produced, blockItems), itemBytes);
27    Value * bytePtr = getStreamView(i8PtrTy, "codeUnitBuffer", blockNo, byteOffset);
28   
29    Value * nRead = iBuilder->CreateReadCall(stdin_fileno, bytePtr, segmentBytes);
30    Value * bytesRead = iBuilder->CreateSelect(iBuilder->CreateICmpSLT(nRead, iBuilder->getSize(0)), iBuilder->getSize(0), nRead);
31    produced = iBuilder->CreateAdd(produced, iBuilder->CreateUDiv(bytesRead, itemBytes));
32    setProducedItemCount("codeUnitBuffer", produced);
33    Value * lessThanFullSegment = iBuilder->CreateICmpULT(bytesRead, segmentBytes);
34    iBuilder->CreateCondBr(lessThanFullSegment, setTermination, stdInExit);
35    iBuilder->SetInsertPoint(setTermination);
36    setTerminationSignal();
37    iBuilder->CreateBr(stdInExit);
38   
39    iBuilder->SetInsertPoint(stdInExit);
40
41   
42}
43
44StdInKernel::StdInKernel(IDISA::IDISA_Builder * iBuilder, unsigned blocksPerSegment, unsigned codeUnitWidth)
45: SegmentOrientedKernel(iBuilder, "stdin_source", {}, {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {}, {}, {})
46, mSegmentBlocks(blocksPerSegment)
47, mCodeUnitWidth(codeUnitWidth) {
48   
49}
50
51void FileSource::generateInitMethod() {
52    BasicBlock * setTerminationOnFailure = CreateBasicBlock("setTerminationOnFailure");
53    BasicBlock * fileSourceInitExit = CreateBasicBlock("fileSourceInitExit");
54    Value * handle = iBuilder->CreateFOpenCall(getScalarField("fileName"), iBuilder->CreateGlobalStringPtr("r"));
55    setScalarField("IOstreamPtr", handle);
56    Value * failure = iBuilder->CreateICmpEQ(iBuilder->CreatePtrToInt(handle, iBuilder->getSizeTy()), iBuilder->getSize(0));
57    iBuilder->CreateCondBr(failure, setTerminationOnFailure, fileSourceInitExit);
58    iBuilder->SetInsertPoint(setTerminationOnFailure);
59    setTerminationSignal();
60    iBuilder->CreateBr(fileSourceInitExit);
61    iBuilder->SetInsertPoint(fileSourceInitExit);
62}
63   
64void FileSource::generateDoSegmentMethod(Value * doFinal, const std::vector<Value *> & producerPos) {
65
66    PointerType * i8PtrTy = iBuilder->getInt8PtrTy();
67    BasicBlock * closeFile = CreateBasicBlock("closeFile");
68    BasicBlock * fileSourceExit = CreateBasicBlock("fileSourceExit");
69    Constant * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
70    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
71   
72    Value * produced = getProducedItemCount("codeUnitBuffer");
73    Value * blockNo = iBuilder->CreateUDiv(produced, blockItems);
74    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(produced, blockItems), itemBytes);
75    Value * bytePtr = getStreamView(i8PtrTy, "codeUnitBuffer", blockNo, byteOffset);
76    Value * IOstreamPtr = getScalarField("IOstreamPtr");
77    Value * itemsToDo = iBuilder->getSize(mSegmentBlocks * iBuilder->getBitBlockWidth());
78    Value * nRead = iBuilder->CreateFReadCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
79    produced = iBuilder->CreateAdd(produced, nRead);
80    setProducedItemCount("codeUnitBuffer", produced);
81    Value * lessThanFullSegment = iBuilder->CreateICmpULT(nRead, itemsToDo);
82    iBuilder->CreateCondBr(lessThanFullSegment, closeFile, fileSourceExit);
83
84    iBuilder->SetInsertPoint(closeFile);
85    iBuilder->CreateFCloseCall(IOstreamPtr);
86    setTerminationSignal();
87    iBuilder->CreateBr(fileSourceExit);
88   
89    iBuilder->SetInsertPoint(fileSourceExit);
90   
91}
92   
93FileSource::FileSource(IDISA::IDISA_Builder * iBuilder, unsigned blocksPerSegment, unsigned codeUnitWidth)
94: SegmentOrientedKernel(iBuilder, "filesink", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {},
95                {Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getFILEptrTy(), "IOstreamPtr"}})
96, mSegmentBlocks(blocksPerSegment)
97, mCodeUnitWidth(codeUnitWidth) {
98}
99
100}
Note: See TracBrowser for help on using the repository browser.