-
Notifications
You must be signed in to change notification settings - Fork 10
/
canonicalization_patterns.cc
497 lines (422 loc) · 18.8 KB
/
canonicalization_patterns.cc
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/UseDefLists.h"
#include "mlir/IR/Value.h"
#include "mlir/Support/LogicalResult.h"
#include "sair_attributes.h"
#include "sair_op_interfaces.h"
#include "sair_ops.h"
#include "sair_types.h"
#include "sequence.h"
namespace sair {
namespace {
// Extends an mapping with the identity mapping to match the given number
// of dimensions.
MappingAttr ExtendWithIdentity(MappingAttr old_mapping, int domain_size,
int new_mapping_size) {
llvm::SmallVector<MappingExpr, 4> dimensions;
dimensions.reserve(new_mapping_size);
llvm::append_range(dimensions, old_mapping);
for (int i = dimensions.size(); i < new_mapping_size; ++i) {
dimensions.push_back(MappingDimExpr::get(i, old_mapping.getContext()));
}
return MappingAttr::get(old_mapping.getContext(), domain_size, dimensions);
}
// Redirects `use` to the `init` operand of `op` if `op` has an empty sequential
// domain. Returns true if any change was made.
bool SimplifyFbyOp(ValueOperand &use, SairFbyOp op) {
if (!op.getSequentialDomain().empty()) return false;
use.SubstituteValue(op.Init().Get());
return true;
}
// Simplify a sair.proj_operand of a Sair operation, by bypassing projections
// with an empty projection domain and by flattening chains of projections.
// Returns true is any simplification was made.
template <typename ProjOp>
bool SimplifyProjOp(ValueOperand &use, ProjOp op,
mlir::PatternRewriter &rewriter) {
if (op.getProjectionDomain().empty()) {
use.SubstituteValue(op.Value().Get());
return true;
}
ProjOp prev_op = op.getValue().template getDefiningOp<ProjOp>();
if (prev_op == nullptr) return false;
if (prev_op.GetCopies(0).size() != 0) return false;
llvm::SmallVector<mlir::Value, 4> projection_domain;
projection_domain.reserve(op.getProjectionDomain().size() +
prev_op.getProjectionDomain().size());
llvm::append_range(projection_domain, op.getProjectionDomain());
llvm::append_range(projection_domain, prev_op.getProjectionDomain());
llvm::SmallVector<DomainShapeDim, 4> shape_dims;
shape_dims.reserve(op.getShape().NumDimensions() +
prev_op.getProjectionDomain().size());
llvm::append_range(shape_dims, op.getShape().Dimensions());
llvm::ArrayRef<DomainShapeDim> prev_shape_dims =
prev_op.getShape().Dimensions();
llvm::append_range(shape_dims, prev_op.getShape().Dimensions().drop_front(
prev_op.results_rank()));
DomainShapeAttr shape = DomainShapeAttr::get(op.getContext(), shape_dims);
MappingAttr new_mapping =
ExtendWithIdentity(op.Value().Mapping(), shape_dims.size(),
prev_op.getDomain().size())
.Compose(prev_op.Value().Mapping());
mlir::ArrayAttr mapping_array = rewriter.getArrayAttr({new_mapping});
rewriter.setInsertionPoint(op);
ProjOp new_op = rewriter.create<ProjOp>(
op.getLoc(), op.getType(), op.getParallelDomain(), projection_domain,
mapping_array, prev_op.getValue(), shape, op.getInstancesAttr(),
op.getCopiesAttr());
use.set_value(new_op.getResult());
return true;
}
// simplify the operands of the Sair operation:
// - Folds sair.proj_any and sair.proj_last operations with an empty projection
// domain.
// - Folds sair.fby operations with an empty sequential domain.
// - Flatten chains of sair.proj_last and sair.proj_any operations.
class SimplifySairOperands : public RewritePattern {
public:
SimplifySairOperands(MLIRContext *context)
: RewritePattern(mlir::Pattern::MatchAnyOpTypeTag(), 1, context) {}
mlir::LogicalResult matchAndRewrite(
mlir::Operation *op, mlir::PatternRewriter &rewriter) const override {
SairOp sair_op = dyn_cast<SairOp>(op);
if (sair_op == nullptr) return mlir::failure();
bool simplified = false;
rewriter.startOpModification(op);
for (ValueOperand operand : sair_op.ValueOperands()) {
mlir::Operation *defining_op = operand.value().getDefiningOp();
if (auto proj_last = dyn_cast<SairProjLastOp>(defining_op)) {
simplified |= SimplifyProjOp(operand, proj_last, rewriter);
}
if (auto proj_any = dyn_cast<SairProjAnyOp>(defining_op)) {
simplified |= SimplifyProjOp(operand, proj_any, rewriter);
}
if (auto fby = dyn_cast<SairFbyOp>(defining_op)) {
simplified |= SimplifyFbyOp(operand, fby);
}
MappingAttr canonicalized_mapping = operand.Mapping().Canonicalize();
if (canonicalized_mapping != operand.Mapping()) {
operand.SetMapping(canonicalized_mapping);
simplified = true;
}
}
if (simplified) {
rewriter.finalizeOpModification(op);
} else {
rewriter.cancelOpModification(op);
}
return mlir::success(simplified);
}
};
// Remove duplicate inputs and duplicate outputs of sair.map operations.
mlir::LogicalResult DeduplicateMapInputsOutputs(
SairMapOp op, mlir::PatternRewriter &rewriter) {
if (op.HasCopies()) return mlir::failure();
int domain_size = op.getDomain().size();
llvm::SmallVector<mlir::Value> new_operands;
llvm::SmallVector<mlir::Attribute> new_mappings;
llvm::SmallVector<mlir::Value> old_results_to_keep;
llvm::SmallVector<mlir::Value> new_scalar_results;
llvm::SmallVector<mlir::Type> new_result_types;
llvm::SmallBitVector remaining_outputs(op->getNumResults());
std::vector<int> block_args_to_erase;
for (ValueOperand operand : op.ValueOperands()) {
mlir::Value argument =
op.block().getArgument(domain_size + operand.position());
// Deduplicate inputs.
auto previous_operands = op.ValueOperands().take_front(operand.position());
for (ValueOperand previous_operand : previous_operands) {
if (operand.Get() != previous_operand.Get()) continue;
mlir::Value previous_argument =
op.block_inputs()[previous_operand.position()];
// Don't deduplicate with dead arguments that will be removed.
if (previous_argument.use_empty()) continue;
argument.replaceAllUsesWith(previous_argument);
break;
}
// Remove dead inputs.
if (argument.use_empty()) {
block_args_to_erase.push_back(domain_size + operand.position());
continue;
}
new_operands.push_back(operand.value());
new_mappings.push_back(operand.Mapping());
}
for (int i = block_args_to_erase.size() - 1; i >= 0; --i) {
op.block().eraseArgument(block_args_to_erase[i]);
}
SairReturnOp return_op = cast<SairReturnOp>(op.block().getTerminator());
for (int i = 0, e = op.getNumResults(); i < e; ++i) {
mlir::Value scalar_value = return_op.getOperand(i);
mlir::Value result = op.getResult(i);
// Deduplicate results.
for (int j = 0; j < i; ++j) {
if (scalar_value != return_op.getOperand(j)) continue;
bool same_storage = true;
for (int k = 0, e = op.NumInstances(); k < e; ++k) {
if (op.GetDecisions(k).storage() != op.GetDecisions(j).storage()) {
same_storage = false;
break;
}
}
if (!same_storage) continue;
// Don't deduplicate with dead results that will be removed.
if (op.getResult(j).use_empty()) continue;
result.replaceAllUsesWith(op.getResult(j));
break;
}
// Remove dead results.
if (result.use_empty() && op.GetCopies(i).empty()) continue;
// Add the result and corresponding attributes to the list of results to
// preserve.
old_results_to_keep.push_back(result);
new_scalar_results.push_back(scalar_value);
new_result_types.push_back(result.getType());
remaining_outputs.set(i);
}
// Create the new operation if necessary.
if (new_operands.size() == op.ValueOperands().size() &&
old_results_to_keep.size() == op.getNumResults()) {
return mlir::failure();
}
rewriter.setInsertionPoint(return_op);
rewriter.create<SairReturnOp>(return_op.getLoc(), new_scalar_results);
rewriter.eraseOp(return_op);
rewriter.setInsertionPoint(op);
mlir::ArrayAttr new_instances = MkArrayAttrMapper<DecisionsAttr>(
MapStorage(MkArrayAttrFilter(remaining_outputs)))(op.getInstancesAttr());
for (int operand : llvm::reverse(block_args_to_erase)) {
new_instances = EraseOperandFromDecisions(new_instances, operand);
}
mlir::ArrayAttr new_copies =
MkArrayAttrFilter(remaining_outputs)(op.getCopiesAttr());
SairMapOp new_op = rewriter.create<SairMapOp>(
op.getLoc(), new_result_types, op.getDomain(),
rewriter.getArrayAttr(new_mappings), new_operands, op.getShape(),
new_instances, new_copies);
new_op.getBody().takeBody(op.getBody());
for (auto [old_res, new_res] :
llvm::zip(old_results_to_keep, new_op.getResults())) {
old_res.replaceAllUsesWith(new_res);
}
rewriter.eraseOp(op);
return mlir::success();
}
// Remove a followed-by operation that depends on its own result, i.e.
// %1 = sair.fby[...] %0(...) then[...] %1(d0, d1, ..., dn)
// and make its users use the init value instead.
class RemoveCyclicFby : public OpRewritePattern<SairFbyOp> {
public:
using OpRewritePattern<SairFbyOp>::OpRewritePattern;
mlir::LogicalResult matchAndRewrite(
SairFbyOp op, PatternRewriter &rewriter) const override {
// Only apply to cycling followed-by with identity mappings.
if (op.getResult() != op.getValue() || !op.Value().Mapping().IsIdentity())
return mlir::failure();
UpdateValueUses(op.getResult(), op.Init().Get());
op.erase();
return mlir::success();
}
};
// Given a bit vector indicating which dimensions are actually in use, populate
// `parallel_dimensions` and `other_dimensions` with values from
// `parallel_domain` and `other_domain`, respectively, that correspond to the
// domain dimensions that are in use. Assume `other_domain` immediately follows
// `parallel_domain` in a sequential dimension indexing scheme. Also set
// `mapping` to be a mapping from original (combined) domain
// dimensions to the new dimensions.
void RemoveUnusedDomainDimensions(
mlir::MLIRContext *context, const llvm::SmallBitVector &used_dimensions,
mlir::ValueRange parallel_domain, mlir::ValueRange other_domain,
llvm::SmallVectorImpl<mlir::Value> ¶llel_dimensions,
llvm::SmallVectorImpl<mlir::Value> &other_dimensions,
MappingAttr &mapping) {
assert(used_dimensions.size() ==
parallel_domain.size() + other_domain.size());
int num_parallel_dims = parallel_domain.size();
llvm::SmallVector<MappingExpr, 4> exprs;
for (int dimension : used_dimensions.set_bits()) {
if (dimension >= num_parallel_dims) {
other_dimensions.push_back(other_domain[dimension - num_parallel_dims]);
} else {
parallel_dimensions.push_back(parallel_domain[dimension]);
}
exprs.push_back(MappingDimExpr::get(dimension, context));
}
mapping = MappingAttr::get(context, used_dimensions.size(), exprs);
}
// Canonicalization pattern that drops unused dimensions from projection ops.
template <typename OpTy>
class RemoveUnreferencedDims : public OpRewritePattern<OpTy> {
static_assert(llvm::is_one_of<OpTy, SairProjAnyOp, SairProjLastOp>::value,
"pattern applies to projection ops only");
public:
using OpRewritePattern<OpTy>::OpRewritePattern;
mlir::LogicalResult matchAndRewrite(
OpTy op, PatternRewriter &rewriter) const override {
// Collect dimensions that appear in the mapping.
llvm::SmallBitVector used_dimensions(op.getDomain().size());
used_dimensions |= op.Value().Mapping().DependencyMask();
if (used_dimensions.all()) return mlir::failure();
if (op.HasCopies()) return mlir::failure();
// Prepare op components with unused dimensions removed.
MappingAttr mapping;
llvm::SmallVector<mlir::Value> parallel_dimensions, projection_dimensions;
RemoveUnusedDomainDimensions(op.getContext(), used_dimensions,
op.getParallelDomain(),
op.getProjectionDomain(), parallel_dimensions,
projection_dimensions, mapping);
DomainShapeAttr new_shape = op.getShape().AccessedShape(mapping);
SairOp new_op =
op.ReCreateWithNewDomain({parallel_dimensions, projection_dimensions},
new_shape, mapping.Inverse(), rewriter);
// Replace the original op. The result type has the rank equal to that of
// the parallel domain. Trim the mapping accordingly.
MappingAttr partial_mapping = mapping.Resize(parallel_dimensions.size());
UpdateValueUses(op, {new_op->getResult(0), partial_mapping});
rewriter.eraseOp(op);
return mlir::success();
}
};
// Canonicalization pattern that drops unused dimensions from the followed-by
// operation.
template <>
class RemoveUnreferencedDims<SairFbyOp> : public OpRewritePattern<SairFbyOp> {
public:
using OpRewritePattern<SairFbyOp>::OpRewritePattern;
mlir::LogicalResult matchAndRewrite(
SairFbyOp op, PatternRewriter &rewriter) const override {
// Collect dimensions that appear in mappings.
llvm::SmallBitVector used_dimensions(op.getDomain().size());
used_dimensions |= op.Value().Mapping().DependencyMask();
used_dimensions |= op.Init().Mapping().DependencyMask();
if (used_dimensions.all()) return mlir::failure();
if (op.HasCopies()) return mlir::failure();
// Prepare op components with unused dimensions removed.
MappingAttr direct_mapping;
llvm::SmallVector<mlir::Value> parallel_dimensions, sequential_dimensions;
RemoveUnusedDomainDimensions(op.getContext(), used_dimensions,
op.getParallelDomain(),
op.getSequentialDomain(), parallel_dimensions,
sequential_dimensions, direct_mapping);
DomainShapeAttr new_shape = op.getShape().AccessedShape(direct_mapping);
SairOp new_op =
op.ReCreateWithNewDomain({parallel_dimensions, sequential_dimensions},
new_shape, direct_mapping.Inverse(), rewriter);
// Replace the original op.
UpdateValueUses(op, {new_op->getResult(0), direct_mapping});
rewriter.eraseOp(op);
return mlir::success();
}
};
// Canonicalization patternt that updates the sequence numbers of compute
// operations in the program operation to be contiguous zero-based values.
class NormalizeSequenceNumbers : public mlir::OpRewritePattern<SairProgramOp> {
public:
using mlir::OpRewritePattern<SairProgramOp>::OpRewritePattern;
mlir::LogicalResult matchAndRewrite(
SairProgramOp op, mlir::PatternRewriter &rewriter) const override {
SequenceAnalysis sequence_analysis(op);
bool changed = false;
for (auto en : llvm::enumerate(sequence_analysis.Ops())) {
ComputeOpInstance nested_op = en.value();
int64_t inferred_sequence_number = en.index();
DecisionsAttr decisions = nested_op.GetDecisions();
if (decisions.sequence() == nullptr) continue;
int64_t current_sequence_number = decisions.sequence().getInt();
if (current_sequence_number != inferred_sequence_number) {
rewriter.modifyOpInPlace(op, [&] {
nested_op.SetDecisions(
UpdateSequence(decisions, inferred_sequence_number));
});
changed = true;
}
}
return success(changed);
}
};
} // end namespace
void SairCopyOp::getCanonicalizationPatterns(mlir::RewritePatternSet &patterns,
mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairExitOp::getCanonicalizationPatterns(mlir::RewritePatternSet &patterns,
mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairFromMemRefOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairLoadFromMemRefOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
results.add<SimplifySairOperands>(context);
}
void SairFbyOp::getCanonicalizationPatterns(mlir::RewritePatternSet &patterns,
mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
patterns.add<RemoveCyclicFby, RemoveUnreferencedDims<SairFbyOp>>(context);
}
void SairFromScalarOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairMapReduceOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairProjAnyOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
patterns.add<RemoveUnreferencedDims<SairProjAnyOp>>(context);
}
void SairProjLastOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
patterns.add<RemoveUnreferencedDims<SairProjLastOp>>(context);
}
void SairDynRangeOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairPlaceholderOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairStaticRangeOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairToMemRefOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairStoreToMemRefOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
results.add<SimplifySairOperands>(context);
}
void SairMapOp::getCanonicalizationPatterns(mlir::RewritePatternSet &patterns,
mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
patterns.add(DeduplicateMapInputsOutputs);
}
void SairAllocOp::getCanonicalizationPatterns(mlir::RewritePatternSet &patterns,
mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairFreeOp::getCanonicalizationPatterns(mlir::RewritePatternSet &patterns,
mlir::MLIRContext *context) {
patterns.add<SimplifySairOperands>(context);
}
void SairProgramOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
patterns.add<NormalizeSequenceNumbers>(context);
}
} // namespace sair