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

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

Kernel infrastructure: move common logic into KernelBuilder? base class; demo linking in wc

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
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   
24    for (auto binding : scalar_parameters) {
25        addScalar(binding.scalarType, binding.scalarName);
26    }
27    for (auto binding : scalar_outputs) {
28        addScalar(binding.scalarType, binding.scalarName);
29    }
30    for (auto binding : internal_scalars) {
31        addScalar(binding.scalarType, binding.scalarName);
32    }
33}
34
35void KernelBuilder::addScalar(Type * t, std::string scalarName) {
36    if (LLVM_UNLIKELY(mKernelStateType != nullptr)) {
37        throw std::runtime_error("Illegal addition of kernel field after kernel state finalized: " + scalarName);
38    }
39    unsigned index = mKernelFields.size();
40    mKernelFields.push_back(t);
41    mInternalStateNameMap.emplace(scalarName, iBuilder->getInt32(index));
42}
43
44void KernelBuilder::prepareKernelStateType() {
45    mKernelStateType = StructType::create(getGlobalContext(), mKernelFields, mKernelName);
46}
47
48std::unique_ptr<Module> KernelBuilder::createKernelModule() {
49    Module * saveModule = iBuilder->getModule();
50    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
51    std::unique_ptr<Module> theModule = make_unique<Module>(mKernelName + "_" + iBuilder->getBitBlockTypeName(), getGlobalContext());
52    Module * m = theModule.get();
53    iBuilder->setModule(m);
54    generateKernel();
55    iBuilder->setModule(saveModule);
56    iBuilder->restoreIP(savePoint);
57    return theModule;
58}
59
60void KernelBuilder::generateKernel() {
61    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
62    Module * m = iBuilder->getModule();
63
64    prepareKernelStateType();  // possibly overriden by the KernelBuilder subtype
65    KernelInterface::addKernelDeclarations(m);
66    generateDoBlockMethod();     // must be implemented by the KernelBuilder subtype
67    generateFinalBlockMethod();  // possibly overriden by the KernelBuilder subtype
68
69    // Implement the accumulator get functions
70    for (auto binding : mScalarOutputs) {
71        auto fnName = mKernelName + accumulator_infix + binding.scalarName;
72        Function * accumFn = m->getFunction(fnName);
73        iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "get_" + binding.scalarName, accumFn, 0));
74        Value * self = &*(accumFn->arg_begin());
75        Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), getScalarIndex(binding.scalarName)});
76        Value * retVal = iBuilder->CreateLoad(ptr);
77        iBuilder->CreateRet(retVal);
78    }
79    // Implement the initializer function
80    Function * initFunction = m->getFunction(mKernelName + init_suffix);
81    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "Init_entry", initFunction, 0));
82   
83    Function::arg_iterator args = initFunction->arg_begin();
84    Value * self = &*(args++);
85    iBuilder->CreateStore(Constant::getNullValue(mKernelStateType), self);
86    for (auto binding : mScalarInputs) {
87        Value * parm = &*(args++);
88        Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), getScalarIndex(binding.scalarName)});
89        iBuilder->CreateStore(parm, ptr);
90    }
91    iBuilder->CreateRetVoid();
92    iBuilder->restoreIP(savePoint);
93}
94
95//  The default finalBlock method simply dispatches to the doBlock routine.
96void KernelBuilder::generateFinalBlockMethod() {
97    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
98    Module * m = iBuilder->getModule();
99    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
100    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
101    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_entry", finalBlockFunction, 0));
102    // Final Block arguments: self, remaining, then the standard DoBlock args.
103    Function::arg_iterator args = finalBlockFunction->arg_begin();
104    Value * self = &*(args++);
105    /* Skip "remaining" arg */ args++;
106    std::vector<Value *> doBlockArgs = {self};
107    while (args != finalBlockFunction->arg_end()){
108        doBlockArgs.push_back(&*args++);
109    }
110    iBuilder->CreateCall(doBlockFunction, doBlockArgs);
111    iBuilder->CreateRetVoid();
112    iBuilder->restoreIP(savePoint);
113}
114
115Value * KernelBuilder::getScalarIndex(std::string fieldName) {
116    const auto f = mInternalStateNameMap.find(fieldName);
117    if (LLVM_UNLIKELY(f == mInternalStateNameMap.end())) {
118        throw std::runtime_error("Kernel does not contain internal state: " + fieldName);
119    }
120    return f->second;
121}
122
123Value * KernelBuilder::getScalarField(Value * self, std::string fieldName) {
124    Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), getScalarIndex(fieldName)});
125    return iBuilder->CreateLoad(ptr);
126}
127
128void KernelBuilder::setScalarField(Value * self, std::string fieldName, Value * newFieldVal) {
129    Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), getScalarIndex(fieldName)});
130    iBuilder->CreateStore(newFieldVal, ptr);
131}
132
133
134Value * KernelBuilder::getParameter(Function * f, std::string paramName) {
135    for (Function::arg_iterator argIter = f->arg_begin(), end = f->arg_end(); argIter != end; argIter++) {
136        Value * arg = &*argIter;
137        if (arg->getName() == paramName) return arg;
138    }
139    throw std::runtime_error("Method does not have parameter: " + paramName);
140}
141
142
143
Note: See TracBrowser for help on using the repository browser.