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

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

Removed BlockNo? + updated LookAhead? to use getInputStream.

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