source: icGREP/icgrep-devel/icgrep/kernels/mmap_kernel.cpp @ 5260

Last change on this file since 5260 was 5260, checked in by nmedfort, 9 months ago

Changes working towards simplifying accessing stream elements + some modifications to simplify include / forward declarations within the CodeGen? library.

File size: 4.1 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5#include "mmap_kernel.h"
6#include <llvm/IR/Function.h>  // for Function, Function::arg_iterator
7#include <llvm/IR/Module.h>
8#include <IR_Gen/idisa_builder.h>
9#include <kernels/streamset.h>
10namespace llvm { class BasicBlock; }
11namespace llvm { class Constant; }
12namespace llvm { class Module; }
13namespace llvm { class Value; }
14
15using namespace llvm;
16
17namespace kernel {
18           
19void MMapSourceKernel::generateDoSegmentMethod() const {
20    auto savePoint = iBuilder->saveIP();
21    Module * m = iBuilder->getModule();
22    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
23   
24    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), mKernelName + "_entry", doSegmentFunction, 0));
25    BasicBlock * setTermination = BasicBlock::Create(iBuilder->getContext(), "setTermination", doSegmentFunction, 0);
26    BasicBlock * mmapSourceExit = BasicBlock::Create(iBuilder->getContext(), "mmapSourceExit", doSegmentFunction, 0);
27    Constant * segmentItems = iBuilder->getSize(mSegmentBlocks * iBuilder->getBitBlockWidth());
28   
29   
30    Function::arg_iterator args = doSegmentFunction->arg_begin();
31    Value * self = &*(args);
32   
33    Value * fileItems = getScalarField(self, "fileSize");
34    if (mCodeUnitWidth > 8) {
35        fileItems = iBuilder->CreateUDiv(fileItems, iBuilder->getSize(mCodeUnitWidth/8));
36    }
37    Value * produced = getProducedItemCount(self, "sourceBuffer");
38    Value * itemsAvail = iBuilder->CreateSub(fileItems, produced);
39    Value * lessThanFullSegment = iBuilder->CreateICmpULT(itemsAvail, segmentItems);
40    Value * itemsToDo = iBuilder->CreateSelect(lessThanFullSegment, itemsAvail, segmentItems);
41    produced = iBuilder->CreateAdd(produced, itemsToDo);
42    setProducedItemCount(self, "sourceBuffer", produced);
43   
44    iBuilder->CreateCondBr(lessThanFullSegment, setTermination, mmapSourceExit);
45    iBuilder->SetInsertPoint(setTermination);
46    setTerminationSignal(self);
47    iBuilder->CreateBr(mmapSourceExit);
48    iBuilder->SetInsertPoint(mmapSourceExit);
49    Value * ssStructPtr = getStreamSetStructPtr(self, "sourceBuffer");
50    Value * producerPosPtr = mStreamSetOutputBuffers[0]->getProducerPosPtr(ssStructPtr);
51    iBuilder->CreateAtomicStoreRelease(produced, producerPosPtr);
52   
53    iBuilder->CreateRetVoid();
54    iBuilder->restoreIP(savePoint);
55}
56
57void MMapSourceKernel::generateFinalSegmentMethod() const {
58    auto savePoint = iBuilder->saveIP();
59    Module * m = iBuilder->getModule();
60    Function * finalSegmentFunction = m->getFunction(mKernelName + finalSegment_suffix);
61    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", finalSegmentFunction, 0));
62       
63    Function::arg_iterator args = finalSegmentFunction->arg_begin();
64    Value * self = &*(args++);
65    Value * blocksToDo = &*(args);
66   
67    createDoSegmentCall(self, blocksToDo);
68
69    iBuilder->CreateRetVoid();
70    iBuilder->restoreIP(savePoint);
71}
72
73
74// The doBlock method is deprecated.   But in case it is used, just call doSegment with
75// 1 as the number of blocks to do.
76void MMapSourceKernel::generateDoBlockMethod() const {
77    auto savePoint = iBuilder->saveIP();
78    Module * m = iBuilder->getModule();
79    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
80    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
81    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
82    Value * self = getParameter(doBlockFunction, "self");
83    iBuilder->CreateCall(doSegmentFunction, {self, iBuilder->getSize(1)});
84    iBuilder->CreateRetVoid();
85    iBuilder->restoreIP(savePoint);
86}
87
88MMapSourceKernel::MMapSourceKernel(IDISA::IDISA_Builder * iBuilder, unsigned blocksPerSegment, unsigned codeUnitWidth) :
89KernelBuilder(iBuilder, "mmap_source",
90              {}, {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "sourceBuffer"}},
91              {Binding{iBuilder->getSizeTy(), "fileSize"}}, {}, {})
92, mSegmentBlocks(blocksPerSegment)
93, mCodeUnitWidth(codeUnitWidth) {
94
95}
96
97}
Note: See TracBrowser for help on using the repository browser.