source: icGREP/icgrep-devel/icgrep/kernels/interface.cpp @ 5220

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

Automate instance creation

File size: 6.5 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    auto 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.type, {selfType}, false);
26        std::string fnName = mKernelName + accumulator_infix + binding.name;
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.type);
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.name);
50    }
51
52    // Create the doBlock and finalBlock function prototypes
53   
54    std::vector<Type *> doBlockParameters = {selfType};
55    std::vector<Type *> finalBlockParameters = {selfType, iBuilder->getSizeTy()};
56    FunctionType * doBlockFunctionType = FunctionType::get(iBuilder->getVoidTy(), doBlockParameters, false);
57    std::string doBlockName = mKernelName + doBlock_suffix;
58    Function * doBlockFn = Function::Create(doBlockFunctionType, GlobalValue::ExternalLinkage, doBlockName, client);
59    doBlockFn->setCallingConv(CallingConv::C);
60    doBlockFn->setDoesNotThrow();
61    doBlockFn->setDoesNotCapture(1);
62   
63    FunctionType * finalBlockType = FunctionType::get(iBuilder->getVoidTy(), finalBlockParameters, false);
64    std::string finalBlockName = mKernelName + finalBlock_suffix;
65    Function * finalBlockFn = Function::Create(finalBlockType, GlobalValue::ExternalLinkage, finalBlockName, client);
66    finalBlockFn->setCallingConv(CallingConv::C);
67    finalBlockFn->setDoesNotThrow();
68    finalBlockFn->setDoesNotCapture(1);
69   
70    Function::arg_iterator doBlockArgs = doBlockFn->arg_begin();
71    Function::arg_iterator finalBlockArgs = finalBlockFn->arg_begin();
72    Value * doBlockArg = &*(doBlockArgs++);
73    doBlockArg->setName("self");
74    Value * finalBlockArg = &*(finalBlockArgs++);
75    finalBlockArg->setName("self");
76    finalBlockArg = &*(finalBlockArgs++);
77    finalBlockArg->setName("remainingBytes");
78
79    // Create the doSegment function prototype.
80    std::vector<Type *> doSegmentParameters = {selfType, iBuilder->getSizeTy()};
81    FunctionType * doSegmentFunctionType = FunctionType::get(iBuilder->getVoidTy(), doSegmentParameters, false);
82    std::string doSegmentName = mKernelName + doSegment_suffix;
83    Function * doSegmentFn = Function::Create(doSegmentFunctionType, GlobalValue::ExternalLinkage, doSegmentName, client);
84    doSegmentFn->setCallingConv(CallingConv::C);
85    doSegmentFn->setDoesNotThrow();
86    Function::arg_iterator args = doSegmentFn->arg_begin();
87    Value * arg = &*(args++);
88    arg->setName("self");
89    arg = &*(args++);
90    arg->setName("blockCnt");
91    doSegmentFn->setDoesNotCapture(1); // for self parameter only.
92    iBuilder->setModule(saveModule);
93    iBuilder->restoreIP(savePoint);
94}
95
96
97void KernelInterface::setInitialArguments(std::vector<Value *> args) {
98    mInitialArguments = args;
99}
100
101void KernelInterface::createInstance() {
102    mKernelInstance = iBuilder->CreateCacheAlignedAlloca(mKernelStateType);
103    Module * m = iBuilder->getModule();
104    std::vector<Value *> init_args = {mKernelInstance};
105    for (auto a : mInitialArguments) {
106        init_args.push_back(a);
107    }
108    std::string initFnName = mKernelName + init_suffix;
109    Function * initMethod = m->getFunction(initFnName);
110    if (!initMethod) {
111        throw std::runtime_error("Cannot find " + initFnName);
112    }
113    iBuilder->CreateCall(initMethod, init_args);
114}
115
116Value * KernelInterface::createDoBlockCall(Value * self) {
117    Module * m = iBuilder->getModule();
118    std::string doBlockName = mKernelName + doBlock_suffix;
119    Function * doBlockMethod = m->getFunction(doBlockName);
120    if (!doBlockMethod) {
121        throw std::runtime_error("Cannot find " + doBlockName);
122    }
123    std::vector<Value *> args = {self};
124    return iBuilder->CreateCall(doBlockMethod, args);
125}
126
127Value * KernelInterface::createFinalBlockCall(Value * self, Value * remainingBytes) {
128    Module * m = iBuilder->getModule();
129    std::string finalBlockName = mKernelName + finalBlock_suffix;
130    Function * finalBlockMethod = m->getFunction(finalBlockName);
131    if (!finalBlockMethod) {
132        throw std::runtime_error("Cannot find " + finalBlockName);
133    }
134    std::vector<Value *> args = {self, remainingBytes};
135    return iBuilder->CreateCall(finalBlockMethod, args);
136}
137
138
139Value * KernelInterface::createDoSegmentCall(Value * self, Value * blksToDo) {
140    Module * m = iBuilder->getModule();
141    std::string fnName = mKernelName + doSegment_suffix;
142    Function * method = m->getFunction(fnName);
143    if (!method) {
144        throw std::runtime_error("Cannot find " + fnName);
145    }
146    return iBuilder->CreateCall(method, {self, blksToDo});
147}
148
149Value * KernelInterface::createGetAccumulatorCall(Value * self, std::string accumName) {
150    Module * m = iBuilder->getModule();
151    std::string fnName = mKernelName + accumulator_infix + accumName;
152    Function * accumMethod = m->getFunction(fnName);
153    if (!accumMethod) {
154        throw std::runtime_error("Cannot find " + fnName);
155    }
156    return iBuilder->CreateCall(accumMethod, {self});
157}
158
159
Note: See TracBrowser for help on using the repository browser.