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

Last change on this file since 5375 was 5375, checked in by cameron, 2 years ago

Processing rates can be defined with respect to named stream sets.

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