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

Last change on this file since 5370 was 5370, checked in by xuedongx, 2 years ago

Add1 processing rate; pablo Count only up to EOFbit

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