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

Last change on this file since 5133 was 5133, checked in by cameron, 3 years ago

Defer binding of buffers to stream sets until kernel generation

File size: 4.1 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 <string>
10#include <vector>
11#include <IDISA/idisa_builder.h>
12#include <llvm/IR/Type.h>
13   
14namespace parabix {
15   
16enum FieldType {i1 = 1, i2 = 2, i4 = 4, i8 = 8, i16 = 16, i32 = 32, i64 = 64, i128 = 128, i256 = 256};
17
18class StreamSetType {
19public:
20    StreamSetType(int count, int width) : mStreamCount(count), mFieldWidth(width) {}
21    int StreamCount() { return mStreamCount;}
22    int StreamFieldWidth() { return mFieldWidth;}
23    bool operator== (StreamSetType& other) {return (mStreamCount == other.mStreamCount) && (mFieldWidth == other.mFieldWidth);}
24   
25    llvm::Type * getStreamSetBlockType(IDISA::IDISA_Builder * iBuilder);
26   
27private:
28    int mStreamCount;
29    int mFieldWidth;
30};
31
32class StreamSetBuffer {
33public:
34    enum class BufferKind : unsigned {BlockBuffer, ExternalUnboundedBuffer, CircularBuffer, ExpandingBuffer};
35    inline BufferKind getBufferKind() const {return mBufferKind;}
36    inline StreamSetType& getBufferStreamSetType() {return mStreamSetType;}
37
38    virtual llvm::PointerType * getStreamBufferPointerType();
39
40    virtual size_t getBufferSize() = 0;
41   
42    virtual llvm::Value * allocateBuffer() = 0;
43   
44    llvm::Value * getStreamSetBasePtr() {return mStreamSetBufferPtr;}
45   
46    // Get the buffer pointer for a given block of the stream.
47    virtual llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferBasePtr, llvm::Value * blockNo) = 0;
48   
49   
50   
51protected:
52    StreamSetBuffer(BufferKind k, IDISA::IDISA_Builder * b, StreamSetType ss_type) :
53        mBufferKind(k), iBuilder(b), mStreamSetType(ss_type), mBufferBlocks(1), mAddrSpace(0), mStreamSetBufferPtr(nullptr) {
54    }
55    const BufferKind       mBufferKind;
56    IDISA::IDISA_Builder * iBuilder;
57    StreamSetType mStreamSetType;
58    size_t mBufferBlocks;
59    int mAddrSpace;
60    llvm::Value * mStreamSetBufferPtr;
61
62};   
63   
64
65class SingleBlockBuffer : public StreamSetBuffer {
66public:
67    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::BlockBuffer;}
68   
69    SingleBlockBuffer(IDISA::IDISA_Builder * b, StreamSetType ss_type) :
70    StreamSetBuffer(BufferKind::BlockBuffer, b, ss_type) {}
71   
72    size_t getBufferSize() override;
73    llvm::Value * allocateBuffer() override;
74    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferBasePtr, llvm::Value * blockNo) override;
75};
76   
77class ExternalUnboundedBuffer : public StreamSetBuffer {
78public:
79    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::ExternalUnboundedBuffer;}
80   
81    ExternalUnboundedBuffer(IDISA::IDISA_Builder * b, StreamSetType ss_type, unsigned AddressSpace = 0) :
82        StreamSetBuffer(BufferKind::ExternalUnboundedBuffer, b, ss_type) {
83            mBufferBlocks = 0;
84            mAddrSpace = AddressSpace;
85        }
86    llvm::PointerType * getStreamBufferPointerType() override;
87
88    void setStreamSetBuffer(llvm::Value * ptr);
89   
90    size_t getBufferSize() override;
91    // Can't allocate - raise an error. */
92    llvm::Value * allocateBuffer() override;
93    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferBasePtr, llvm::Value * blockNo) override;
94
95private:
96    unsigned mAddrSpace;
97};
98
99class CircularBuffer : public StreamSetBuffer {
100public:
101    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::CircularBuffer;}
102 
103    CircularBuffer(IDISA::IDISA_Builder * b, StreamSetType ss_type, size_t bufferBlocks) :
104        StreamSetBuffer(BufferKind::CircularBuffer, b, ss_type) {
105            mBufferBlocks = bufferBlocks;
106            if (((bufferBlocks - 1) & bufferBlocks) != 0) {
107                throw std::runtime_error("CircularStreamSetBuffer: number of blocks must be a power of 2!");
108            }
109        }
110
111    size_t getBufferSize() override;
112    llvm::Value * allocateBuffer() override;
113    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferBasePtr, llvm::Value * blockNo) override;
114};
115
116}
117#endif // STREAMSET_H
Note: See TracBrowser for help on using the repository browser.