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

Last change on this file since 5261 was 5261, checked in by cameron, 11 months ago

Move responsibility for ProducedItemCount? into doSegment unless overridden

File size: 8.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 blockNoScalar = "blockNo";
23const std::string logicalSegmentNoScalar = "logicalSegNo";
24const std::string processedItemCountSuffix = "_processedItemCount";
25const std::string producedItemCountSuffix = "_producedItemCount";
26const std::string terminationSignal = "terminationSignal";
27const std::string structPtrSuffix = "_structPtr";
28const std::string blkMaskSuffix = "_blkMask";
29
30namespace kernel {
31   
32class KernelBuilder : public KernelInterface {
33    using NameMap = boost::container::flat_map<std::string, unsigned>;
34public:
35   
36    // Create a module for the kernel, including the kernel state type declaration and
37    // the full implementation of all required methods.     
38    //
39    std::unique_ptr<llvm::Module> createKernelModule(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
40   
41    // Generate the Kernel to the current module (iBuilder->getModule()).
42    void generateKernel(const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
43   
44    void createInstance() override;
45
46    llvm::Function * generateThreadFunction(const std::string & name) const;
47
48    llvm::Value * getBlockNo(llvm::Value * self) const;
49    virtual llvm::Value * getProcessedItemCount(llvm::Value * self, const std::string & ssName) const override;
50    virtual llvm::Value * getProducedItemCount(llvm::Value * self, const std::string & ssName) const override;
51   
52    bool hasNoTerminateAttribute() { return mNoTerminateAttribute;}
53   
54    llvm::Value * getTerminationSignal(llvm::Value * self) const override;
55   
56    inline llvm::IntegerType * getSizeTy() const {
57        return getBuilder()->getSizeTy();
58    }
59
60    inline llvm::Type * getStreamTy(const unsigned FieldWidth = 1) {
61        return getBuilder()->getStreamTy(FieldWidth);
62    }
63   
64    inline llvm::Type * getStreamSetTy(const unsigned NumElements = 1, const unsigned FieldWidth = 1) {
65        return getBuilder()->getStreamSetTy(NumElements, FieldWidth);
66    }
67   
68    // Synchronization actions for executing a kernel for a particular logical segment.
69    //
70    // Before the segment is processed, acquireLogicalSegmentNo must be used to load
71    // the segment number of the kernel state to ensure that the previous segment is
72    // complete (by checking that the acquired segment number is equal to the desired segment
73    // number).
74    // After all segment processing actions for the kernel are complete, and any necessary
75    // data has been extracted from the kernel for further pipeline processing, the
76    // segment number must be incremented and stored using releaseLogicalSegmentNo.
77    llvm::LoadInst * acquireLogicalSegmentNo(llvm::Value * self) const;
78
79    void releaseLogicalSegmentNo(llvm::Value * self, llvm::Value * newFieldVal) const;
80
81    virtual ~KernelBuilder() = 0;
82   
83    const std::vector<const parabix::StreamSetBuffer *> & getStreamSetInputBuffers() const { return mStreamSetInputBuffers; }
84
85    const std::vector<const parabix::StreamSetBuffer *> & getStreamSetOutputBuffers() const { return mStreamSetOutputBuffers; }
86
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(bool noTerminate = true) {mNoTerminateAttribute = noTerminate;}
111    void setDoBlockUpdatesProducedItemCountsAttribute(bool doesUpdate = true) {mDoBlockUpdatesProducedItemCountsAttribute = doesUpdate;}
112   
113    virtual void prepareKernel();
114   
115    // Each kernel builder subtype must provide its own logic for generating
116    // doBlock calls.
117    virtual void generateDoBlockMethod() const = 0;
118
119    virtual void generateDoBlockLogic(llvm::Value * self, llvm::Value * blockNo) const;
120
121    // Each kernel builder subtypre must also specify the logic for processing the
122    // final block of stream data, if there is any special processing required
123    // beyond simply calling the doBlock function.   In the case that the final block
124    // processing may be trivially implemented by dispatching to the doBlock method
125    // without additional preparation, the default generateFinalBlockMethod need
126    // not be overridden.
127   
128    virtual void generateFinalBlockMethod() const;
129   
130    virtual void generateInitMethod() const;
131   
132    virtual void generateDoSegmentMethod() const;
133   
134    virtual void generateFinalSegmentMethod() const;
135   
136    // Add an additional scalar field to the KernelState struct.
137    // Must occur before any call to addKernelDeclarations or createKernelModule.
138    unsigned addScalar(llvm::Type * type, const std::string & name);
139
140    unsigned getScalarCount() const;
141
142    // Run-time access of Kernel State and parameters of methods for
143    // use in implementing kernels.
144   
145    // Get the index of a named scalar field within the kernel state struct.
146    llvm::ConstantInt * getScalarIndex(const std::string & name) const;
147   
148    // Get the value of a scalar field for a given instance.
149    llvm::Value * getScalarField(llvm::Value * self, const std::string & fieldName) const;
150
151    // Set the value of a scalar field for a given instance.
152    void setScalarField(llvm::Value * self, const std::string & fieldName, llvm::Value * newFieldVal) const;
153   
154    // Get a parameter by name.
155    llvm::Value * getParameter(llvm::Function * f, const std::string & paramName) const;
156
157    llvm::Value * getStream(llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
158
159    llvm::Value * getStream(llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index1, llvm::Value * index2) const;
160
161    llvm::Value * getStreamView(llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
162
163    llvm::Value * getStreamView(llvm::Type * type, llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
164
165    // Stream set helpers.
166    unsigned getStreamSetIndex(const std::string & name) const;
167   
168    llvm::Value * getScalarFieldPtr(llvm::Value * self, const std::string & name) const;
169
170    llvm::Value * getStreamSetStructPtr(llvm::Value * self, const std::string & name) const;
171
172    llvm::Value * getStreamSetPtr(llvm::Value * self, const std::string & name, llvm::Value * blockNo) const;
173   
174    void setBlockNo(llvm::Value * self, llvm::Value * value) const;
175
176    virtual void setProcessedItemCount(llvm::Value * self, const std::string & ssName, llvm::Value * newFieldVal) const;
177
178    virtual void setProducedItemCount(llvm::Value * self, const std::string & ssName, llvm::Value * newFieldVal) const;
179
180    void setTerminationSignal(llvm::Value * self) const;
181
182private:
183
184    const parabix::StreamSetBuffer * getStreamSetBuffer(const std::string & name) 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}
198#endif
Note: See TracBrowser for help on using the repository browser.