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

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

Multiblock kernels use whole block copy to/from temp buffers; allow input stream sets with variable rates

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