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

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

Streamset refactoring

File size: 4.9 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, ExternalFileBuffer, CircularBuffer, ExpandingBuffer};
35    inline BufferKind getBufferKind() const {return mBufferKind;}
36    inline StreamSetType& getBufferStreamSetType() {return mStreamSetType;}
37
38    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    virtual llvm::Value * getProducerPosPtr(Value * ptr);
50
51    virtual void setProducerPos(Value * ptr, Value * pos);
52
53    virtual llvm::Value * getConsumerPosPtr(Value * ptr);
54
55    virtual void setConsumerPos(Value * ptr, Value * pos);
56
57    virtual llvm::Value * hasEndOfInputPtr(Value * ptr);
58
59    virtual void setEndOfInput(Value * ptr);
60
61    virtual llvm::PointerType * getStreamSetStructPointerType();
62
63    virtual llvm::Value * getStreamSetStructPtr();
64   
65protected:
66    StreamSetBuffer(BufferKind k, IDISA::IDISA_Builder * b, StreamSetType ss_type, int AddressSpace = 0) :
67        mBufferKind(k), iBuilder(b), mStreamSetType(ss_type), mBufferBlocks(1), mAddrSpace(AddressSpace), mStreamSetBufferPtr(nullptr) {
68            mStreamSetStructType =
69                StructType::get(iBuilder->getContext(),
70                                std::vector<Type *>({iBuilder->getSizeTy(),
71                                                    iBuilder->getSizeTy(),
72                                                    iBuilder->getInt8Ty(),
73                                                    PointerType::get(mStreamSetType.getStreamSetBlockType(iBuilder), AddressSpace)}));
74    }
75   
76    const BufferKind       mBufferKind;
77    IDISA::IDISA_Builder * iBuilder;
78    StreamSetType mStreamSetType;
79    size_t mBufferBlocks;
80    int mAddrSpace;
81    llvm::Value * mStreamSetBufferPtr;
82    llvm::Value * mStreamSetStructPtr;
83    llvm::Type * mStreamSetStructType;
84};   
85   
86
87class SingleBlockBuffer : public StreamSetBuffer {
88public:
89    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::BlockBuffer;}
90   
91    SingleBlockBuffer(IDISA::IDISA_Builder * b, StreamSetType ss_type) :
92    StreamSetBuffer(BufferKind::BlockBuffer, b, ss_type, 0) { }
93   
94    size_t getBufferSize() override;
95    llvm::Value * allocateBuffer() override;
96    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferBasePtr, llvm::Value * blockNo) override;
97};
98   
99class ExternalFileBuffer : public StreamSetBuffer {
100public:
101    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::ExternalFileBuffer;}
102   
103    ExternalFileBuffer(IDISA::IDISA_Builder * b, StreamSetType ss_type, int AddressSpace = 0) :
104        StreamSetBuffer(BufferKind::ExternalFileBuffer, b, ss_type, AddressSpace) {}
105
106    void setStreamSetBuffer(llvm::Value * ptr, llvm::Value * fileSize);
107   
108    size_t getBufferSize() override;
109    // Can't allocate - raise an error. */
110    llvm::Value * allocateBuffer() override;
111    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferBasePtr, llvm::Value * blockNo) override;
112
113};
114
115class CircularBuffer : public StreamSetBuffer {
116public:
117    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::CircularBuffer;}
118 
119    CircularBuffer(IDISA::IDISA_Builder * b, StreamSetType ss_type, size_t bufferBlocks) :
120        StreamSetBuffer(BufferKind::CircularBuffer, b, ss_type) {
121            mBufferBlocks = bufferBlocks;
122            if (((bufferBlocks - 1) & bufferBlocks) != 0) {
123                throw std::runtime_error("CircularStreamSetBuffer: number of blocks must be a power of 2!");
124            }
125        }
126
127    size_t getBufferSize() override;
128    llvm::Value * allocateBuffer() override;
129    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferBasePtr, llvm::Value * blockNo) override;
130};
131
132}
133#endif // STREAMSET_H
Note: See TracBrowser for help on using the repository browser.