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

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

Deprecating SingleBlockBuffer?

File size: 11.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    // The number of items that cam be linearly accessed from a given logical stream position.
78    virtual llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * fromPosition) const;
79   
80    virtual llvm::Value * getLinearlyAccessibleBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * fromBlock) const;
81   
82    virtual void createBlockAlignedCopy(IDISA::IDISA_Builder * const iBuilder, llvm::Value * targetBlockPtr, llvm::Value * sourceBlockPtr, llvm::Value * itemsToCopy) const;
83
84    virtual llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * fromPosition) const;
85   
86    virtual llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * fromBlock) const;
87   
88    virtual ~StreamSetBuffer() = 0;
89
90    kernel::Kernel * getProducer() const {
91        return mProducer;
92    }
93
94    const std::vector<kernel::Kernel *> & getConsumers() const {
95        return mConsumers;
96    }
97
98protected:
99
100    StreamSetBuffer(BufferKind k, llvm::Type * baseType, llvm::Type * resolvedType, unsigned BufferBlocks, unsigned AddressSpace);
101
102    // Get the buffer pointer for a given block of the stream set.
103    virtual llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * blockNo) const = 0;
104
105    bool isCapacityGuaranteed(const llvm::Value * const index, const size_t capacity) const;
106
107    llvm::Value * modByBufferBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * const offset) const;
108
109    virtual llvm::Value * getBaseAddress(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const;
110
111    void setProducer(kernel::Kernel * const producer) {
112        assert (producer);
113        mProducer = producer;
114    }
115
116    void addConsumer(kernel::Kernel * const consumer) {
117        assert (consumer);
118        mConsumers.push_back(consumer);
119    }
120
121protected:
122    const BufferKind                 mBufferKind;
123    llvm::Type * const               mType;
124    const size_t                     mBufferBlocks;
125    const unsigned                   mAddressSpace;
126    llvm::Value *                    mStreamSetBufferPtr;
127    llvm::Type * const               mBaseType;
128    std::string                      mUniqueID;
129    kernel::Kernel *                 mProducer;
130    std::vector<kernel::Kernel *>    mConsumers;
131};   
132
133class SourceBuffer final : public StreamSetBuffer {
134public:
135    static inline bool classof(const StreamSetBuffer * b) {
136        return b->getBufferKind() == BufferKind::SourceBuffer;
137    }
138
139    SourceBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, unsigned MemoryAddressSpace = 0, unsigned StructAddressSpace = 0);
140
141    void setBaseAddress(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * addr) const override;
142
143    void setBufferedSize(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * size) const override;
144
145    llvm::Value * getBufferedSize(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
146
147    llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * fromPosition) const override;
148
149protected:
150
151    llvm::Value * getBaseAddress(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
152
153    llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * blockNo) const override;
154
155};
156
157class ExternalBuffer final : public StreamSetBuffer {
158public:
159    static inline bool classof(const StreamSetBuffer * b) {
160        return b->getBufferKind() == BufferKind::ExternalBuffer;
161    }
162
163    ExternalBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, llvm::Value * addr, unsigned AddressSpace = 0);
164
165    // Can't allocate - raise an error. */
166    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
167
168    llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * fromPosition) const override;
169
170protected:
171    llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * blockNo) const override;
172};
173
174class CircularBuffer : public StreamSetBuffer {
175public:
176    static inline bool classof(const StreamSetBuffer * b) {
177        return b->getBufferKind() == BufferKind::CircularBuffer;
178    }
179   
180    CircularBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
181
182    llvm::Value * getRawItemPointer(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * streamIndex, llvm::Value * absolutePosition) const final;
183
184protected:
185
186    CircularBuffer(const BufferKind kind, const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
187
188    llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * blockIndex) const final;
189};
190   
191
192//
193//  A CircularCopybackBuffer operates as a circular buffer buffer with an overflow area
194//  for temporary use by the kernel that writes to it.   If the kernel uses the overflow
195//  area, it must perform the doCopyBack action before releasing the buffer for use by
196//  subsequent kernels.
197//  Kernels that read from a CircularCopybackBuffer must not access the overflow area.
198//
199class CircularCopybackBuffer final : public CircularBuffer {
200public:
201    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::CircularCopybackBuffer;}
202   
203    CircularCopybackBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t bufferBlocks, size_t overflowBlocks, unsigned AddressSpace = 0);
204
205    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
206   
207    // Generate copyback code for the given number of overflowItems.
208    void createCopyBack(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * overflowItems) const;
209       
210    llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * fromPosition) const override;
211   
212    llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * const iBuilder, llvm::Value * fromBlock) const override;
213   
214private:
215    size_t mOverflowBlocks;
216
217};
218   
219class SwizzledCopybackBuffer final : public StreamSetBuffer {
220public:
221    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::SwizzledCopybackBuffer;}
222   
223    SwizzledCopybackBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t bufferBlocks, size_t overflowBlocks, unsigned fieldwidth = 64, unsigned AddressSpace = 0);
224   
225    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
226   
227    void createBlockAlignedCopy(IDISA::IDISA_Builder * const iBuilder, llvm::Value * targetBlockPtr, llvm::Value * sourceBlockPtr, llvm::Value * itemsToCopy) const override;
228
229    // Generate copyback code for the given number of overflowItems.
230    void createCopyBack(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * overflowItems) const;
231   
232    llvm::Value * getLinearlyWritableItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * fromPosition) const override;
233   
234    llvm::Value * getLinearlyWritableBlocks(IDISA::IDISA_Builder * constiBuilder, llvm::Value * fromBlock) const override;
235   
236protected:
237    llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * blockIndex) const override;
238private:
239    size_t mOverflowBlocks;
240    unsigned mFieldWidth;
241   
242};
243
244// ExpandableBuffers do not allow access to the base stream set but will automatically increase the number of streams
245// within their set whenever the index exceeds its capacity
246//
247class ExpandableBuffer final : public StreamSetBuffer {
248public:
249    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::ExpandableBuffer;}
250
251    ExpandableBuffer(const std::unique_ptr<kernel::KernelBuilder> & b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
252
253    llvm::Value * getStreamBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const override;
254
255    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;
256
257    llvm::Value * getLinearlyAccessibleItems(IDISA::IDISA_Builder * const iBuilder, llvm::Value * fromPosition) const override;
258
259    void allocateBuffer(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) override;
260
261    llvm::Value * getStreamSetCount(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
262
263    void releaseBuffer(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
264
265protected:
266
267    llvm::Value * getBaseAddress(IDISA::IDISA_Builder * const iBuilder, llvm::Value * self) const override;
268
269    llvm::Value * getStreamSetBlockPtr(IDISA::IDISA_Builder * const iBuilder, llvm::Value * blockIndex, llvm::Value *) const override;
270
271private:
272
273    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;
274
275private:
276
277    const uint64_t  mInitialCapacity;
278
279};
280
281}
282#endif // STREAMSET_H
Note: See TracBrowser for help on using the repository browser.