MLIR  21.0.0git
Transforms.h
Go to the documentation of this file.
1 //===- Transforms.h - Transforms Entrypoints --------------------*- C++ -*-===//
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 header file defines a set of transforms specific for the AffineOps
10 // dialect.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_DIALECT_AFFINE_TRANSFORMS_TRANSFORMS_H
15 #define MLIR_DIALECT_AFFINE_TRANSFORMS_TRANSFORMS_H
16 
18 #include "mlir/Support/LLVM.h"
19 
20 namespace mlir {
21 class AffineMap;
22 class Location;
23 class OpBuilder;
24 class OpFoldResult;
25 class RewritePatternSet;
26 class RewriterBase;
27 class Value;
28 
29 namespace presburger {
30 enum class BoundType;
31 } // namespace presburger
32 
33 namespace affine {
34 class AffineApplyOp;
35 class AffineDelinearizeIndexOp;
36 class AffineLinearizeIndexOp;
37 
38 /// Lowers `affine.delinearize_index` into a sequence of division and remainder
39 /// operations.
40 LogicalResult lowerAffineDelinearizeIndexOp(RewriterBase &rewriter,
41  AffineDelinearizeIndexOp op);
42 
43 /// Lowers `affine.linearize_index` into a sequence of multiplications and
44 /// additions. Make a best effort to sort the input indices so that
45 /// the most loop-invariant terms are at the left of the additions
46 /// to enable loop-invariant code motion.
47 LogicalResult lowerAffineLinearizeIndexOp(RewriterBase &rewriter,
48  AffineLinearizeIndexOp op);
49 
50 /// Populate patterns that expand affine index operations into more fundamental
51 /// operations (not necessarily restricted to Affine dialect).
52 void populateAffineExpandIndexOpsPatterns(RewritePatternSet &patterns);
53 
54 /// Populate patterns that expand affine index operations into their equivalent
55 /// `affine.apply` representations.
57 
58 /// Helper function to rewrite `op`'s affine map and reorder its operands such
59 /// that they are in increasing order of hoistability (i.e. the least hoistable)
60 /// operands come first in the operand list.
61 void reorderOperandsByHoistability(RewriterBase &rewriter, AffineApplyOp op);
62 
63 /// Split an "affine.apply" operation into smaller ops.
64 /// This reassociates a large AffineApplyOp into an ordered list of smaller
65 /// AffineApplyOps. This can be used right before lowering affine ops to arith
66 /// to exhibit more opportunities for CSE and LICM.
67 /// Return the sink AffineApplyOp on success or failure if `op` does not
68 /// decompose into smaller AffineApplyOps.
69 /// Note that this can be undone by canonicalization which tries to
70 /// maximally compose chains of AffineApplyOps.
71 FailureOr<AffineApplyOp> decompose(RewriterBase &rewriter, AffineApplyOp op);
72 
73 /// Reify a bound for the given variable in terms of SSA values for which
74 /// `stopCondition` is met.
75 ///
76 /// By default, lower/equal bounds are closed and upper bounds are open. If
77 /// `closedUB` is set to "true", upper bounds are also closed.
78 FailureOr<OpFoldResult>
79 reifyValueBound(OpBuilder &b, Location loc, presburger::BoundType type,
80  const ValueBoundsConstraintSet::Variable &var,
82  bool closedUB = false);
83 
84 /// Reify a bound for the given index-typed value in terms of SSA values for
85 /// which `stopCondition` is met. If no stop condition is specified, reify in
86 /// terms of the operands of the owner op.
87 ///
88 /// By default, lower/equal bounds are closed and upper bounds are open. If
89 /// `closedUB` is set to "true", upper bounds are also closed.
90 ///
91 /// Example:
92 /// %0 = arith.addi %a, %b : index
93 /// %1 = arith.addi %0, %c : index
94 ///
95 /// * If `stopCondition` evaluates to "true" for %0 and %c, "%0 + %c" is an EQ
96 /// bound for %1.
97 /// * If `stopCondition` evaluates to "true" for %a, %b and %c, "%a + %b + %c"
98 /// is an EQ bound for %1.
99 /// * Otherwise, if the owners of %a, %b or %c do not implement the
100 /// ValueBoundsOpInterface, no bound can be computed.
101 FailureOr<OpFoldResult> reifyIndexValueBound(
102  OpBuilder &b, Location loc, presburger::BoundType type, Value value,
103  ValueBoundsConstraintSet::StopConditionFn stopCondition = nullptr,
104  bool closedUB = false);
105 
106 /// Reify a bound for the specified dimension of the given shaped value in terms
107 /// of SSA values for which `stopCondition` is met. If no stop condition is
108 /// specified, reify in terms of the operands of the owner op.
109 ///
110 /// By default, lower/equal bounds are closed and upper bounds are open. If
111 /// `closedUB` is set to "true", upper bounds are also closed.
112 FailureOr<OpFoldResult> reifyShapedValueDimBound(
113  OpBuilder &b, Location loc, presburger::BoundType type, Value value,
114  int64_t dim,
115  ValueBoundsConstraintSet::StopConditionFn stopCondition = nullptr,
116  bool closedUB = false);
117 
118 /// Materialize an already computed bound with Affine dialect ops.
119 ///
120 /// * `ValueBoundsOpInterface::computeBound` computes bounds but does not
121 /// create IR. It is dialect independent.
122 /// * `materializeComputedBound` materializes computed bounds with Affine
123 /// dialect ops.
124 /// * `reifyIndexValueBound`/`reifyShapedValueDimBound` are a combination of
125 /// the two functions mentioned above.
126 OpFoldResult materializeComputedBound(
127  OpBuilder &b, Location loc, AffineMap boundMap,
128  ArrayRef<std::pair<Value, std::optional<int64_t>>> mapOperands);
129 
130 } // namespace affine
131 } // namespace mlir
132 
133 #endif // MLIR_DIALECT_AFFINE_TRANSFORMS_TRANSFORMS_H
std::function< bool(Value, std::optional< int64_t >, ValueBoundsConstraintSet &cstr)> StopConditionFn
The stop condition when traversing the backward slice of a shaped value/ index-type value.
FailureOr< OpFoldResult > reifyValueBound(OpBuilder &b, Location loc, presburger::BoundType type, const ValueBoundsConstraintSet::Variable &var, ValueBoundsConstraintSet::StopConditionFn stopCondition, bool closedUB=false)
Reify a bound for the given variable in terms of SSA values for which stopCondition is met.
LogicalResult lowerAffineDelinearizeIndexOp(RewriterBase &rewriter, AffineDelinearizeIndexOp op)
Lowers affine.delinearize_index into a sequence of division and remainder operations.
FailureOr< OpFoldResult > reifyIndexValueBound(OpBuilder &b, Location loc, presburger::BoundType type, Value value, ValueBoundsConstraintSet::StopConditionFn stopCondition=nullptr, bool closedUB=false)
Reify a bound for the given index-typed value in terms of SSA values for which stopCondition is met.
FailureOr< AffineApplyOp > decompose(RewriterBase &rewriter, AffineApplyOp op)
Split an "affine.apply" operation into smaller ops.
LogicalResult lowerAffineLinearizeIndexOp(RewriterBase &rewriter, AffineLinearizeIndexOp op)
Lowers affine.linearize_index into a sequence of multiplications and additions.
void populateAffineExpandIndexOpsPatterns(RewritePatternSet &patterns)
Populate patterns that expand affine index operations into more fundamental operations (not necessari...
OpFoldResult materializeComputedBound(OpBuilder &b, Location loc, AffineMap boundMap, ArrayRef< std::pair< Value, std::optional< int64_t >>> mapOperands)
Materialize an already computed bound with Affine dialect ops.
FailureOr< OpFoldResult > reifyShapedValueDimBound(OpBuilder &b, Location loc, presburger::BoundType type, Value value, int64_t dim, ValueBoundsConstraintSet::StopConditionFn stopCondition=nullptr, bool closedUB=false)
Reify a bound for the specified dimension of the given shaped value in terms of SSA values for which ...
void reorderOperandsByHoistability(RewriterBase &rewriter, AffineApplyOp op)
Helper function to rewrite op's affine map and reorder its operands such that they are in increasing ...
void populateAffineExpandIndexOpsAsAffinePatterns(RewritePatternSet &patterns)
Populate patterns that expand affine index operations into their equivalent affine....
BoundType
The type of bound: equal, lower bound or upper bound.
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns