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

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

Continued work on processing stdin input. Partial integration of ParabixDriver? methods into icgrep and editd. Object cache does not currently work for recursive REs.

File size: 6.7 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 (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 * 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 *> params = {selfType, iBuilder->getInt1Ty()};
98    // const auto count = mStreamSetInputs.size() + mStreamSetOutputs.size();
99    for (unsigned i = 0; i < mStreamSetInputs.size(); ++i) {
100        params.push_back(iBuilder->getSizeTy());
101    }
102
103    FunctionType * doSegmentType = FunctionType::get(iBuilder->getVoidTy(), params, false);
104    Function * doSegment = Function::Create(doSegmentType, GlobalValue::ExternalLinkage, getName() + DO_SEGMENT_SUFFIX, client);
105    doSegment->setCallingConv(CallingConv::C);
106    doSegment->setDoesNotThrow();
107    doSegment->setDoesNotCapture(1); // for self parameter only.
108    args = doSegment->arg_begin();
109    args->setName("self");
110    (++args)->setName("doFinal");
111    for (const Binding & input : mStreamSetInputs) {
112        (++args)->setName(input.name + "_availableItems");
113    }
114//    for (const Binding & output : mStreamSetOutputs) {
115//        (++args)->setName(output.name + "_consumedItems");
116//    }
117
118    // Create the accumulator get function prototypes
119    for (const auto & binding : mScalarOutputs) {
120        FunctionType * accumFnType = FunctionType::get(binding.type, {selfType}, false);
121        Function * accumFn = Function::Create(accumFnType, GlobalValue::ExternalLinkage, getName() + ACCUMULATOR_INFIX + binding.name, client);
122        accumFn->setCallingConv(CallingConv::C);
123        accumFn->setDoesNotThrow();
124        accumFn->setDoesNotCapture(1);
125        auto args = accumFn->arg_begin();
126        args->setName("self");
127    }
128
129    iBuilder->setModule(saveModule);
130    iBuilder->restoreIP(savePoint);
131}
132
133void KernelInterface::setInitialArguments(std::vector<Value *> args) {
134    mInitialArguments = args;
135}
136
137llvm::Function * KernelInterface::getAccumulatorFunction(const std::string & accumName) const {
138    const auto name = getName() + ACCUMULATOR_INFIX + accumName;
139    Function * f = iBuilder->getModule()->getFunction(name);
140    if (LLVM_UNLIKELY(f == nullptr)) {
141        llvm::report_fatal_error("Cannot find " + name);
142    }
143    return f;
144}
145
146Function * KernelInterface::getInitFunction() const {
147    const auto name = getName() + INIT_SUFFIX;
148    Function * f = iBuilder->getModule()->getFunction(name);
149    if (LLVM_UNLIKELY(f == nullptr)) {
150        llvm::report_fatal_error("Cannot find " + name);
151    }
152    return f;
153}
154
155Function * KernelInterface::getDoSegmentFunction() const {
156    const auto name = getName() + DO_SEGMENT_SUFFIX;
157    Function * f = iBuilder->getModule()->getFunction(name);
158    if (LLVM_UNLIKELY(f == nullptr)) {
159        llvm::report_fatal_error("Cannot find " + name);
160    }
161    return f;
162}
Note: See TracBrowser for help on using the repository browser.