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

Last change on this file since 5390 was 5390, checked in by nmedfort, 2 years ago

Minor changes in preparation of incorporating a consumed stream set position into select kernels.

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/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
24ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string referenceStreamSet) {
25    return ProcessingRate(ProcessingRate::ProcessingRateKind::Fixed, strmItemsPer, perPrincipalInputItems, referenceStreamSet);
26}
27
28ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string referenceStreamSet) {
29    return ProcessingRate(ProcessingRate::ProcessingRateKind::Max, strmItemsPer, perPrincipalInputItems, referenceStreamSet);
30}
31
32ProcessingRate RoundUpToMultiple(unsigned itemMultiple, std::string referenceStreamSet) {
33    return ProcessingRate(ProcessingRate::ProcessingRateKind::RoundUp, itemMultiple, itemMultiple, referenceStreamSet);
34}
35
36ProcessingRate Add1(std::string referenceStreamSet) {
37    return ProcessingRate(ProcessingRate::ProcessingRateKind::Add1, 0, 0, referenceStreamSet);
38}
39
40ProcessingRate UnknownRate() {
41    return ProcessingRate(ProcessingRate::ProcessingRateKind::Unknown, 0, 0, "");
42}
43
44Value * ProcessingRate::CreateRatioCalculation(IDISA::IDISA_Builder * b, Value * principalInputItems, Value * doFinal) const {
45    if (mKind == ProcessingRate::ProcessingRateKind::Fixed || mKind == ProcessingRate::ProcessingRateKind::Max) {
46        if (ratio_numerator == 1) {
47            return principalInputItems;
48        }
49        Type * const T = principalInputItems->getType();
50        Constant * const numerator = ConstantInt::get(T, ratio_numerator);
51        Constant * const denominator = ConstantInt::get(T, ratio_denominator);
52        Constant * const denominatorLess1 = ConstantInt::get(T, ratio_denominator - 1);
53        Value * strmItems = b->CreateMul(principalInputItems, numerator);
54        return b->CreateUDiv(b->CreateAdd(denominatorLess1, strmItems), denominator);
55    }
56    if (mKind == ProcessingRate::ProcessingRateKind::RoundUp) {
57        Type * const T = principalInputItems->getType();
58        Constant * const denominator = ConstantInt::get(T, ratio_denominator);
59        Constant * const denominatorLess1 = ConstantInt::get(T, ratio_denominator - 1);
60        return b->CreateMul(b->CreateUDiv(b->CreateAdd(principalInputItems, denominatorLess1), denominator), denominator);
61    }
62    if (mKind == ProcessingRate::ProcessingRateKind::Add1) {
63        if (doFinal) {
64            Type * const T = principalInputItems->getType();
65            principalInputItems = b->CreateAdd(principalInputItems, b->CreateZExt(doFinal, T));
66        }
67        return principalInputItems;
68    }
69    return nullptr;
70}
71
72void KernelInterface::addKernelDeclarations(Module * client) {
73    Module * saveModule = iBuilder->getModule();
74    auto savePoint = iBuilder->saveIP();
75    iBuilder->setModule(client);
76    if (mKernelStateType == nullptr) {
77        throw std::runtime_error("Kernel interface " + getName() + " not yet finalized.");
78    }
79    PointerType * selfType = PointerType::getUnqual(mKernelStateType);
80
81    // Create the initialization function prototype
82    std::vector<Type *> initParameters = {selfType};
83    for (auto binding : mScalarInputs) {
84        initParameters.push_back(binding.type);
85    }
86    FunctionType * initType = FunctionType::get(iBuilder->getVoidTy(), initParameters, false);
87    Function * init = Function::Create(initType, GlobalValue::ExternalLinkage, getName() + INIT_SUFFIX, client);
88    init->setCallingConv(CallingConv::C);
89    init->setDoesNotThrow();
90    auto args = init->arg_begin();
91    args->setName("self");
92    for (auto binding : mScalarInputs) {
93        (++args)->setName(binding.name);
94    }
95
96    // Create the doSegment function prototype.
97    std::vector<Type *> doSegmentParameters = {selfType, iBuilder->getInt1Ty()};
98    for (unsigned i = 0; i < mStreamSetInputs.size(); ++i) {
99        doSegmentParameters.push_back(iBuilder->getSizeTy());
100    }
101    FunctionType * doSegmentType = FunctionType::get(iBuilder->getVoidTy(), doSegmentParameters, false);
102    Function * doSegment = Function::Create(doSegmentType, GlobalValue::ExternalLinkage, getName() + DO_SEGMENT_SUFFIX, client);
103    doSegment->setCallingConv(CallingConv::C);
104    doSegment->setDoesNotThrow();
105    doSegment->setDoesNotCapture(1); // for self parameter only.
106    args = doSegment->arg_begin();
107    args->setName("self");
108    (++args)->setName("doFinal");
109    for (auto ss : mStreamSetInputs) {
110        (++args)->setName(ss.name + "_availableItems");
111    }
112
113    // Create the accumulator get function prototypes
114    for (const auto & binding : mScalarOutputs) {
115        FunctionType * accumFnType = FunctionType::get(binding.type, {selfType}, false);
116        Function * accumFn = Function::Create(accumFnType, GlobalValue::ExternalLinkage, getName() + ACCUMULATOR_INFIX + binding.name, client);
117        accumFn->setCallingConv(CallingConv::C);
118        accumFn->setDoesNotThrow();
119        accumFn->setDoesNotCapture(1);
120        auto args = accumFn->arg_begin();
121        args->setName("self");
122    }
123
124    iBuilder->setModule(saveModule);
125    iBuilder->restoreIP(savePoint);
126}
127
128void KernelInterface::setInitialArguments(std::vector<Value *> args) {
129    mInitialArguments = args;
130}
131
132llvm::Function * KernelInterface::getAccumulatorFunction(const std::string & accumName) const {
133    const auto name = getName() + ACCUMULATOR_INFIX + accumName;
134    Function * f = iBuilder->getModule()->getFunction(name);
135    if (LLVM_UNLIKELY(f == nullptr)) {
136        llvm::report_fatal_error("Cannot find " + name);
137    }
138    return f;
139}
140
141Function * KernelInterface::getInitFunction() const {
142    const auto name = getName() + INIT_SUFFIX;
143    Function * f = iBuilder->getModule()->getFunction(name);
144    if (LLVM_UNLIKELY(f == nullptr)) {
145        llvm::report_fatal_error("Cannot find " + name);
146    }
147    return f;
148}
149
150Function * KernelInterface::getDoSegmentFunction() const {
151    const auto name = getName() + DO_SEGMENT_SUFFIX;
152    Function * f = iBuilder->getModule()->getFunction(name);
153    if (LLVM_UNLIKELY(f == nullptr)) {
154        llvm::report_fatal_error("Cannot find " + name);
155    }
156    return f;
157}
Note: See TracBrowser for help on using the repository browser.