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

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

partial refactoring check in with change for Linda.

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>
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
22ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string && referenceStreamSet) {
23    return ProcessingRate(ProcessingRate::ProcessingRateKind::Fixed, strmItemsPer, perPrincipalInputItems, std::move(referenceStreamSet));
24}
25
26ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string && referenceStreamSet) {
27    return ProcessingRate(ProcessingRate::ProcessingRateKind::Max, strmItemsPer, perPrincipalInputItems, std::move(referenceStreamSet));
28}
29
30ProcessingRate RoundUpToMultiple(unsigned itemMultiple, std::string && referenceStreamSet) {
31    return ProcessingRate(ProcessingRate::ProcessingRateKind::RoundUp, itemMultiple, itemMultiple, std::move(referenceStreamSet));
32}
33
34ProcessingRate Add1(std::string && referenceStreamSet) {
35    return ProcessingRate(ProcessingRate::ProcessingRateKind::Add1, 0, 0, std::move(referenceStreamSet));
36}
37
38ProcessingRate UnknownRate() {
39    return ProcessingRate(ProcessingRate::ProcessingRateKind::Unknown, 0, 0, "");
40}
41
42Value * ProcessingRate::CreateRatioCalculation(IDISA::IDISA_Builder * b, Value * principalInputItems, Value * doFinal) const {
43    if (mKind == ProcessingRate::ProcessingRateKind::Fixed || mKind == ProcessingRate::ProcessingRateKind::Max) {
44        if (mRatioNumerator == 1) {
45            return principalInputItems;
46        }
47        Type * const T = principalInputItems->getType();
48        Constant * const numerator = ConstantInt::get(T, mRatioNumerator);
49        Constant * const denominator = ConstantInt::get(T, mRatioDenominator);
50        Constant * const denominatorLess1 = ConstantInt::get(T, mRatioDenominator - 1);
51        Value * strmItems = b->CreateMul(principalInputItems, numerator);
52        return b->CreateUDiv(b->CreateAdd(denominatorLess1, strmItems), denominator);
53    }
54    if (mKind == ProcessingRate::ProcessingRateKind::RoundUp) {
55        Type * const T = principalInputItems->getType();
56        Constant * const denominator = ConstantInt::get(T, mRatioDenominator);
57        Constant * const denominatorLess1 = ConstantInt::get(T, mRatioDenominator - 1);
58        return b->CreateMul(b->CreateUDiv(b->CreateAdd(principalInputItems, denominatorLess1), denominator), denominator);
59    }
60    if (mKind == ProcessingRate::ProcessingRateKind::Add1) {
61        if (doFinal) {
62            Type * const T = principalInputItems->getType();
63            principalInputItems = b->CreateAdd(principalInputItems, b->CreateZExt(doFinal, T));
64        }
65        return principalInputItems;
66    }
67    return nullptr;
68}
69
70void KernelInterface::addKernelDeclarations() {
71
72    if (mKernelStateType == nullptr) {
73        throw std::runtime_error("Kernel interface " + getName() + " not yet finalized.");
74    }
75
76    Module * const module = iBuilder->getModule();
77    PointerType * const selfType = mKernelStateType->getPointerTo();
78    IntegerType * const sizeTy = iBuilder->getSizeTy();
79    PointerType * const consumerTy = StructType::get(sizeTy, sizeTy->getPointerTo()->getPointerTo(), nullptr)->getPointerTo();
80    Type * const voidTy = iBuilder->getVoidTy();
81
82    // Create the initialization function prototype
83    std::vector<Type *> initParameters = {selfType};
84    for (auto binding : mScalarInputs) {
85        initParameters.push_back(binding.type);
86    }
87    initParameters.insert(initParameters.end(), mStreamSetOutputs.size(), consumerTy);
88
89    FunctionType * const initType = FunctionType::get(voidTy, initParameters, false);
90    Function * const initFunc = Function::Create(initType, GlobalValue::ExternalLinkage, getName() + INIT_SUFFIX, module);
91    initFunc->setCallingConv(CallingConv::C);
92    initFunc->setDoesNotThrow();
93    auto args = initFunc->arg_begin();
94    args->setName("self");
95    for (auto binding : mScalarInputs) {
96        (++args)->setName(binding.name);
97    }
98    for (auto binding : mStreamSetOutputs) {
99        (args++)->setName(binding.name + "ConsumerLocks");
100    }
101
102    // Create the doSegment function prototype.
103    std::vector<Type *> params = {selfType, iBuilder->getInt1Ty()};
104    params.insert(params.end(), mStreamSetInputs.size(), sizeTy);
105
106    FunctionType * const doSegmentType = FunctionType::get(voidTy, params, false);
107    Function * const doSegment = Function::Create(doSegmentType, GlobalValue::ExternalLinkage, getName() + DO_SEGMENT_SUFFIX, module);
108    doSegment->setCallingConv(CallingConv::C);
109    doSegment->setDoesNotThrow();
110    doSegment->setDoesNotCapture(1); // for self parameter only.
111    args = doSegment->arg_begin();
112    args->setName("self");
113    (++args)->setName("doFinal");
114    for (const Binding & input : mStreamSetInputs) {
115        (++args)->setName(input.name + "AvailableItems");
116    }
117
118    // Create the terminate function prototype
119    Type * resultType = nullptr;
120    if (mScalarOutputs.empty()) {
121        resultType = iBuilder->getVoidTy();
122    } else {
123        const auto n = mScalarOutputs.size();
124        Type * outputType[n];
125        for (unsigned i = 0; i < n; ++i) {
126            outputType[i] = mScalarOutputs[i].type;
127        }
128        if (n == 1) {
129            resultType = outputType[0];
130        } else {
131            resultType = StructType::get(iBuilder->getContext(), ArrayRef<Type *>(outputType, n));
132        }
133    }
134    FunctionType * const terminateType = FunctionType::get(resultType, {selfType}, false);
135    Function * const terminateFunc = Function::Create(terminateType, GlobalValue::ExternalLinkage, getName() + TERMINATE_SUFFIX, module);
136    terminateFunc->setCallingConv(CallingConv::C);
137    terminateFunc->setDoesNotThrow();
138    terminateFunc->setDoesNotCapture(1);
139    args = terminateFunc->arg_begin();
140    args->setName("self");
141
142    linkExternalMethods();
143}
144
145void KernelInterface::setInitialArguments(std::vector<Value *> args) {
146    mInitialArguments = args;
147}
148
149Function * KernelInterface::getInitFunction(Module * const module) const {
150    const auto name = getName() + INIT_SUFFIX;
151    Function * f = module->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(llvm::Module * const module) const {
159    const auto name = getName() + DO_SEGMENT_SUFFIX;
160    Function * f = module->getFunction(name);
161    if (LLVM_UNLIKELY(f == nullptr)) {
162        llvm::report_fatal_error("Cannot find " + name);
163    }
164    return f;
165}
166
167Function * KernelInterface::getTerminateFunction(Module * const module) const {
168    const auto name = getName() + TERMINATE_SUFFIX;
169    Function * f = module->getFunction(name);
170    if (LLVM_UNLIKELY(f == nullptr)) {
171        llvm::report_fatal_error("Cannot find " + name);
172    }
173    return f;
174}
Note: See TracBrowser for help on using the repository browser.