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

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

Added enable asserts (-ea) command line flag + restructured BlockOrientedKernels? to allow for inlined code.

File size: 6.4 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    void setName(std::string newName) { mKernelName = newName; }
87       
88    const std::vector<Binding> & getStreamInputs() const { return mStreamSetInputs; }
89
90    const std::vector<Binding> & getStreamOutputs() const { return mStreamSetOutputs; }
91
92    const std::vector<Binding> & getScalarInputs() const { return mScalarInputs; }
93
94    const std::vector<Binding> & getScalarOutputs() const { return mScalarOutputs; }
95       
96    // Add ExternalLinkage method declarations for the kernel to a given client module.
97    void addKernelDeclarations(llvm::Module * client);
98
99    virtual void createInstance() = 0;
100
101    void setInitialArguments(std::vector<llvm::Value *> args);
102
103    llvm::Value * getInstance() const { return mKernelInstance; }
104
105    unsigned getLookAhead() const {
106        return mLookAheadPositions;
107    }
108   
109    IDISA::IDISA_Builder * getBuilder() const {
110        return iBuilder;
111    }
112
113    virtual llvm::Value * getProcessedItemCount(llvm::Value * instance, const std::string & name) const = 0;
114
115    virtual void setProcessedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
116
117    virtual llvm::Value * getProducedItemCount(llvm::Value * instance, const std::string & name) const = 0;
118
119    virtual void setProducedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
120
121    virtual llvm::Value * getTerminationSignal(llvm::Value * instance) const = 0;
122
123    virtual void setTerminationSignal(llvm::Value * instance) const = 0;
124   
125    void setLookAhead(unsigned lookAheadPositions) {
126        mLookAheadPositions = lookAheadPositions;
127    }
128
129    llvm::Function * getInitFunction() const;
130
131    llvm::Function * getDoSegmentFunction() const;
132
133    llvm::Function * getAccumulatorFunction(const std::string & accumName) const;
134
135protected:
136
137    KernelInterface(IDISA::IDISA_Builder * builder,
138                    std::string && kernelName,
139                    std::vector<Binding> && stream_inputs,
140                    std::vector<Binding> && stream_outputs,
141                    std::vector<Binding> && scalar_inputs,
142                    std::vector<Binding> && scalar_outputs,
143                    std::vector<Binding> && internal_scalars)
144    : iBuilder(builder)
145    , mKernelName(kernelName)
146    , mStreamSetInputs(stream_inputs)
147    , mStreamSetOutputs(stream_outputs)
148    , mScalarInputs(scalar_inputs)
149    , mScalarOutputs(scalar_outputs)
150    , mInternalScalars(internal_scalars)
151    , mKernelStateType(nullptr)
152    , mKernelInstance(nullptr)
153    , mLookAheadPositions(0) {
154
155    }
156   
157//    virtual void addAdditionalKernelDeclarations(llvm::Module * module, llvm::PointerType * selfType) {}
158
159protected:
160   
161    IDISA::IDISA_Builder * const iBuilder;
162    std::string mKernelName;
163    std::vector<llvm::Value *> mInitialArguments;
164    std::vector<Binding> mStreamSetInputs;
165    std::vector<Binding> mStreamSetOutputs;
166    std::vector<Binding> mScalarInputs;
167    std::vector<Binding> mScalarOutputs;
168    std::vector<Binding> mInternalScalars;
169    llvm::StructType * mKernelStateType;
170    llvm::Value * mKernelInstance;
171    unsigned mLookAheadPositions;
172   
173};
174
175#endif
Note: See TracBrowser for help on using the repository browser.