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

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

Potential bug fix for 32-bit. Modified MRemap to check for Linux OS support. Added MMapAdvise to CBuilder.

File size: 7.9 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 TERMINATE_SUFFIX = "_Terminate";
19
20static const auto DO_SEGMENT_SUFFIX = "_DoSegment";
21
22static const auto ACCUMULATOR_INFIX = "_get_";
23
24using namespace llvm;
25
26ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string && referenceStreamSet) {
27    return ProcessingRate(ProcessingRate::ProcessingRateKind::Fixed, strmItemsPer, perPrincipalInputItems, std::move(referenceStreamSet));
28}
29
30ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string && referenceStreamSet) {
31    return ProcessingRate(ProcessingRate::ProcessingRateKind::Max, strmItemsPer, perPrincipalInputItems, std::move(referenceStreamSet));
32}
33
34ProcessingRate RoundUpToMultiple(unsigned itemMultiple, std::string && referenceStreamSet) {
35    return ProcessingRate(ProcessingRate::ProcessingRateKind::RoundUp, itemMultiple, itemMultiple, std::move(referenceStreamSet));
36}
37
38ProcessingRate Add1(std::string && referenceStreamSet) {
39    return ProcessingRate(ProcessingRate::ProcessingRateKind::Add1, 0, 0, std::move(referenceStreamSet));
40}
41
42ProcessingRate UnknownRate() {
43    return ProcessingRate(ProcessingRate::ProcessingRateKind::Unknown, 0, 0, "");
44}
45
46Value * ProcessingRate::CreateRatioCalculation(IDISA::IDISA_Builder * b, Value * principalInputItems, Value * doFinal) const {
47    if (mKind == ProcessingRate::ProcessingRateKind::Fixed || mKind == ProcessingRate::ProcessingRateKind::Max) {
48        if (mRatioNumerator == 1) {
49            return principalInputItems;
50        }
51        Type * const T = principalInputItems->getType();
52        Constant * const numerator = ConstantInt::get(T, mRatioNumerator);
53        Constant * const denominator = ConstantInt::get(T, mRatioDenominator);
54        Constant * const denominatorLess1 = ConstantInt::get(T, mRatioDenominator - 1);
55        Value * strmItems = b->CreateMul(principalInputItems, numerator);
56        return b->CreateUDiv(b->CreateAdd(denominatorLess1, strmItems), denominator);
57    }
58    if (mKind == ProcessingRate::ProcessingRateKind::RoundUp) {
59        Type * const T = principalInputItems->getType();
60        Constant * const denominator = ConstantInt::get(T, mRatioDenominator);
61        Constant * const denominatorLess1 = ConstantInt::get(T, mRatioDenominator - 1);
62        return b->CreateMul(b->CreateUDiv(b->CreateAdd(principalInputItems, denominatorLess1), denominator), denominator);
63    }
64    if (mKind == ProcessingRate::ProcessingRateKind::Add1) {
65        if (doFinal) {
66            Type * const T = principalInputItems->getType();
67            principalInputItems = b->CreateAdd(principalInputItems, b->CreateZExt(doFinal, T));
68        }
69        return principalInputItems;
70    }
71    return nullptr;
72}
73
74void KernelInterface::addKernelDeclarations(Module * const client) {
75    Module * saveModule = iBuilder->getModule();
76    auto savePoint = iBuilder->saveIP();
77    iBuilder->setModule(client);
78    if (mKernelStateType == nullptr) {
79        throw std::runtime_error("Kernel interface " + getName() + " not yet finalized.");
80    }
81    PointerType * const selfType = mKernelStateType->getPointerTo();
82    IntegerType * const sizeTy = iBuilder->getSizeTy();
83    PointerType * const consumerTy = StructType::get(sizeTy, sizeTy->getPointerTo()->getPointerTo(), nullptr)->getPointerTo();
84
85    // Create the initialization function prototype
86    std::vector<Type *> initParameters = {selfType};   
87    for (auto binding : mScalarInputs) {
88        initParameters.push_back(binding.type);
89    }
90    initParameters.insert(initParameters.end(), mStreamSetOutputs.size(), consumerTy);
91
92    FunctionType * initType = FunctionType::get(iBuilder->getVoidTy(), initParameters, false);
93    Function * init = Function::Create(initType, GlobalValue::ExternalLinkage, getName() + INIT_SUFFIX, client);
94    init->setCallingConv(CallingConv::C);
95    init->setDoesNotThrow();
96    auto args = init->arg_begin();
97    args->setName("self");
98    for (auto binding : mScalarInputs) {
99        (++args)->setName(binding.name);
100    }
101    for (auto binding : mStreamSetOutputs) {
102        args->setName(binding.name + "ConsumerLogicalSegments");       
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    // Create the terminate function prototype
123    FunctionType * terminateType = FunctionType::get(iBuilder->getVoidTy(), {selfType}, false);
124    Function * terminateFunc = Function::Create(terminateType, GlobalValue::ExternalLinkage, getName() + TERMINATE_SUFFIX, client);
125    terminateFunc->setCallingConv(CallingConv::C);
126    terminateFunc->setDoesNotThrow();
127    terminateFunc->setDoesNotCapture(1);
128    args = terminateFunc->arg_begin();
129    args->setName("self");
130
131    /// INVESTIGATE: replace the accumulator methods with a single Exit method that handles any clean up and returns
132    /// a struct containing all scalar outputs?
133
134    // Create the accumulator get function prototypes
135    for (const auto & binding : mScalarOutputs) {
136        FunctionType * accumFnType = FunctionType::get(binding.type, {selfType}, false);
137        Function * accumFn = Function::Create(accumFnType, GlobalValue::ExternalLinkage, getName() + ACCUMULATOR_INFIX + binding.name, client);
138        accumFn->setCallingConv(CallingConv::C);
139        accumFn->setDoesNotThrow();
140        accumFn->setDoesNotCapture(1);
141        auto args = accumFn->arg_begin();
142        args->setName("self");
143    }
144
145    iBuilder->setModule(saveModule);
146    iBuilder->restoreIP(savePoint);
147}
148
149void KernelInterface::setInitialArguments(std::vector<Value *> args) {
150    mInitialArguments = args;
151}
152
153llvm::Function * KernelInterface::getAccumulatorFunction(const std::string & accumName) const {
154    const auto name = getName() + ACCUMULATOR_INFIX + accumName;
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::getInitFunction() const {
163    const auto name = getName() + INIT_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}
170
171Function * KernelInterface::getDoSegmentFunction() const {
172    const auto name = getName() + DO_SEGMENT_SUFFIX;
173    Function * f = iBuilder->getModule()->getFunction(name);
174    if (LLVM_UNLIKELY(f == nullptr)) {
175        llvm::report_fatal_error("Cannot find " + name);
176    }
177    return f;
178}
179
180Function * KernelInterface::getTerminateFunction() const {
181    const auto name = getName() + TERMINATE_SUFFIX;
182    Function * f = iBuilder->getModule()->getFunction(name);
183    if (LLVM_UNLIKELY(f == nullptr)) {
184        llvm::report_fatal_error("Cannot find " + name);
185    }
186    return f;
187}
Note: See TracBrowser for help on using the repository browser.