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

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

setName method for kernels; so that two instances may be created.

File size: 6.4 KB
RevLine 
[5047]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
[5260]9#include <string>  // for string
10#include <vector>  // for vector
11namespace IDISA { class IDISA_Builder; }
[5307]12namespace llvm { class ConstantInt; }
[5285]13namespace llvm { class Function; }
[5260]14namespace llvm { class Module; }
[5287]15namespace llvm { class PointerType; }
[5260]16namespace llvm { class StructType; }
17namespace llvm { class Type; }
18namespace llvm { class Value; }
[5047]19
[5307]20
[5325]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
[5328]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).
[5325]30//
31// Kernels which produce a variable number of items use MaxRatio(n), for a maximum
[5328]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.
[5327]34//
[5328]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//
[5307]38
[5328]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;
[5331]45    friend ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems);
46    friend ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems);
[5328]47    friend ProcessingRate RoundUpToMultiple(unsigned itemMultiple);
48    friend ProcessingRate UnknownRate();
[5325]49   
[5328]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}; 
[5325]58
[5331]59ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems = 1);
60ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems = 1);
[5328]61ProcessingRate RoundUpToMultiple(unsigned itemMultiple);
62ProcessingRate UnknownRate();
[5325]63
64struct Binding {
[5328]65    Binding(llvm::Type * type, const std::string & name, ProcessingRate r = FixedRatio(1))
66    : type(type), name(name), rate(r) { }
[5307]67    llvm::Type *        type;
68    std::string         name;
[5328]69    ProcessingRate      rate;
[5047]70};
[5060]71
[5047]72class KernelInterface {
73
74public:
[5174]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     */
[5251]83   
[5297]84    const std::string & getName() const { return mKernelName; }
[5346]85
86    void setName(std::string newName) { mKernelName = newName; }
[5246]87       
[5297]88    const std::vector<Binding> & getStreamInputs() const { return mStreamSetInputs; }
[5285]89
[5297]90    const std::vector<Binding> & getStreamOutputs() const { return mStreamSetOutputs; }
[5285]91
[5297]92    const std::vector<Binding> & getScalarInputs() const { return mScalarInputs; }
[5285]93
[5297]94    const std::vector<Binding> & getScalarOutputs() const { return mScalarOutputs; }
[5292]95       
[5174]96    // Add ExternalLinkage method declarations for the kernel to a given client module.
[5292]97    void addKernelDeclarations(llvm::Module * client);
[5285]98
[5246]99    virtual void createInstance() = 0;
[5285]100
[5260]101    void setInitialArguments(std::vector<llvm::Value *> args);
[5285]102
[5246]103    llvm::Value * getInstance() const { return mKernelInstance; }
[5174]104
[5202]105    unsigned getLookAhead() const {
106        return mLookAheadPositions;
107    }
[5174]108   
[5202]109    IDISA::IDISA_Builder * getBuilder() const {
110        return iBuilder;
111    }
112
[5292]113    virtual llvm::Value * getProcessedItemCount(llvm::Value * instance, const std::string & name) const = 0;
[5285]114
[5292]115    virtual void setProcessedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
[5285]116
[5292]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;
[5174]124   
[5202]125    void setLookAhead(unsigned lookAheadPositions) {
126        mLookAheadPositions = lookAheadPositions;
127    }
128
[5287]129    llvm::Function * getInitFunction() const;
130
[5285]131    llvm::Function * getDoSegmentFunction() const;
[5202]132
[5287]133    llvm::Function * getAccumulatorFunction(const std::string & accumName) const;
134
[5174]135protected:
[5202]136
[5047]137    KernelInterface(IDISA::IDISA_Builder * builder,
[5267]138                    std::string && kernelName,
[5283]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)
[5267]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    }
[5047]156   
[5292]157    virtual void addAdditionalKernelDeclarations(llvm::Module * module, llvm::PointerType * selfType) {}
[5287]158
[5202]159protected:
[5053]160   
[5202]161    IDISA::IDISA_Builder * const iBuilder;
[5346]162    std::string mKernelName;
[5260]163    std::vector<llvm::Value *> mInitialArguments;
[5202]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;
[5227]169    llvm::StructType * mKernelStateType;
[5220]170    llvm::Value * mKernelInstance;
[5141]171    unsigned mLookAheadPositions;
172   
[5047]173};
[5285]174
[5047]175#endif
Note: See TracBrowser for help on using the repository browser.