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

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

Changes towards separate compilation

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(Module * const client) {
71    Module * const saveModule = iBuilder->getModule();
72    iBuilder->setModule(client);
73    if (mKernelStateType == nullptr) {
74        throw std::runtime_error("Kernel interface " + getName() + " not yet finalized.");
75    }
76    PointerType * const selfType = mKernelStateType->getPointerTo();
77    IntegerType * const sizeTy = iBuilder->getSizeTy();
78    PointerType * const consumerTy = StructType::get(sizeTy, sizeTy->getPointerTo()->getPointerTo(), nullptr)->getPointerTo();
79    Type * const voidTy = iBuilder->getVoidTy();
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    initParameters.insert(initParameters.end(), mStreamSetOutputs.size(), consumerTy);
87
88    FunctionType * const initType = FunctionType::get(voidTy, initParameters, false);
89    Function * const initFunc = Function::Create(initType, GlobalValue::ExternalLinkage, getName() + INIT_SUFFIX, client);
90    initFunc->setCallingConv(CallingConv::C);
91    initFunc->setDoesNotThrow();
92    auto args = initFunc->arg_begin();
93    args->setName("self");
94    for (auto binding : mScalarInputs) {
95        (++args)->setName(binding.name);
96    }
97    for (auto binding : mStreamSetOutputs) {
98        (args++)->setName(binding.name + "ConsumerLocks");
99    }
100
101    // Create the doSegment function prototype.
102    std::vector<Type *> params = {selfType, iBuilder->getInt1Ty()};
103    params.insert(params.end(), mStreamSetInputs.size(), sizeTy);
104
105    FunctionType * const doSegmentType = FunctionType::get(voidTy, params, false);
106    Function * const doSegment = Function::Create(doSegmentType, GlobalValue::ExternalLinkage, getName() + DO_SEGMENT_SUFFIX, client);
107    doSegment->setCallingConv(CallingConv::C);
108    doSegment->setDoesNotThrow();
109    doSegment->setDoesNotCapture(1); // for self parameter only.   
110    args = doSegment->arg_begin();
111    args->setName("self");
112    (++args)->setName("doFinal");
113    for (const Binding & input : mStreamSetInputs) {
114        (++args)->setName(input.name + "AvailableItems");
115    }
116
117    // Create the terminate function prototype
118    Type * resultType = nullptr;
119    if (mScalarOutputs.empty()) {
120        resultType = iBuilder->getVoidTy();
121    } else {
122        const auto n = mScalarOutputs.size();
123        Type * outputType[n];
124        for (unsigned i = 0; i < n; ++i) {
125            outputType[i] = mScalarOutputs[i].type;
126        }
127        if (n == 1) {
128            resultType = outputType[0];
129        } else {
130            resultType = StructType::get(iBuilder->getContext(), ArrayRef<Type *>(outputType, n));
131        }
132    }
133    FunctionType * const terminateType = FunctionType::get(resultType, {selfType}, false);
134    Function * const terminateFunc = Function::Create(terminateType, GlobalValue::ExternalLinkage, getName() + TERMINATE_SUFFIX, client);
135    terminateFunc->setCallingConv(CallingConv::C);
136    terminateFunc->setDoesNotThrow();
137    terminateFunc->setDoesNotCapture(1);
138    args = terminateFunc->arg_begin();
139    args->setName("self");
140
141    linkExternalMethods();
142
143    iBuilder->setModule(saveModule);
144}
145
146void KernelInterface::setInitialArguments(std::vector<Value *> args) {
147    mInitialArguments = args;
148}
149
150Function * KernelInterface::getInitFunction() const {
151    const auto name = getName() + INIT_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}
158
159Function * KernelInterface::getDoSegmentFunction() const {
160    const auto name = getName() + DO_SEGMENT_SUFFIX;
161    Function * f = iBuilder->getModule()->getFunction(name);
162    if (LLVM_UNLIKELY(f == nullptr)) {
163        llvm::report_fatal_error("Cannot find " + name);
164    }
165    return f;
166}
167
168Function * KernelInterface::getTerminateFunction() const {
169    const auto name = getName() + TERMINATE_SUFFIX;
170    Function * f = iBuilder->getModule()->getFunction(name);
171    if (LLVM_UNLIKELY(f == nullptr)) {
172        llvm::report_fatal_error("Cannot find " + name);
173    }
174    return f;
175}
Note: See TracBrowser for help on using the repository browser.