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

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

Kernel/pipeline progress: sychronize with logicalSegmentNo

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