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

Last change on this file since 5227 was 5227, checked in by nmedfort, 3 years ago

Rewrite of the CarryManager? to support non-carry-collapsing loops.

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