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

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

Ability to set input/output signatures for Pablo functions in the constructor

File size: 12.7 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 { class IntegerType; }
18namespace llvm { class LoadInst; }
19namespace llvm { class Type; }
20namespace llvm { class Value; }
21namespace parabix { class StreamSetBuffer; }
22
23namespace kernel {
24   
25class KernelBuilder : public KernelInterface {
26    using NameMap = boost::container::flat_map<std::string, unsigned>;
27public:
28   
29    // Create a module for the kernel, including the kernel state type declaration and
30    // the full implementation of all required methods.     
31    //
32    std::unique_ptr<llvm::Module> createKernelModule(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
33   
34    // Generate the Kernel to the current module (iBuilder->getModule()).
35    void generateKernel(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
36   
37    void createInstance() override;
38
39    virtual llvm::Value * getProcessedItemCount(llvm::Value * instance, const std::string & name) const override;
40
41    virtual void setProcessedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const;
42
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
47    bool hasNoTerminateAttribute() { return mNoTerminateAttribute;}
48   
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
79    inline llvm::IntegerType * getSizeTy() const {
80        return getBuilder()->getSizeTy();
81    }
82
83    inline llvm::Type * getStreamTy(const unsigned FieldWidth = 1) {
84        return getBuilder()->getStreamTy(FieldWidth);
85    }
86   
87    inline llvm::Type * getStreamSetTy(const unsigned NumElements = 1, const unsigned FieldWidth = 1) {
88        return getBuilder()->getStreamSetTy(NumElements, FieldWidth);
89    }
90   
91    virtual ~KernelBuilder() = 0;
92   
93    const std::vector<const parabix::StreamSetBuffer *> & getStreamSetInputBuffers() const { return mStreamSetInputBuffers; }
94
95    const std::vector<const parabix::StreamSetBuffer *> & getStreamSetOutputBuffers() const { return mStreamSetOutputBuffers; }
96
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
101protected:
102
103    // Constructor
104    KernelBuilder(IDISA::IDISA_Builder * builder,
105                    std::string && kernelName,
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);
111
112    //
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.
122   
123    void setNoTerminateAttribute(const bool noTerminate = true) {
124        mNoTerminateAttribute = noTerminate;
125    }
126
127    void setDoBlockUpdatesProducedItemCountsAttribute(const bool doesUpdate = true) {
128        mDoBlockUpdatesProducedItemCountsAttribute = doesUpdate;
129    }
130   
131    void prepareKernelSignature();
132
133    virtual void prepareKernel();
134
135    virtual void generateInitMethod() { }
136   
137    virtual void generateDoSegmentMethod(llvm::Value * doFinal, const std::vector<llvm::Value *> & producerPos) = 0;
138
139    virtual void generateInternalMethods() { }
140
141    // Add an additional scalar field to the KernelState struct.
142    // Must occur before any call to addKernelDeclarations or createKernelModule.
143    unsigned addScalar(llvm::Type * type, const std::string & name);
144
145    unsigned addUnnamedScalar(llvm::Type * type);
146
147    unsigned getScalarCount() const;
148
149    // Run-time access of Kernel State and parameters of methods for
150    // use in implementing kernels.
151   
152    // Get the index of a named scalar field within the kernel state struct.
153    llvm::ConstantInt * getScalarIndex(const std::string & name) const;
154
155    llvm::Value * getBlockNo() const;
156
157    void setBlockNo(llvm::Value * value) const;
158   
159    // Get the value of a scalar field for a given instance.
160    llvm::Value * getScalarField(const std::string & fieldName) const {
161        return getScalarField(getSelf(), fieldName);
162    }
163
164    llvm::Value * getScalarField(llvm::Value * index) const {
165        return getScalarField(getSelf(), index);
166    }
167
168    // Set the value of a scalar field for a given instance.
169    void setScalarField(const std::string & fieldName, llvm::Value * value) const {
170        return setScalarField(getSelf(), fieldName, value);
171    }
172
173    void setScalarField(llvm::Value * index, llvm::Value * value) const {
174        return setScalarField(getSelf(), index, value);
175    }
176
177    llvm::Value * getInputStream(const std::string & name, llvm::Value * index) const;
178
179    llvm::Value * getInputStream(const std::string & name, llvm::Value * index1, llvm::Value * index2) const;
180
181    llvm::Value * getOutputStream(const std::string & name, llvm::Value * index) const;
182
183    llvm::Value * getOutputStream(const std::string & name, llvm::Value * index1, llvm::Value * index2) const;
184
185    llvm::Value * getStreamView(llvm::Type * type, const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
186
187
188    llvm::Value * getScalarFieldPtr(const std::string & name) const {
189        return getScalarFieldPtr(getSelf(), name);
190    }
191
192    llvm::Value * getScalarFieldPtr(llvm::Value * index) const {
193        return getScalarFieldPtr(getSelf(), index);
194    }
195
196    llvm::Value * getStreamSetPtr(const std::string & name, llvm::Value * blockNo) const;
197
198    inline llvm::Value * getProcessedItemCount(const std::string & name) const {
199        return getProcessedItemCount(getSelf(), name);
200    }
201
202    inline void setProcessedItemCount(const std::string & name, llvm::Value * value) const {
203        setProcessedItemCount(getSelf(), name, value);
204    }
205
206    inline llvm::Value * getProducedItemCount(const std::string & name) const {
207        return getProducedItemCount(getSelf(), name);
208    }
209
210    inline void setProducedItemCount(const std::string & name, llvm::Value * value) const {
211        setProducedItemCount(getSelf(), name, value);
212    }
213
214    llvm::Value * getTerminationSignal() const {
215        return getTerminationSignal(getSelf());
216    }
217
218    void setTerminationSignal() const {
219        return setTerminationSignal(getSelf());
220    }
221
222
223    llvm::Value * getSelf() const {
224        return mSelf;
225    }
226
227    llvm::BasicBlock * CreateBasicBlock(std::string && name) const;
228
229    // Stream set helpers.
230//    llvm::Value * getStream(const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
231
232//    llvm::Value * getStream(const std::string & name, llvm::Value * blockNo, llvm::Value * index1, llvm::Value * index2) const;
233
234    llvm::Value * getStreamSetBufferPtr(const std::string & name) const;
235
236    llvm::Value * getStreamSetBufferPtr(llvm::Value * index) const;
237
238    llvm::Value * getScalarFieldPtr(llvm::Value * instance, const std::string & name) const;
239
240    llvm::Value * getScalarFieldPtr(llvm::Value * instance, llvm::Value * index) const;
241
242    unsigned getStreamSetIndex(const std::string & name) const;
243
244    const parabix::StreamSetBuffer * getStreamSetBuffer(const std::string & name) const;
245
246    void callGenerateInitMethod();
247
248    void callGenerateDoSegmentMethod();
249
250protected:
251
252    llvm::Value *                                   mSelf;
253    llvm::Function *                                mCurrentFunction;
254
255    std::vector<llvm::Type *>                       mKernelFields;
256    NameMap                                         mKernelMap;
257    NameMap                                         mStreamSetNameMap;
258    std::vector<const parabix::StreamSetBuffer *>   mStreamSetInputBuffers;
259    std::vector<const parabix::StreamSetBuffer *>   mStreamSetOutputBuffers;
260    bool                                            mNoTerminateAttribute;
261    bool                                            mDoBlockUpdatesProducedItemCountsAttribute;
262
263};
264
265class SegmentOrientedKernel : public KernelBuilder {
266protected:
267
268    SegmentOrientedKernel(IDISA::IDISA_Builder * builder,
269                          std::string && kernelName,
270                          std::vector<Binding> && stream_inputs,
271                          std::vector<Binding> && stream_outputs,
272                          std::vector<Binding> && scalar_parameters,
273                          std::vector<Binding> && scalar_outputs,
274                          std::vector<Binding> && internal_scalars);
275
276    virtual ~SegmentOrientedKernel() { }
277
278};
279
280class BlockOrientedKernel : public KernelBuilder {
281public:
282
283    llvm::CallInst * CreateDoBlockMethodCall() const;
284
285    llvm::CallInst * CreateDoFinalBlockMethodCall(llvm::Value * remainingItems) const;
286
287protected:
288
289    virtual void addAdditionalKernelDeclarations(llvm::Module * module, llvm::PointerType * selfType);
290
291    // Each kernel builder subtype must provide its own logic for generating
292    // doBlock calls.
293    virtual void generateDoBlockMethod() = 0;
294
295    // Each kernel builder subtypre must also specify the logic for processing the
296    // final block of stream data, if there is any special processing required
297    // beyond simply calling the doBlock function.   In the case that the final block
298    // processing may be trivially implemented by dispatching to the doBlock method
299    // without additional preparation, the default generateFinalBlockMethod need
300    // not be overridden.
301
302    virtual void generateFinalBlockMethod(llvm::Value * remainingBytes);
303
304    virtual void generateDoSegmentMethod(llvm::Value * doFinal, const std::vector<llvm::Value *> & producerPos) final;
305
306    void generateInternalMethods() override final;
307
308    BlockOrientedKernel(IDISA::IDISA_Builder * builder,
309                        std::string && kernelName,
310                        std::vector<Binding> && stream_inputs,
311                        std::vector<Binding> && stream_outputs,
312                        std::vector<Binding> && scalar_parameters,
313                        std::vector<Binding> && scalar_outputs,
314                        std::vector<Binding> && internal_scalars);
315
316    virtual ~BlockOrientedKernel() { }
317
318    llvm::Function * getDoBlockFunction() const;
319
320    llvm::Function * getDoFinalBlockFunction() const;
321
322private:
323
324    void callGenerateDoBlockMethod();
325
326    void callGenerateDoFinalBlockMethod();
327
328};
329
330
331}
332#endif
Note: See TracBrowser for help on using the repository browser.