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

Last change on this file since 5264 was 5264, checked in by cameron, 2 years ago

Small fixes

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