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

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

CreateAtomicLoadAcquire?, CreateAtomicStoreRelease? with NVPTX overrides

File size: 6.0 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 * itemBytes = ConstantInt::get(iBuilder->getSizeTy(), 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->CreateAtomicLoadAcquire(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr));
57    //iBuilder->CallPrintInt("producerPos", producerPos);
58    Value * endSignal = iBuilder->CreateLoad(mStreamSetInputBuffers[0]->hasEndOfInputPtr(streamStructPtr));
59
60    Value * blockNo = getScalarField(self, blockNoScalar);
61    //iBuilder->CallPrintInt("blockNo", blockNo);
62    Value * basePtr = getStreamSetBlockPtr(self, "codeUnitBuffer", blockNo);
63    //iBuilder->CallPrintInt("basePtr", iBuilder->CreatePtrToInt(basePtr, iBuilder->getInt64Ty()));
64   
65    Value * processed = getProcessedItemCount(self);
66    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
67    //iBuilder->CallPrintInt("previously processed", processed);
68    Value * blocksAvail = iBuilder->CreateUDiv(itemsAvail, stride);
69    //iBuilder->CallPrintInt("blocksAvail", blocksAvail);
70   
71    Value * lessThanFullSegment = iBuilder->CreateICmpULT(blocksAvail, blocksToDo);
72    Value * inFinalSegment = iBuilder->CreateAnd(endSignal, lessThanFullSegment);
73    /* Adjust the number of full blocks to do, based on the available data, if necessary. */
74    blocksToDo = iBuilder->CreateSelect(lessThanFullSegment, blocksAvail, blocksToDo);
75   
76    Value * itemsToDo = iBuilder->CreateMul(blocksToDo, stride);
77    itemsToDo = iBuilder->CreateSelect(inFinalSegment, itemsAvail, itemsToDo);
78    //iBuilder->CallPrintInt("bytesToDo", bytesToDo);
79    iBuilder->CreateCall(writefn, std::vector<Value *>({iBuilder->getInt32(1), iBuilder->CreateBitCast(basePtr, i8PtrTy), iBuilder->CreateMul(itemsToDo, itemBytes)}));
80   
81    setScalarField(self, blockNoScalar, iBuilder->CreateAdd(blockNo, blocksToDo));
82    processed = iBuilder->CreateAdd(processed, itemsToDo);
83    setProcessedItemCount(self, processed);
84    mStreamSetInputBuffers[0]->setConsumerPos(streamStructPtr, processed);
85    // Must be the last action, for synchronization.
86    setLogicalSegmentNo(self, iBuilder->CreateAdd(segmentNo, ConstantInt::get(iBuilder->getSizeTy(), 1)));
87   
88    iBuilder->CreateRetVoid();
89    iBuilder->restoreIP(savePoint);
90}
91
92void stdOutKernel::generateFinalBlockMethod() {
93    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
94    Module * m = iBuilder->getModule();
95    Function * writefn = create_write(m);
96    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
97    Type * i8PtrTy = iBuilder->getInt8PtrTy();
98   
99    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_flush", finalBlockFunction, 0));
100    Value * self = getParameter(finalBlockFunction, "self");
101    Value * streamStructPtr = getStreamSetStructPtr(self, "codeUnitBuffer");
102    LoadInst * producerPos = iBuilder->CreateAtomicLoadAcquire(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr));
103    Value * processed = getProcessedItemCount(self);
104    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
105    Value * blockNo = getScalarField(self, blockNoScalar);
106    Value * basePtr = getStreamSetBlockPtr(self, "codeUnitBuffer", blockNo);
107    Value * bytesToDo = iBuilder->CreateMul(itemsAvail, ConstantInt::get(iBuilder->getSizeTy(), mCodeUnitWidth/8));
108
109    iBuilder->CreateCall(writefn, std::vector<Value *>({iBuilder->getInt32(1), iBuilder->CreateBitCast(basePtr, i8PtrTy), bytesToDo}));
110   
111    setProcessedItemCount(self, producerPos);
112    mStreamSetInputBuffers[0]->setConsumerPos(streamStructPtr, producerPos);
113    iBuilder->CreateRetVoid();
114    iBuilder->restoreIP(savePoint);
115}
116}
Note: See TracBrowser for help on using the repository browser.