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

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

Continuation of work to simplify Kernel writing

File size: 10.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>
14namespace llvm { class ConstantInt; }
15namespace llvm { class Function; }
16namespace llvm { class IntegerType; }
17namespace llvm { class LoadInst; }
18namespace llvm { class Type; }
19namespace llvm { class Value; }
20namespace parabix { class StreamSetBuffer; }
21
22const std::string logicalSegmentNoScalar = "logicalSegNo";
23const std::string processedItemCountSuffix = "_processedItemCount";
24const std::string producedItemCountSuffix = "_producedItemCount";
25const std::string terminationSignal = "terminationSignal";
26const std::string bufferPtrSuffix = "_bufferPtr";
27const std::string blkMaskSuffix = "_blkMask";
28
29namespace kernel {
30   
31class KernelBuilder : public KernelInterface {
32    using NameMap = boost::container::flat_map<std::string, unsigned>;
33public:
34   
35    // Create a module for the kernel, including the kernel state type declaration and
36    // the full implementation of all required methods.     
37    //
38    std::unique_ptr<llvm::Module> createKernelModule(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
39   
40    // Generate the Kernel to the current module (iBuilder->getModule()).
41    void generateKernel(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
42   
43    void createInstance() override;
44
45    llvm::Value * getBlockNo(llvm::Value * self) const;
46
47    virtual llvm::Value * getProcessedItemCount(llvm::Value * self, const std::string & ssName) const override;
48
49    virtual llvm::Value * getProducedItemCount(llvm::Value * self, const std::string & ssName) const override;
50   
51    bool hasNoTerminateAttribute() { return mNoTerminateAttribute;}
52   
53    llvm::Value * getTerminationSignal(llvm::Value * self) const override;
54   
55    inline llvm::IntegerType * getSizeTy() const {
56        return getBuilder()->getSizeTy();
57    }
58
59    inline llvm::Type * getStreamTy(const unsigned FieldWidth = 1) {
60        return getBuilder()->getStreamTy(FieldWidth);
61    }
62   
63    inline llvm::Type * getStreamSetTy(const unsigned NumElements = 1, const unsigned FieldWidth = 1) {
64        return getBuilder()->getStreamSetTy(NumElements, FieldWidth);
65    }
66   
67    // Synchronization actions for executing a kernel for a particular logical segment.
68    //
69    // Before the segment is processed, acquireLogicalSegmentNo must be used to load
70    // the segment number of the kernel state to ensure that the previous segment is
71    // complete (by checking that the acquired segment number is equal to the desired segment
72    // number).
73    // After all segment processing actions for the kernel are complete, and any necessary
74    // data has been extracted from the kernel for further pipeline processing, the
75    // segment number must be incremented and stored using releaseLogicalSegmentNo.
76    llvm::LoadInst * acquireLogicalSegmentNo(llvm::Value * self) const;
77
78    void releaseLogicalSegmentNo(llvm::Value * self, llvm::Value * newFieldVal) const;
79
80    virtual ~KernelBuilder() = 0;
81   
82    const std::vector<const parabix::StreamSetBuffer *> & getStreamSetInputBuffers() const { return mStreamSetInputBuffers; }
83
84    const std::vector<const parabix::StreamSetBuffer *> & getStreamSetOutputBuffers() const { return mStreamSetOutputBuffers; }
85
86    void setTerminationSignal(llvm::Value * self) const;
87
88protected:
89
90    // Constructor
91    KernelBuilder(IDISA::IDISA_Builder * builder,
92                    std::string && kernelName,
93                    std::vector<Binding> && stream_inputs,
94                    std::vector<Binding> && stream_outputs,
95                    std::vector<Binding> && scalar_parameters,
96                    std::vector<Binding> && scalar_outputs,
97                    std::vector<Binding> && internal_scalars);
98
99    //
100    // Kernel builder subtypes define their logic of kernel construction
101    // in terms of 3 virtual methods for
102    // (a) preparing the Kernel state data structure
103    // (b) defining the logic of the doBlock function, and
104    // (c) defining the logic of the finalBlock function.
105    //
106    // Note: the kernel state data structure must only be finalized after
107    // all scalar fields have been added.   If there are no fields to
108    // be added, the default method for preparing kernel state may be used.
109   
110    void setNoTerminateAttribute(const bool noTerminate = true) {
111        mNoTerminateAttribute = noTerminate;
112    }
113
114    void setDoBlockUpdatesProducedItemCountsAttribute(const bool doesUpdate = true) {
115        mDoBlockUpdatesProducedItemCountsAttribute = doesUpdate;
116    }
117   
118    virtual void prepareKernel();
119       
120    virtual void generateInitMethod(llvm::Function * initFunction, llvm::Value * self) const;
121   
122    virtual void generateDoSegmentMethod(llvm::Function * function, llvm::Value * self, llvm::Value * doFinal, const std::vector<llvm::Value *> & producerPos) const = 0;
123   
124    // Add an additional scalar field to the KernelState struct.
125    // Must occur before any call to addKernelDeclarations or createKernelModule.
126    unsigned addScalar(llvm::Type * type, const std::string & name);
127
128    unsigned addUnnamedScalar(llvm::Type * type);
129
130    unsigned getScalarCount() const;
131
132    // Run-time access of Kernel State and parameters of methods for
133    // use in implementing kernels.
134   
135    // Get the index of a named scalar field within the kernel state struct.
136    llvm::ConstantInt * getScalarIndex(const std::string & name) const;
137   
138    // Get the value of a scalar field for a given instance.
139    llvm::Value * getScalarField(llvm::Value * self, const std::string & fieldName) const;
140
141    llvm::Value * getScalarField(llvm::Value * self, llvm::Value * index) const;
142
143    // Set the value of a scalar field for a given instance.
144    void setScalarField(llvm::Value * self, const std::string & fieldName, llvm::Value * value) const;
145
146    void setScalarField(llvm::Value * self, llvm::Value * index, llvm::Value * value) const;
147
148    // Get a parameter by name.
149    llvm::Argument * getParameter(llvm::Function * f, const std::string & name) const;
150
151    llvm::Value * getStream(llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
152
153    llvm::Value * getStream(llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index1, llvm::Value * index2) const;
154
155    llvm::Value * getStreamView(llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
156
157    llvm::Value * getStreamView(llvm::Type * type, llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
158
159    // Stream set helpers.
160    unsigned getStreamSetIndex(const std::string & name) const;
161   
162    llvm::Value * getScalarFieldPtr(llvm::Value * self, const std::string & name) const;
163
164    llvm::Value * getScalarFieldPtr(llvm::Value * self, llvm::Value * index) const;
165
166    llvm::Value * getStreamSetBufferPtr(llvm::Value * self, const std::string & name) const;
167
168    llvm::Value * getStreamSetBufferPtr(llvm::Value * self, llvm::Value * index) const;
169
170    llvm::Value * getStreamSetPtr(llvm::Value * self, const std::string & name, llvm::Value * blockNo) const;
171   
172    void setBlockNo(llvm::Value * self, llvm::Value * value) const;
173
174    virtual void setProcessedItemCount(llvm::Value * self, const std::string & name, llvm::Value * value) const;
175
176    virtual void setProducedItemCount(llvm::Value * self, const std::string & name, llvm::Value * value) const;
177
178    const parabix::StreamSetBuffer * getStreamSetBuffer(const std::string & name) const;
179
180private:
181
182    void callGenerateInitMethod() const;
183
184    void callGenerateDoSegmentMethod() const;
185
186protected:
187
188    std::vector<llvm::Type *>                       mKernelFields;
189    NameMap                                         mKernelMap;
190    NameMap                                         mStreamSetNameMap;
191    std::vector<const parabix::StreamSetBuffer *>   mStreamSetInputBuffers;
192    std::vector<const parabix::StreamSetBuffer *>   mStreamSetOutputBuffers;
193    bool                                            mNoTerminateAttribute;
194    bool                                            mDoBlockUpdatesProducedItemCountsAttribute;
195
196};
197
198class BlockOrientedKernel : public KernelBuilder {
199protected:
200
201    // Each kernel builder subtype must provide its own logic for generating
202    // doBlock calls.
203    virtual void generateDoBlockMethod(llvm::Function * function, llvm::Value * self, llvm::Value * blockNo) const = 0;
204
205    // Each kernel builder subtypre must also specify the logic for processing the
206    // final block of stream data, if there is any special processing required
207    // beyond simply calling the doBlock function.   In the case that the final block
208    // processing may be trivially implemented by dispatching to the doBlock method
209    // without additional preparation, the default generateFinalBlockMethod need
210    // not be overridden.
211
212    virtual void generateFinalBlockMethod(llvm::Function * function, llvm::Value * self, llvm::Value * remainingBytes, llvm::Value * blockNo) const;
213
214    virtual void generateDoSegmentMethod(llvm::Function * function, llvm::Value * self, llvm::Value * doFinal, const std::vector<llvm::Value *> & producerPos) const final;
215
216    BlockOrientedKernel(IDISA::IDISA_Builder * builder,
217                        std::string && kernelName,
218                        std::vector<Binding> && stream_inputs,
219                        std::vector<Binding> && stream_outputs,
220                        std::vector<Binding> && scalar_parameters,
221                        std::vector<Binding> && scalar_outputs,
222                        std::vector<Binding> && internal_scalars);
223
224    virtual ~BlockOrientedKernel() { }
225
226    llvm::Function * getDoBlockFunction() const;
227
228    llvm::Function * getDoFinalBlockFunction() const;
229
230private:
231    void callGenerateDoBlockMethod() const;
232
233    void callGenerateDoFinalBlockMethod() const;
234};
235
236class SegmentOrientedKernel : public KernelBuilder {
237protected:
238
239    SegmentOrientedKernel(IDISA::IDISA_Builder * builder,
240                          std::string && kernelName,
241                          std::vector<Binding> && stream_inputs,
242                          std::vector<Binding> && stream_outputs,
243                          std::vector<Binding> && scalar_parameters,
244                          std::vector<Binding> && scalar_outputs,
245                          std::vector<Binding> && internal_scalars);
246
247    virtual ~SegmentOrientedKernel() { }
248};
249
250
251
252}
253#endif
Note: See TracBrowser for help on using the repository browser.