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

Last change on this file since 6034 was 6023, checked in by cameron, 17 months ago

getArgumentList dropped from LLVM 5

File size: 5.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 <kernels/kernel_builder.h>
13
14
15using namespace llvm;
16
17namespace kernel {
18
19const static auto INIT_SUFFIX = "_Init";
20const static auto DO_SEGMENT_SUFFIX = "_DoSegment";
21const static auto TERMINATE_SUFFIX = "_Terminate";
22
23void KernelInterface::addKernelDeclarations(const std::unique_ptr<kernel::KernelBuilder> & idb) {
24
25    if (mKernelStateType == nullptr) {
26        throw std::runtime_error("Kernel interface " + getName() + " not yet finalized.");
27    }
28
29    Module * const module = idb->getModule();
30    PointerType * const selfType = mKernelStateType->getPointerTo();
31    IntegerType * const sizeTy = idb->getSizeTy();
32    PointerType * const consumerTy = StructType::get(idb->getContext(), {sizeTy, sizeTy->getPointerTo()->getPointerTo()})->getPointerTo();
33    Type * const voidTy = idb->getVoidTy();
34
35    // Create the initialization function prototype
36    std::vector<Type *> initParameters = {selfType};
37    for (auto binding : mScalarInputs) {
38        initParameters.push_back(binding.getType());
39    }
40    initParameters.insert(initParameters.end(), mStreamSetOutputs.size(), consumerTy);
41
42    FunctionType * const initType = FunctionType::get(voidTy, initParameters, false);
43    Function * const initFunc = Function::Create(initType, GlobalValue::ExternalLinkage, getName() + INIT_SUFFIX, module);
44    initFunc->setCallingConv(CallingConv::C);
45    initFunc->setDoesNotThrow();
46    auto args = initFunc->arg_begin();
47    args->setName("self");
48    for (const Binding & binding : mScalarInputs) {
49        (++args)->setName(binding.getName());
50    }
51    for (const Binding & binding : mStreamSetOutputs) {
52        (++args)->setName(binding.getName() + "ConsumerLocks");
53    }
54
55    // Create the doSegment function prototype.
56    std::vector<Type *> params = {selfType, idb->getInt1Ty()};
57
58    const auto count = mStreamSetInputs.size();
59    params.insert(params.end(), count, sizeTy);
60
61    FunctionType * const doSegmentType = FunctionType::get(voidTy, params, false);
62    Function * const doSegment = Function::Create(doSegmentType, GlobalValue::ExternalLinkage, getName() + DO_SEGMENT_SUFFIX, module);
63    doSegment->setCallingConv(CallingConv::C);
64    doSegment->setDoesNotThrow();
65    args = doSegment->arg_begin();
66    args->setName("self");
67    (++args)->setName("isFinal");
68    for (const Binding & input : mStreamSetInputs) {
69        (++args)->setName(input.getName() + "AvailableItems");
70    }
71
72    // Create the terminate function prototype
73    Type * resultType = nullptr;
74    if (mScalarOutputs.empty()) {
75        resultType = idb->getVoidTy();
76    } else {
77        const auto n = mScalarOutputs.size();
78        Type * outputType[n];
79        for (unsigned i = 0; i < n; ++i) {
80            outputType[i] = mScalarOutputs[i].getType();
81        }
82        if (n == 1) {
83            resultType = outputType[0];
84        } else {
85            resultType = StructType::get(idb->getContext(), ArrayRef<Type *>(outputType, n));
86        }
87    }
88    FunctionType * const terminateType = FunctionType::get(resultType, {selfType}, false);
89    Function * const terminateFunc = Function::Create(terminateType, GlobalValue::ExternalLinkage, getName() + TERMINATE_SUFFIX, module);
90    terminateFunc->setCallingConv(CallingConv::C);
91    terminateFunc->setDoesNotThrow();
92    args = terminateFunc->arg_begin();
93    args->setName("self");
94
95    linkExternalMethods(idb);
96}
97
98void  KernelInterface::setInstance(Value * const instance) {
99    assert ("kernel instance cannot be null!" && instance);
100    assert ("kernel instance must point to a valid kernel state type!" && (instance->getType()->getPointerElementType() == mKernelStateType));
101    mKernelInstance = instance;
102}
103
104Function * KernelInterface::getInitFunction(Module * const module) const {
105    const auto name = getName() + INIT_SUFFIX;
106    Function * f = module->getFunction(name);
107    if (LLVM_UNLIKELY(f == nullptr)) {
108        llvm::report_fatal_error("Cannot find " + name);
109    }
110    return f;
111}
112
113Function * KernelInterface::getDoSegmentFunction(Module * const module) const {
114    const auto name = getName() + DO_SEGMENT_SUFFIX;
115    Function * f = module->getFunction(name);
116    if (LLVM_UNLIKELY(f == nullptr)) {
117        llvm::report_fatal_error("Cannot find " + name);
118    }
119    return f;
120}
121
122Function * KernelInterface::getTerminateFunction(Module * const module) const {
123    const auto name = getName() + TERMINATE_SUFFIX;
124    Function * f = module->getFunction(name);
125    if (LLVM_UNLIKELY(f == nullptr)) {
126        llvm::report_fatal_error("Cannot find " + name);
127    }
128    return f;
129}
130
131CallInst * KernelInterface::makeDoSegmentCall(kernel::KernelBuilder & idb, const std::vector<llvm::Value *> & args) const {
132    Function * const doSegment = getDoSegmentFunction(idb.getModule());
133    assert (doSegment->arg_size() <= args.size());
134    return idb.CreateCall(doSegment, args);
135}
136
137}
Note: See TracBrowser for help on using the repository browser.