MLIR  17.0.0git
Utils.h
Go to the documentation of this file.
1 //===- Utils.h - SCF dialect utilities --------------------------*- 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 prototypes for various SCF utilities.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_DIALECT_SCF_UTILS_UTILS_H_
14 #define MLIR_DIALECT_SCF_UTILS_UTILS_H_
15 
17 #include "mlir/IR/PatternMatch.h"
18 #include "mlir/Support/LLVM.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include <optional>
22 
23 namespace mlir {
24 class Location;
25 class Operation;
26 class OpBuilder;
27 class Region;
28 class RewriterBase;
29 class ValueRange;
30 class Value;
31 
32 namespace func {
33 class CallOp;
34 class FuncOp;
35 } // namespace func
36 
37 /// Replace the `loop` with `newIterOperands` added as new initialization
38 /// values. `newYieldValuesFn` is a callback that can be used to specify
39 /// the additional values to be yielded by the loop. The number of
40 /// values returned by the callback should match the number of new
41 /// initialization values. This function
42 /// - Moves (i.e. doesnt clone) operations from the `loop` to the newly created
43 /// loop
44 /// - Replaces the uses of `loop` with the new loop.
45 /// - `loop` isnt erased, but is left in a "no-op" state where the body of the
46 /// loop just yields the basic block arguments that correspond to the
47 /// initialization values of a loop. The loop is dead after this method.
48 /// - If `replaceIterOperandsUsesInLoop` is true, all uses of the
49 /// `newIterOperands` within the generated new loop are replaced
50 /// with the corresponding `BlockArgument` in the loop body.
51 using NewYieldValueFn = std::function<SmallVector<Value>(
52  OpBuilder &b, Location loc, ArrayRef<BlockArgument> newBBArgs)>;
53 scf::ForOp replaceLoopWithNewYields(OpBuilder &builder, scf::ForOp loop,
54  ValueRange newIterOperands,
55  const NewYieldValueFn &newYieldValuesFn,
56  bool replaceIterOperandsUsesInLoop = true);
57 
58 /// Update a perfectly nested loop nest to yield new values from the innermost
59 /// loop and propagating it up through the loop nest. This function
60 /// - Expects `loopNest` to be a perfectly nested loop with outer most loop
61 /// first and innermost loop last.
62 /// - `newIterOperands` are the initialization values to be used for the
63 /// outermost loop
64 /// - `newYielValueFn` is the callback that generates the new values to be
65 /// yielded from within the innermost loop.
66 /// - The original loops are not erased, but are left in a "no-op" state where
67 /// the body of the loop just yields the basic block arguments that correspond
68 /// to the initialization values of a loop. The original loops are dead after
69 /// this method.
70 /// - If `replaceIterOperandsUsesInLoop` is true, all uses of the
71 /// `newIterOperands` within the generated new loop are replaced with the
72 /// corresponding `BlockArgument` in the loop body.
75  ValueRange newIterOperands,
76  const NewYieldValueFn &newYieldValueFn,
77  bool replaceIterOperandsUsesInLoop = true);
78 
79 /// Outline a region with a single block into a new FuncOp.
80 /// Assumes the FuncOp result types is the type of the yielded operands of the
81 /// single block. This constraint makes it easy to determine the result.
82 /// This method also clones the `arith::ConstantIndexOp` at the start of
83 /// `outlinedFuncBody` to alloc simple canonicalizations.
84 /// Creates a new FuncOp and thus cannot be used in a FuncOp pass.
85 /// The client is responsible for providing a unique `funcName` that will not
86 /// collide with another FuncOp name. If `callOp` is provided, it will be set
87 /// to point to the operation that calls the outlined function.
88 // TODO: support more than single-block regions.
89 // TODO: more flexible constant handling.
92  StringRef funcName, func::CallOp *callOp = nullptr);
93 
94 /// Outline the then and/or else regions of `ifOp` as follows:
95 /// - if `thenFn` is not null, `thenFnName` must be specified and the `then`
96 /// region is inlined into a new FuncOp that is captured by the pointer.
97 /// - if `elseFn` is not null, `elseFnName` must be specified and the `else`
98 /// region is inlined into a new FuncOp that is captured by the pointer.
99 /// Creates new FuncOps and thus cannot be used in a FuncOp pass.
100 /// The client is responsible for providing a unique `thenFnName`/`elseFnName`
101 /// that will not collide with another FuncOp name.
102 LogicalResult outlineIfOp(RewriterBase &b, scf::IfOp ifOp, func::FuncOp *thenFn,
103  StringRef thenFnName, func::FuncOp *elseFn,
104  StringRef elseFnName);
105 
106 /// Get a list of innermost parallel loops contained in `rootOp`. Innermost
107 /// parallel loops are those that do not contain further parallel loops
108 /// themselves.
111 
112 /// Return the min/max expressions for `value` if it is an induction variable
113 /// from scf.for or scf.parallel loop.
114 /// if `loopFilter` is passed, the filter determines which loop to consider.
115 /// Other induction variables are ignored.
116 std::optional<std::pair<AffineExpr, AffineExpr>>
118  SmallVectorImpl<Value> &symbols,
119  llvm::function_ref<bool(Operation *)> loopFilter = nullptr);
120 
121 /// Replace a perfect nest of "for" loops with a single linearized loop. Assumes
122 /// `loops` contains a list of perfectly nested loops with bounds and steps
123 /// independent of any loop induction variable involved in the nest.
125 
126 /// Take the ParallelLoop and for each set of dimension indices, combine them
127 /// into a single dimension. combinedDimensions must contain each index into
128 /// loops exactly once.
129 void collapseParallelLoops(scf::ParallelOp loops,
130  ArrayRef<std::vector<unsigned>> combinedDimensions);
131 
132 /// Promotes the loop body of a scf::ForOp to its containing block if the loop
133 /// was known to have a single iteration.
134 LogicalResult promoteIfSingleIteration(scf::ForOp forOp);
135 
136 /// Unrolls this for operation by the specified unroll factor. Returns failure
137 /// if the loop cannot be unrolled either due to restrictions or due to invalid
138 /// unroll factors. Requires positive loop bounds and step. If specified,
139 /// annotates the Ops in each unrolled iteration by applying `annotateFn`.
141  scf::ForOp forOp, uint64_t unrollFactor,
142  function_ref<void(unsigned, Operation *, OpBuilder)> annotateFn = nullptr);
143 
144 /// Tile a nest of standard for loops rooted at `rootForOp` by finding such
145 /// parametric tile sizes that the outer loops have a fixed number of iterations
146 /// as defined in `sizes`.
148 using TileLoops = std::pair<Loops, Loops>;
149 TileLoops extractFixedOuterLoops(scf::ForOp rootFOrOp, ArrayRef<int64_t> sizes);
150 
151 /// Performs tiling fo imperfectly nested loops (with interchange) by
152 /// strip-mining the `forOps` by `sizes` and sinking them, in their order of
153 /// occurrence in `forOps`, under each of the `targets`.
154 /// Returns the new AffineForOps, one per each of (`forOps`, `targets`) pair,
155 /// nested immediately under each of `targets`.
157  ArrayRef<scf::ForOp> targets);
158 
159 /// Performs tiling (with interchange) by strip-mining the `forOps` by `sizes`
160 /// and sinking them, in their order of occurrence in `forOps`, under `target`.
161 /// Returns the new AffineForOps, one per `forOps`, nested immediately under
162 /// `target`.
164  scf::ForOp target);
165 
166 /// Tile a nest of scf::ForOp loops rooted at `rootForOp` with the given
167 /// (parametric) sizes. Sizes are expected to be strictly positive values at
168 /// runtime. If more sizes than loops are provided, discard the trailing values
169 /// in sizes. Assumes the loop nest is permutable.
170 /// Returns the newly created intra-tile loops.
171 Loops tilePerfectlyNested(scf::ForOp rootForOp, ArrayRef<Value> sizes);
172 
173 /// Get perfectly nested sequence of loops starting at root of loop nest
174 /// (the first op being another AffineFor, and the second op - a terminator).
175 /// A loop is perfectly nested iff: the first op in the loop's body is another
176 /// AffineForOp, and the second op is a terminator).
178  scf::ForOp root);
179 
180 } // namespace mlir
181 
182 #endif // MLIR_DIALECT_SCF_UTILS_UTILS_H_
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
This class helps build Operations.
Definition: Builders.h:199
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:75
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:399
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:350
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:93
Include the generated interface declarations.
LogicalResult loopUnrollByFactor(AffineForOp forOp, uint64_t unrollFactor, function_ref< void(unsigned, Operation *, OpBuilder)> annotateFn=nullptr, bool cleanUpUnroll=false)
Unrolls this for operation by the specified unroll factor.
Definition: LoopUtils.cpp:1092
LogicalResult outlineIfOp(RewriterBase &b, scf::IfOp ifOp, func::FuncOp *thenFn, StringRef thenFnName, func::FuncOp *elseFn, StringRef elseFnName)
Outline the then and/or else regions of ifOp as follows:
Definition: Utils.cpp:291
void collapseParallelLoops(scf::ParallelOp loops, ArrayRef< std::vector< unsigned >> combinedDimensions)
Take the ParallelLoop and for each set of dimension indices, combine them into a single dimension.
Definition: Utils.cpp:716
std::pair< Loops, Loops > TileLoops
Definition: Utils.h:148
SmallVector< SmallVector< AffineForOp, 8 >, 8 > tile(ArrayRef< AffineForOp > forOps, ArrayRef< uint64_t > sizes, ArrayRef< AffineForOp > targets)
Performs tiling fo imperfectly nested loops (with interchange) by strip-mining the forOps by sizes an...
Definition: LoopUtils.cpp:1702
scf::ForOp replaceLoopWithNewYields(OpBuilder &builder, scf::ForOp loop, ValueRange newIterOperands, const NewYieldValueFn &newYieldValuesFn, bool replaceIterOperandsUsesInLoop=true)
Definition: Utils.cpp:42
void getPerfectlyNestedLoops(SmallVectorImpl< AffineForOp > &nestedLoops, AffineForOp root)
Get perfectly nested sequence of loops starting at root of loop nest (the first op being another Affi...
Definition: LoopUtils.cpp:946
bool getInnermostParallelLoops(Operation *rootOp, SmallVectorImpl< scf::ParallelOp > &result)
Get a list of innermost parallel loops contained in rootOp.
Definition: Utils.cpp:314
LogicalResult promoteIfSingleIteration(AffineForOp forOp)
Promotes the loop body of a AffineForOp to its containing block if the loop was known to have a singl...
Definition: LoopUtils.cpp:131
std::optional< std::pair< AffineExpr, AffineExpr > > getSCFMinMaxExpr(Value value, SmallVectorImpl< Value > &dims, SmallVectorImpl< Value > &symbols, llvm::function_ref< bool(Operation *)> loopFilter=nullptr)
Return the min/max expressions for value if it is an induction variable from scf.for or scf....
FailureOr< func::FuncOp > outlineSingleBlockRegion(RewriterBase &rewriter, Location loc, Region &region, StringRef funcName, func::CallOp *callOp=nullptr)
Outline a region with a single block into a new FuncOp.
Definition: Utils.cpp:187
LogicalResult coalesceLoops(MutableArrayRef< AffineForOp > loops)
Replace a perfect nest of "for" loops with a single linearized loop.
Definition: LoopUtils.cpp:1725
SmallVector< scf::ForOp > replaceLoopNestWithNewYields(OpBuilder &builder, ArrayRef< scf::ForOp > loopNest, ValueRange newIterOperands, const NewYieldValueFn &newYieldValueFn, bool replaceIterOperandsUsesInLoop=true)
Update a perfectly nested loop nest to yield new values from the innermost loop and propagating it up...
Definition: Utils.cpp:109
std::function< SmallVector< Value >(OpBuilder &b, Location loc, ArrayRef< BlockArgument > newBBArgs)> NewYieldValueFn
Replace the loop with newIterOperands added as new initialization values.
Definition: Utils.h:52
TileLoops extractFixedOuterLoops(scf::ForOp rootFOrOp, ArrayRef< int64_t > sizes)
Definition: Utils.cpp:973
LogicalResult tilePerfectlyNested(MutableArrayRef< AffineForOp > input, ArrayRef< unsigned > tileSizes, SmallVectorImpl< AffineForOp > *tiledNest=nullptr)
Tiles the specified band of perfectly nested loops creating tile-space loops and intra-tile loops.
Definition: LoopUtils.cpp:860
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26