source: icGREP/icgrep-devel/icgrep/kernels/kernel_builder.h @ 6258

Last change on this file since 6258 was 6258, checked in by nmedfort, 10 months ago

Safer fix for LLVM bug + minor fixes/changes

File size: 7.0 KB
Line 
1#ifndef KERNEL_BUILDER_H
2#define KERNEL_BUILDER_H
3
4#include <IR_Gen/idisa_builder.h>
5#include <kernels/kernel.h>
6
7namespace kernel {
8
9class Kernel;
10
11class KernelBuilder : public virtual IDISA::IDISA_Builder {
12    friend class Kernel;
13    friend class MultiBlockKernel;
14    friend class PipelineGenerator;
15public:
16
17    // Get the value of a scalar field for the current instance.
18    llvm::Value * getScalarFieldPtr(llvm::Value * index);
19
20    llvm::Value * getScalarFieldPtr(const llvm::StringRef fieldName);
21
22    llvm::Value * getScalarField(const llvm::StringRef fieldName);
23
24    // Set the value of a scalar field for the current instance.
25    void setScalarField(const llvm::StringRef fieldName, llvm::Value * value);
26
27    llvm::Value * getAvailableItemCount(const llvm::StringRef name) {
28        return mKernel->getAvailableInputItems(name);
29    }
30
31    llvm::Value * getAccessibleItemCount(const llvm::StringRef name) {
32        return mKernel->getAccessibleInputItems(name);
33    }
34
35    llvm::Value * getProcessedItemCount(const llvm::StringRef name);
36
37    void setProcessedItemCount(const llvm::StringRef name, llvm::Value * value);
38
39    llvm::Value * getProducedItemCount(const llvm::StringRef name);
40
41    void setProducedItemCount(const llvm::StringRef name, llvm::Value * value);
42
43    llvm::Value * getConsumedItemCount(const llvm::StringRef name) const;
44
45    llvm::Value * getTerminationSignal();
46
47    void setTerminationSignal() { setTerminationSignal(getTrue()); }
48
49    void setTerminationSignal(llvm::Value * const value);
50
51    llvm::Value * getCycleCountPtr();
52
53    // Run-time access of Kernel State and parameters of methods for
54    // use in implementing kernels.
55
56    llvm::Value * getInputStreamBlockPtr(const std::string & name, llvm::Value * streamIndex) {
57        return getInputStreamBlockPtr(name, streamIndex, nullptr);
58    }
59
60    llvm::Value * getInputStreamBlockPtr(const std::string & name, llvm::Value * streamIndex, llvm::Value * blockOffset);
61
62    llvm::Value * getInputStreamLogicalBasePtr(const Binding & input);
63
64    llvm::Value * loadInputStreamBlock(const std::string & name, llvm::Value * streamIndex) {
65        return loadInputStreamBlock(name, streamIndex, nullptr);
66    }
67
68    llvm::Value * loadInputStreamBlock(const std::string & name, llvm::Value * streamIndex, llvm::Value * blockOffset);
69
70    llvm::Value * getInputStreamPackPtr(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex) {
71        return getInputStreamPackPtr(name, streamIndex, packIndex, nullptr);
72    }
73
74    llvm::Value * getInputStreamPackPtr(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex, llvm::Value * blockOffset);
75
76    llvm::Value * loadInputStreamPack(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex) {
77        return loadInputStreamPack(name, streamIndex, packIndex, nullptr);
78    }
79
80    llvm::Value * loadInputStreamPack(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex, llvm::Value * blockOffset);
81
82    llvm::Value * getInputStreamSetCount(const std::string & name);
83
84    llvm::Value * getOutputStreamBlockPtr(const std::string & name, llvm::Value * streamIndex) {
85        return getOutputStreamBlockPtr(name, streamIndex, nullptr);
86    }
87
88    llvm::Value * getOutputStreamBlockPtr(const std::string & name, llvm::Value * streamIndex, llvm::Value * blockOffset);
89
90    llvm::Value * getOutputStreamLogicalBasePtr(const Binding & output);
91
92    llvm::StoreInst * storeOutputStreamBlock(const std::string & name, llvm::Value * streamIndex, llvm::Value * toStore) {
93        return storeOutputStreamBlock(name, streamIndex, nullptr, toStore);
94    }
95
96    llvm::StoreInst * storeOutputStreamBlock(const std::string & name, llvm::Value * streamIndex, llvm::Value * blockOffset, llvm::Value * toStore);
97
98    llvm::Value * getOutputStreamPackPtr(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex) {
99        return getOutputStreamPackPtr(name, streamIndex, packIndex, nullptr);
100    }
101
102    llvm::Value * getOutputStreamPackPtr(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex, llvm::Value * blockOffset);
103
104    llvm::StoreInst * storeOutputStreamPack(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex, llvm::Value * toStore) {
105        return storeOutputStreamPack(name, streamIndex, packIndex, nullptr, toStore);
106    }
107
108    llvm::StoreInst * storeOutputStreamPack(const std::string & name, llvm::Value * streamIndex, llvm::Value * packIndex, llvm::Value * blockOffset, llvm::Value * toStore);
109
110    llvm::Value * getOutputStreamSetCount(const std::string & name);
111
112    llvm::Value * getRawInputPointer(const std::string & name, llvm::Value * absolutePosition);
113
114    llvm::Value * getRawOutputPointer(const std::string & name, llvm::Value * absolutePosition);
115
116    llvm::Value * getBaseAddress(const std::string & name);
117
118    void setBaseAddress(const std::string & name, llvm::Value * addr);
119
120    llvm::Value * getCapacity(const std::string & name);
121
122    void setCapacity(const std::string & name, llvm::Value * capacity);
123
124    llvm::CallInst * createDoSegmentCall(const std::vector<llvm::Value *> & args);
125
126    const Kernel * getKernel() const {
127        return mKernel;
128    }
129
130    void setKernel(const Kernel * const kernel) {
131        mKernel = kernel;
132    }
133
134    // overloading wrongly subsitutes this for CBuilder function. renamed for now until I can investigate why.
135    llvm::Value * CreateUDiv2(llvm::Value * const number, const ProcessingRate::RateValue & divisor, const llvm::Twine & Name = "");
136
137    llvm::Value * CreateCeilUDiv2(llvm::Value * const number, const ProcessingRate::RateValue & divisor, const llvm::Twine & Name = "");
138
139    llvm::Value * CreateMul2(llvm::Value * const number, const ProcessingRate::RateValue & factor, const llvm::Twine & Name = "");
140
141    llvm::Value * CreateCeilUMul2(llvm::Value * const number, const ProcessingRate::RateValue & factor, const llvm::Twine & Name = "");
142
143    unsigned getStride() const {
144        return mStride;
145    }
146
147protected:
148
149    KernelBuilder(llvm::LLVMContext & C, unsigned nativeVectorWidth, unsigned vectorWidth, unsigned laneWidth)
150    : IDISA::IDISA_Builder(C, nativeVectorWidth, vectorWidth, laneWidth)
151    , mStride(vectorWidth)
152    , mKernel(nullptr) {
153
154    }
155
156    const unsigned mStride;
157
158    llvm::Value * getScalarFieldPtr(llvm::Value * handle, llvm::Value * index);
159
160    llvm::Value * getScalarFieldPtr(llvm::Value * instance, const std::string & fieldName);
161
162    std::string getKernelName() const final;
163
164protected:
165    const Kernel * mKernel;
166
167};
168
169template <class SpecifiedArchitectureBuilder>
170class KernelBuilderImpl final : public KernelBuilder, public SpecifiedArchitectureBuilder {
171public:
172    KernelBuilderImpl(llvm::LLVMContext & C, unsigned vectorWidth, unsigned laneWidth)
173    : IDISA::IDISA_Builder(C, SpecifiedArchitectureBuilder::NativeBitBlockWidth, vectorWidth, laneWidth)
174    , KernelBuilder(C, SpecifiedArchitectureBuilder::NativeBitBlockWidth, vectorWidth, laneWidth)
175    , SpecifiedArchitectureBuilder(C, vectorWidth, laneWidth) {
176
177    }
178};
179
180}
181
182#endif // KERNEL_BUILDER_H
Note: See TracBrowser for help on using the repository browser.