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

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

Linear buffers; stdout_kernel; cache-aligned alloca, restructured u8u16, clean-ups

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    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    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
97Value * KernelInterface::createInstance(std::vector<Value *> args) {
98    Value * kernelInstance = iBuilder->CreateCacheAlignedAlloca(mKernelStateType);
99    Module * m = iBuilder->getModule();
100    std::vector<Value *> init_args = {kernelInstance};
101    for (auto a : args) {
102        init_args.push_back(a);
103    }
104    std::string initFnName = mKernelName + init_suffix;
105    Function * initMethod = m->getFunction(initFnName);
106    if (!initMethod) {
107        throw std::runtime_error("Cannot find " + initFnName);
108    }
109    iBuilder->CreateCall(initMethod, init_args);
110    return kernelInstance;
111}
112
113Value * KernelInterface::createDoBlockCall(Value * self) {
114    Module * m = iBuilder->getModule();
115    std::string doBlockName = mKernelName + doBlock_suffix;
116    Function * doBlockMethod = m->getFunction(doBlockName);
117    if (!doBlockMethod) {
118        throw std::runtime_error("Cannot find " + doBlockName);
119    }
120    std::vector<Value *> args = {self};
121    return iBuilder->CreateCall(doBlockMethod, args);
122}
123
124Value * KernelInterface::createFinalBlockCall(Value * self, Value * remainingBytes) {
125    Module * m = iBuilder->getModule();
126    std::string finalBlockName = mKernelName + finalBlock_suffix;
127    Function * finalBlockMethod = m->getFunction(finalBlockName);
128    if (!finalBlockMethod) {
129        throw std::runtime_error("Cannot find " + finalBlockName);
130    }
131    std::vector<Value *> args = {self, remainingBytes};
132    return iBuilder->CreateCall(finalBlockMethod, args);
133}
134
135
136Value * KernelInterface::createDoSegmentCall(Value * self, Value * blksToDo) {
137    Module * m = iBuilder->getModule();
138    std::string fnName = mKernelName + doSegment_suffix;
139    Function * method = m->getFunction(fnName);
140    if (!method) {
141        throw std::runtime_error("Cannot find " + fnName);
142    }
143    std::vector<Value *> args = {self, blksToDo};
144    return iBuilder->CreateCall(method, args);
145}
146
147Value * KernelInterface::createGetAccumulatorCall(Value * self, std::string accumName) {
148    Module * m = iBuilder->getModule();
149    std::string fnName = mKernelName + accumulator_infix + accumName;
150    Function * accumMethod = m->getFunction(fnName);
151    if (!accumMethod) {
152        throw std::runtime_error("Cannot find " + fnName);
153    }
154    return iBuilder->CreateCall(accumMethod, {self});
155}
156
157
Note: See TracBrowser for help on using the repository browser.