source: icGREP/icgrep-devel/icgrep/kernels/interface.h @ 5331

Last change on this file since 5331 was 5331, checked in by cameron, 2 years ago

Move default arguments from friend declarations to satisfy strict compilers

File size: 6.3 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#ifndef KERNEL_INTERFACE_H
7#define KERNEL_INTERFACE_H
8
9#include <string>  // for string
10#include <vector>  // for vector
11namespace IDISA { class IDISA_Builder; }
12namespace llvm { class ConstantInt; }
13namespace llvm { class Function; }
14namespace llvm { class Module; }
15namespace llvm { class PointerType; }
16namespace llvm { class StructType; }
17namespace llvm { class Type; }
18namespace llvm { class Value; }
19
20
21// Processing rate attributes are required for all stream set bindings for a kernel.
22// These attributes describe the number of items that are processed or produced as
23// a ratio in comparison to the principal input stream set (or the principal output
24// stream set if there is no input.
25//
26// The default ratio is FixedRatio(1) which means that there is one item processed or
27// produced for every item of the principal input or output stream.
28// FixedRatio(m, n) means that for every group of n items of the principal stream,
29// there are m items in the output stream (rounding up).
30//
31// Kernels which produce a variable number of items use MaxRatio(n), for a maximum
32// of n items produced or consumed per principal input or output item.  MaxRatio(m, n)
33// means there are at most m items for every n items of the principal stream.
34//
35// RoundUpToMultiple(n) means that number of items produced is the same as the
36// number of input items, rounded up to an exact multiple of n.
37//
38
39struct ProcessingRate  {
40    enum ProcessingRateKind : uint8_t {Fixed, RoundUp, Max, Unknown};
41    ProcessingRate() {}
42    ProcessingRateKind getKind() const {return mKind;}
43    bool isExact() const {return (mKind == Fixed)||(mKind == RoundUp) ;}
44    llvm::Value * CreateRatioCalculation(IDISA::IDISA_Builder * b, llvm::Value * principalInputItems) const;
45    friend ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems);
46    friend ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems);
47    friend ProcessingRate RoundUpToMultiple(unsigned itemMultiple);
48    friend ProcessingRate UnknownRate();
49   
50protected:
51    ProcessingRate(ProcessingRateKind k, unsigned numerator, unsigned denominator) 
52    : mKind(k), ratio_numerator(numerator), ratio_denominator(denominator) {}
53    ProcessingRateKind mKind;
54    uint16_t ratio_numerator;
55    uint16_t ratio_denominator;
56    bool isVariableRate();
57}; 
58
59ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems = 1);
60ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems = 1);
61ProcessingRate RoundUpToMultiple(unsigned itemMultiple);
62ProcessingRate UnknownRate();
63
64struct Binding {
65    Binding(llvm::Type * type, const std::string & name, ProcessingRate r = FixedRatio(1))
66    : type(type), name(name), rate(r) { }
67    llvm::Type *        type;
68    std::string         name;
69    ProcessingRate      rate;
70};
71
72class KernelInterface {
73
74public:
75    /*
76     
77     This class defines the methods to be used to generate the code 
78     necessary for declaring, allocating, calling and synchronizing
79     kernels.   The methods to be used for constructing kernels are defined
80     within the KernelBuilder class of kernel.h
81     
82     */
83   
84    const std::string & getName() const { return mKernelName; }
85       
86    const std::vector<Binding> & getStreamInputs() const { return mStreamSetInputs; }
87
88    const std::vector<Binding> & getStreamOutputs() const { return mStreamSetOutputs; }
89
90    const std::vector<Binding> & getScalarInputs() const { return mScalarInputs; }
91
92    const std::vector<Binding> & getScalarOutputs() const { return mScalarOutputs; }
93       
94    // Add ExternalLinkage method declarations for the kernel to a given client module.
95    void addKernelDeclarations(llvm::Module * client);
96
97    virtual void createInstance() = 0;
98
99    void setInitialArguments(std::vector<llvm::Value *> args);
100
101    llvm::Value * getInstance() const { return mKernelInstance; }
102
103    unsigned getLookAhead() const {
104        return mLookAheadPositions;
105    }
106   
107    IDISA::IDISA_Builder * getBuilder() const {
108        return iBuilder;
109    }
110
111    virtual llvm::Value * getProcessedItemCount(llvm::Value * instance, const std::string & name) const = 0;
112
113    virtual void setProcessedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
114
115    virtual llvm::Value * getProducedItemCount(llvm::Value * instance, const std::string & name) const = 0;
116
117    virtual void setProducedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
118
119    virtual llvm::Value * getTerminationSignal(llvm::Value * instance) const = 0;
120
121    virtual void setTerminationSignal(llvm::Value * instance) const = 0;
122   
123    void setLookAhead(unsigned lookAheadPositions) {
124        mLookAheadPositions = lookAheadPositions;
125    }
126
127    llvm::Function * getInitFunction() const;
128
129    llvm::Function * getDoSegmentFunction() const;
130
131    llvm::Function * getAccumulatorFunction(const std::string & accumName) const;
132
133protected:
134
135    KernelInterface(IDISA::IDISA_Builder * builder,
136                    std::string && kernelName,
137                    std::vector<Binding> && stream_inputs,
138                    std::vector<Binding> && stream_outputs,
139                    std::vector<Binding> && scalar_inputs,
140                    std::vector<Binding> && scalar_outputs,
141                    std::vector<Binding> && internal_scalars)
142    : iBuilder(builder)
143    , mKernelName(kernelName)
144    , mStreamSetInputs(stream_inputs)
145    , mStreamSetOutputs(stream_outputs)
146    , mScalarInputs(scalar_inputs)
147    , mScalarOutputs(scalar_outputs)
148    , mInternalScalars(internal_scalars)
149    , mKernelStateType(nullptr)
150    , mKernelInstance(nullptr)
151    , mLookAheadPositions(0) {
152
153    }
154   
155    virtual void addAdditionalKernelDeclarations(llvm::Module * module, llvm::PointerType * selfType) {}
156
157protected:
158   
159    IDISA::IDISA_Builder * const iBuilder;
160    const std::string mKernelName;
161    std::vector<llvm::Value *> mInitialArguments;
162    std::vector<Binding> mStreamSetInputs;
163    std::vector<Binding> mStreamSetOutputs;
164    std::vector<Binding> mScalarInputs;
165    std::vector<Binding> mScalarOutputs;
166    std::vector<Binding> mInternalScalars;
167    llvm::StructType * mKernelStateType;
168    llvm::Value * mKernelInstance;
169    unsigned mLookAheadPositions;
170   
171};
172
173#endif
Note: See TracBrowser for help on using the repository browser.