-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCollectionPatterns.cpp
More file actions
254 lines (222 loc) · 8.13 KB
/
Copy pathCollectionPatterns.cpp
File metadata and controls
254 lines (222 loc) · 8.13 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
#include "Conversion/PythonToPythonBytecode/LoweringHelpers.hpp"
#include "Conversion/PythonToPythonBytecode/PatternPopulators.hpp"
#include "Dialect/EmitPythonBytecode/IR/EmitPythonBytecode.hpp"
#include "Dialect/Python/IR/PythonOps.hpp"
#include "utilities.hpp"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/PatternMatch.h"
#include "llvm/ADT/STLExtras.h"
#include <optional>
#include <vector>
namespace mlir::py {
namespace {
struct BuildDictOpLowering : public mlir::OpRewritePattern<mlir::py::BuildDictOp>
{
using OpRewritePattern<mlir::py::BuildDictOp>::OpRewritePattern;
mlir::LogicalResult matchAndRewrite(mlir::py::BuildDictOp op,
mlir::PatternRewriter &rewriter) const final
{
const auto &requires_expansion = op.getRequiresExpansion();
if (std::any_of(requires_expansion.begin(),
requires_expansion.end(),
[](const auto &el) { return el; })) {
std::optional<mlir::Value> result;
std::vector<mlir::Value> keys;
std::vector<mlir::Value> values;
for (auto [key, value, to_expand] :
llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) {
if (to_expand) {
if (!result.has_value()) {
result = mlir::emitpybytecode::BuildDict::create(
rewriter, op.getLoc(), op.getOutput().getType(), keys, values);
keys.clear();
values.clear();
}
mlir::emitpybytecode::DictUpdate::create(
rewriter, op.getLoc(), *result, value);
} else {
if (!result.has_value()) {
keys.push_back(key);
values.push_back(value);
} else {
ASSERT(keys.empty());
ASSERT(values.empty());
mlir::emitpybytecode::DictAdd::create(
rewriter, op.getLoc(), *result, key, value);
}
}
}
ASSERT(result.has_value());
ASSERT(keys.empty());
ASSERT(values.empty());
rewriter.replaceOp(op, { *result });
} else {
// Plain dict literal: lower 1:1. The register-pressure
// optimisation for large literals lives as a
// canonicalize pattern on emitpybytecode.BuildDict
// (ExpandLargeBuildDict in EmitPythonBytecode.cpp) so
// any path that reaches that op benefits, not just the
// one through this lowering.
rewriter.replaceOpWithNewOp<mlir::emitpybytecode::BuildDict>(
op, op.getOutput().getType(), op.getKeys(), op.getValues());
}
return success();
}
};
// Build a list incrementally by walking (element, requires_expansion)
// pairs: each "expand" entry produces a ListExtend (unpacking *args
// or **kwargs in literals), each non-expand entry produces a
// ListAppend. Returns the resulting BuildList value, which both
// BuildListOp and BuildTupleOp lowerings hand off to their final
// step (replaceOp / wrap in ListToTuple respectively).
mlir::emitpybytecode::BuildList build_list_with_expansion(mlir::PatternRewriter &rewriter,
mlir::Location loc,
mlir::Type list_type,
mlir::ValueRange elements,
llvm::ArrayRef<bool> requires_expansion)
{
auto list =
mlir::emitpybytecode::BuildList::create(rewriter, loc, list_type, mlir::ValueRange{});
for (auto [el, expand] : llvm::zip(elements, requires_expansion)) {
if (expand) {
mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el);
} else {
mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el);
}
}
return list;
}
struct BuildListOpLowering : public mlir::OpRewritePattern<mlir::py::BuildListOp>
{
using OpRewritePattern<mlir::py::BuildListOp>::OpRewritePattern;
mlir::LogicalResult matchAndRewrite(mlir::py::BuildListOp op,
mlir::PatternRewriter &rewriter) const final
{
const auto &requires_expansion = op.getRequiresExpansion();
if (std::any_of(requires_expansion.begin(),
requires_expansion.end(),
[](const auto &el) { return el == 1; })) {
auto list = build_list_with_expansion(rewriter,
op.getLoc(),
op.getOutput().getType(),
op.getElements(),
requires_expansion);
rewriter.replaceOp(op, list);
} else {
// Plain list literal: lower 1:1. The all-constants
// register-pressure rewrite lives as a canonicalize
// pattern on emitpybytecode.BuildList
// (FoldAllConstBuildListIntoExtend in
// EmitPythonBytecode.cpp), so any caller landing on
// the bytecode-level op benefits, not just this path.
rewriter.replaceOpWithNewOp<mlir::emitpybytecode::BuildList>(
op, op.getOutput().getType(), op.getElements());
}
return success();
}
};
using ListAppendOpLowering =
detail::DirectReplaceLowering<mlir::py::ListAppendOp, mlir::emitpybytecode::ListAppend>;
using DictAddOpLowering =
detail::DirectReplaceLowering<mlir::py::DictAddOp, mlir::emitpybytecode::DictAdd>;
struct BuildTupleOpLowering : public mlir::OpRewritePattern<mlir::py::BuildTupleOp>
{
using OpRewritePattern<mlir::py::BuildTupleOp>::OpRewritePattern;
mlir::LogicalResult matchAndRewrite(mlir::py::BuildTupleOp op,
mlir::PatternRewriter &rewriter) const final
{
const auto &requires_expansion = op.getRequiresExpansion();
if (std::any_of(requires_expansion.begin(),
requires_expansion.end(),
[](const auto &el) { return el == 1; })) {
auto list = build_list_with_expansion(rewriter,
op.getLoc(),
op.getOutput().getType(),
op.getElements(),
requires_expansion);
rewriter.replaceOpWithNewOp<mlir::emitpybytecode::ListToTuple>(
op, op.getOutput().getType(), list);
} else {
rewriter.replaceOpWithNewOp<mlir::emitpybytecode::BuildTuple>(
op, op.getOutput().getType(), op.getElements());
}
return success();
}
};
struct BuildSetOpLowering : public mlir::OpRewritePattern<mlir::py::BuildSetOp>
{
using OpRewritePattern<mlir::py::BuildSetOp>::OpRewritePattern;
mlir::LogicalResult matchAndRewrite(mlir::py::BuildSetOp op,
mlir::PatternRewriter &rewriter) const final
{
const auto &requires_expansion = op.getRequiresExpansion();
if (std::any_of(requires_expansion.begin(),
requires_expansion.end(),
[](const auto &el) { return el == 1; })) {
std::vector<mlir::Value> elements;
std::optional<mlir::Value> set;
for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) {
if (expand) {
if (!set.has_value()) {
set = mlir::emitpybytecode::BuildSet::create(
rewriter, op->getLoc(), op.getOutput().getType(), elements);
} else {
for (auto el : elements) {
mlir::emitpybytecode::SetAdd::create(
rewriter, op.getLoc(), *set, el);
}
}
elements.clear();
mlir::emitpybytecode::SetUpdate::create(rewriter, op.getLoc(), *set, el);
} else {
elements.push_back(el);
}
}
ASSERT(set.has_value());
for (auto el : elements) {
mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el);
}
rewriter.replaceOp(op, *set);
} else {
rewriter.replaceOpWithNewOp<mlir::emitpybytecode::BuildSet>(
op, op.getOutput().getType(), op.getElements());
}
return success();
}
};
using SetAddOpLowering =
detail::DirectReplaceLowering<mlir::py::SetAddOp, mlir::emitpybytecode::SetAdd>;
using BuildStringOpLowering =
detail::DirectReplaceLowering<mlir::py::BuildStringOp, mlir::emitpybytecode::BuildString>;
struct FormatValueOpLowering : public mlir::OpRewritePattern<mlir::py::FormatValueOp>
{
using OpRewritePattern<mlir::py::FormatValueOp>::OpRewritePattern;
mlir::LogicalResult matchAndRewrite(mlir::py::FormatValueOp op,
mlir::PatternRewriter &rewriter) const final
{
rewriter.replaceOpWithNewOp<mlir::emitpybytecode::FormatValue>(op,
op.getOutput().getType(),
op.getValue(),
static_cast<uint8_t>(op.getConversion()));
return success();
}
};
using BuildSliceOpLowering =
detail::DirectReplaceLowering<mlir::py::BuildSliceOp, mlir::emitpybytecode::BuildSlice>;
}// namespace
void populateCollectionPatterns(mlir::RewritePatternSet &patterns)
{
auto *ctx = patterns.getContext();
patterns.add<BuildDictOpLowering,
DictAddOpLowering,
BuildListOpLowering,
ListAppendOpLowering,
BuildTupleOpLowering,
BuildSetOpLowering,
SetAddOpLowering,
BuildStringOpLowering,
FormatValueOpLowering,
BuildSliceOpLowering>(ctx);
}
}// namespace mlir::py