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 index-typed value in terms of SSA values for
57 /// which `stopCondition` is met. If no stop condition is specified, reify in
58 /// terms of the operands of the owner op.
59 ///
60 /// By default, lower/equal bounds are closed and upper bounds are open. If
61 /// `closedUB` is set to "true", upper bounds are also closed.
62 ///
63 /// Example:
64 /// %0 = arith.addi %a, %b : index
65 /// %1 = arith.addi %0, %c : index
66 ///
67 /// * If `stopCondition` evaluates to "true" for %0 and %c, "%0 + %c" is an EQ
68 /// bound for %1.
69 /// * If `stopCondition` evaluates to "true" for %a, %b and %c, "%a + %b + %c"
70 /// is an EQ bound for %1.
71 /// * Otherwise, if the owners of %a, %b or %c do not implement the
72 /// ValueBoundsOpInterface, no bound can be computed.
73 FailureOr<OpFoldResult> reifyIndexValueBound(
74  OpBuilder &b, Location loc, presburger::BoundType type, Value value,
75  ValueBoundsConstraintSet::StopConditionFn stopCondition = nullptr,
76  bool closedUB = false);
77 
78 /// Reify a bound for the specified dimension of the given shaped value in terms
79 /// of SSA values for which `stopCondition` is met. If no stop condition is
80 /// specified, reify in terms of the operands of the owner op.
81 ///
82 /// By default, lower/equal bounds are closed and upper bounds are open. If
83 /// `closedUB` is set to "true", upper bounds are also closed.
84 FailureOr<OpFoldResult> reifyShapedValueDimBound(
85  OpBuilder &b, Location loc, presburger::BoundType type, Value value,
86  int64_t dim,
87  ValueBoundsConstraintSet::StopConditionFn stopCondition = nullptr,
88  bool closedUB = false);
89 
90 /// Materialize an already computed bound with Affine dialect ops.
91 ///
92 /// * `ValueBoundsOpInterface::computeBound` computes bounds but does not
93 /// create IR. It is dialect independent.
94 /// * `materializeComputedBound` materializes computed bounds with Affine
95 /// dialect ops.
96 /// * `reifyIndexValueBound`/`reifyShapedValueDimBound` are a combination of
97 /// the two functions mentioned above.
98 OpFoldResult materializeComputedBound(
99  OpBuilder &b, Location loc, AffineMap boundMap,
100  ArrayRef<std::pair<Value, std::optional<int64_t>>> mapOperands);
101 
102 } // namespace affine
103 } // namespace mlir
104 
105 #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 > 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.