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

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

First attempt to allow Kernels to wait for consumers to finish processing before performing a realloc.

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, std::move(referenceStreamSet));
26}
27
28ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string && referenceStreamSet) {
29    return ProcessingRate(ProcessingRate::ProcessingRateKind::Max, strmItemsPer, perPrincipalInputItems, std::move(referenceStreamSet));
30}
31
32ProcessingRate RoundUpToMultiple(unsigned itemMultiple, std::string && referenceStreamSet) {
33    return ProcessingRate(ProcessingRate::ProcessingRateKind::RoundUp, itemMultiple, itemMultiple, std::move(referenceStreamSet));
34}
35
36ProcessingRate Add1(std::string && referenceStreamSet) {
37    return ProcessingRate(ProcessingRate::ProcessingRateKind::Add1, 0, 0, std::move(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 (mRatioNumerator == 1) {
47            return principalInputItems;
48        }
49        Type * const T = principalInputItems->getType();
50        Constant * const numerator = ConstantInt::get(T, mRatioNumerator);
51        Constant * const denominator = ConstantInt::get(T, mRatioDenominator);
52        Constant * const denominatorLess1 = ConstantInt::get(T, mRatioDenominator - 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, mRatioDenominator);
59        Constant * const denominatorLess1 = ConstantInt::get(T, mRatioDenominator - 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 * const 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 * const selfType = mKernelStateType->getPointerTo();
80    IntegerType * const sizeTy = iBuilder->getSizeTy();
81    PointerType * const consumerTy = StructType::get(sizeTy, sizeTy->getPointerTo()->getPointerTo(), nullptr)->getPointerTo();
82
83    // Create the initialization function prototype
84    std::vector<Type *> initParameters = {selfType};   
85    for (auto binding : mScalarInputs) {
86        initParameters.push_back(binding.type);
87    }
88    initParameters.insert(initParameters.end(), mStreamSetOutputs.size(), consumerTy);
89
90    FunctionType * initType = FunctionType::get(iBuilder->getVoidTy(), initParameters, false);
91    Function * init = Function::Create(initType, GlobalValue::ExternalLinkage, getName() + INIT_SUFFIX, client);
92    init->setCallingConv(CallingConv::C);
93    init->setDoesNotThrow();
94    auto args = init->arg_begin();
95    args->setName("self");
96    for (auto binding : mScalarInputs) {
97        (++args)->setName(binding.name);
98    }
99    for (auto binding : mStreamSetOutputs) {
100        args->setName(binding.name + "ConsumerLogicalSegments");       
101//        args->addAttr(Attribute::NoCapture);
102//        args->addAttr(Attribute::ReadOnly);
103        ++args;
104    }
105
106    // Create the doSegment function prototype.
107    std::vector<Type *> params = {selfType, iBuilder->getInt1Ty()};
108    params.insert(params.end(), mStreamSetInputs.size(), sizeTy);
109
110    FunctionType * const doSegmentType = FunctionType::get(iBuilder->getVoidTy(), params, false);
111    Function * doSegment = Function::Create(doSegmentType, GlobalValue::ExternalLinkage, getName() + DO_SEGMENT_SUFFIX, client);
112    doSegment->setCallingConv(CallingConv::C);
113    doSegment->setDoesNotThrow();
114    doSegment->setDoesNotCapture(1); // for self parameter only.
115    args = doSegment->arg_begin();
116    args->setName("self");
117    (++args)->setName("doFinal");
118    for (const Binding & input : mStreamSetInputs) {
119        (++args)->setName(input.name + "AvailableItems");
120    }
121
122    /// INVESTIGATE: replace the accumulator methods with a single Exit method that handles any clean up and returns
123    /// a struct containing all scalar outputs?
124
125    // Create the accumulator get function prototypes
126    for (const auto & binding : mScalarOutputs) {
127        FunctionType * accumFnType = FunctionType::get(binding.type, {selfType}, false);
128        Function * accumFn = Function::Create(accumFnType, GlobalValue::ExternalLinkage, getName() + ACCUMULATOR_INFIX + binding.name, client);
129        accumFn->setCallingConv(CallingConv::C);
130        accumFn->setDoesNotThrow();
131        accumFn->setDoesNotCapture(1);
132        auto args = accumFn->arg_begin();
133        args->setName("self");
134    }
135
136    iBuilder->setModule(saveModule);
137    iBuilder->restoreIP(savePoint);
138}
139
140void KernelInterface::setInitialArguments(std::vector<Value *> args) {
141    mInitialArguments = args;
142}
143
144llvm::Function * KernelInterface::getAccumulatorFunction(const std::string & accumName) const {
145    const auto name = getName() + ACCUMULATOR_INFIX + accumName;
146    Function * f = iBuilder->getModule()->getFunction(name);
147    if (LLVM_UNLIKELY(f == nullptr)) {
148        llvm::report_fatal_error("Cannot find " + name);
149    }
150    return f;
151}
152
153Function * KernelInterface::getInitFunction() const {
154    const auto name = getName() + INIT_SUFFIX;
155    Function * f = iBuilder->getModule()->getFunction(name);
156    if (LLVM_UNLIKELY(f == nullptr)) {
157        llvm::report_fatal_error("Cannot find " + name);
158    }
159    return f;
160}
161
162Function * KernelInterface::getDoSegmentFunction() const {
163    const auto name = getName() + DO_SEGMENT_SUFFIX;
164    Function * f = iBuilder->getModule()->getFunction(name);
165    if (LLVM_UNLIKELY(f == nullptr)) {
166        llvm::report_fatal_error("Cannot find " + name);
167    }
168    return f;
169}
Note: See TracBrowser for help on using the repository browser.