source: icGREP/icgrep-devel/icgrep/kernels/kernel.h @ 5356

Last change on this file since 5356 was 5356, checked in by cameron, 3 years ago

Eliminate obsolete DoBlockUpdatesProducedItemCounts? attribute

File size: 13.1 KB
RevLine 
[4924]1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
[5063]6#ifndef KERNEL_BUILDER_H
7#define KERNEL_BUILDER_H
8
[5260]9#include <string>           // for string
10#include <memory>           // for unique_ptr
11#include "interface.h"      // for KernelInterface
12#include <boost/container/flat_map.hpp>
[5238]13#include <IR_Gen/idisa_builder.h>
[5350]14
[5260]15namespace llvm { class ConstantInt; }
16namespace llvm { class Function; }
17namespace llvm { class IntegerType; }
18namespace llvm { class LoadInst; }
19namespace llvm { class Type; }
20namespace llvm { class Value; }
21namespace parabix { class StreamSetBuffer; }
[4924]22
[4974]23namespace kernel {
[5051]24   
[5063]25class KernelBuilder : public KernelInterface {
[5104]26    using NameMap = boost::container::flat_map<std::string, unsigned>;
[4924]27public:
[5051]28   
[5074]29    // Create a module for the kernel, including the kernel state type declaration and
30    // the full implementation of all required methods.     
31    //
[5260]32    std::unique_ptr<llvm::Module> createKernelModule(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
[5051]33   
[5074]34    // Generate the Kernel to the current module (iBuilder->getModule()).
[5260]35    void generateKernel(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
[5051]36   
[5220]37    void createInstance() override;
[5135]38
[5292]39    virtual llvm::Value * getProcessedItemCount(llvm::Value * instance, const std::string & name) const override;
[5283]40
[5292]41    virtual void setProcessedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const;
[5283]42
[5292]43    virtual llvm::Value * getProducedItemCount(llvm::Value * instance, const std::string & name) const override;
44
45    virtual void setProducedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const;
46
[5252]47    bool hasNoTerminateAttribute() { return mNoTerminateAttribute;}
48   
[5292]49    llvm::Value * getTerminationSignal(llvm::Value * instance) const override final;
50
51    void setTerminationSignal(llvm::Value * instance) const override final;
52
53    // Get the value of a scalar field for a given instance.
54    llvm::Value * getScalarField(llvm::Value * instance, const std::string & fieldName) const;
55
56    llvm::Value * getScalarField(llvm::Value * instance, llvm::Value * index) const;
57
58    // Set the value of a scalar field for a given instance.
59    void setScalarField(llvm::Value *instance, const std::string & fieldName, llvm::Value * value) const;
60
61    void setScalarField(llvm::Value * instance, llvm::Value * index, llvm::Value * value) const;
62
63    // Synchronization actions for executing a kernel for a particular logical segment.
64    //
65    // Before the segment is processed, acquireLogicalSegmentNo must be used to load
66    // the segment number of the kernel state to ensure that the previous segment is
67    // complete (by checking that the acquired segment number is equal to the desired segment
68    // number).
69    // After all segment processing actions for the kernel are complete, and any necessary
70    // data has been extracted from the kernel for further pipeline processing, the
71    // segment number must be incremented and stored using releaseLogicalSegmentNo.
72    llvm::LoadInst * acquireLogicalSegmentNo(llvm::Value * instance) const;
73
74    void releaseLogicalSegmentNo(llvm::Value * instance, llvm::Value * newFieldVal) const;
75
76    // Get a parameter by name.
77    llvm::Argument * getParameter(llvm::Function * f, const std::string & name) const;
78
[5260]79    inline llvm::IntegerType * getSizeTy() const {
[5217]80        return getBuilder()->getSizeTy();
81    }
[5202]82
[5260]83    inline llvm::Type * getStreamTy(const unsigned FieldWidth = 1) {
[5229]84        return getBuilder()->getStreamTy(FieldWidth);
85    }
86   
[5260]87    inline llvm::Type * getStreamSetTy(const unsigned NumElements = 1, const unsigned FieldWidth = 1) {
[5217]88        return getBuilder()->getStreamSetTy(NumElements, FieldWidth);
89    }
[5224]90   
[5246]91    virtual ~KernelBuilder() = 0;
[5251]92   
[5260]93    const std::vector<const parabix::StreamSetBuffer *> & getStreamSetInputBuffers() const { return mStreamSetInputBuffers; }
[5246]94
[5260]95    const std::vector<const parabix::StreamSetBuffer *> & getStreamSetOutputBuffers() const { return mStreamSetOutputBuffers; }
[5251]96
[5287]97    llvm::Value * createDoSegmentCall(const std::vector<llvm::Value *> & args) const;
98
99    llvm::Value * createGetAccumulatorCall(llvm::Value * self, const std::string & accumName) const;
100
[5097]101protected:
[5246]102
103    // Constructor
104    KernelBuilder(IDISA::IDISA_Builder * builder,
[5267]105                    std::string && kernelName,
[5283]106                    std::vector<Binding> && stream_inputs,
107                    std::vector<Binding> && stream_outputs,
108                    std::vector<Binding> && scalar_parameters,
109                    std::vector<Binding> && scalar_outputs,
110                    std::vector<Binding> && internal_scalars);
[5246]111
[5063]112    //
[5074]113    // Kernel builder subtypes define their logic of kernel construction
114    // in terms of 3 virtual methods for
115    // (a) preparing the Kernel state data structure
116    // (b) defining the logic of the doBlock function, and
117    // (c) defining the logic of the finalBlock function.
118    //
119    // Note: the kernel state data structure must only be finalized after
120    // all scalar fields have been added.   If there are no fields to
121    // be added, the default method for preparing kernel state may be used.
[5051]122   
[5283]123    void setNoTerminateAttribute(const bool noTerminate = true) {
124        mNoTerminateAttribute = noTerminate;
125    }
126
[5299]127    void prepareKernelSignature();
128
[5076]129    virtual void prepareKernel();
[5299]130
[5292]131    virtual void generateInitMethod() { }
[5250]132   
[5292]133    virtual void generateDoSegmentMethod(llvm::Value * doFinal, const std::vector<llvm::Value *> & producerPos) = 0;
134
[5074]135    // Add an additional scalar field to the KernelState struct.
136    // Must occur before any call to addKernelDeclarations or createKernelModule.
[5260]137    unsigned addScalar(llvm::Type * type, const std::string & name);
[5227]138
[5283]139    unsigned addUnnamedScalar(llvm::Type * type);
140
[5227]141    unsigned getScalarCount() const;
142
[5063]143    // Run-time access of Kernel State and parameters of methods for
144    // use in implementing kernels.
145   
146    // Get the index of a named scalar field within the kernel state struct.
[5260]147    llvm::ConstantInt * getScalarIndex(const std::string & name) const;
[5292]148
[5063]149    // Get the value of a scalar field for a given instance.
[5292]150    llvm::Value * getScalarField(const std::string & fieldName) const {
151        return getScalarField(getSelf(), fieldName);
152    }
[5202]153
[5292]154    llvm::Value * getScalarField(llvm::Value * index) const {
155        return getScalarField(getSelf(), index);
156    }
[5283]157
[5063]158    // Set the value of a scalar field for a given instance.
[5292]159    void setScalarField(const std::string & fieldName, llvm::Value * value) const {
160        return setScalarField(getSelf(), fieldName, value);
161    }
[5283]162
[5292]163    void setScalarField(llvm::Value * index, llvm::Value * value) const {
164        return setScalarField(getSelf(), index, value);
165    }
[5283]166
[5317]167    llvm::Value * getInputStreamBlockPtr(const std::string & name, llvm::Value * streamIndex) const;
[5329]168
[5317]169    llvm::Value * loadInputStreamBlock(const std::string & name, llvm::Value * streamIndex) const;
170   
171    llvm::Value * getInputStreamPackPtr(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex) const;
172   
173    llvm::Value * loadInputStreamPack(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex) const;
174   
[5329]175    llvm::Value * getInputStreamSetCount(const std::string & name) const;
176
[5317]177    llvm::Value * getOutputStreamBlockPtr(const std::string & name, llvm::Value * streamIndex) const;
178   
179    void storeOutputStreamBlock(const std::string & name, llvm::Value * streamIndex, llvm::Value * toStore) const;
180   
181    llvm::Value * getOutputStreamPackPtr(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex) const;
182   
183    void storeOutputStreamPack(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex, llvm::Value * toStore) const;
[5329]184
185    llvm::Value * getOutputStreamSetCount(const std::string & name) const;
186
[5317]187    llvm::Value * getAdjustedInputStreamBlockPtr(llvm::Value * blockAdjustment, const std::string & name, llvm::Value * streamIndex) const;
[5246]188
[5310]189    llvm::Value * getRawInputPointer(const std::string & name, llvm::Value * streamIndex, llvm::Value * absolutePosition) const;
[5260]190
[5310]191    llvm::Value * getRawOutputPointer(const std::string & name, llvm::Value * streamIndex, llvm::Value * absolutePosition) const;
192
[5292]193    llvm::Value * getScalarFieldPtr(const std::string & name) const {
194        return getScalarFieldPtr(getSelf(), name);
195    }
[5109]196
[5292]197    llvm::Value * getScalarFieldPtr(llvm::Value * index) const {
198        return getScalarFieldPtr(getSelf(), index);
199    }
[5283]200
[5292]201    inline llvm::Value * getProcessedItemCount(const std::string & name) const {
202        return getProcessedItemCount(getSelf(), name);
203    }
[5246]204
[5292]205    inline void setProcessedItemCount(const std::string & name, llvm::Value * value) const {
206        setProcessedItemCount(getSelf(), name, value);
207    }
[5246]208
[5292]209    inline llvm::Value * getProducedItemCount(const std::string & name) const {
210        return getProducedItemCount(getSelf(), name);
211    }
[5260]212
[5292]213    inline void setProducedItemCount(const std::string & name, llvm::Value * value) const {
214        setProducedItemCount(getSelf(), name, value);
215    }
216
217    llvm::Value * getTerminationSignal() const {
218        return getTerminationSignal(getSelf());
219    }
220
221    void setTerminationSignal() const {
222        return setTerminationSignal(getSelf());
223    }
224
225
226    llvm::Value * getSelf() const {
227        return mSelf;
228    }
229
230    llvm::BasicBlock * CreateBasicBlock(std::string && name) const;
231
[5297]232    // Stream set helpers.
233
234    llvm::Value * getStreamSetBufferPtr(const std::string & name) const;
235
236    llvm::Value * getScalarFieldPtr(llvm::Value * instance, const std::string & name) const;
237
238    llvm::Value * getScalarFieldPtr(llvm::Value * instance, llvm::Value * index) const;
239
[5292]240    unsigned getStreamSetIndex(const std::string & name) const;
[5286]241
[5310]242    const parabix::StreamSetBuffer * getInputStreamSetBuffer(const std::string & name) const {
243        return mStreamSetInputBuffers[getStreamSetIndex(name)];
244    }
[5286]245
[5310]246    const parabix::StreamSetBuffer * getOutputStreamSetBuffer(const std::string & name) const {
247        return mStreamSetOutputBuffers[getStreamSetIndex(name)];
248    }
249
[5292]250    void callGenerateInitMethod();
251
252    void callGenerateDoSegmentMethod();
253
[5307]254private:
255
256    llvm::Value * computeBlockIndex(const std::vector<Binding> & binding, const std::string & name, llvm::Value * itemCount) const;
257
[5097]258protected:
[4959]259
[5292]260    llvm::Value *                                   mSelf;
[5347]261    llvm::Function *                                mCurrentMethod;
[5292]262
[5260]263    std::vector<llvm::Type *>                       mKernelFields;
264    NameMap                                         mKernelMap;
265    NameMap                                         mStreamSetNameMap;
266    std::vector<const parabix::StreamSetBuffer *>   mStreamSetInputBuffers;
267    std::vector<const parabix::StreamSetBuffer *>   mStreamSetOutputBuffers;
268    bool                                            mNoTerminateAttribute;
[5133]269
[4924]270};
[5283]271
[5287]272class SegmentOrientedKernel : public KernelBuilder {
273protected:
274
275    SegmentOrientedKernel(IDISA::IDISA_Builder * builder,
276                          std::string && kernelName,
277                          std::vector<Binding> && stream_inputs,
278                          std::vector<Binding> && stream_outputs,
279                          std::vector<Binding> && scalar_parameters,
280                          std::vector<Binding> && scalar_outputs,
281                          std::vector<Binding> && internal_scalars);
282
283    virtual ~SegmentOrientedKernel() { }
284
285};
286
[5283]287class BlockOrientedKernel : public KernelBuilder {
288protected:
289
[5347]290    void CreateDoBlockMethodCall();
[5292]291
[5283]292    // Each kernel builder subtype must provide its own logic for generating
293    // doBlock calls.
[5297]294    virtual void generateDoBlockMethod() = 0;
[5283]295
296    // Each kernel builder subtypre must also specify the logic for processing the
297    // final block of stream data, if there is any special processing required
298    // beyond simply calling the doBlock function.   In the case that the final block
299    // processing may be trivially implemented by dispatching to the doBlock method
300    // without additional preparation, the default generateFinalBlockMethod need
301    // not be overridden.
302
[5347]303    virtual void generateFinalBlockMethod(llvm::Value * remainingItems);
[5283]304
[5347]305    void generateDoSegmentMethod(llvm::Value * doFinal, const std::vector<llvm::Value *> & producerPos) override final;
[5283]306
307    BlockOrientedKernel(IDISA::IDISA_Builder * builder,
308                        std::string && kernelName,
309                        std::vector<Binding> && stream_inputs,
310                        std::vector<Binding> && stream_outputs,
311                        std::vector<Binding> && scalar_parameters,
312                        std::vector<Binding> && scalar_outputs,
313                        std::vector<Binding> && internal_scalars);
314
315    virtual ~BlockOrientedKernel() { }
[5285]316
[5350]317private:
[5307]318
[5350]319    bool useIndirectBr() const {
320        return iBuilder->supportsIndirectBr();
[5347]321    }
[5307]322
[5347]323    void writeDoBlockMethod();
[5292]324
[5350]325    void writeFinalBlockMethod(llvm::Value * remainingItems);
[5347]326
327private:
328
[5350]329    llvm::Function *        mDoBlockMethod;
330    llvm::BasicBlock *      mStrideLoopBody;
331    llvm::IndirectBrInst *  mStrideLoopBranch;
[5351]332    llvm::PHINode *         mStrideLoopTarget;
[5283]333};
334
[5285]335
[4959]336}
[5063]337#endif
Note: See TracBrowser for help on using the repository browser.