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

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

Removed 'function' and 'self' parameters from generateXXXMethod() functions.

File size: 4.2 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/Value.h>         // for Value
8#include <llvm/IR/CallingConv.h>   // for ::C
9#include <llvm/IR/DerivedTypes.h>  // for FunctionType (ptr only), PointerType
10#include <llvm/IR/Function.h>      // for Function, Function::arg_iterator
11#include <llvm/IR/Module.h>
12#include <IR_Gen/idisa_builder.h>
13namespace llvm { class Module; }
14namespace llvm { class Type; }
15
16static const auto INIT_SUFFIX = "_Init";
17
18static const auto DO_SEGMENT_SUFFIX = "_DoSegment";
19
20static const auto ACCUMULATOR_INFIX = "_get_";
21
22using namespace llvm;
23
24void KernelInterface::addKernelDeclarations(Module * client) {
25    Module * saveModule = iBuilder->getModule();
26    auto savePoint = iBuilder->saveIP();
27    iBuilder->setModule(client);
28    if (mKernelStateType == nullptr) {
29        throw std::runtime_error("Kernel interface " + mKernelName + " not yet finalized.");
30    }
31    PointerType * selfType = PointerType::getUnqual(mKernelStateType);
32
33    // Create the initialization function prototype
34    std::vector<Type *> initParameters = {selfType};
35    for (auto binding : mScalarInputs) {
36        initParameters.push_back(binding.type);
37    }
38    FunctionType * initType = FunctionType::get(iBuilder->getVoidTy(), initParameters, false);
39    Function * init = Function::Create(initType, GlobalValue::ExternalLinkage, mKernelName + INIT_SUFFIX, client);
40    init->setCallingConv(CallingConv::C);
41    init->setDoesNotThrow();
42    auto args = init->arg_begin();
43    args++->setName("self");
44    for (auto binding : mScalarInputs) {
45        args++->setName(binding.name);
46    }
47    assert (args == init->arg_end());
48
49    // Create the doSegment function prototype.
50    std::vector<Type *> doSegmentParameters = {selfType, iBuilder->getInt1Ty()};
51    for (unsigned i = 0; i < mStreamSetInputs.size(); ++i) {
52        doSegmentParameters.push_back(iBuilder->getSizeTy());
53    }
54    FunctionType * doSegmentType = FunctionType::get(iBuilder->getVoidTy(), doSegmentParameters, false);
55    Function * doSegment = Function::Create(doSegmentType, GlobalValue::ExternalLinkage, mKernelName + DO_SEGMENT_SUFFIX, client);
56    doSegment->setCallingConv(CallingConv::C);
57    doSegment->setDoesNotThrow();
58    doSegment->setDoesNotCapture(1); // for self parameter only.
59    args = doSegment->arg_begin();
60    args++->setName("self");
61    args++->setName("doFinal");
62    for (auto ss : mStreamSetInputs) {
63        args++->setName(ss.name + "_availableItems");
64    }
65    assert (args == doSegment->arg_end());
66
67    // Add any additional kernel declarations
68    addAdditionalKernelDeclarations(client, selfType);
69
70    // Create the accumulator get function prototypes
71    for (const auto & binding : mScalarOutputs) {
72        FunctionType * accumFnType = FunctionType::get(binding.type, {selfType}, false);
73        Function * accumFn = Function::Create(accumFnType, GlobalValue::ExternalLinkage, mKernelName + ACCUMULATOR_INFIX + binding.name, client);
74        accumFn->setCallingConv(CallingConv::C);
75        accumFn->setDoesNotThrow();
76        auto self = accumFn->arg_begin();
77        self->setName("self");
78    }
79
80    iBuilder->setModule(saveModule);
81    iBuilder->restoreIP(savePoint);
82}
83
84void KernelInterface::setInitialArguments(std::vector<Value *> args) {
85    mInitialArguments = args;
86}
87
88llvm::Function * KernelInterface::getAccumulatorFunction(const std::string & accumName) const {
89    const auto name = mKernelName + ACCUMULATOR_INFIX + accumName;
90    Function * f = iBuilder->getModule()->getFunction(name);
91    if (LLVM_UNLIKELY(f == nullptr)) {
92        llvm::report_fatal_error("Cannot find " + name);
93    }
94    return f;
95}
96
97Function * KernelInterface::getInitFunction() const {
98    const auto name = mKernelName + INIT_SUFFIX;
99    Function * f = iBuilder->getModule()->getFunction(name);
100    if (LLVM_UNLIKELY(f == nullptr)) {
101        llvm::report_fatal_error("Cannot find " + name);
102    }
103    return f;
104}
105
106Function * KernelInterface::getDoSegmentFunction() const {
107    const auto name = mKernelName + DO_SEGMENT_SUFFIX;
108    Function * f = iBuilder->getModule()->getFunction(name);
109    if (LLVM_UNLIKELY(f == nullptr)) {
110        llvm::report_fatal_error("Cannot find " + name);
111    }
112    return f;
113}
Note: See TracBrowser for help on using the repository browser.