source: icGREP/icgrep-devel/icgrep/kernels/interface.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: 8.8 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 "interface.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 parabix;
14
15void KernelInterface::addKernelDeclarations(Module * client) {
16    Module * saveModule = iBuilder->getModule();
17    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
18    iBuilder->setModule(client);
19    if (mKernelStateType == nullptr) {
20        throw std::runtime_error("Kernel interface " + mKernelName + " not yet finalized.");
21    }
22    Type * selfType = PointerType::getUnqual(mKernelStateType);
23    // Create the accumulator get function prototypes
24    for (auto binding : mScalarOutputs) {
25        FunctionType * accumFnType = FunctionType::get(binding.scalarType, {selfType}, false);
26        std::string fnName = mKernelName + accumulator_infix + binding.scalarName;
27        Function * accumFn = Function::Create(accumFnType, GlobalValue::ExternalLinkage, fnName, client);
28        accumFn->setCallingConv(CallingConv::C);
29        accumFn->setDoesNotThrow();
30        Value * self = &*(accumFn->arg_begin());
31        self->setName("self");       
32    }
33    // Create the initialization function prototype
34
35    std::vector<Type *> initParameters = {selfType};
36    for (auto binding : mScalarInputs) {
37        initParameters.push_back(binding.scalarType);
38    }
39    FunctionType * initFunctionType = FunctionType::get(iBuilder->getVoidTy(), initParameters, false);
40    std::string initFnName = mKernelName + init_suffix;
41    Function * initFn = Function::Create(initFunctionType, GlobalValue::ExternalLinkage, initFnName, client);
42    initFn->setCallingConv(CallingConv::C);
43    initFn->setDoesNotThrow();
44    Function::arg_iterator initArgs = initFn->arg_begin();
45    Value * initArg = &*(initArgs++);
46    initArg->setName("self");
47    for (auto binding : mScalarInputs) {
48        initArg = &*(initArgs++);
49        initArg->setName(binding.scalarName);
50    }
51
52    // Create the doBlock and finalBlock function prototypes
53   
54    std::vector<Type *> doBlockParameters = {selfType};
55    std::vector<Type *> finalBlockParameters = {selfType, iBuilder->getInt64Ty()};
56    /*
57    for (auto inputSet : mStreamSetInputs) {
58        Type * inputSetParmType = PointerType::getUnqual(inputSet.ssType.getStreamSetBlockType(iBuilder));
59        doBlockParameters.push_back(inputSetParmType);
60        finalBlockParameters.push_back(inputSetParmType);
61    }
62    for (auto outputSet : mStreamSetOutputs) {
63        Type * outputSetParmType = PointerType::getUnqual(outputSet.ssType.getStreamSetBlockType(iBuilder));
64        doBlockParameters.push_back(outputSetParmType);
65        finalBlockParameters.push_back(outputSetParmType);
66    }
67     */
68    FunctionType * doBlockFunctionType = FunctionType::get(mDoBlockReturnType, doBlockParameters, false);
69    std::string doBlockName = mKernelName + doBlock_suffix;
70    Function * doBlockFn = Function::Create(doBlockFunctionType, GlobalValue::ExternalLinkage, doBlockName, client);
71    doBlockFn->setCallingConv(CallingConv::C);
72    doBlockFn->setDoesNotThrow();
73    for (int i = 1; i <= doBlockParameters.size(); i++) {
74        doBlockFn->setDoesNotCapture(i);
75    }
76   
77    FunctionType * finalBlockType = FunctionType::get(mDoBlockReturnType, finalBlockParameters, false);
78    std::string finalBlockName = mKernelName + finalBlock_suffix;
79    Function * finalBlockFn = Function::Create(finalBlockType, GlobalValue::ExternalLinkage, finalBlockName, client);
80    finalBlockFn->setCallingConv(CallingConv::C);
81    finalBlockFn->setDoesNotThrow();
82    finalBlockFn->setDoesNotCapture(1);
83    // Parameter #2 is not a pointer; nocapture is irrelevant
84    for (int i = 3; i <= finalBlockParameters.size(); i++) {
85        finalBlockFn->setDoesNotCapture(i);
86    }
87   
88    Function::arg_iterator doBlockArgs = doBlockFn->arg_begin();
89    Function::arg_iterator finalBlockArgs = finalBlockFn->arg_begin();
90    Value * doBlockArg = &*(doBlockArgs++);
91    doBlockArg->setName("self");
92    Value * finalBlockArg = &*(finalBlockArgs++);
93    finalBlockArg->setName("self");
94    finalBlockArg = &*(finalBlockArgs++);
95    finalBlockArg->setName("remainingBytes");
96
97/*    for (auto inputSet : mStreamSetInputs) {
98        doBlockArg = &*(doBlockArgs++);
99        finalBlockArg = &*(finalBlockArgs++);
100        doBlockArg->setName(inputSet.ssName);
101        finalBlockArg->setName(inputSet.ssName);
102    }
103    for (auto outputSet : mStreamSetOutputs) {
104        doBlockArg = &*(doBlockArgs++);
105        finalBlockArg = &*(finalBlockArgs++);
106        doBlockArg->setName(outputSet.ssName);
107        finalBlockArg->setName(outputSet.ssName);
108    }
109    */
110    // Create the doSegment function prototype.
111    std::vector<Type *> doSegmentParameters = {selfType, iBuilder->getInt64Ty()};
112    FunctionType * doSegmentFunctionType = FunctionType::get(mDoBlockReturnType, doSegmentParameters, false);
113    std::string doSegmentName = mKernelName + doSegment_suffix;
114    Function * doSegmentFn = Function::Create(doSegmentFunctionType, GlobalValue::ExternalLinkage, doSegmentName, client);
115    doSegmentFn->setCallingConv(CallingConv::C);
116    doSegmentFn->setDoesNotThrow();
117    Function::arg_iterator args = doSegmentFn->arg_begin();
118    Value * arg = &*(args++);
119    arg->setName("self");
120    arg = &*(args++);
121    arg->setName("blockCnt");
122    doSegmentFn->setDoesNotCapture(1); // for self parameter only.
123    iBuilder->setModule(saveModule);
124    iBuilder->restoreIP(savePoint);
125}
126
127
128Value * KernelInterface::createInstance(std::vector<Value *> args) {
129    Value * kernelInstance = iBuilder->CreateAlloca(mKernelStateType);
130    Module * m = iBuilder->getModule();
131    std::vector<Value *> init_args = {kernelInstance};
132    for (auto a : args) {
133        init_args.push_back(a);
134    }
135    std::string initFnName = mKernelName + init_suffix;
136    Function * initMethod = m->getFunction(initFnName);
137    if (!initMethod) {
138        throw std::runtime_error("Cannot find " + initFnName);
139        //Or just zero-initialize???
140        //iBuilder->CreateStore(Constant::getNullValue(mKernelStateType), kernelInstance);
141        //return kernelInstance;
142    }
143    iBuilder->CreateCall(initMethod, init_args);
144    return kernelInstance;
145}
146
147Value * KernelInterface::createInstance(std::vector<Value *> args, 
148                                        std::vector<StreamSetBuffer *> inputBuffers,
149                                        std::vector<StreamSetBuffer *> outputBuffers) {
150    Value * kernelInstance = iBuilder->CreateAlloca(mKernelStateType);
151    Module * m = iBuilder->getModule();
152    std::vector<Value *> init_args = {kernelInstance};
153    for (auto a : args) {
154        init_args.push_back(a);
155    }
156    for (auto b : inputBuffers) { 
157        init_args.push_back(b->getStreamSetBasePtr());
158    }
159    for (auto b : outputBuffers) { 
160        init_args.push_back(b->getStreamSetBasePtr());
161    }
162    std::string initFnName = mKernelName + init_suffix;
163    Function * initMethod = m->getFunction(initFnName);
164    if (!initMethod) {
165        throw std::runtime_error("Cannot find " + initFnName);
166    }
167    iBuilder->CreateCall(initMethod, init_args);
168    return kernelInstance;
169}
170
171Value * KernelInterface::createDoBlockCall(Value * self) {
172    Module * m = iBuilder->getModule();
173    std::string doBlockName = mKernelName + doBlock_suffix;
174    Function * doBlockMethod = m->getFunction(doBlockName);
175    if (!doBlockMethod) {
176        throw std::runtime_error("Cannot find " + doBlockName);
177    }
178    std::vector<Value *> args = {self};
179    return iBuilder->CreateCall(doBlockMethod, args);
180}
181
182Value * KernelInterface::createFinalBlockCall(Value * self, Value * remainingBytes) {
183    Module * m = iBuilder->getModule();
184    std::string finalBlockName = mKernelName + finalBlock_suffix;
185    Function * finalBlockMethod = m->getFunction(finalBlockName);
186    if (!finalBlockMethod) {
187        throw std::runtime_error("Cannot find " + finalBlockName);
188    }
189    std::vector<Value *> args = {self, remainingBytes};
190    return iBuilder->CreateCall(finalBlockMethod, args);
191}
192
193
194Value * KernelInterface::createDoSegmentCall(Value * self, Value * blksToDo) {
195    Module * m = iBuilder->getModule();
196    std::string fnName = mKernelName + doSegment_suffix;
197    Function * method = m->getFunction(fnName);
198    if (!method) {
199        throw std::runtime_error("Cannot find " + fnName);
200    }
201    std::vector<Value *> args = {self, blksToDo};
202    return iBuilder->CreateCall(method, args);
203}
204
205Value * KernelInterface::createGetAccumulatorCall(Value * self, std::string accumName) {
206    Module * m = iBuilder->getModule();
207    std::string fnName = mKernelName + accumulator_infix + accumName;
208    Function * accumMethod = m->getFunction(fnName);
209    if (!accumMethod) {
210        throw std::runtime_error("Cannot find " + fnName);
211    }
212    return iBuilder->CreateCall(accumMethod, {self});
213}
214
215
Note: See TracBrowser for help on using the repository browser.