1//===- SimplifyAffineStructures.cpp ---------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a pass to simplify affine structures in operations.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/Affine/Transforms/Passes.h"
14
15#include "mlir/Dialect/Affine/Analysis/Utils.h"
16#include "mlir/Dialect/Affine/IR/AffineOps.h"
17#include "mlir/Dialect/Affine/Utils.h"
18#include "mlir/Dialect/Func/IR/FuncOps.h"
19#include "mlir/IR/IntegerSet.h"
20#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
21
22namespace mlir {
23namespace affine {
24#define GEN_PASS_DEF_SIMPLIFYAFFINESTRUCTURES
25#include "mlir/Dialect/Affine/Transforms/Passes.h.inc"
26} // namespace affine
27} // namespace mlir
28
29#define DEBUG_TYPE "simplify-affine-structure"
30
31using namespace mlir;
32using namespace mlir::affine;
33
34namespace {
35
36/// Simplifies affine maps and sets appearing in the operations of the Function.
37/// This part is mainly to test the simplifyAffineExpr method. In addition,
38/// all memrefs with non-trivial layout maps are converted to ones with trivial
39/// identity layout ones.
40struct SimplifyAffineStructures
41 : public affine::impl::SimplifyAffineStructuresBase<
42 SimplifyAffineStructures> {
43 void runOnOperation() override;
44
45 /// Utility to simplify an affine attribute and update its entry in the parent
46 /// operation if necessary.
47 template <typename AttributeT>
48 void simplifyAndUpdateAttribute(Operation *op, StringAttr name,
49 AttributeT attr) {
50 auto &simplified = simplifiedAttributes[attr];
51 if (simplified == attr)
52 return;
53
54 // This is a newly encountered attribute.
55 if (!simplified) {
56 // Try to simplify the value of the attribute.
57 auto value = attr.getValue();
58 auto simplifiedValue = simplify(value);
59 if (simplifiedValue == value) {
60 simplified = attr;
61 return;
62 }
63 simplified = AttributeT::get(simplifiedValue);
64 }
65
66 // Simplification was successful, so update the attribute.
67 if (op->getInherentAttr(name).has_value())
68 op->setInherentAttr(name, value: simplified);
69 else
70 op->setDiscardableAttr(name, simplified);
71 }
72
73 IntegerSet simplify(IntegerSet set) { return simplifyIntegerSet(set); }
74
75 /// Performs basic affine map simplifications.
76 AffineMap simplify(AffineMap map) {
77 MutableAffineMap mMap(map);
78 mMap.simplify();
79 return mMap.getAffineMap();
80 }
81
82 DenseMap<Attribute, Attribute> simplifiedAttributes;
83};
84
85} // namespace
86
87std::unique_ptr<OperationPass<func::FuncOp>>
88mlir::affine::createSimplifyAffineStructuresPass() {
89 return std::make_unique<SimplifyAffineStructures>();
90}
91
92void SimplifyAffineStructures::runOnOperation() {
93 auto func = getOperation();
94 simplifiedAttributes.clear();
95 RewritePatternSet patterns(func.getContext());
96 AffineApplyOp::getCanonicalizationPatterns(results&: patterns, context: func.getContext());
97 AffineForOp::getCanonicalizationPatterns(results&: patterns, context: func.getContext());
98 AffineIfOp::getCanonicalizationPatterns(results&: patterns, context: func.getContext());
99 FrozenRewritePatternSet frozenPatterns(std::move(patterns));
100
101 // The simplification of affine attributes will likely simplify the op. Try to
102 // fold/apply canonicalization patterns when we have affine dialect ops.
103 SmallVector<Operation *> opsToSimplify;
104 func.walk(callback: [&](Operation *op) {
105 NamedAttrList attrs(op->getDiscardableAttrDictionary());
106 op->getName().walkInherentAttrs(
107 op, visitor: [&](StringRef name, Attribute &attr) { attrs.append(name, attr); });
108 for (auto attr : attrs) {
109 if (auto mapAttr = dyn_cast<AffineMapAttr>(Val: attr.getValue()))
110 simplifyAndUpdateAttribute(op, name: attr.getName(), attr: mapAttr);
111 else if (auto setAttr = dyn_cast<IntegerSetAttr>(Val: attr.getValue()))
112 simplifyAndUpdateAttribute(op, name: attr.getName(), attr: setAttr);
113 }
114
115 if (isa<AffineForOp, AffineIfOp, AffineApplyOp>(Val: op))
116 opsToSimplify.push_back(Elt: op);
117 });
118 (void)applyOpPatternsGreedily(
119 ops: opsToSimplify, patterns: frozenPatterns,
120 config: GreedyRewriteConfig().setStrictness(
121 GreedyRewriteStrictness::ExistingAndNewOps));
122}
123