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