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

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

Swizzled bitstream deletion and -enable-AVX-deletion in u8u16

File size: 7.5 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};
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    virtual llvm::Value * getLinearlyAccessibleBlocks(llvm::Value * fromBlock) const;
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
146       
147       
148protected:
149    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockIndex) const override;
150private:
151    size_t mOverflowBlocks;
152
153};
154   
155class SwizzledCopybackBuffer : public StreamSetBuffer {
156public:
157    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::SwizzledCopybackBuffer;}
158   
159    SwizzledCopybackBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, size_t overflowBlocks, unsigned fieldwidth = 64, unsigned AddressSpace = 0);
160   
161    void allocateBuffer() override;
162   
163    // Generate copyback code for the given number of overflowItems.
164    void createCopyBack(llvm::Value * self, llvm::Value * overflowItems) const;
165protected:
166    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockIndex) const override;
167private:
168    size_t mOverflowBlocks;
169    unsigned mFieldWidth;
170   
171};
172
173// ExpandableBuffers do not allow access to the base stream set but will automatically increase the number of streams
174// within their set whenever the index exceeds its capacity
175//
176// ExpandableBuffers do not allow access to the base stream set but will automatically increase the number of streams
177// within their set whenever the index exceeds its capacity
178//
179class ExpandableBuffer : public StreamSetBuffer {
180public:
181    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::ExpandableBuffer;}
182
183    ExpandableBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
184
185    llvm::Value * getStreamBlockPtr(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const override;
186
187    llvm::Value * getStreamPackPtr(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, llvm::Value * packIndex, const bool readOnly) const override;
188
189    llvm::Value * getLinearlyAccessibleItems(llvm::Value * fromPosition) const override;
190
191    void allocateBuffer() override;
192
193    llvm::Value * getStreamSetCount(llvm::Value * self) const override;
194
195protected:
196
197    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockIndex) const override;
198
199private:
200
201    std::pair<llvm::Value *, llvm::Value *> getInternalStreamBuffer(llvm::Value * self, llvm::Value * streamIndex, llvm::Value * blockIndex, const bool readOnly) const;
202
203private:
204
205    const uint64_t  mInitialCapacity;
206
207};
208
209}
210#endif // STREAMSET_H
Note: See TracBrowser for help on using the repository browser.