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

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

Added enable asserts (-ea) command line flag + restructured BlockOrientedKernels? to allow for inlined code.

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