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

Last change on this file since 5256 was 5256, checked in by cameron, 9 months ago

noTerminate attribute for stdout kernel

File size: 5.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 <kernels/stdout_kernel.h>
6#include <kernels/kernel.h>
7#include <IR_Gen/idisa_builder.h>
8
9namespace kernel {
10
11// The doBlock method is deprecated.   But in case it is used, just call doSegment with
12// 1 as the number of blocks to do.
13void StdOutKernel::generateDoBlockMethod() const {
14    auto savePoint = iBuilder->saveIP();
15    Module * m = iBuilder->getModule();
16    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
17    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
18    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
19    Value * self = getParameter(doBlockFunction, "self");
20    iBuilder->CreateCall(doSegmentFunction, {self, iBuilder->getSize(1)});
21    iBuilder->CreateRetVoid();
22    iBuilder->restoreIP(savePoint);
23}
24           
25// Rather than using doBlock logic to write one block at a time, this custom
26// doSegment method, writes the entire segment with a single write call.
27void StdOutKernel::generateDoSegmentMethod() const {
28    auto savePoint = iBuilder->saveIP();
29    Module * m = iBuilder->getModule();
30    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
31    Type * i8PtrTy = iBuilder->getInt8PtrTy();
32   
33    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doSegmentFunction, 0));
34    Constant * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
35    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
36   
37    Function::arg_iterator args = doSegmentFunction->arg_begin();
38    Value * self = &*(args++);
39    Value * blocksToDo = &*(args);
40    ////iBuilder->CallPrintInt("blocksToDo", blocksToDo);
41    Value * streamStructPtr = getStreamSetStructPtr(self, "codeUnitBuffer");
42    //iBuilder->CallPrintInt("streamStructPtr", iBuilder->CreatePtrToInt(streamStructPtr, iBuilder->getInt64Ty()));
43
44    LoadInst * producerPos = iBuilder->CreateAtomicLoadAcquire(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr));
45    //iBuilder->CallPrintInt("producerPos", producerPos);
46    Value * processed = getProcessedItemCount(self, "codeUnitBuffer");
47    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
48    Value * itemsMax = iBuilder->CreateMul(blocksToDo, blockItems);
49    Value * lessThanFullSegment = iBuilder->CreateICmpULT(itemsAvail, itemsMax);
50    Value * itemsToDo = iBuilder->CreateSelect(lessThanFullSegment, itemsAvail, itemsMax);
51   
52    Value * blockNo = getScalarField(self, blockNoScalar);
53    //iBuilder->CallPrintInt("blockNo", blockNo);
54    Value * basePtr = getStreamSetBlockPtr(self, "codeUnitBuffer", blockNo);
55    //iBuilder->CallPrintInt("basePtr", iBuilder->CreatePtrToInt(basePtr, iBuilder->getInt64Ty()));
56    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
57    Value * bytePtr = iBuilder->CreateGEP(iBuilder->CreateBitCast(basePtr, i8PtrTy), byteOffset);
58
59    iBuilder->CreateWriteCall(iBuilder->getInt32(1), bytePtr, iBuilder->CreateMul(itemsToDo, itemBytes));
60
61    processed = iBuilder->CreateAdd(processed, itemsToDo);
62    setProcessedItemCount(self, "codeUnitBuffer", processed);
63    setScalarField(self, blockNoScalar, iBuilder->CreateUDiv(processed, blockItems));
64    mStreamSetInputBuffers[0]->setConsumerPos(streamStructPtr, processed);
65
66    iBuilder->CreateRetVoid();
67    iBuilder->restoreIP(savePoint);
68}
69
70void StdOutKernel::generateFinalBlockMethod() const {
71    auto savePoint = iBuilder->saveIP();
72    Module * m = iBuilder->getModule();
73    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
74    Type * i8PtrTy = iBuilder->getInt8PtrTy();
75   
76    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_flush", finalBlockFunction, 0));
77    Constant * blockItems = iBuilder->getSize(iBuilder->getBitBlockWidth());
78    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
79    Value * self = getParameter(finalBlockFunction, "self");
80    Value * streamStructPtr = getStreamSetStructPtr(self, "codeUnitBuffer");
81    LoadInst * producerPos = iBuilder->CreateAtomicLoadAcquire(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr));
82    Value * processed = getProcessedItemCount(self, "codeUnitBuffer");
83    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
84    Value * blockNo = getScalarField(self, blockNoScalar);
85    Value * basePtr = getStreamSetBlockPtr(self, "codeUnitBuffer", blockNo);
86    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
87    Value * bytePtr = iBuilder->CreateGEP(iBuilder->CreateBitCast(basePtr, i8PtrTy), byteOffset);
88    iBuilder->CreateWriteCall(iBuilder->getInt32(1), bytePtr, iBuilder->CreateMul(itemsAvail, itemBytes));
89    setProcessedItemCount(self, "codeUnitBuffer", producerPos);
90    mStreamSetInputBuffers[0]->setConsumerPos(streamStructPtr, producerPos);
91    setTerminationSignal(self);
92    iBuilder->CreateRetVoid();
93    iBuilder->restoreIP(savePoint);
94}
95}
Note: See TracBrowser for help on using the repository browser.