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

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

Changes towards separate compilation

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