source: icGREP/icgrep-devel/icgrep/kernels/kernel.cpp @ 5103

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

Clean out hard-coded circular buffer

File size: 9.7 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
6#include "kernel.h"
7#include <llvm/IR/Module.h>
8#include <llvm/IR/Type.h>
9#include <llvm/IR/Value.h>
10#include <llvm/Support/raw_ostream.h>
11
12using namespace llvm;
13using namespace kernel;
14
15KernelBuilder::KernelBuilder(IDISA::IDISA_Builder * builder,
16                                 std::string kernelName,
17                                 std::vector<StreamSetBinding> stream_inputs,
18                                 std::vector<StreamSetBinding> stream_outputs,
19                                 std::vector<ScalarBinding> scalar_parameters,
20                                 std::vector<ScalarBinding> scalar_outputs,
21                                 std::vector<ScalarBinding> internal_scalars) :
22    KernelInterface(builder, kernelName, stream_inputs, stream_outputs, scalar_parameters, scalar_outputs, internal_scalars) {}
23
24void KernelBuilder::addScalar(Type * t, std::string scalarName) {
25    if (LLVM_UNLIKELY(mKernelStateType != nullptr)) {
26        throw std::runtime_error("Illegal addition of kernel field after kernel state finalized: " + scalarName);
27    }
28    unsigned index = mKernelFields.size();
29    mKernelFields.push_back(t);
30    mInternalStateNameMap.emplace(scalarName, iBuilder->getInt32(index));
31}
32
33void KernelBuilder::setDoBlockReturnType(llvm::Type * t) {
34    mDoBlockReturnType = t;
35}
36
37void KernelBuilder::prepareKernel() {
38    if (!mDoBlockReturnType) mDoBlockReturnType = iBuilder->getVoidTy();
39    addScalar(iBuilder->getInt64Ty(), blockNoScalar);
40    for (auto sSet : mStreamSetInputs) {
41        mScalarInputs.push_back(ScalarBinding{PointerType::get(sSet.ssType.getStreamSetBlockType(), 0), sSet.ssName + basePtrSuffix});
42    }
43    for (auto sSet : mStreamSetOutputs) {
44        mScalarInputs.push_back(ScalarBinding{PointerType::get(sSet.ssType.getStreamSetBlockType(), 0), sSet.ssName + basePtrSuffix});
45    }
46    for (auto binding : mScalarInputs) {
47        addScalar(binding.scalarType, binding.scalarName);
48    }
49    for (auto binding : mScalarOutputs) {
50        addScalar(binding.scalarType, binding.scalarName);
51    }
52    for (auto binding : mInternalScalars) {
53        addScalar(binding.scalarType, binding.scalarName);
54    }
55    mKernelStateType = StructType::create(getGlobalContext(), mKernelFields, mKernelName);
56}
57
58std::unique_ptr<Module> KernelBuilder::createKernelModule() {
59    Module * saveModule = iBuilder->getModule();
60    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
61    std::unique_ptr<Module> theModule = make_unique<Module>(mKernelName + "_" + iBuilder->getBitBlockTypeName(), getGlobalContext());
62    Module * m = theModule.get();
63    iBuilder->setModule(m);
64    generateKernel();
65    iBuilder->setModule(saveModule);
66    iBuilder->restoreIP(savePoint);
67    return theModule;
68}
69
70void KernelBuilder::generateKernel() {
71    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
72    Module * m = iBuilder->getModule();
73
74    prepareKernel();  // possibly overriden by the KernelBuilder subtype
75    KernelInterface::addKernelDeclarations(m);
76    generateDoBlockMethod();     // must be implemented by the KernelBuilder subtype
77    generateFinalBlockMethod();  // possibly overriden by the KernelBuilder subtype
78    generateDoSegmentMethod();
79
80    // Implement the accumulator get functions
81    for (auto binding : mScalarOutputs) {
82        auto fnName = mKernelName + accumulator_infix + binding.scalarName;
83        Function * accumFn = m->getFunction(fnName);
84        iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "get_" + binding.scalarName, accumFn, 0));
85        Value * self = &*(accumFn->arg_begin());
86        Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), getScalarIndex(binding.scalarName)});
87        Value * retVal = iBuilder->CreateLoad(ptr);
88        iBuilder->CreateRet(retVal);
89    }
90    // Implement the initializer function
91    Function * initFunction = m->getFunction(mKernelName + init_suffix);
92    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "Init_entry", initFunction, 0));
93   
94    Function::arg_iterator args = initFunction->arg_begin();
95    Value * self = &*(args++);
96    iBuilder->CreateStore(Constant::getNullValue(mKernelStateType), self);
97    for (auto binding : mScalarInputs) {
98        Value * parm = &*(args++);
99        Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), getScalarIndex(binding.scalarName)});
100        iBuilder->CreateStore(parm, ptr);
101    }
102    iBuilder->CreateRetVoid();
103    iBuilder->restoreIP(savePoint);
104}
105
106//  The default finalBlock method simply dispatches to the doBlock routine.
107void KernelBuilder::generateFinalBlockMethod() {
108    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
109    Module * m = iBuilder->getModule();
110    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
111    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
112    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_entry", finalBlockFunction, 0));
113    // Final Block arguments: self, remaining, then the standard DoBlock args.
114    Function::arg_iterator args = finalBlockFunction->arg_begin();
115    Value * self = &*(args++);
116    /* Skip "remaining" arg */ args++;
117    std::vector<Value *> doBlockArgs = {self};
118    while (args != finalBlockFunction->arg_end()){
119        doBlockArgs.push_back(&*args++);
120    }
121    Value * rslt = iBuilder->CreateCall(doBlockFunction, doBlockArgs);
122    if (mDoBlockReturnType->isVoidTy()) {
123        iBuilder->CreateRetVoid();
124    }
125    else {
126        iBuilder->CreateRet(rslt);
127    }
128    iBuilder->restoreIP(savePoint);
129}
130
131//  The default doSegment method simply dispatches to the doBlock routine.
132void KernelBuilder::generateDoSegmentMethod() {
133    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
134    Module * m = iBuilder->getModule();
135    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
136    Function * doSegmentFunction = m->getFunction(mKernelName + doSegment_suffix);
137    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doSegmentFunction, 0));
138    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
139    BasicBlock * blockLoop = BasicBlock::Create(iBuilder->getContext(), "blockLoop", doSegmentFunction, 0);
140    BasicBlock * blocksDone = BasicBlock::Create(iBuilder->getContext(), "blocksDone", doSegmentFunction, 0);
141
142   
143    Function::arg_iterator args = doSegmentFunction->arg_begin();
144    Value * self = &*(args++);
145    Value * blocksToDo = &*(args);
146   
147    iBuilder->CreateBr(blockLoop);
148   
149    iBuilder->SetInsertPoint(blockLoop);
150    if (mDoBlockReturnType->isVoidTy()) {
151        PHINode * blocksRemaining = iBuilder->CreatePHI(iBuilder->getInt64Ty(), 2, "blocksRemaining");
152        blocksRemaining->addIncoming(blocksToDo, entryBlock);
153       
154        Value * blockNo = getScalarField(self, blockNoScalar);
155       
156        iBuilder->CreateCall(doBlockFunction, {self});
157        setScalarField(self, blockNoScalar, iBuilder->CreateAdd(blockNo, iBuilder->getInt64(1)));
158        blocksToDo = iBuilder->CreateSub(blocksRemaining, iBuilder->getInt64(1));
159        blocksRemaining->addIncoming(blocksToDo, blockLoop);
160        Value * notDone = iBuilder->CreateICmpUGT(blocksToDo, iBuilder->getInt64(0));
161        iBuilder->CreateCondBr(notDone, blockLoop, blocksDone);
162       
163        iBuilder->SetInsertPoint(blocksDone);
164        iBuilder->CreateRetVoid();
165    }
166    else {
167        PHINode * blocksRemaining = iBuilder->CreatePHI(iBuilder->getInt64Ty(), 2, "blocksRemaining");
168        blocksRemaining->addIncoming(blocksToDo, entryBlock);
169        PHINode * total = iBuilder->CreatePHI(mDoBlockReturnType, 2, "resultTotal");
170        total->addIncoming(ConstantInt::getNullValue(mDoBlockReturnType), entryBlock);
171
172        Value * blockNo = getScalarField(self, blockNoScalar);
173        std::vector<Value *> doBlockArgs = {self};
174       
175        Value * rslt = iBuilder->CreateCall(doBlockFunction, {self});
176        setScalarField(self, blockNoScalar, iBuilder->CreateAdd(blockNo, iBuilder->getInt64(1)));
177        blocksToDo = iBuilder->CreateSub(blocksRemaining, iBuilder->getInt64(1));
178        blocksRemaining->addIncoming(blocksToDo, blockLoop);
179        Value * notDone = iBuilder->CreateICmpUGT(blocksToDo, iBuilder->getInt64(0));
180        Value * totalSoFar = iBuilder->CreateAdd(total, rslt);
181        total->addIncoming(totalSoFar, blockLoop);
182        iBuilder->CreateCondBr(notDone, blockLoop, blocksDone);
183       
184        iBuilder->SetInsertPoint(blocksDone);
185        iBuilder->CreateRet(rslt);
186    }
187    iBuilder->restoreIP(savePoint);
188}
189
190Value * KernelBuilder::getScalarIndex(std::string fieldName) {
191    const auto f = mInternalStateNameMap.find(fieldName);
192    if (LLVM_UNLIKELY(f == mInternalStateNameMap.end())) {
193        throw std::runtime_error("Kernel does not contain internal state: " + fieldName);
194    }
195    return f->second;
196}
197
198Value * KernelBuilder::getScalarField(Value * self, std::string fieldName) {
199    Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), getScalarIndex(fieldName)});
200    return iBuilder->CreateLoad(ptr);
201}
202
203void KernelBuilder::setScalarField(Value * self, std::string fieldName, Value * newFieldVal) {
204    Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), getScalarIndex(fieldName)});
205    iBuilder->CreateStore(newFieldVal, ptr);
206}
207
208
209Value * KernelBuilder::getParameter(Function * f, std::string paramName) {
210    for (Function::arg_iterator argIter = f->arg_begin(), end = f->arg_end(); argIter != end; argIter++) {
211        Value * arg = &*argIter;
212        if (arg->getName() == paramName) return arg;
213    }
214    throw std::runtime_error("Method does not have parameter: " + paramName);
215}
216
217
Note: See TracBrowser for help on using the repository browser.