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

Last change on this file since 5353 was 5351, checked in by nmedfort, 3 years ago

Update to BlockOrientedKernel? to move the indirect branch out of the StrideLoopBody? and into StrideLoopDone? to simplify branch prediction.

File size: 5.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) {
25    return ProcessingRate(ProcessingRate::ProcessingRateKind::Fixed, strmItemsPer, perPrincipalInputItems);
26}
27
28ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems) {
29    return ProcessingRate(ProcessingRate::ProcessingRateKind::Max, strmItemsPer, perPrincipalInputItems);
30}
31
32ProcessingRate RoundUpToMultiple(unsigned itemMultiple) {
33    return ProcessingRate(ProcessingRate::ProcessingRateKind::RoundUp, itemMultiple, itemMultiple);
34}
35
36ProcessingRate UnknownRate() {
37    return ProcessingRate(ProcessingRate::ProcessingRateKind::Unknown, 0, 0);
38}
39
40Value * ProcessingRate::CreateRatioCalculation(IDISA::IDISA_Builder * b, Value * principalInputItems) const {
41    Type * T = principalInputItems->getType();
42    if (mKind == ProcessingRate::ProcessingRateKind::Fixed || mKind == ProcessingRate::ProcessingRateKind::Max) {
43        Value * strmItems = (ratio_numerator == 1) ? principalInputItems : b->CreateMul(principalInputItems, ConstantInt::get(T, ratio_numerator)); 
44        if (ratio_denominator == 1) return strmItems;
45        return b->CreateUDiv(b->CreateAdd(ConstantInt::get(T, ratio_denominator - 1), strmItems), ConstantInt::get(T, ratio_denominator)); 
46    }
47    if (mKind == ProcessingRate::ProcessingRateKind::RoundUp) {
48        Constant * multiple = ConstantInt::get(T, ratio_denominator);
49        Constant * multipleLess1 = ConstantInt::get(T, ratio_denominator - 1);
50        return b->CreateMul(b->CreateUDiv(b->CreateAdd(principalInputItems, multipleLess1), multiple), multiple);
51    }
52    return nullptr;
53}
54
55void KernelInterface::addKernelDeclarations(Module * client) {
56    Module * saveModule = iBuilder->getModule();
57    auto savePoint = iBuilder->saveIP();
58    iBuilder->setModule(client);
59    if (mKernelStateType == nullptr) {
60        throw std::runtime_error("Kernel interface " + getName() + " not yet finalized.");
61    }
62    PointerType * selfType = PointerType::getUnqual(mKernelStateType);
63
64    // Create the initialization function prototype
65    std::vector<Type *> initParameters = {selfType};
66    for (auto binding : mScalarInputs) {
67        initParameters.push_back(binding.type);
68    }
69    FunctionType * initType = FunctionType::get(iBuilder->getVoidTy(), initParameters, false);
70    Function * init = Function::Create(initType, GlobalValue::ExternalLinkage, getName() + INIT_SUFFIX, client);
71    init->setCallingConv(CallingConv::C);
72    init->setDoesNotThrow();
73    auto args = init->arg_begin();
74    args->setName("self");
75    for (auto binding : mScalarInputs) {
76        (++args)->setName(binding.name);
77    }
78
79    // Create the doSegment function prototype.
80    std::vector<Type *> doSegmentParameters = {selfType, iBuilder->getInt1Ty()};
81    for (unsigned i = 0; i < mStreamSetInputs.size(); ++i) {
82        doSegmentParameters.push_back(iBuilder->getSizeTy());
83    }
84    FunctionType * doSegmentType = FunctionType::get(iBuilder->getVoidTy(), doSegmentParameters, false);
85    Function * doSegment = Function::Create(doSegmentType, GlobalValue::ExternalLinkage, getName() + DO_SEGMENT_SUFFIX, client);
86    doSegment->setCallingConv(CallingConv::C);
87    doSegment->setDoesNotThrow();
88    doSegment->setDoesNotCapture(1); // for self parameter only.
89    args = doSegment->arg_begin();
90    args->setName("self");
91    (++args)->setName("doFinal");
92    for (auto ss : mStreamSetInputs) {
93        (++args)->setName(ss.name + "_availableItems");
94    }
95
96    // Create the accumulator get function prototypes
97    for (const auto & binding : mScalarOutputs) {
98        FunctionType * accumFnType = FunctionType::get(binding.type, {selfType}, false);
99        Function * accumFn = Function::Create(accumFnType, GlobalValue::ExternalLinkage, getName() + ACCUMULATOR_INFIX + binding.name, client);
100        accumFn->setCallingConv(CallingConv::C);
101        accumFn->setDoesNotThrow();
102        accumFn->setDoesNotCapture(1);
103        auto args = accumFn->arg_begin();
104        args->setName("self");
105    }
106
107    iBuilder->setModule(saveModule);
108    iBuilder->restoreIP(savePoint);
109}
110
111void KernelInterface::setInitialArguments(std::vector<Value *> args) {
112    mInitialArguments = args;
113}
114
115llvm::Function * KernelInterface::getAccumulatorFunction(const std::string & accumName) const {
116    const auto name = getName() + ACCUMULATOR_INFIX + accumName;
117    Function * f = iBuilder->getModule()->getFunction(name);
118    if (LLVM_UNLIKELY(f == nullptr)) {
119        llvm::report_fatal_error("Cannot find " + name);
120    }
121    return f;
122}
123
124Function * KernelInterface::getInitFunction() const {
125    const auto name = getName() + INIT_SUFFIX;
126    Function * f = iBuilder->getModule()->getFunction(name);
127    if (LLVM_UNLIKELY(f == nullptr)) {
128        llvm::report_fatal_error("Cannot find " + name);
129    }
130    return f;
131}
132
133Function * KernelInterface::getDoSegmentFunction() const {
134    const auto name = getName() + DO_SEGMENT_SUFFIX;
135    Function * f = iBuilder->getModule()->getFunction(name);
136    if (LLVM_UNLIKELY(f == nullptr)) {
137        llvm::report_fatal_error("Cannot find " + name);
138    }
139    return f;
140}
Note: See TracBrowser for help on using the repository browser.