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

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

Progress on parenthesis matching example

File size: 6.6 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, ExpandableBuffer};
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 * fromPosition) const;
59   
60protected:
61
62    StreamSetBuffer(BufferKind k, IDISA::IDISA_Builder * b, llvm::Type * baseType, llvm::Type * resolvedType, unsigned blocks, unsigned AddressSpace);
63
64    // Get the buffer pointer for a given block of the stream.
65    virtual llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockNo) const = 0;
66
67    bool isCapacityGuaranteed(const llvm::Value * const index, const size_t capacity) const;
68
69    llvm::Value * modByBufferBlocks(llvm::Value * const offset) const;
70
71protected:
72    const BufferKind                mBufferKind;
73    IDISA::IDISA_Builder * const    iBuilder;
74    llvm::Type * const              mType;
75    const size_t                    mBufferBlocks;
76    const unsigned                  mAddressSpace;
77    llvm::Value *                   mStreamSetBufferPtr;
78    llvm::Type * const              mBaseType;
79};   
80
81class SingleBlockBuffer : public StreamSetBuffer {
82public:
83    static inline bool classof(const StreamSetBuffer * b) {
84        return b->getBufferKind() == BufferKind::BlockBuffer;
85    }   
86
87    SingleBlockBuffer(IDISA::IDISA_Builder * b, llvm::Type * type);
88
89protected:
90    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockNo) const override;
91};
92
93class ExternalFileBuffer : public StreamSetBuffer {
94public:
95    static inline bool classof(const StreamSetBuffer * b) {
96        return b->getBufferKind() == BufferKind::ExternalFileBuffer;
97    }
98   
99    ExternalFileBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, unsigned AddressSpace = 0);
100
101    void setStreamSetBuffer(llvm::Value * ptr, llvm::Value * fileSize);
102
103    void setEmptyBuffer(llvm::Value * buffer_ptr);
104
105    // Can't allocate - raise an error. */
106    void allocateBuffer() override;
107
108    llvm::Value * getLinearlyAccessibleItems(llvm::Value * fromPosition) const override;
109   
110protected:
111    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockNo) const override;
112};
113   
114class CircularBuffer : public StreamSetBuffer {
115public:
116    static inline bool classof(const StreamSetBuffer * b) {
117        return b->getBufferKind() == BufferKind::CircularBuffer;
118    }
119   
120    CircularBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
121
122protected:
123    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockIndex) const override;
124};
125   
126
127//
128//  A CircularCopybackBuffer operates as a circular buffer buffer with an overflow area
129//  for temporary use by the kernel that writes to it.   If the kernel uses the overflow
130//  area, it must perform the doCopyBack action before releasing the buffer for use by
131//  subsequent kernels.
132//  Kernels that read from a CircularCopybackBuffer must not access the overflow area.
133//
134class CircularCopybackBuffer : public StreamSetBuffer {
135public:
136    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::CircularCopybackBuffer;}
137   
138    CircularCopybackBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, size_t overflowBlocks, unsigned AddressSpace = 0);
139
140    void allocateBuffer() override;
141   
142    // Generate copyback code for the given number of overflowItems.
143    void createCopyBack(llvm::Value * self, llvm::Value * overflowItems) const;
144   
145   
146protected:
147    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockIndex) const override;
148private:
149    size_t mOverflowBlocks;
150
151};
152
153// ExpandableBuffers do not allow access to the base stream set but will automatically increase the number of streams
154// within their set whenever the index exceeds its capacity
155//
156// ExpandableBuffers do not allow access to the base stream set but will automatically increase the number of streams
157// within their set whenever the index exceeds its capacity
158//
159class ExpandableBuffer : public StreamSetBuffer {
160public:
161    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::ExpandableBuffer;}
162
163    ExpandableBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
164
165    llvm::Value * getStreamBlockPtr(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const override;
166
167    llvm::Value * getStreamPackPtr(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, llvm::Value * packIndex, const bool readOnly) const override;
168
169    llvm::Value * getLinearlyAccessibleItems(llvm::Value * fromPosition) const override;
170
171    void allocateBuffer() override;
172
173    llvm::Value * getStreamSetCount(llvm::Value * self) const override;
174
175protected:
176
177    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockIndex) const override;
178
179private:
180
181    std::pair<llvm::Value *, llvm::Value *> getInternalStreamBuffer(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const;
182
183private:
184
185    const uint64_t  mInitialCapacity;
186
187};
188
189}
190#endif // STREAMSET_H
Note: See TracBrowser for help on using the repository browser.