This repository has been archived by the owner on May 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathVisitor.h
221 lines (194 loc) · 6.9 KB
/
Visitor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//===----------------------------------------------------------------------===//
//
// Adapated from ScaleHLS https://github.com/hanchenye/scalehls
//
//===----------------------------------------------------------------------===//
#ifndef OPENHLS_DIALECT_HLS_VISITOR_H
#define OPENHLS_DIALECT_HLS_VISITOR_H
#include "InitAllDialects.h"
#include "llvm/ADT/TypeSwitch.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
namespace mlir {
namespace openhls {
/// This class is a visitor for SSACFG operation nodes.
template <typename ConcreteType, typename ResultType, typename... ExtraArgs>
class HLSVisitorBase {
public:
ResultType dispatchVisitor(Operation *op, ExtraArgs... args) {
auto *thisCast = static_cast<ConcreteType *>(this);
return TypeSwitch<Operation *, ResultType>(op)
.template Case<
// SCF statements.
scf::ForOp, scf::IfOp, scf::ParallelOp, scf::ReduceOp,
scf::ReduceReturnOp, scf::YieldOp,
// Affine statements.
AffineForOp, AffineIfOp, AffineParallelOp, AffineApplyOp,
AffineMaxOp, AffineMinOp, AffineLoadOp, AffineStoreOp,
AffineVectorLoadOp, AffineVectorStoreOp, AffineYieldOp,
// Vector-related statements.
vector::TransferReadOp, vector::TransferWriteOp,
vector::BroadcastOp,
// Memref-related statements.
memref::AllocOp, memref::AllocaOp, memref::LoadOp, memref::StoreOp,
memref::GlobalOp, memref::GetGlobalOp,
memref::DeallocOp, memref::CopyOp, memref::SubViewOp, memref::TensorStoreOp,
tensor::ReshapeOp, memref::ReshapeOp, memref::CollapseShapeOp,
memref::ExpandShapeOp, memref::ReinterpretCastOp,
bufferization::ToMemrefOp, bufferization::ToTensorOp,
// HLS dialect operations.
// DataflowBufferOp, StreamChannelOp, StreamReadOp, StreamWriteOp,
// PrimMulOp, PrimCastOp, PrimBufferOp,
// Control flow operations.
func::CallOp, func::ReturnOp,
// Unary expressions.
math::AbsOp, math::CeilOp, math::CosOp, math::SinOp, math::TanhOp,
math::SqrtOp, math::RsqrtOp, math::ExpOp, math::Exp2Op, math::LogOp,
math::Log2Op, math::Log10Op, arith::NegFOp,
// Float binary expressions.
arith::CmpFOp, arith::AddFOp, arith::SubFOp, arith::MulFOp,
arith::DivFOp, arith::RemFOp, arith::MaxFOp, arith::MinFOp,
// Integer binary expressions.
arith::CmpIOp, arith::AddIOp, arith::SubIOp, arith::MulIOp,
arith::DivSIOp, arith::RemSIOp, arith::DivUIOp, arith::RemUIOp,
arith::XOrIOp, arith::AndIOp, arith::OrIOp, arith::ShLIOp,
arith::ShRSIOp, arith::ShRUIOp, arith::MaxSIOp, arith::MinSIOp,
arith::MaxUIOp, arith::MinUIOp,
// Special expressions.
arith::SelectOp, arith::ConstantOp, arith::TruncIOp,
arith::TruncFOp, arith::ExtUIOp, arith::ExtSIOp, arith::IndexCastOp,
arith::UIToFPOp, arith::SIToFPOp, arith::FPToSIOp, arith::FPToUIOp>(
[&](auto opNode) -> ResultType {
return thisCast->visitOp(opNode, args...);
})
.Default([&](auto opNode) -> ResultType {
return thisCast->visitInvalidOp(op, args...);
});
}
/// This callback is invoked on any invalid operations.
ResultType visitInvalidOp(Operation *op, ExtraArgs... args) {
op->emitOpError("is unsupported operation.");
abort();
}
/// This callback is invoked on any operations that are not handled by the
/// concrete visitor.
ResultType visitUnhandledOp(Operation *op, ExtraArgs... args) {
return ResultType();
}
#define HANDLE(OPTYPE) \
ResultType visitOp(OPTYPE op, ExtraArgs... args) { \
return static_cast<ConcreteType *>(this)->visitUnhandledOp(op, args...); \
}
// SCF statements.
HANDLE(scf::ForOp);
HANDLE(scf::IfOp);
HANDLE(scf::ParallelOp);
HANDLE(scf::ReduceOp);
HANDLE(scf::ReduceReturnOp);
HANDLE(scf::YieldOp);
// Affine statements.
HANDLE(AffineForOp);
HANDLE(AffineIfOp);
HANDLE(AffineParallelOp);
HANDLE(AffineApplyOp);
HANDLE(AffineMaxOp);
HANDLE(AffineMinOp);
HANDLE(AffineLoadOp);
HANDLE(AffineStoreOp);
HANDLE(AffineVectorLoadOp);
HANDLE(AffineVectorStoreOp);
HANDLE(AffineYieldOp);
// Vector-related statements.
HANDLE(vector::TransferReadOp);
HANDLE(vector::TransferWriteOp);
HANDLE(vector::BroadcastOp);
// Memref-related statements.
HANDLE(memref::AllocOp);
HANDLE(memref::AllocaOp);
HANDLE(memref::LoadOp);
HANDLE(memref::StoreOp);
HANDLE(memref::GlobalOp);
HANDLE(memref::GetGlobalOp);
HANDLE(memref::DeallocOp);
HANDLE(memref::CopyOp);
HANDLE(memref::SubViewOp);
HANDLE(memref::TensorStoreOp);
HANDLE(tensor::ReshapeOp);
HANDLE(memref::ReshapeOp);
HANDLE(memref::CollapseShapeOp);
HANDLE(memref::ExpandShapeOp);
HANDLE(memref::ReinterpretCastOp);
HANDLE(bufferization::ToMemrefOp);
HANDLE(bufferization::ToTensorOp);
// HLS dialect operations.
// HANDLE(DataflowBufferOp);
// HANDLE(StreamChannelOp);
// HANDLE(StreamReadOp);
// HANDLE(StreamWriteOp);
// HANDLE(PrimMulOp);
// HANDLE(PrimCastOp);
// HANDLE(PrimBufferOp);
// Control flow operations.
HANDLE(func::CallOp);
HANDLE(func::ReturnOp);
// Unary expressions.
HANDLE(math::AbsOp);
HANDLE(math::CeilOp);
HANDLE(math::CosOp);
HANDLE(math::SinOp);
HANDLE(math::TanhOp);
HANDLE(math::SqrtOp);
HANDLE(math::RsqrtOp);
HANDLE(math::ExpOp);
HANDLE(math::Exp2Op);
HANDLE(math::LogOp);
HANDLE(math::Log2Op);
HANDLE(math::Log10Op);
HANDLE(arith::NegFOp);
// Float binary expressions.
HANDLE(arith::CmpFOp);
HANDLE(arith::AddFOp);
HANDLE(arith::SubFOp);
HANDLE(arith::MulFOp);
HANDLE(arith::DivFOp);
HANDLE(arith::RemFOp);
HANDLE(arith::MaxFOp);
HANDLE(arith::MinFOp);
// Integer binary expressions.
HANDLE(arith::CmpIOp);
HANDLE(arith::AddIOp);
HANDLE(arith::SubIOp);
HANDLE(arith::MulIOp);
HANDLE(arith::DivSIOp);
HANDLE(arith::RemSIOp);
HANDLE(arith::DivUIOp);
HANDLE(arith::RemUIOp);
HANDLE(arith::XOrIOp);
HANDLE(arith::AndIOp);
HANDLE(arith::OrIOp);
HANDLE(arith::ShLIOp);
HANDLE(arith::ShRSIOp);
HANDLE(arith::ShRUIOp);
HANDLE(arith::MaxSIOp);
HANDLE(arith::MinSIOp);
HANDLE(arith::MaxUIOp);
HANDLE(arith::MinUIOp);
// Special expressions.
HANDLE(arith::SelectOp);
HANDLE(arith::ConstantOp);
HANDLE(arith::TruncIOp);
HANDLE(arith::TruncFOp);
HANDLE(arith::ExtUIOp);
HANDLE(arith::ExtSIOp);
HANDLE(arith::ExtFOp);
HANDLE(arith::IndexCastOp);
HANDLE(arith::UIToFPOp);
HANDLE(arith::SIToFPOp);
HANDLE(arith::FPToUIOp);
HANDLE(arith::FPToSIOp);
#undef HANDLE
};
} // namespace openhls
} // namespace mlir
#endif // OPENHLS_DIALECT_HLS_VISITOR_H