source: icGREP/icgrep-devel/icgrep/kernels/streamset.h @ 5503

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

Progress on multi-block kernels, including support for SourceBuffers?

File size: 12.8 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 STREAMSET_H
7#define STREAMSET_H
8
9#include <llvm/IR/Type.h>  // for Type
10namespace IDISA { class IDISA_Builder; }
11namespace llvm { class Value; }
12namespace kernel { class Kernel; }
13namespace kernel { class KernelBuilder; }
14
15namespace parabix {
16   
17class StreamSetBuffer {
18    friend class kernel::Kernel;
19    friend class kernel::KernelBuilder;
20public:
21
22    enum class BufferKind : unsigned {
23        SourceBuffer
24        , ExternalBuffer
25        , CircularBuffer
26        , CircularCopybackBuffer
27        , SwizzledCopybackBuffer
28        , ExpandableBuffer
29    };
30
31    BufferKind getBufferKind() const {
32        return mBufferKind;
33    }
34   
35    std::string getUniqueID() const {
36        return mUniqueID;
37    }
38
39    llvm::Type * getType() const {
40        return mType;
41    }
42
43    llvm::Type * getBaseType() const {
44        return mBaseType;
45    }
46
47    llvm::PointerType * getPointerType() const {
48        return getType()->getPointerTo(mAddressSpace);
49    }
50
51    size_t getBufferBlocks() const {
52        return mBufferBlocks;
53    }
54
55    llvm::Value * getStreamSetBasePtr() const {
56        return mStreamSetBufferPtr;
57    }
58
59    virtual void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder);
60
61    virtual void releaseBuffer(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const;
62
63    virtual llvm::Value * getStreamBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const;
64
65    virtual llvm::Value * getStreamPackPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, llvm::Value * packIndex, const bool readOnly) const;
66   
67    virtual llvm::Value * getStreamSetCount(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const;
68
69    virtual llvm::Value * getRawItemPointer(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * streamIndex, llvm::Value * absolutePosition) const;
70
71    virtual void setBaseAddress(IDISA::IDISA_Builder * const iBuilder, llvm::Value * addr, llvm::Value *) const;
72
73    virtual void setBufferedSize(IDISA::IDISA_Builder * const iBuilder, llvm::Value * size, llvm::Value *) const;
74   
75    virtual llvm::Value * getBufferedSize(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const;
76   
77    virtual void setCapacity(IDISA::IDISA_Builder * const iBuilder, llvm::Value * size, llvm::Value *) const;
78   
79    virtual llvm::Value * getCapacity(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const;
80   
81    // The number of items that cam be linearly accessed from a given logical stream position.
82    virtual llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const;
83   
84    virtual llvm::Value * getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const;
85   
86    void createBlockCopy(IDISA::IDISA_Builder * const iBuilder, llvm::Value * targetBlockPtr, llvm::Value * sourceBlockPtr, llvm::Value * blocksToCopy) const;
87
88    virtual void createBlockAlignedCopy(IDISA::IDISA_Builder * const iBuilder, llvm::Value * targetBlockPtr, llvm::Value * sourceBlockPtr, llvm::Value * itemsToCopy) const;
89
90    virtual llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const;
91   
92    virtual llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const;
93   
94    virtual ~StreamSetBuffer() = 0;
95
96    kernel::Kernel * getProducer() const {
97        return mProducer;
98    }
99
100    const std::vector<kernel::Kernel *> & getConsumers() const {
101        return mConsumers;
102    }
103
104protected:
105
106    StreamSetBuffer(BufferKind k, llvm::Type * baseType, llvm::Type * resolvedType, unsigned BufferBlocks, unsigned AddressSpace);
107
108    // Get the buffer pointer for a given block of the stream set.
109    virtual llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * blockNo) const = 0;
110
111    bool isCapacityGuaranteed(const llvm::Value * const index, const size_t capacity) const;
112
113    llvm::Value * modByBufferBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * const offset) const;
114
115    virtual llvm::Value * getBaseAddress(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const;
116
117    void setProducer(kernel::Kernel * const producer) {
118        assert (producer);
119        mProducer = producer;
120    }
121
122    void addConsumer(kernel::Kernel * const consumer) {
123        assert (consumer);
124        mConsumers.push_back(consumer);
125    }
126
127protected:
128    const BufferKind                 mBufferKind;
129    llvm::Type * const               mType;
130    const size_t                     mBufferBlocks;
131    const unsigned                   mAddressSpace;
132    llvm::Value *                    mStreamSetBufferPtr;
133    llvm::Type * const               mBaseType;
134    std::string                      mUniqueID;
135    kernel::Kernel *                 mProducer;
136    std::vector<kernel::Kernel *>    mConsumers;
137};   
138
139class SourceBuffer final : public StreamSetBuffer {
140public:
141    static inline bool classof(const StreamSetBuffer * b) {
142        return b->getBufferKind() == BufferKind::SourceBuffer;
143    }
144
145    SourceBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, unsigned MemoryAddressSpace = 0, unsigned StructAddressSpace = 0);
146
147    void setBaseAddress(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * addr) const override;
148
149    void setBufferedSize(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * size) const override;
150
151    void setCapacity(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * c) const override;
152
153    llvm::Value * getBufferedSize(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
154   
155    llvm::Value * getCapacity(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
156   
157    llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const override;
158   
159    llvm::Value * getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const override;
160
161
162protected:
163   
164    enum class Field {BaseAddress, BufferedSize, Capacity};
165
166    llvm::Value * getBaseAddress(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
167
168    llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * blockNo) const override;
169
170};
171
172class ExternalBuffer final : public StreamSetBuffer {
173public:
174    static inline bool classof(const StreamSetBuffer * b) {
175        return b->getBufferKind() == BufferKind::ExternalBuffer;
176    }
177
178    ExternalBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, llvm::Value * addr, unsigned AddressSpace = 0);
179
180    // Can't allocate - raise an error. */
181    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
182
183    llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const override;
184
185protected:
186    llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * blockNo) const override;
187};
188
189class CircularBuffer : public StreamSetBuffer {
190public:
191    static inline bool classof(const StreamSetBuffer * b) {
192        return b->getBufferKind() == BufferKind::CircularBuffer;
193    }
194   
195    CircularBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
196
197    llvm::Value * getRawItemPointer(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * streamIndex, llvm::Value * absolutePosition) const final;
198
199protected:
200
201    CircularBuffer(const BufferKind kind, const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
202
203    llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * blockIndex) const final;
204};
205   
206
207//
208//  A CircularCopybackBuffer operates as a circular buffer buffer with an overflow area
209//  for temporary use by the kernel that writes to it.   If the kernel uses the overflow
210//  area, it must perform the doCopyBack action before releasing the buffer for use by
211//  subsequent kernels.
212//  Kernels that read from a CircularCopybackBuffer must not access the overflow area.
213//
214class CircularCopybackBuffer final : public CircularBuffer {
215public:
216    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::CircularCopybackBuffer;}
217   
218    CircularCopybackBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t bufferBlocks, size_t overflowBlocks, unsigned AddressSpace = 0);
219
220    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
221   
222    // Generate copyback code for the given number of overflowItems.
223    void createCopyBack(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * overflowItems) const;
224       
225    llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const override;
226   
227    llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const override;
228   
229private:
230    size_t mOverflowBlocks;
231
232};
233   
234class SwizzledCopybackBuffer final : public StreamSetBuffer {
235public:
236    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::SwizzledCopybackBuffer;}
237   
238    SwizzledCopybackBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t bufferBlocks, size_t overflowBlocks, unsigned fieldwidth = 64, unsigned AddressSpace = 0);
239   
240    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
241   
242    void createBlockAlignedCopy(IDISA::IDISA_Builder * const iBuilder, llvm::Value * targetBlockPtr, llvm::Value * sourceBlockPtr, llvm::Value * itemsToCopy) const override;
243
244    // Generate copyback code for the given number of overflowItems.
245    void createCopyBack(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * overflowItems) const;
246   
247    llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const override;
248   
249    llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromBlock) const override;
250   
251protected:
252    llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * blockIndex) const override;
253private:
254    size_t mOverflowBlocks;
255    unsigned mFieldWidth;
256   
257};
258
259// ExpandableBuffers do not allow access to the base stream set but will automatically increase the number of streams
260// within their set whenever the index exceeds its capacity
261//
262class ExpandableBuffer final : public StreamSetBuffer {
263public:
264    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::ExpandableBuffer;}
265
266    ExpandableBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
267
268    llvm::Value * getStreamBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const override;
269
270    llvm::Value * getStreamPackPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, llvm::Value * packIndex, const bool readOnly) const override;
271
272    llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * fromPosition) const override;
273
274    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
275
276    llvm::Value * getStreamSetCount(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
277
278    void releaseBuffer(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
279
280protected:
281
282    llvm::Value * getBaseAddress(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
283
284    llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * blockIndex, llvm::Value *) const override;
285
286private:
287
288    std::pair<llvm::Value *, llvm::Value *> getInternalStreamBuffer(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const;
289
290private:
291
292    const uint64_t  mInitialCapacity;
293
294};
295
296}
297#endif // STREAMSET_H
Note: See TracBrowser for help on using the repository browser.