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

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

Initial work on adding types to PabloAST and mutable Var objects.

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