-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOps.cpp
More file actions
296 lines (268 loc) · 9.5 KB
/
Ops.cpp
File metadata and controls
296 lines (268 loc) · 9.5 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "Dialect.hpp"
#include "PythonAttributes.hpp"
#include "PythonOps.hpp"
#include "PythonTypes.hpp"
#include "mlir/IR/AttributeSupport.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/Interfaces/FunctionImplementation.h"
#include "llvm/ADT/TypeSwitch.h"
#include "Python/IR/Dialect.cpp.inc"
namespace mlir {
namespace py {
void PythonDialect::initialize()
{
addOperations<
#define GET_OP_LIST
#include "Python/IR/Ops.cpp.inc"
>();
addTypes<
#define GET_TYPEDEF_LIST
#include "Python/IR/PythonTypes.cpp.inc"
>();
addAttributes<
#define GET_ATTRDEF_LIST
#include "Python/IR/PythonAttributes.cpp.inc"
>();
}
void ConstantOp::build(mlir::OpBuilder &builder, mlir::OperationState &state, double value)
{
ConstantOp::build(builder,
state,
PyObjectType::get(builder.getContext()),
FloatAttr::get(builder.getF64Type(), value));
}
void ConstantOp::build(mlir::OpBuilder &builder, mlir::OperationState &state, bool value)
{
ConstantOp::build(
builder, state, PyObjectType::get(builder.getContext()), builder.getBoolAttr(value));
}
void ConstantOp::build(mlir::OpBuilder &builder, mlir::OperationState &state, mlir::NoneType)
{
ConstantOp::build(
builder, state, PyObjectType::get(builder.getContext()), builder.getUnitAttr());
}
void ConstantOp::build(mlir::OpBuilder &builder,
mlir::OperationState &state,
mlir::StringAttr str)
{
ConstantOp::build(builder, state, PyObjectType::get(builder.getContext()), str);
}
void ConstantOp::build(mlir::OpBuilder &builder,
mlir::OperationState &state,
mlir::IntegerAttr int_attr)
{
ConstantOp::build(builder, state, PyObjectType::get(builder.getContext()), int_attr);
}
void ConstantOp::build(mlir::OpBuilder &builder,
mlir::OperationState &state,
std::vector<std::byte> bytes)
{
auto byte_array_attr = mlir::DenseIntElementsAttr::get(
mlir::VectorType::get(
static_cast<int64_t>(bytes.size()), builder.getIntegerType(8, false)),
mlir::ArrayRef<unsigned char>{
llvm::bit_cast<unsigned char *>(bytes.data()), bytes.size() });
ConstantOp::build(builder, state, PyObjectType::get(builder.getContext()), byte_array_attr);
}
void ConstantOp::build(mlir::OpBuilder &builder,
mlir::OperationState &state,
mlir::py::PyEllipsisType)
{
ConstantOp::build(builder,
state,
PyObjectType::get(builder.getContext()),
EllipsisAttr::get(builder.getContext()));
}
void ConstantOp::build(mlir::OpBuilder &builder,
mlir::OperationState &state,
mlir::ArrayRef<mlir::Attribute> elements)
{
ConstantOp::build(builder,
state,
PyObjectType::get(builder.getContext()),
mlir::ArrayAttr::get(builder.getContext(), std::move(elements)));
}
EllipsisAttr EllipsisAttr::get(mlir::MLIRContext *context)
{
return mlir::detail::AttributeUniquer::get<mlir::py::EllipsisAttr>(context);
}
SuccessorOperands CondBranchSubclassOp::getSuccessorOperands(unsigned index)
{
assert(index < getNumSuccessors() && "invalid successor index");
return SuccessorOperands(
index == 0 ? getTrueDestOperandsMutable() : getFalseDestOperandsMutable());
}
// Based on CIR loop interface implementation
void WhileOp::getSuccessorRegions(mlir::RegionBranchPoint point,
llvm::SmallVectorImpl<mlir::RegionSuccessor> ®ions)
{
// Branching to first region: go to condition.
if (point.isParent()) {
regions.emplace_back(&getCondition(), getCondition().getArguments());
}
// Branching from condition: go to body or, exit or orelse if non-empty.
else if (point.getRegionOrNull() == &getCondition()) {
if (getOrelse().empty()) {
regions.emplace_back(RegionSuccessor(getOperation()->getResults()));
} else {
regions.emplace_back(&getOrelse(), getOrelse().getArguments());
}
regions.emplace_back(&getBody(), getBody().getArguments());
}
// Branching from body: go to condition.
else if (point.getRegionOrNull() == &getBody()) {
regions.emplace_back(&getCondition(), getCondition().getArguments());
}
// Branching from orelse - can't go anywhere else.
else if (point.getRegionOrNull() == &getOrelse()) {
} else {
llvm_unreachable("unexpected branch origin");
}
}
void ForLoopOp::getSuccessorRegions(mlir::RegionBranchPoint point,
llvm::SmallVectorImpl<mlir::RegionSuccessor> ®ions)
{
// Branching to first region: go to step.
if (point.isParent()) {
regions.emplace_back(&getStep(), getStep().getArguments());
}
// Branching from condition: go to body or, exit or orelse if non-empty.
else if (point.getRegionOrNull() == &getStep()) {
if (getOrelse().empty()) {
regions.emplace_back(getOperation()->getResults());
} else {
regions.emplace_back(&getOrelse(), getOrelse().getArguments());
}
regions.emplace_back(&getBody(), getBody().getArguments());
}
// Branching from body: go to step.
else if (point.getRegionOrNull() == &getBody()) {
regions.emplace_back(&getStep(), getStep().getArguments());
}
// Branching from orelse - can't go anywhere else.
else if (point.getRegionOrNull() == &getOrelse()) {
} else {
llvm_unreachable("unexpected branch origin");
}
}
void TryOp::getSuccessorRegions(mlir::RegionBranchPoint point,
llvm::SmallVectorImpl<mlir::RegionSuccessor> ®ions)
{
// Branching to first region: go to try body.
if (point.isParent()) {
regions.emplace_back(&getBody(), getBody().getArguments());
}
// Branching from try body: go to first handler and orelse block if non-empty, if there no
// handlers go to finally
else if (point.getRegionOrNull() == &getBody()) {
if (!getHandlers().empty()) {
regions.emplace_back(&getHandlers().front(), getHandlers().front().getArguments());
if (!getOrelse().empty()) {
regions.emplace_back(&getOrelse(), getOrelse().getArguments());
}
} else {
assert(getOrelse().empty());
regions.emplace_back(&getFinally(), getFinally().getArguments());
}
}
// Branching from handler: go to next handler if there is one, if not go to finally.
else if (auto it = std::find_if(getHandlers().begin(),
getHandlers().end(),
[&point](
mlir::Region &handler) { return point.getRegionOrNull() == &handler; });
it != getHandlers().end()) {
if (std::next(it) != getHandlers().end()) {
it++;
regions.emplace_back(&*it, it->getArguments());
}
if (!getFinally().empty()) {
regions.emplace_back(&getFinally(), getFinally().getArguments());
}
// regions.emplace_back(getOperation()->getParentRegion());
}
// Branch from orelse: go to finally or parent
else if (point.getRegionOrNull() == &getOrelse()) {
if (!getFinally().empty()) {
regions.emplace_back(&getFinally(), getFinally().getArguments());
} else {
regions.emplace_back(getOperation()->getParentRegion());
}
}
// Branch from finally: go to parent
else if (point.getRegionOrNull() == &getFinally()) {
regions.emplace_back(getOperation()->getParentRegion());
}
}
void ControlFlowYield::getSuccessorRegions(llvm::ArrayRef<mlir::Attribute> operands,
llvm::SmallVectorImpl<mlir::RegionSuccessor> ®ions)
{
static_assert(ControlFlowYield::hasTrait<
mlir::OpTrait::HasParent<TryOp, ForLoopOp, WithOp, WhileOp, TryHandlerScope>::Impl>());
if (getKind().has_value()) { return; }
auto result =
llvm::TypeSwitch<Operation *, LogicalResult>(getOperation()->getParentOp())
.Case<TryOp>([®ions](TryOp op) -> LogicalResult {
// regions.emplace_back(&op.getRegion());
llvm_unreachable("TODO");
return failure();
})
.Case<ForLoopOp>([this, ®ions](ForLoopOp op) -> LogicalResult {
if (getOperation()->getParentRegion() == &op.getStep()) {
regions.emplace_back(&op.getBody());
} else if (getOperation()->getParentRegion() == &op.getBody()) {
regions.emplace_back(&op.getStep());
} else if (getOperation()->getParentRegion() == &op.getOrelse()) {
} else {
llvm_unreachable("unexpected branch origin");
}
return success();
})
.Case<WithOp>([®ions](WithOp op) -> LogicalResult {
regions.emplace_back(op->getParentRegion());
return success();
})
.Case<WhileOp>([this, ®ions](WhileOp op) -> LogicalResult {
if (getOperation()->getParentRegion() == &op.getCondition()) {
regions.emplace_back(&op.getBody());
} else if (getOperation()->getParentRegion() == &op.getBody()) {
regions.emplace_back(&op.getCondition());
} else if (getOperation()->getParentRegion() == &op.getOrelse()) {
} else {
llvm_unreachable("unexpected branch origin");
}
return success();
})
.Case<TryHandlerScope>([this, ®ions](TryHandlerScope op) -> LogicalResult {
llvm_unreachable("todo");
return failure();
})
.Default([](Operation *) -> LogicalResult {
llvm_unreachable("TODO");
std::abort();
return failure();
});
assert(result.succeeded());
}
void TryHandlerScope::getSuccessorRegions(mlir::RegionBranchPoint point,
llvm::SmallVectorImpl<mlir::RegionSuccessor> ®ions)
{
if (point.getRegionOrNull() == &getCond()) {
regions.emplace_back(&getHandler(), getHandler().getArguments());
}
regions.emplace_back(getOperation()->getParentRegion());
}
}// namespace py
}// namespace mlir
#define GET_OP_CLASSES
#include "Python/IR/Ops.cpp.inc"
#define GET_TYPEDEF_CLASSES
#include "Python/IR/PythonTypes.cpp.inc"
#include "Python/IR/PythonOpsEnums.cpp.inc"
#define GET_ATTRDEF_CLASSES
#include "Python/IR/PythonAttributes.cpp.inc"