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

Last change on this file since 5276 was 5276, checked in by cameron, 2 years ago

Elimination of StreamSetStructs? wrapping StreamSetBuffers?

File size: 8.5 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 bufferPtrSuffix = "_bufferPtr";
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::Value * getBlockNo(llvm::Value * self) const;
47    virtual llvm::Value * getProcessedItemCount(llvm::Value * self, const std::string & ssName) const override;
48    virtual llvm::Value * getProducedItemCount(llvm::Value * self, const std::string & ssName) const override;
49   
50    bool hasNoTerminateAttribute() { return mNoTerminateAttribute;}
51   
52    llvm::Value * getTerminationSignal(llvm::Value * self) const override;
53   
54    inline llvm::IntegerType * getSizeTy() const {
55        return getBuilder()->getSizeTy();
56    }
57
58    inline llvm::Type * getStreamTy(const unsigned FieldWidth = 1) {
59        return getBuilder()->getStreamTy(FieldWidth);
60    }
61   
62    inline llvm::Type * getStreamSetTy(const unsigned NumElements = 1, const unsigned FieldWidth = 1) {
63        return getBuilder()->getStreamSetTy(NumElements, FieldWidth);
64    }
65   
66    // Synchronization actions for executing a kernel for a particular logical segment.
67    //
68    // Before the segment is processed, acquireLogicalSegmentNo must be used to load
69    // the segment number of the kernel state to ensure that the previous segment is
70    // complete (by checking that the acquired segment number is equal to the desired segment
71    // number).
72    // After all segment processing actions for the kernel are complete, and any necessary
73    // data has been extracted from the kernel for further pipeline processing, the
74    // segment number must be incremented and stored using releaseLogicalSegmentNo.
75    llvm::LoadInst * acquireLogicalSegmentNo(llvm::Value * self) const;
76
77    void releaseLogicalSegmentNo(llvm::Value * self, llvm::Value * newFieldVal) const;
78
79    virtual ~KernelBuilder() = 0;
80   
81    const std::vector<const parabix::StreamSetBuffer *> & getStreamSetInputBuffers() const { return mStreamSetInputBuffers; }
82
83    const std::vector<const parabix::StreamSetBuffer *> & getStreamSetOutputBuffers() const { return mStreamSetOutputBuffers; }
84
85    void setTerminationSignal(llvm::Value * self) const;
86
87protected:
88
89    // Constructor
90    KernelBuilder(IDISA::IDISA_Builder * builder,
91                    std::string && kernelName,
92                    std::vector<Binding> stream_inputs,
93                    std::vector<Binding> stream_outputs,
94                    std::vector<Binding> scalar_parameters,
95                    std::vector<Binding> scalar_outputs,
96                    std::vector<Binding> internal_scalars);
97
98    //
99    // Kernel builder subtypes define their logic of kernel construction
100    // in terms of 3 virtual methods for
101    // (a) preparing the Kernel state data structure
102    // (b) defining the logic of the doBlock function, and
103    // (c) defining the logic of the finalBlock function.
104    //
105    // Note: the kernel state data structure must only be finalized after
106    // all scalar fields have been added.   If there are no fields to
107    // be added, the default method for preparing kernel state may be used.
108   
109    void setNoTerminateAttribute(bool noTerminate = true) {mNoTerminateAttribute = noTerminate;}
110    void setDoBlockUpdatesProducedItemCountsAttribute(bool doesUpdate = true) {mDoBlockUpdatesProducedItemCountsAttribute = doesUpdate;}
111   
112    virtual void prepareKernel();
113   
114    // Each kernel builder subtype must provide its own logic for generating
115    // doBlock calls.
116    virtual void generateDoBlockMethod() const;
117
118    virtual void generateDoBlockLogic(llvm::Value * self, llvm::Value * blockNo) const;
119
120    // Each kernel builder subtypre must also specify the logic for processing the
121    // final block of stream data, if there is any special processing required
122    // beyond simply calling the doBlock function.   In the case that the final block
123    // processing may be trivially implemented by dispatching to the doBlock method
124    // without additional preparation, the default generateFinalBlockMethod need
125    // not be overridden.
126   
127    virtual void generateFinalBlockMethod() const;
128   
129    virtual void generateInitMethod() const;
130   
131    virtual void generateDoSegmentMethod() const;
132   
133    // Add an additional scalar field to the KernelState struct.
134    // Must occur before any call to addKernelDeclarations or createKernelModule.
135    unsigned addScalar(llvm::Type * type, const std::string & name);
136
137    unsigned getScalarCount() const;
138
139    // Run-time access of Kernel State and parameters of methods for
140    // use in implementing kernels.
141   
142    // Get the index of a named scalar field within the kernel state struct.
143    llvm::ConstantInt * getScalarIndex(const std::string & name) const;
144   
145    // Get the value of a scalar field for a given instance.
146    llvm::Value * getScalarField(llvm::Value * self, const std::string & fieldName) const;
147
148    // Set the value of a scalar field for a given instance.
149    void setScalarField(llvm::Value * self, const std::string & fieldName, llvm::Value * newFieldVal) const;
150   
151    // Get a parameter by name.
152    llvm::Value * getParameter(llvm::Function * f, const std::string & paramName) const;
153
154    llvm::Value * getStream(llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
155
156    llvm::Value * getStream(llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index1, llvm::Value * index2) const;
157
158    llvm::Value * getStreamView(llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
159
160    llvm::Value * getStreamView(llvm::Type * type, llvm::Value * self, const std::string & name, llvm::Value * blockNo, llvm::Value * index) const;
161
162    // Stream set helpers.
163    unsigned getStreamSetIndex(const std::string & name) const;
164   
165    llvm::Value * getScalarFieldPtr(llvm::Value * self, const std::string & name) const;
166
167    llvm::Value * getStreamSetBufferPtr(llvm::Value * self, const std::string & name) const;
168
169    llvm::Value * getStreamSetPtr(llvm::Value * self, const std::string & name, llvm::Value * blockNo) const;
170   
171    void setBlockNo(llvm::Value * self, llvm::Value * value) const;
172
173    virtual void setProcessedItemCount(llvm::Value * self, const std::string & ssName, llvm::Value * newFieldVal) const;
174
175    virtual void setProducedItemCount(llvm::Value * self, const std::string & ssName, llvm::Value * newFieldVal) const;
176
177
178private:
179
180    const parabix::StreamSetBuffer * getStreamSetBuffer(const std::string & name) const;
181
182protected:
183
184    std::vector<llvm::Type *>                       mKernelFields;
185    NameMap                                         mKernelMap;
186    NameMap                                         mStreamSetNameMap;
187    std::vector<const parabix::StreamSetBuffer *>   mStreamSetInputBuffers;
188    std::vector<const parabix::StreamSetBuffer *>   mStreamSetOutputBuffers;
189    bool                                            mNoTerminateAttribute;
190    bool                                            mDoBlockUpdatesProducedItemCountsAttribute;
191
192};
193}
194#endif
Note: See TracBrowser for help on using the repository browser.