24#define GEN_PASS_DEF_SIMPLIFYAFFINESTRUCTURES
25#include "mlir/Dialect/Affine/Transforms/Passes.h.inc"
29#define DEBUG_TYPE "simplify-affine-structure"
40struct SimplifyAffineStructures
41 :
public affine::impl::SimplifyAffineStructuresBase<
42 SimplifyAffineStructures> {
43 void runOnOperation()
override;
47 template <
typename AttributeT>
48 void simplifyAndUpdateAttribute(
Operation *op, StringAttr name,
50 auto &simplified = simplifiedAttributes[attr];
51 if (simplified == attr)
57 auto value = attr.getValue();
58 auto simplifiedValue = simplify(value);
59 if (simplifiedValue == value) {
63 simplified = AttributeT::get(simplifiedValue);
87std::unique_ptr<OperationPass<func::FuncOp>>
89 return std::make_unique<SimplifyAffineStructures>();
92void SimplifyAffineStructures::runOnOperation() {
93 auto func = getOperation();
94 simplifiedAttributes.clear();
96 AffineApplyOp::getCanonicalizationPatterns(patterns,
func.getContext());
97 AffineForOp::getCanonicalizationPatterns(patterns,
func.getContext());
98 AffineIfOp::getCanonicalizationPatterns(patterns,
func.getContext());
107 op, [&](StringRef name,
Attribute &attr) { attrs.append(name, attr); });
108 for (
auto attr : attrs) {
109 if (
auto mapAttr = dyn_cast<AffineMapAttr>(attr.getValue()))
110 simplifyAndUpdateAttribute(op, attr.getName(), mapAttr);
111 else if (
auto setAttr = dyn_cast<IntegerSetAttr>(attr.getValue()))
112 simplifyAndUpdateAttribute(op, attr.getName(), setAttr);
115 if (isa<AffineForOp, AffineIfOp, AffineApplyOp>(op))
116 opsToSimplify.push_back(op);
119 opsToSimplify, frozenPatterns,
120 GreedyRewriteConfig().setStrictness(
121 GreedyRewriteStrictness::ExistingAndNewOps));
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Attributes are known-constant values of operations.
This class represents a frozen set of patterns that can be processed by a pattern applicator.
An integer set representing a conjunction of one or more affine equalities and inequalities.
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
void walkInherentAttrs(Operation *op, InherentAttrVisitor visitor) const
Visit the inherent attributes stored in the properties of op.
Operation is the basic unit of execution within MLIR.
void setInherentAttr(StringAttr name, Attribute value)
Set an inherent attribute by name.
void setDiscardableAttr(StringAttr name, Attribute value)
Set a discardable attribute by name.
std::optional< Attribute > getInherentAttr(StringRef name)
Access an inherent attribute by name: returns an empty optional if there is no inherent attribute wit...
OperationName getName()
The name of an operation is the key identifier for it.
DictionaryAttr getDiscardableAttrDictionary()
Return all of the discardable attributes on this operation as a DictionaryAttr.
IntegerSet simplifyIntegerSet(IntegerSet set)
Simplify the integer set by simplifying the underlying affine expressions by flattening and some simp...
std::unique_ptr< OperationPass< func::FuncOp > > createSimplifyAffineStructuresPass()
Creates a simplification pass for affine structures (maps and sets).
Include the generated interface declarations.
LogicalResult applyOpPatternsGreedily(ArrayRef< Operation * > ops, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr, bool *allErased=nullptr)
Rewrite the specified ops by repeatedly applying the highest benefit patterns in a greedy worklist dr...
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
A mutable affine map. Its affine expressions are however unique.
AffineMap getAffineMap() const
Get the AffineMap corresponding to this MutableAffineMap.
void simplify()
Simplify the (result) expressions in this map using analysis (used by.