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

Last change on this file since 5822 was 5755, checked in by nmedfort, 17 months ago

Bug fixes and simplified MultiBlockKernel? logic

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