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

Last change on this file since 5234 was 5234, checked in by nmedfort, 2 years ago

Modified memory alignment mechanism for GetPropertyValueGrepString? + misc. changes.

File size: 6.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 <kernels/stdout_kernel.h>
6#include <kernels/kernel.h>
7#include <IDISA/idisa_builder.h>
8
9namespace kernel {
10
11static Function * create_write(Module * const mod, IDISA::IDISA_Builder * builder) {
12    Function * write = mod->getFunction("write");
13    if (write == nullptr) {
14        IntegerType * sizeTy = builder->getSizeTy();
15        IntegerType * int32Ty = builder->getInt32Ty();
16        PointerType * int8PtrTy = builder->getInt8PtrTy();
17        write = cast<Function>(mod->getOrInsertFunction("write",
18                                 AttributeSet().addAttribute(mod->getContext(), 2U, Attribute::NoAlias),
19                                 sizeTy, int32Ty, int8PtrTy, sizeTy, nullptr));
20    }
21    return write;
22}
23
24// The doBlock method is deprecated.   But in case it is used, just call doSegment with
25// 1 as the number of blocks to do.
26void StdOutKernel::generateDoBlockMethod() {
27    auto savePoint = iBuilder->saveIP();
28    Module * m = iBuilder->getModule();
29    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
30    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
31    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
32    Value * self = getParameter(doBlockFunction, "self");
33    iBuilder->CreateCall(doSegmentFunction, {self, iBuilder->getSize(1)});
34    iBuilder->CreateRetVoid();
35    iBuilder->restoreIP(savePoint);
36}
37           
38// Rather than using doBlock logic to write one block at a time, this custom
39// doSegment method, writes the entire segment with a single write call.
40void StdOutKernel::generateDoSegmentMethod() {
41    auto savePoint = iBuilder->saveIP();
42    Module * m = iBuilder->getModule();
43    Function * writefn = create_write(m, iBuilder);
44    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
45    Type * i8PtrTy = iBuilder->getInt8PtrTy();
46   
47    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doSegmentFunction, 0));
48    BasicBlock * setTermination = BasicBlock::Create(iBuilder->getContext(), "setTermination", doSegmentFunction, 0);
49    BasicBlock * stdOutexit = BasicBlock::Create(iBuilder->getContext(), "stdOutexit", doSegmentFunction, 0);
50    Constant * blockItems = ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getBitBlockWidth());
51    Constant * itemBytes = ConstantInt::get(iBuilder->getSizeTy(), mCodeUnitWidth/8);
52   
53    Function::arg_iterator args = doSegmentFunction->arg_begin();
54    Value * self = &*(args++);
55    Value * blocksToDo = &*(args);
56    ////iBuilder->CallPrintInt("blocksToDo", blocksToDo);
57    Value * streamStructPtr = getStreamSetStructPtr(self, "codeUnitBuffer");
58    //iBuilder->CallPrintInt("streamStructPtr", iBuilder->CreatePtrToInt(streamStructPtr, iBuilder->getInt64Ty()));
59
60    LoadInst * producerPos = iBuilder->CreateAtomicLoadAcquire(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr));
61    //iBuilder->CallPrintInt("producerPos", producerPos);
62    Value * processed = getProcessedItemCount(self);
63    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
64    Value * itemsMax = iBuilder->CreateMul(blocksToDo, blockItems);
65    Value * lessThanFullSegment = iBuilder->CreateICmpULT(itemsAvail, itemsMax);
66    Value * itemsToDo = iBuilder->CreateSelect(lessThanFullSegment, itemsAvail, itemsMax);
67   
68    Value * blockNo = getScalarField(self, blockNoScalar);
69    //iBuilder->CallPrintInt("blockNo", blockNo);
70    Value * basePtr = getStreamSetBlockPtr(self, "codeUnitBuffer", blockNo);
71    //iBuilder->CallPrintInt("basePtr", iBuilder->CreatePtrToInt(basePtr, iBuilder->getInt64Ty()));
72    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
73    Value * bytePtr = iBuilder->CreateGEP(iBuilder->CreateBitCast(basePtr, i8PtrTy), byteOffset);
74
75    iBuilder->CreateCall(writefn, std::vector<Value *>({iBuilder->getInt32(1), bytePtr, iBuilder->CreateMul(itemsToDo, itemBytes)}));
76   
77    processed = iBuilder->CreateAdd(processed, itemsToDo);
78    setProcessedItemCount(self, processed);
79    setScalarField(self, blockNoScalar, iBuilder->CreateUDiv(processed, blockItems));
80    mStreamSetInputBuffers[0]->setConsumerPos(streamStructPtr, processed);
81
82    Value * endSignal = iBuilder->CreateLoad(mStreamSetInputBuffers[0]->getEndOfInputPtr(streamStructPtr));
83    Value * inFinalSegment = iBuilder->CreateAnd(endSignal, lessThanFullSegment);
84   
85    iBuilder->CreateCondBr(inFinalSegment, setTermination, stdOutexit);
86    iBuilder->SetInsertPoint(setTermination);
87
88    setTerminationSignal(self);
89
90    iBuilder->CreateBr(stdOutexit);
91    iBuilder->SetInsertPoint(stdOutexit);
92    iBuilder->CreateRetVoid();
93    iBuilder->restoreIP(savePoint);
94}
95
96void StdOutKernel::generateFinalBlockMethod() {
97    auto savePoint = iBuilder->saveIP();
98    Module * m = iBuilder->getModule();
99    Function * writefn = create_write(m, iBuilder);
100    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
101    Type * i8PtrTy = iBuilder->getInt8PtrTy();
102   
103    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_flush", finalBlockFunction, 0));
104    Constant * blockItems = ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getBitBlockWidth());
105    Constant * itemBytes = ConstantInt::get(iBuilder->getSizeTy(), mCodeUnitWidth/8);
106    Value * self = getParameter(finalBlockFunction, "self");
107    Value * streamStructPtr = getStreamSetStructPtr(self, "codeUnitBuffer");
108    LoadInst * producerPos = iBuilder->CreateAtomicLoadAcquire(mStreamSetInputBuffers[0]->getProducerPosPtr(streamStructPtr));
109    Value * processed = getProcessedItemCount(self);
110    Value * itemsAvail = iBuilder->CreateSub(producerPos, processed);
111    Value * blockNo = getScalarField(self, blockNoScalar);
112    Value * basePtr = getStreamSetBlockPtr(self, "codeUnitBuffer", blockNo);
113    Value * byteOffset = iBuilder->CreateMul(iBuilder->CreateURem(processed, blockItems), itemBytes);
114    Value * bytePtr = iBuilder->CreateGEP(iBuilder->CreateBitCast(basePtr, i8PtrTy), byteOffset);
115    iBuilder->CreateCall(writefn, std::vector<Value *>({iBuilder->getInt32(1), bytePtr, iBuilder->CreateMul(itemsAvail, itemBytes)}));
116    setProcessedItemCount(self, producerPos);
117    mStreamSetInputBuffers[0]->setConsumerPos(streamStructPtr, producerPos);
118    setTerminationSignal(self);
119    iBuilder->CreateRetVoid();
120    iBuilder->restoreIP(savePoint);
121}
122}
Note: See TracBrowser for help on using the repository browser.