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

Last change on this file since 5307 was 5307, checked in by nmedfort, 3 years ago

Continued work on eliminating BlockNo?

File size: 13.1 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 * streamIndex) const;
178
179    llvm::Value * getInputStream(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex) const;
180
181    llvm::Value * getOutputStream(const std::string & name, llvm::Value * streamIndex) const;
182
183    llvm::Value * getOutputStream(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex) const;
184
185    llvm::Value * getRawItemPointer(const std::string & name, llvm::Value * streamIndex, llvm::Value * absolutePosition) const;
186
187    llvm::Value * getScalarFieldPtr(const std::string & name) const {
188        return getScalarFieldPtr(getSelf(), name);
189    }
190
191    llvm::Value * getScalarFieldPtr(llvm::Value * index) const {
192        return getScalarFieldPtr(getSelf(), index);
193    }
194
195    llvm::Value * getStreamSetPtr(const std::string & name, llvm::Value * blockNo) const;
196
197    inline llvm::Value * getProcessedItemCount(const std::string & name) const {
198        return getProcessedItemCount(getSelf(), name);
199    }
200
201    inline void setProcessedItemCount(const std::string & name, llvm::Value * value) const {
202        setProcessedItemCount(getSelf(), name, value);
203    }
204
205    inline llvm::Value * getProducedItemCount(const std::string & name) const {
206        return getProducedItemCount(getSelf(), name);
207    }
208
209    inline void setProducedItemCount(const std::string & name, llvm::Value * value) const {
210        setProducedItemCount(getSelf(), name, value);
211    }
212
213    llvm::Value * getTerminationSignal() const {
214        return getTerminationSignal(getSelf());
215    }
216
217    void setTerminationSignal() const {
218        return setTerminationSignal(getSelf());
219    }
220
221
222    llvm::Value * getSelf() const {
223        return mSelf;
224    }
225
226    llvm::BasicBlock * CreateBasicBlock(std::string && name) const;
227
228    // Stream set helpers.
229//    llvm::Value * getStream(const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
230
231//    llvm::Value * getStream(const std::string & name, llvm::Value * blockNo, llvm::Value * index1, llvm::Value * index2) const;
232
233    llvm::Value * getStreamSetBufferPtr(const std::string & name) const;
234
235//    llvm::Value * getStreamSetBufferPtr(llvm::Value * index) const;
236
237    llvm::Value * getScalarFieldPtr(llvm::Value * instance, const std::string & name) const;
238
239    llvm::Value * getScalarFieldPtr(llvm::Value * instance, llvm::Value * index) const;
240
241    unsigned getStreamSetIndex(const std::string & name) const;
242
243    const parabix::StreamSetBuffer * getStreamSetBuffer(const std::string & name) const;
244
245    void callGenerateInitMethod();
246
247    void callGenerateDoSegmentMethod();
248
249private:
250
251    llvm::Value * computeBlockIndex(const std::vector<Binding> & binding, const std::string & name, llvm::Value * itemCount) const;
252
253protected:
254
255    llvm::Value *                                   mSelf;
256    llvm::Function *                                mCurrentFunction;
257
258    std::vector<llvm::Type *>                       mKernelFields;
259    NameMap                                         mKernelMap;
260    NameMap                                         mStreamSetNameMap;
261    std::vector<const parabix::StreamSetBuffer *>   mStreamSetInputBuffers;
262    std::vector<const parabix::StreamSetBuffer *>   mStreamSetOutputBuffers;
263    bool                                            mNoTerminateAttribute;
264    bool                                            mDoBlockUpdatesProducedItemCountsAttribute;
265
266};
267
268class SegmentOrientedKernel : public KernelBuilder {
269protected:
270
271    SegmentOrientedKernel(IDISA::IDISA_Builder * builder,
272                          std::string && kernelName,
273                          std::vector<Binding> && stream_inputs,
274                          std::vector<Binding> && stream_outputs,
275                          std::vector<Binding> && scalar_parameters,
276                          std::vector<Binding> && scalar_outputs,
277                          std::vector<Binding> && internal_scalars);
278
279    virtual ~SegmentOrientedKernel() { }
280
281};
282
283class BlockOrientedKernel : public KernelBuilder {
284public:
285
286    llvm::CallInst * CreateDoBlockMethodCall() const;
287
288    llvm::CallInst * CreateDoFinalBlockMethodCall(llvm::Value * remainingItems) const;
289
290protected:
291
292    virtual void addAdditionalKernelDeclarations(llvm::Module * module, llvm::PointerType * selfType);
293
294    // Each kernel builder subtype must provide its own logic for generating
295    // doBlock calls.
296    virtual void generateDoBlockMethod() = 0;
297
298    // Each kernel builder subtypre must also specify the logic for processing the
299    // final block of stream data, if there is any special processing required
300    // beyond simply calling the doBlock function.   In the case that the final block
301    // processing may be trivially implemented by dispatching to the doBlock method
302    // without additional preparation, the default generateFinalBlockMethod need
303    // not be overridden.
304
305    virtual void generateFinalBlockMethod(llvm::Value * remainingBytes);
306
307    virtual void generateDoSegmentMethod(llvm::Value * doFinal, const std::vector<llvm::Value *> & producerPos) final;
308
309    void generateInternalMethods() override final;
310
311    BlockOrientedKernel(IDISA::IDISA_Builder * builder,
312                        std::string && kernelName,
313                        std::vector<Binding> && stream_inputs,
314                        std::vector<Binding> && stream_outputs,
315                        std::vector<Binding> && scalar_parameters,
316                        std::vector<Binding> && scalar_outputs,
317                        std::vector<Binding> && internal_scalars);
318
319    virtual ~BlockOrientedKernel() { }
320
321    llvm::Value * loadBlock(const std::string & inputName, llvm::Value * const streamIndex) const;
322
323    llvm::Value * loadPack(const std::string & inputName, llvm::Value * const streamIndex, llvm::Value * const packIndex) const;
324
325    llvm::Function * getDoBlockFunction() const;
326
327    llvm::Function * getDoFinalBlockFunction() const;
328
329private:
330
331    void callGenerateDoBlockMethod();
332
333    void callGenerateDoFinalBlockMethod();
334
335};
336
337
338}
339#endif
Note: See TracBrowser for help on using the repository browser.