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

Last change on this file since 5185 was 5185, checked in by cameron, 3 years ago

Linear buffers; stdout_kernel; cache-aligned alloca, restructured u8u16, clean-ups

File size: 5.8 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 <IDISA/idisa_builder.h>
8#include <llvm/IR/TypeBuilder.h>
9
10namespace kernel {
11
12static Function * create_write(Module * const mod) {
13    Function * write = mod->getFunction("write");
14    if (write == nullptr) {
15        FunctionType *write_type =
16        TypeBuilder<long(int, char *, long), false>::get(mod->getContext());
17        write = cast<Function>(mod->getOrInsertFunction("write", write_type,
18                                                         AttributeSet().addAttribute(mod->getContext(), 2U, Attribute::NoAlias)));
19    }
20    return write;
21}
22
23// The doBlock method is deprecated.   But incase it is used, just call doSegment with
24// 1 as the number of blocks to do.
25void stdOutKernel::generateDoBlockMethod() {
26    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
27    Module * m = iBuilder->getModule();
28    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
29    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
30    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
31    Value * self = getParameter(doBlockFunction, "self");
32    iBuilder->CreateCall(doSegmentFunction, {self, ConstantInt::get(iBuilder->getSizeTy(), 1)});
33    iBuilder->CreateRetVoid();
34    iBuilder->restoreIP(savePoint);
35}
36           
37void stdOutKernel::generateDoSegmentMethod() {
38    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
39    Module * m = iBuilder->getModule();
40    Function * writefn = create_write(m);
41    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
42    Type * i8PtrTy = iBuilder->getInt8PtrTy();
43   
44    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doSegmentFunction, 0));
45    Constant * stride = ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride());
46    Constant * strideBytes = ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride() * mCodeUnitWidth/8);
47   
48    Function::arg_iterator args = doSegmentFunction->arg_begin();
49    Value * self = &*(args++);
50    Value * blocksToDo = &*(args);
51    ////iBuilder->CallPrintInt("blocksToDo", blocksToDo);
52    Value * segmentNo = getLogicalSegmentNo(self);
53    Value * streamStructPtr = getStreamSetStructPtr(self, "codeUnitBuffer");
54    //iBuilder->CallPrintInt("streamStructPtr", iBuilder->CreatePtrToInt(streamStructPtr, iBuilder->getInt64Ty()));
55
56    LoadInst * producerPos = iBuilder->CreateAlignedLoad(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr), sizeof(size_t));
57    producerPos->setOrdering(AtomicOrdering::Acquire);
58    //iBuilder->CallPrintInt("producerPos", producerPos);
59
60    Value * processed = getProcessedItemCount(self);
61    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
62    //iBuilder->CallPrintInt("previously processed", processed);
63    Value * blocksAvail = iBuilder->CreateUDiv(itemsAvail, stride);
64    //iBuilder->CallPrintInt("blocksAvail", blocksAvail);
65    /* Adjust the number of full blocks to do, based on the available data, if necessary. */
66    blocksToDo = iBuilder->CreateSelect(iBuilder->CreateICmpULT(blocksToDo, blocksAvail), blocksToDo, blocksAvail);
67    Value * blockNo = getScalarField(self, blockNoScalar);
68    //iBuilder->CallPrintInt("blockNo", blockNo);
69    Value * basePtr = getStreamSetBlockPtr(self, "codeUnitBuffer", blockNo);
70    //iBuilder->CallPrintInt("basePtr", iBuilder->CreatePtrToInt(basePtr, iBuilder->getInt64Ty()));
71    Value * bytesToDo = iBuilder->CreateMul(blocksToDo, strideBytes);
72    //iBuilder->CallPrintInt("bytesToDo", bytesToDo);
73    iBuilder->CreateCall(writefn, std::vector<Value *>({iBuilder->getInt32(1), iBuilder->CreateBitCast(basePtr, i8PtrTy), bytesToDo}));
74   
75    setScalarField(self, blockNoScalar, iBuilder->CreateAdd(blockNo, blocksToDo));
76    processed = iBuilder->CreateAdd(processed, iBuilder->CreateMul(blocksToDo, stride));
77    setProcessedItemCount(self, processed);
78    mStreamSetInputBuffers[0]->setConsumerPos(streamStructPtr, processed);
79    // Must be the last action, for synchronization.
80    setLogicalSegmentNo(self, iBuilder->CreateAdd(segmentNo, ConstantInt::get(iBuilder->getSizeTy(), 1)));
81   
82    iBuilder->CreateRetVoid();
83    iBuilder->restoreIP(savePoint);
84}
85
86void stdOutKernel::generateFinalBlockMethod() {
87    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
88    Module * m = iBuilder->getModule();
89    Function * writefn = create_write(m);
90    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
91    Type * i8PtrTy = iBuilder->getInt8PtrTy();
92   
93    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_flush", finalBlockFunction, 0));
94    Value * self = getParameter(finalBlockFunction, "self");
95    Value * streamStructPtr = getStreamSetStructPtr(self, "codeUnitBuffer");
96    LoadInst * producerPos = iBuilder->CreateAlignedLoad(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr), sizeof(size_t));
97    producerPos->setOrdering(AtomicOrdering::Acquire);
98    Value * processed = getProcessedItemCount(self);
99    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
100    Value * blockNo = getScalarField(self, blockNoScalar);
101    Value * basePtr = getStreamSetBlockPtr(self, "codeUnitBuffer", blockNo);
102    Value * bytesToDo = iBuilder->CreateMul(itemsAvail, ConstantInt::get(iBuilder->getSizeTy(), mCodeUnitWidth/8));
103
104    iBuilder->CreateCall(writefn, std::vector<Value *>({iBuilder->getInt32(1), iBuilder->CreateBitCast(basePtr, i8PtrTy), bytesToDo}));
105   
106    setProcessedItemCount(self, producerPos);
107    mStreamSetInputBuffers[0]->setConsumerPos(streamStructPtr, producerPos);
108    iBuilder->CreateRetVoid();
109    iBuilder->restoreIP(savePoint);
110}
111}
Note: See TracBrowser for help on using the repository browser.