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

Last change on this file since 5217 was 5217, checked in by nmedfort, 3 years ago

Merged PabloFunction? and PabloKernel? classes. Updated projects where necessary.

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 <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
18// Stream Set Structs hold information about the current state of a stream set buffer.
19
20llvm::Value * getProducerPosPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr);
21llvm::Value * getConsumerPosPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr);
22llvm::Value * getEndOfInputPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr);
23llvm::Value * getStreamSetBufferPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr);
24
25class StreamSetBuffer {
26public:
27
28    enum class BufferKind : unsigned {BlockBuffer, ExternalFileBuffer, CircularBuffer, LinearCopybackBuffer};
29
30    inline BufferKind getBufferKind() const {
31        return mBufferKind;
32    }
33
34    inline llvm::Type * getBufferStreamSetType() const {
35        return mStreamSetType;
36    }
37
38    llvm::PointerType * getStreamBufferPointerType() const {
39        return mStreamSetType->getPointerTo(mAddrSpace);
40    }
41
42    llvm::PointerType * getStreamSetStructPointerType() const {
43        return mStreamSetStructType->getPointerTo();
44    }
45
46    size_t getBufferSize() const { return mBufferBlocks; }
47       
48    llvm::Value * getStreamSetBasePtr() const { return mStreamSetBufferPtr; }
49   
50    llvm::Value * getStreamSetStructPtr() const { return mStreamSetStructPtr; }
51
52    virtual void allocateBuffer();
53
54    // Get the buffer pointer for a given block of the stream.
55    virtual llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) = 0;
56   
57    llvm::Value * getProducerPosPtr(Value * bufferStructPtr);
58
59    void setProducerPos(Value * bufferStructPtr, Value * pos);
60
61    llvm::Value * getConsumerPosPtr(Value * bufferStructPtr);
62
63    virtual void setConsumerPos(Value * bufferStructPtr, Value * pos);
64
65    llvm::Value * getEndOfInputPtr(Value * bufferStructPtr);
66
67    void setEndOfInput(Value * bufferStructPtr);
68   
69protected:
70    StreamSetBuffer(BufferKind k, IDISA::IDISA_Builder * b, llvm::Type * type, unsigned blocks, unsigned AddressSpace = 0)
71    : mBufferKind(k)
72    , iBuilder(b)
73    , mStreamSetType(isa<IDISA::StreamType>(type) ? cast<IDISA::StreamType>(type)->resolveType(b) : type)
74    , mBufferBlocks(blocks)
75    , mAddrSpace(AddressSpace)
76    , mStreamSetBufferPtr(nullptr)
77    , mStreamSetStructPtr(nullptr)
78    , mStreamSetStructType(StructType::get(b->getContext(),
79                            {{b->getSizeTy(),
80                              b->getSizeTy(),
81                              b->getInt1Ty(),
82                              PointerType::get(mStreamSetType, AddressSpace)}})) {
83
84    }
85protected:
86    const BufferKind        mBufferKind;
87    IDISA::IDISA_Builder *  iBuilder;
88    llvm::Type * const      mStreamSetType;
89    size_t                  mBufferBlocks;
90    int                     mAddrSpace;
91    llvm::Value *           mStreamSetBufferPtr;
92    llvm::Value *           mStreamSetStructPtr;
93    llvm::Type * const      mStreamSetStructType;
94};   
95
96class SingleBlockBuffer : public StreamSetBuffer {
97public:
98    static inline bool classof(const StreamSetBuffer * b) {
99        return b->getBufferKind() == BufferKind::BlockBuffer;
100    }   
101    SingleBlockBuffer(IDISA::IDISA_Builder * b, llvm::Type * type)
102    : StreamSetBuffer(BufferKind::BlockBuffer, b, type, 1, 0) {
103
104    }
105    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferBasePtr, llvm::Value * blockNo) override;
106};
107
108class ExternalFileBuffer : public StreamSetBuffer {
109public:
110    static inline bool classof(const StreamSetBuffer * b) {
111        return b->getBufferKind() == BufferKind::ExternalFileBuffer;
112    }
113   
114    ExternalFileBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, unsigned AddressSpace = 0)
115    : StreamSetBuffer(BufferKind::ExternalFileBuffer, b, type, 0, AddressSpace) {
116
117    }
118
119    void setStreamSetBuffer(llvm::Value * ptr, llvm::Value * fileSize);
120    void setEmptyBuffer(llvm::Value * buffer_ptr);
121
122    // Can't allocate - raise an error. */
123    void allocateBuffer() override;
124
125    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) override;
126};
127   
128class CircularBuffer : public StreamSetBuffer {
129public:
130    static inline bool classof(const StreamSetBuffer * b) {
131        return b->getBufferKind() == BufferKind::CircularBuffer;
132    }
133 
134    CircularBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0)
135    : StreamSetBuffer(BufferKind::CircularBuffer, b, type, bufferBlocks, AddressSpace) {
136
137    }
138
139    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferBasePtr, llvm::Value * blockNo) override;
140};
141   
142// Linear buffers extending from the current ConsumerPos forward.   Within the buffer, the
143// offset of the block containing the current consumer position is always zero.
144//
145class LinearCopybackBuffer : public StreamSetBuffer {
146public:
147    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::LinearCopybackBuffer;}
148   
149    LinearCopybackBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0) :
150        StreamSetBuffer(BufferKind::LinearCopybackBuffer, b, type, bufferBlocks, AddressSpace) {}
151   
152    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) override;
153   
154    // Reset the buffer to contain data starting at the base block of new_consumer_pos,
155    // copying back any data beyond that position.
156    void setConsumerPos(Value * bufferStructPtr, Value * new_consumer_pos) override;
157};
158
159}
160#endif // STREAMSET_H
Note: See TracBrowser for help on using the repository browser.