Ignore:
Timestamp:
Apr 3, 2016, 4:31:22 PM (3 years ago)
Author:
nmedfort
Message:

Redesigned buffer system to allow the pipeline to control selection of the current input and output streams; DoBlock? functions containing lookahead now take multiple input stream arguments. Selection and passing occurs automatically. Some work on Symbol Table.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/kernels/kernel.h

    r4995 r5000  
    99#include <vector>
    1010#include <boost/container/flat_map.hpp>
     11#include <IDISA/idisa_builder.h>
    1112
    1213namespace llvm {
     
    2829}
    2930
    30 namespace IDISA {
    31     class IDISA_Builder;
    32 }
    33 
    3431namespace kernel {
    3532
     
    3936    friend class Instance;
    4037    friend llvm::Function * generateScanWordRoutine(llvm::Module *, IDISA::IDISA_Builder *, unsigned, KernelBuilder *, bool);
    41     using NameMap = boost::container::flat_map<std::string, unsigned>;
     38    using InputStreamMap = boost::container::flat_map<unsigned, llvm::Value *>;
     39    using NameMap = boost::container::flat_map<std::string, llvm::ConstantInt *>;
    4240public:
    43     // sets name & sets internal state to the kernel superclass state
    44     KernelBuilder(std::string name, llvm::Module * m, IDISA::IDISA_Builder * b, const unsigned bufferSize = 1);
     41
     42    KernelBuilder(IDISA::IDISA_Builder * builder, std::string && name, const unsigned defaultBufferSize);
    4543
    4644    template<typename T>
    4745    struct disable_implicit_conversion {
    48         inline disable_implicit_conversion(T const value) : _value(value) {}
     46        inline disable_implicit_conversion(T const value) : _value(value) { assert(_value); }
    4947        inline disable_implicit_conversion(std::nullptr_t) = delete;
    5048        inline disable_implicit_conversion(unsigned) = delete;
     
    6866    unsigned addOutputScalar(llvm::Type * const type);
    6967
    70     llvm::Function * prepareFunction();
     68    inline llvm::Function * prepareFunction() {
     69        return prepareFunction({0});
     70    }
     71
     72    llvm::Function * prepareFunction(std::vector<unsigned> && inputStreamOffsets);
     73
     74    inline llvm::Value * getInternalState(const std::string & name) {
     75        return getInternalState(mKernelStateParam, name);
     76    }
     77
     78    inline void setInternalState(const std::string & name, llvm::Value * value) {
     79        setInternalState(mKernelStateParam, name, value);
     80    }
     81
     82    inline llvm::Value * getInternalState(const unsigned index) {
     83        assert (index < mInternalState.size());
     84        return getInternalState(mKernelStateParam, iBuilder->getInt32(index));
     85    }
     86
     87    inline llvm::Value * getInternalState(disable_implicit_conversion<llvm::Value *> const index) {
     88        return getInternalState(mKernelStateParam, index);
     89    }
     90
     91    void setInternalState(const unsigned index, llvm::Value * value) {
     92        assert (index < mInternalState.size());
     93        setInternalState(mKernelStateParam, iBuilder->getInt32(index), value);
     94    }
     95
     96    void setInternalState(disable_implicit_conversion<llvm::Value *> const index, llvm::Value * value) {
     97        setInternalState(mKernelStateParam, index, value);
     98    }
     99    inline llvm::Type * getKernelStateType() const{
     100        return mKernelStateType;
     101    }
    71102
    72103    inline llvm::Value * getInputStream(const unsigned index, const unsigned streamOffset = 0) {
    73         return getInputStream(mKernelState, index, streamOffset);
     104        assert (index < getNumOfInputStreams());
     105        return getInputStream(iBuilder->getInt32(index), streamOffset);
    74106    }
    75107
    76108    inline llvm::Value * getInputStream(disable_implicit_conversion<llvm::Value *> index, const unsigned streamOffset = 0) {
    77         return getInputStream(mKernelState, index, streamOffset);
     109        const auto f = mInputStreamParam.find(streamOffset);
     110        if (LLVM_UNLIKELY(f == mInputStreamParam.end())) {
     111            throw std::runtime_error("Kernel compilation error: No input stream parameter for stream offset " + std::to_string(streamOffset));
     112        }
     113        return getInputStream(f->second, index);
     114    }
     115
     116    inline unsigned getNumOfInputStreams() const {
     117        return mInputStream.size();
     118    }
     119
     120    inline llvm::Type * getInputStreamType() const {
     121        return mInputStreamType;
    78122    }
    79123
    80124    inline llvm::Value * getInputScalar(const unsigned index) {
    81         return getInputScalar(mKernelState, index);
     125        assert (index < getNumOfInputScalars());
     126        return getInputScalar(iBuilder->getInt32(index));
    82127    }
    83128
    84129    inline llvm::Value * getInputScalar(disable_implicit_conversion<llvm::Value *> const index) {
    85         return getInputScalar(mKernelState, index);
    86     }
    87 
    88     llvm::Value * getInternalState(const std::string & name) {
    89         return getInternalState(mKernelState, name);
    90     }
    91 
    92     void setInternalState(const std::string & name, llvm::Value * value) {
    93         setInternalState(mKernelState, name, value);
    94     }
    95 
    96     llvm::Value * getInternalState(const unsigned index) {
    97         return getInternalState(mKernelState, index);
    98     }
    99 
    100     llvm::Value * getInternalState(disable_implicit_conversion<llvm::Value *> const index) {
    101         return getInternalState(mKernelState, index);
    102     }
    103 
    104     void setInternalState(const unsigned index, llvm::Value * value) {
    105         setInternalState(mKernelState, index, value);
    106     }
    107 
    108     void setInternalState(disable_implicit_conversion<llvm::Value *> const index, llvm::Value * value) {
    109         setInternalState(mKernelState, index, value);
    110     }
    111 
    112     llvm::Value * getOutputStream(const unsigned index, const unsigned streamOffset = 0) {
    113         return getOutputStream(mKernelState, index, streamOffset);
    114     }
    115 
    116     llvm::Value * getOutputStream(disable_implicit_conversion<llvm::Value *> const index, const unsigned streamOffset = 0) {
    117         return getOutputStream(mKernelState, index, streamOffset);
     130        return getInputScalar(mInputScalarParam, index);
     131    }
     132
     133    inline unsigned getNumOfInputScalars() const {
     134        return mInputScalar.size();
     135    }
     136
     137    inline llvm::Type * getInputScalarType() const {
     138        return mInputScalarType;
     139    }
     140
     141    inline llvm::Value * getOutputStream(const unsigned index) {
     142        assert (index < getNumOfOutputStreams());
     143        return getOutputStream(mOutputStreamParam, iBuilder->getInt32(index));
     144    }
     145
     146    inline llvm::Value * getOutputStream(disable_implicit_conversion<llvm::Value *> const index) {
     147        return getOutputStream(mOutputStreamParam, index);
    118148    }
    119149
     
    122152    }
    123153
    124     llvm::Value * getOutputScalar(const unsigned index) {
    125         return getOutputScalar(mKernelState, index);
    126     }
    127 
    128     llvm::Value * getOutputScalar(disable_implicit_conversion<llvm::Value *> const index) {
    129         return getOutputScalar(mKernelState, index);
     154    inline llvm::Type * getOutputStreamType() const {
     155        return mOutputStreamType;
     156    }
     157
     158    inline llvm::Value * getOutputScalar(const unsigned index) {
     159        assert (index < getNumOfOutputScalars());
     160        return getOutputScalar(mOutputScalarParam, iBuilder->getInt32(index));
     161    }
     162
     163    inline llvm::Value * getOutputScalar(disable_implicit_conversion<llvm::Value *> const index) {
     164        return getOutputScalar(mOutputScalarParam, index);
    130165    }
    131166
     
    134169    }
    135170
    136     llvm::Value * getBlockNo() {
    137         return getBlockNo(mKernelState);
    138     }
    139 
    140     llvm::Type * getInputStreamType() const;
    141 
    142     void setInputBufferSize(const unsigned bufferSize);
    143 
    144     unsigned getInputBufferSize() const;
    145 
    146     unsigned getBufferSize() const;
     171    inline llvm::Type * getOutputScalarType() const {
     172        return mOutputStreamType;
     173    }
     174
     175    inline llvm::Value * getBlockNo() {
     176        return getBlockNo(mKernelStateParam);
     177    }
     178
     179    unsigned getDefaultBufferSize() const;
    147180
    148181    void finalize();
    149182
    150     kernel::Instance * instantiate(llvm::Value * const inputStream);
     183    kernel::Instance * instantiate(std::pair<llvm::Value *, unsigned> && inputStreamSet) {
     184        return instantiate(std::move(inputStreamSet), getDefaultBufferSize());
     185    }
     186
     187    kernel::Instance * instantiate(std::pair<llvm::Value *, unsigned> && inputStreamSet, const unsigned outputBufferSize);
     188
     189    kernel::Instance * instantiate(llvm::Value * const inputStream) {
     190        return instantiate(std::make_pair(inputStream, 0));
     191    }
    151192
    152193    kernel::Instance * instantiate(std::initializer_list<llvm::Value *> inputStreams);
    153194
    154     kernel::Instance * instantiate(std::pair<llvm::Value *, unsigned> && inputStream);
    155 
    156     llvm::Type * getKernelStateType() const;
    157 
    158195    llvm::Value * getKernelState() const;
    159196
    160197    llvm::Function * getDoBlockFunction() const;
    161198
    162     void clearOutputStreamSet(llvm::Value * const instance, const unsigned streamOffset = 0);
    163 
    164199protected:
    165200
    166     llvm::Type * packDataTypes(const std::vector<llvm::Type *> & types);
    167 
    168     llvm::Value * getInputStream(llvm::Value * const instance, const unsigned index, const unsigned streamOffset);
    169 
    170     llvm::Value * getInputStream(llvm::Value * const instance, disable_implicit_conversion<llvm::Value *> index, const unsigned streamOffset);
    171 
    172     llvm::Value * getInputScalar(llvm::Value * const instance, const unsigned index);
    173 
    174     llvm::Value * getInputScalar(llvm::Value * const instance, disable_implicit_conversion<llvm::Value *> index);
    175 
    176     llvm::Value * getInternalState(llvm::Value * const instance, const std::string & name);
    177 
    178     void setInternalState(llvm::Value * const instance, const std::string & name, llvm::Value * const value);
    179 
    180     llvm::Value * getInternalState(llvm::Value * const instance, const unsigned index);
    181 
    182     llvm::Value * getInternalState(llvm::Value * const instance, disable_implicit_conversion<llvm::Value *> index);
    183 
    184     void setInternalState(llvm::Value * const instance, const unsigned index, llvm::Value * const value);
    185 
    186     void setInternalState(llvm::Value * const instance, disable_implicit_conversion<llvm::Value *> index, llvm::Value * const value);
    187 
    188     llvm::Value * getOutputStream(llvm::Value * const instance, const unsigned index, const unsigned streamOffset);
    189 
    190     llvm::Value * getOutputStream(llvm::Value * const instance, disable_implicit_conversion<llvm::Value *> index, const unsigned streamOffset);
    191 
    192     llvm::Value * getOutputScalar(llvm::Value * const instance, const unsigned index);
    193 
    194     llvm::Value * getOutputScalar(llvm::Value * const instance, disable_implicit_conversion<llvm::Value *> index);
    195 
    196     llvm::Value * getStreamOffset(llvm::Value * const instance, const unsigned index);
     201    Type * packDataTypes(const std::vector<llvm::Type *> & types);
     202
     203    llvm::Value * getInputStream(llvm::Value * const inputStreamSet, disable_implicit_conversion<llvm::Value *> index);
     204
     205    llvm::Value * getInputScalar(llvm::Value * const inputScalarSet, disable_implicit_conversion<llvm::Value *> index);
     206
     207    llvm::Value * getInternalState(llvm::Value * const kernelState, const std::string & name);
     208
     209    void setInternalState(llvm::Value * const kernelState, const std::string & name, llvm::Value * const value);
     210
     211    llvm::Value * getInternalState(llvm::Value * const kernelState, disable_implicit_conversion<llvm::Value *> index);
     212
     213    void setInternalState(llvm::Value * const kernelState, const unsigned index, llvm::Value * const value);
     214
     215    void setInternalState(llvm::Value * const kernelState, disable_implicit_conversion<llvm::Value *> index, llvm::Value * const value);
     216
     217    llvm::Value * getOutputStream(llvm::Value * const outputStreamSet, disable_implicit_conversion<llvm::Value *> index);
     218
     219    llvm::Value * getOutputScalar(llvm::Value * const outputScalarSet, disable_implicit_conversion<llvm::Value *> index);
    197220
    198221    llvm::Value * getBlockNo(llvm::Value * const instance);
     
    200223    llvm::Function * getOutputStreamSetFunction() const;
    201224
    202     void CreateDoBlockCall(llvm::Value * const instance);
    203 
    204     llvm::Function * CreateModFunction(const unsigned size);
    205 
    206     void eliminateRedundantMemoryOperations(llvm::Function * const function);
     225    const std::vector<unsigned> & getInputStreamOffsets() const {
     226        return mInputStreamOffsets;
     227    }
    207228
    208229private:
    209     llvm::Module *                      mMod;
    210     IDISA::IDISA_Builder *              iBuilder;
    211     std::string                                                 mKernelName;
     230
     231    IDISA::IDISA_Builder * const        iBuilder;
     232    const std::string                   mKernelName;
     233    unsigned                            mDefaultBufferSize;
     234
    212235    llvm::Type *                        mBitBlockType;
     236    llvm::ConstantInt *                 mBlockNoIndex;
    213237    llvm::Function *                                    mConstructor;
    214238    llvm::Function *                                    mDoBlock;
    215239
    216     unsigned                            mBufferSize;
    217 
    218240    llvm::Type *                        mKernelStateType;
     241    llvm::Type *                        mInputScalarType;
    219242    llvm::Type *                        mInputStreamType;
    220     llvm::Type *                        mInputScalarType;
     243    llvm::Type *                        mOutputScalarType;
    221244    llvm::Type *                        mOutputStreamType;
    222245
    223     llvm::Value *                       mKernelState;
    224     unsigned                            mBlockNoIndex;
    225 
     246    llvm::Value *                       mKernelStateParam;
     247    llvm::Value *                       mInputScalarParam;
     248    InputStreamMap                      mInputStreamParam;
     249    llvm::Value *                       mOutputScalarParam;
     250    llvm::Value *                       mOutputStreamParam;
     251
     252    std::vector<llvm::Type *>           mInputScalar;
     253    std::vector<std::string>            mInputScalarName;   
    226254    std::vector<llvm::Type *>           mInputStream;
    227255    std::vector<std::string>            mInputStreamName;
    228     std::vector<llvm::Type *>           mInputScalar;
    229     std::vector<std::string>            mInputScalarName;   
     256    std::vector<unsigned>               mInputStreamOffsets;
     257    std::vector<llvm::Type *>           mOutputScalar;
    230258    std::vector<llvm::Type *>           mOutputStream;
    231     std::vector<llvm::Type *>           mOutputScalar;
    232259    std::vector<llvm::Type *>                   mInternalState;
    233260    NameMap                             mInternalStateNameMap;
     
    238265}
    239266
    240 inline llvm::Type * KernelBuilder::getKernelStateType() const{
    241     return mKernelStateType;
    242 }
    243 
    244267inline llvm::Value * KernelBuilder::getKernelState() const {
    245     return mKernelState;
    246 }
    247 
    248 inline llvm::Type * KernelBuilder::getInputStreamType() const {
    249     return mInputStreamType;
     268    return mKernelStateParam;
    250269}
    251270
     
    254273}
    255274
    256 inline unsigned KernelBuilder::getBufferSize() const {
    257     return mBufferSize;
     275inline unsigned KernelBuilder::getDefaultBufferSize() const {
     276    return mDefaultBufferSize;
    258277}
    259278
Note: See TracChangeset for help on using the changeset viewer.