source: icGREP/icgrep-devel/icgrep/kernels/sequential_kernel.h @ 5895

Last change on this file since 5895 was 5864, checked in by xwa163, 19 months ago

Add LZ4D extract deposit related kernel, target and test cases

File size: 8.5 KB
Line 
1//
2//
3
4#ifndef ICGREP_SEQUENTIAL_SEGMENT_ORIENTED_KERNEL_H
5#define ICGREP_SEQUENTIAL_SEGMENT_ORIENTED_KERNEL_H
6
7#include "kernel.h"
8#include <vector>
9#include <map>
10
11namespace llvm {
12    class Module;
13    class Function;
14    class BasicBlock;
15    class Value;
16}
17
18namespace IDISA { class IDISA_Builder; }
19
20namespace kernel {
21    class SequentialKernel : public MultiBlockKernel {
22    protected:
23        std::map<std::string, size_t> inputStreamIndexMap;
24        std::map<std::string, std::string> clearBufferMap;
25
26        SequentialKernel(
27                const std::unique_ptr<kernel::KernelBuilder> & iBuilder,
28                std::string && kernelName,
29                std::vector<Binding> && stream_inputs,
30                std::vector<Binding> && stream_outputs,
31                std::vector<Binding> && scalar_parameters,
32                std::vector<Binding> && scalar_outputs,
33                std::vector<Binding> && internal_scalars);
34
35        virtual void generateMultiBlockLogic(const std::unique_ptr<KernelBuilder> &iBuilder, llvm::Value * const numOfStrides) override;
36//        virtual void generateDoSegmentMethod(const std::unique_ptr<KernelBuilder> &iBuilder) override;
37
38
39        virtual void generateDoSequentialSegmentMethod(const std::unique_ptr<KernelBuilder> &iBuilder);
40
41        //Index Bit
42        void configIndexBits(const std::unique_ptr<KernelBuilder> &iBuilder, const std::map<std::string, size_t>& inputStreamMap);
43
44        void configOutputBufferToBeClear(const std::map<std::string, std::string>& clearMap);
45
46
47        //Cursor
48        void initBufferCursor(const std::unique_ptr<KernelBuilder> &iBuilder, std::vector<std::string> cursorNames);
49
50        llvm::Value* getCursorValue(const std::unique_ptr<KernelBuilder> &iBuilder, std::string cursorName);
51        void advanceCursor(const std::unique_ptr<KernelBuilder> &iBuilder, std::string cursorName, llvm::Value* nums);
52        void advanceCursorUntilPos(const std::unique_ptr<KernelBuilder> &iBuilder, std::string cursorName, llvm::Value* position);
53        llvm::BasicBlock* advanceCursorUntilNextOne(const std::unique_ptr<KernelBuilder> &iBuilder, std::string cursorName, std::string inputStreamBufferName, llvm::Value* maxPos);
54        llvm::BasicBlock* advanceCursorUntilNextZero(const std::unique_ptr<KernelBuilder> &iBuilder,
55                                                     std::string cursorName, std::string inputStreamBufferName, llvm::Value* maxPos);
56
57        llvm::BasicBlock* memcpy2CursorsUntilNextZero(
58                const std::unique_ptr<KernelBuilder> &iBuilder,
59                std::string sourceBufferName,
60                std::string sourceCursorName,
61                std::string dstBufferName,
62                std::string dstCursorName,
63                std::string sourceMarkerName,
64                llvm::Value* maxPos);
65        llvm::BasicBlock* memcpyOutputDstCursorUntilNextZero(
66                const std::unique_ptr<KernelBuilder> &iBuilder,
67                std::string outputBufferName,
68                llvm::Value* copyOffset,
69                std::string dstCursorName,
70                std::string dstMarkerName,
71                llvm::Value* maxPos
72        );
73
74        llvm::BasicBlock* memcpyOutputDst(
75                const std::unique_ptr<KernelBuilder> &iBuilder,
76                std::string outputBufferName,
77                llvm::Value* copyOffset,
78                llvm::Value* copyLength
79
80        );
81
82        void memcpyCircularBuffer(
83                const std::unique_ptr<KernelBuilder> &iBuilder,
84                std::string sourceBufferName,
85                llvm::Value* sourceOffset,
86                std::string dstBufferName,
87                llvm::Value* dstOffset,
88                llvm::Value* distance
89        );
90
91        // Helper Functions
92        void markCircularOutputBitstreamOnePack(const std::unique_ptr<KernelBuilder> &iBuilder, const std::string& bitstreamName, llvm::Value* start, llvm::Value* end, bool isOne);
93        llvm::BasicBlock* markCircularOutputBitstream(const std::unique_ptr<KernelBuilder> &iBuilder, const std::string& bitstreamName, llvm::Value* start, llvm::Value* end, bool isOne, bool setProduced = true);
94
95        llvm::Value* generateLoadCircularInputPack(const std::unique_ptr<KernelBuilder> &iBuilder, std::string inputBufferName, llvm::Value* offset);
96        llvm::Value* generateLoadCircularInput(const std::unique_ptr<KernelBuilder> &iBuilder, std::string inputBufferName, llvm::Value* offset, llvm::Type* pointerType);
97        llvm::Value* generateLoadSourceInputByte(const std::unique_ptr<KernelBuilder> &iBuilder, std::string sourceBuffername, llvm::Value* offset);
98        void generateStoreCircularOutput(const std::unique_ptr<KernelBuilder> &iBuilder, std::string outputBufferName, llvm::Type* pointerType, llvm::Value* value);
99        llvm::Value* generateLoadCircularOutput(const std::unique_ptr<KernelBuilder> &iBuilder, std::string inputBufferName, llvm::Value* offset, llvm::Type* pointerType);
100        void increaseScalarField(const std::unique_ptr<KernelBuilder> &iBuilder, const std::string& fieldName, llvm::Value* value);
101
102//        void generateStoreCircularOutput1(const std::unique_ptr<KernelBuilder> &iBuilder, std::string outputBufferName, llvm::Type* pointerType, llvm::Value* value);
103
104        llvm::Value* offsetToActualBufferOffset(const std::unique_ptr<KernelBuilder> &iBuilder, std::string inputBufferName, llvm::Value* offset);
105        llvm::Value* offsetToPackOffset(const std::unique_ptr<KernelBuilder> &iBuilder, llvm::Value* offset);
106        llvm::Value* offsetToPackIndex(const std::unique_ptr<KernelBuilder> &iBuilder, llvm::Value* offset);
107        llvm::Value* offsetToPackBaseOffset(const std::unique_ptr<KernelBuilder> &iBuilder, llvm::Value* offset);
108
109        llvm::BasicBlock* waitCursorUntilInputAvailable(const std::unique_ptr<KernelBuilder> &iBuilder, std::string cursorName, std::string inputStreamBufferName);
110
111
112        size_t getInputBufferSize(const std::unique_ptr<KernelBuilder> &iBuilder, std::string bufferName);
113        size_t getOutputBufferSize(const std::unique_ptr<KernelBuilder> &iBuilder, std::string bufferName);
114
115    private:
116
117        // forwardBits, packEnd, exceedAvailable
118        std::pair<llvm::Value*, std::pair<llvm::Value*, llvm::Value*>> genereateCountForwardBitsOnePack(const std::unique_ptr<KernelBuilder> &iBuilder, std::string inputStreamBufferName, llvm::Value* beginOffset, bool isZero = true);
119        llvm::Value* generateCountIndexBit(const std::unique_ptr<KernelBuilder> &iBuilder, std::string bufferName, bool isZero, llvm::Value* beginBitIndex);
120
121        // forwardBits, isFinished
122        // maxCount for final bits
123        std::pair<llvm::Value*, llvm::Value*> generateCountForwardBits(const std::unique_ptr<KernelBuilder> &iBuilder, std::string inputStreamBufferName, llvm::Value* beginOffset, bool isZero = true, llvm::Value* maxPos = NULL);
124        std::pair<llvm::Value*, llvm::Value*> generateCountForwardZeros(const std::unique_ptr<KernelBuilder> &iBuilder, std::string inputStreamBufferName, llvm::Value* beginOffset, llvm::Value* maxPos = NULL);
125        std::pair<llvm::Value*, llvm::Value*> generateCountForwardOnes(const std::unique_ptr<KernelBuilder> &iBuilder, std::string inputStreamBufferName, llvm::Value* beginOffset, llvm::Value* maxPos = NULL);
126
127        void generateDstMatchCopy(const std::unique_ptr<KernelBuilder> & iBuilder, llvm::BasicBlock* entry, llvm::BasicBlock* exit, std::string outputBufferName, llvm::Value* matchOffset, llvm::Value* matchLength, llvm::Value* outputOffset);
128
129        void generateBuildIndexBits(const std::unique_ptr<KernelBuilder> &iBuilder);
130        inline bool hasIndexBits(const std::string& streamName);
131        void generateClearBuffer(const std::unique_ptr<KernelBuilder> &iBuilder);
132
133        std::vector<llvm::BasicBlock*> stateBlocks;
134
135        inline std::string generateCursorFullname(std::string cursorName);
136        inline std::string generateInputZeroIndexName(std::string inputStreamName);
137        inline std::string generateInputOneIndexName(std::string inputStreamName);
138        inline std::string generateInputPreviousAvailableName(std::string inputStreamName);
139
140
141
142        // CursorValue should not be set directly in user implemented kernel
143        void setCursorValue(const std::unique_ptr<KernelBuilder> &iBuilder, std::string cursorName, llvm::Value* value);
144
145        llvm::BasicBlock* exitBlock;
146
147
148        inline void recordCountForwardTempMaxPos(const std::unique_ptr<KernelBuilder> &iBuilder, llvm::Value* maxPos);
149        inline llvm::Value* restoreCountForwardTempMaxPos(const std::unique_ptr<KernelBuilder> &iBuilder, llvm::Value* currentMaxPos);
150    };
151}
152
153#endif //ICGREP_SEQUENTIAL_SEGMENT_ORIENTED_KERNEL_H
Note: See TracBrowser for help on using the repository browser.