MLIR  19.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"
20 
21 namespace mlir {
22 class AffineMap;
23 class Location;
24 class OpBuilder;
25 class OpFoldResult;
26 class RewritePatternSet;
27 class RewriterBase;
28 class Value;
29 
30 namespace presburger {
31 enum class BoundType;
32 } // namespace presburger
33 
34 namespace affine {
35 class AffineApplyOp;
36 
37 /// Populate patterns that expand affine index operations into more fundamental
38 /// operations (not necessarily restricted to Affine dialect).
39 void populateAffineExpandIndexOpsPatterns(RewritePatternSet &patterns);
40 
41 /// Helper function to rewrite `op`'s affine map and reorder its operands such
42 /// that they are in increasing order of hoistability (i.e. the least hoistable)
43 /// operands come first in the operand list.
44 void reorderOperandsByHoistability(RewriterBase &rewriter, AffineApplyOp op);
45 
46 /// Split an "affine.apply" operation into smaller ops.
47 /// This reassociates a large AffineApplyOp into an ordered list of smaller
48 /// AffineApplyOps. This can be used right before lowering affine ops to arith
49 /// to exhibit more opportunities for CSE and LICM.
50 /// Return the sink AffineApplyOp on success or failure if `op` does not
51 /// decompose into smaller AffineApplyOps.
52 /// Note that this can be undone by canonicalization which tries to
53 /// maximally compose chains of AffineApplyOps.
54 FailureOr<AffineApplyOp> decompose(RewriterBase &rewriter, AffineApplyOp op);
55 
56 /// Reify a bound for the given variable in terms of SSA values for which
57 /// `stopCondition` is met.
58 ///
59 /// By default, lower/equal bounds are closed and upper bounds are open. If
60 /// `closedUB` is set to "true", upper bounds are also closed.
61 FailureOr<OpFoldResult>
62 reifyValueBound(OpBuilder &b, Location loc, presburger::BoundType type,
63  const ValueBoundsConstraintSet::Variable &var,
65  bool closedUB = false);
66 
67 /// Reify a bound for the given index-typed value in terms of SSA values for
68 /// which `stopCondition` is met. If no stop condition is specified, reify in
69 /// terms of the operands of the owner op.
70 ///
71 /// By default, lower/equal bounds are closed and upper bounds are open. If
72 /// `closedUB` is set to "true", upper bounds are also closed.
73 ///
74 /// Example:
75 /// %0 = arith.addi %a, %b : index
76 /// %1 = arith.addi %0, %c : index
77 ///
78 /// * If `stopCondition` evaluates to "true" for %0 and %c, "%0 + %c" is an EQ
79 /// bound for %1.
80 /// * If `stopCondition` evaluates to "true" for %a, %b and %c, "%a + %b + %c"
81 /// is an EQ bound for %1.
82 /// * Otherwise, if the owners of %a, %b or %c do not implement the
83 /// ValueBoundsOpInterface, no bound can be computed.
84 FailureOr<OpFoldResult> reifyIndexValueBound(
85  OpBuilder &b, Location loc, presburger::BoundType type, Value value,
86  ValueBoundsConstraintSet::StopConditionFn stopCondition = nullptr,
87  bool closedUB = false);
88 
89 /// Reify a bound for the specified dimension of the given shaped value in terms
90 /// of SSA values for which `stopCondition` is met. If no stop condition is
91 /// specified, reify in terms of the operands of the owner op.
92 ///
93 /// By default, lower/equal bounds are closed and upper bounds are open. If
94 /// `closedUB` is set to "true", upper bounds are also closed.
95 FailureOr<OpFoldResult> reifyShapedValueDimBound(
96  OpBuilder &b, Location loc, presburger::BoundType type, Value value,
97  int64_t dim,
98  ValueBoundsConstraintSet::StopConditionFn stopCondition = nullptr,
99  bool closedUB = false);
100 
101 /// Materialize an already computed bound with Affine dialect ops.
102 ///
103 /// * `ValueBoundsOpInterface::computeBound` computes bounds but does not
104 /// create IR. It is dialect independent.
105 /// * `materializeComputedBound` materializes computed bounds with Affine
106 /// dialect ops.
107 /// * `reifyIndexValueBound`/`reifyShapedValueDimBound` are a combination of
108 /// the two functions mentioned above.
109 OpFoldResult materializeComputedBound(
110  OpBuilder &b, Location loc, AffineMap boundMap,
111  ArrayRef<std::pair<Value, std::optional<int64_t>>> mapOperands);
112 
113 } // namespace affine
114 } // namespace mlir
115 
116 #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.
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.
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 ...
BoundType
The type of bound: equal, lower bound or upper bound.
Include the generated interface declarations.