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

Last change on this file since 5377 was 5377, checked in by nmedfort, 2 years ago

Support for stdin. Needs more testing.

File size: 8.4 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 KernelBuilder; }
13
14namespace parabix {
15   
16class StreamSetBuffer {
17    friend class kernel::KernelBuilder;
18
19public:
20
21    enum class BufferKind : unsigned {BlockBuffer, ExternalFileBuffer, CircularBuffer, CircularCopybackBuffer, SwizzledCopybackBuffer, ExpandableBuffer, ExtensibleBuffer};
22
23    BufferKind getBufferKind() const {
24        return mBufferKind;
25    }
26
27    llvm::Type * getType() const {
28        return mType;
29    }
30
31    llvm::Type * getBaseType() const {
32        return mBaseType;
33    }
34
35    llvm::PointerType * getPointerType() const {
36        return getType()->getPointerTo(mAddressSpace);
37    }
38
39    size_t getBufferBlocks() const {
40        return mBufferBlocks;
41    }
42
43    llvm::Value * getStreamSetBasePtr() const {
44        return mStreamSetBufferPtr;
45    }
46
47    virtual void allocateBuffer();
48
49    virtual llvm::Value * getStreamBlockPtr(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const;
50
51    virtual llvm::Value * getStreamPackPtr(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, llvm::Value * packIndex, const bool readOnly) const;
52   
53    virtual llvm::Value * getStreamSetCount(llvm::Value * self) const;
54
55    llvm::Value * getRawItemPointer(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * absolutePosition) const;
56
57    // The number of items that cam be linearly accessed from a given logical stream position.
58    virtual llvm::Value * getLinearlyAccessibleItems(llvm::Value * self, llvm::Value * fromPosition) const;
59    virtual llvm::Value * getLinearlyAccessibleBlocks(llvm::Value * self, llvm::Value * fromBlock) const;
60
61    virtual llvm::Value * reserveItemCount(llvm::Value * self, llvm::Value * position, llvm::Value * requested) const;
62
63    virtual ~StreamSetBuffer() = 0;
64
65protected:
66
67    StreamSetBuffer(BufferKind k, IDISA::IDISA_Builder * b, llvm::Type * baseType, llvm::Type * resolvedType, unsigned blocks, unsigned AddressSpace);
68
69    // Get the buffer pointer for a given block of the stream.
70    virtual llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockNo) const = 0;
71
72    bool isCapacityGuaranteed(const llvm::Value * const index, const size_t capacity) const;
73
74    llvm::Value * modByBufferBlocks(llvm::Value * const offset) const;
75
76    virtual llvm::Value * getBaseAddress(llvm::Value * self) const;
77
78protected:
79    const BufferKind                mBufferKind;
80    IDISA::IDISA_Builder * const    iBuilder;
81    llvm::Type * const              mType;
82    const size_t                    mBufferBlocks;
83    const unsigned                  mAddressSpace;
84    llvm::Value *                   mStreamSetBufferPtr;
85    llvm::Type * const              mBaseType;
86};   
87
88class SingleBlockBuffer final : public StreamSetBuffer {
89public:
90    static inline bool classof(const StreamSetBuffer * b) {
91        return b->getBufferKind() == BufferKind::BlockBuffer;
92    }   
93
94    SingleBlockBuffer(IDISA::IDISA_Builder * b, llvm::Type * type);
95
96protected:
97    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockNo) const override;
98};
99
100class ExternalFileBuffer final : public StreamSetBuffer {
101public:
102    static inline bool classof(const StreamSetBuffer * b) {
103        return b->getBufferKind() == BufferKind::ExternalFileBuffer;
104    }
105   
106    ExternalFileBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, unsigned AddressSpace = 0);
107
108    void setStreamSetBuffer(llvm::Value * ptr);
109
110    // Can't allocate - raise an error. */
111    void allocateBuffer() override;
112
113    llvm::Value * getLinearlyAccessibleItems(llvm::Value * self, llvm::Value * fromPosition) const override;
114   
115protected:
116    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockNo) const override;
117};
118
119class ExtensibleBuffer final : public StreamSetBuffer {
120public:
121    static inline bool classof(const StreamSetBuffer * b) {
122        return b->getBufferKind() == BufferKind::ExtensibleBuffer;
123    }
124
125    ExtensibleBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
126
127    llvm::Value * getLinearlyAccessibleItems(llvm::Value * self,llvm::Value * fromPosition) const override;
128
129    void allocateBuffer() override;
130
131    llvm::Value * reserveItemCount(llvm::Value * self, llvm::Value * position, llvm::Value * requested) const override;
132
133protected:
134
135    llvm::Value * getBaseAddress(llvm::Value * self) const override;
136
137    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockNo) const override;
138
139};
140   
141class CircularBuffer final : public StreamSetBuffer {
142public:
143    static inline bool classof(const StreamSetBuffer * b) {
144        return b->getBufferKind() == BufferKind::CircularBuffer;
145    }
146   
147    CircularBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
148
149protected:
150    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockIndex) const override;
151};
152   
153
154//
155//  A CircularCopybackBuffer operates as a circular buffer buffer with an overflow area
156//  for temporary use by the kernel that writes to it.   If the kernel uses the overflow
157//  area, it must perform the doCopyBack action before releasing the buffer for use by
158//  subsequent kernels.
159//  Kernels that read from a CircularCopybackBuffer must not access the overflow area.
160//
161class CircularCopybackBuffer final : public StreamSetBuffer {
162public:
163    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::CircularCopybackBuffer;}
164   
165    CircularCopybackBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, size_t overflowBlocks, unsigned AddressSpace = 0);
166
167    void allocateBuffer() override;
168   
169    // Generate copyback code for the given number of overflowItems.
170    void createCopyBack(llvm::Value * self, llvm::Value * overflowItems) const;
171       
172protected:
173    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockIndex) const override;
174private:
175    size_t mOverflowBlocks;
176
177};
178   
179class SwizzledCopybackBuffer final : public StreamSetBuffer {
180public:
181    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::SwizzledCopybackBuffer;}
182   
183    SwizzledCopybackBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, size_t overflowBlocks, unsigned fieldwidth = 64, unsigned AddressSpace = 0);
184   
185    void allocateBuffer() override;
186   
187    // Generate copyback code for the given number of overflowItems.
188    void createCopyBack(llvm::Value * self, llvm::Value * overflowItems) const;
189protected:
190    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockIndex) const override;
191private:
192    size_t mOverflowBlocks;
193    unsigned mFieldWidth;
194   
195};
196
197// ExpandableBuffers do not allow access to the base stream set but will automatically increase the number of streams
198// within their set whenever the index exceeds its capacity
199//
200class ExpandableBuffer final : public StreamSetBuffer {
201public:
202    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::ExpandableBuffer;}
203
204    ExpandableBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
205
206    llvm::Value * getStreamBlockPtr(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const override;
207
208    llvm::Value * getStreamPackPtr(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, llvm::Value * packIndex, const bool readOnly) const override;
209
210    llvm::Value * getLinearlyAccessibleItems(llvm::Value * self, llvm::Value * fromPosition) const override;
211
212    void allocateBuffer() override;
213
214    llvm::Value * getStreamSetCount(llvm::Value * self) const override;
215
216protected:
217
218    llvm::Value * getBaseAddress(llvm::Value * self) const override;
219
220    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockIndex) const override;
221
222private:
223
224    std::pair<llvm::Value *, llvm::Value *> getInternalStreamBuffer(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const;
225
226private:
227
228    const uint64_t  mInitialCapacity;
229
230};
231
232}
233#endif // STREAMSET_H
Note: See TracBrowser for help on using the repository browser.