MLIR  19.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 /// Update a perfectly nested loop nest to yield new values from the innermost
38 /// loop and propagating it up through the loop nest. This function
39 /// - Expects `loopNest` to be a perfectly nested loop with outer most loop
40 /// first and innermost loop last.
41 /// - `newIterOperands` are the initialization values to be used for the
42 /// outermost loop
43 /// - `newYielValueFn` is the callback that generates the new values to be
44 /// yielded from within the innermost loop.
45 /// - The original loops are not erased, but are left in a "no-op" state where
46 /// the body of the loop just yields the basic block arguments that correspond
47 /// to the initialization values of a loop. The original loops are dead after
48 /// this method.
49 /// - If `replaceIterOperandsUsesInLoop` is true, all uses of the
50 /// `newIterOperands` within the generated new loop are replaced with the
51 /// corresponding `BlockArgument` in the loop body.
52 SmallVector<scf::ForOp> replaceLoopNestWithNewYields(
53  RewriterBase &rewriter, MutableArrayRef<scf::ForOp> loopNest,
54  ValueRange newIterOperands, const NewYieldValuesFn &newYieldValuesFn,
55  bool replaceIterOperandsUsesInLoop = true);
56 
57 /// Outline a region with a single block into a new FuncOp.
58 /// Assumes the FuncOp result types is the type of the yielded operands of the
59 /// single block. This constraint makes it easy to determine the result.
60 /// This method also clones the `arith::ConstantIndexOp` at the start of
61 /// `outlinedFuncBody` to alloc simple canonicalizations.
62 /// Creates a new FuncOp and thus cannot be used in a FuncOp pass.
63 /// The client is responsible for providing a unique `funcName` that will not
64 /// collide with another FuncOp name. If `callOp` is provided, it will be set
65 /// to point to the operation that calls the outlined function.
66 // TODO: support more than single-block regions.
67 // TODO: more flexible constant handling.
68 FailureOr<func::FuncOp>
69 outlineSingleBlockRegion(RewriterBase &rewriter, Location loc, Region &region,
70  StringRef funcName, func::CallOp *callOp = nullptr);
71 
72 /// Outline the then and/or else regions of `ifOp` as follows:
73 /// - if `thenFn` is not null, `thenFnName` must be specified and the `then`
74 /// region is inlined into a new FuncOp that is captured by the pointer.
75 /// - if `elseFn` is not null, `elseFnName` must be specified and the `else`
76 /// region is inlined into a new FuncOp that is captured by the pointer.
77 /// Creates new FuncOps and thus cannot be used in a FuncOp pass.
78 /// The client is responsible for providing a unique `thenFnName`/`elseFnName`
79 /// that will not collide with another FuncOp name.
80 LogicalResult outlineIfOp(RewriterBase &b, scf::IfOp ifOp, func::FuncOp *thenFn,
81  StringRef thenFnName, func::FuncOp *elseFn,
82  StringRef elseFnName);
83 
84 /// Get a list of innermost parallel loops contained in `rootOp`. Innermost
85 /// parallel loops are those that do not contain further parallel loops
86 /// themselves.
87 bool getInnermostParallelLoops(Operation *rootOp,
88  SmallVectorImpl<scf::ParallelOp> &result);
89 
90 /// Return the min/max expressions for `value` if it is an induction variable
91 /// from scf.for or scf.parallel loop.
92 /// if `loopFilter` is passed, the filter determines which loop to consider.
93 /// Other induction variables are ignored.
94 std::optional<std::pair<AffineExpr, AffineExpr>>
96  SmallVectorImpl<Value> &symbols,
97  llvm::function_ref<bool(Operation *)> loopFilter = nullptr);
98 
99 /// Replace a perfect nest of "for" loops with a single linearized loop. Assumes
100 /// `loops` contains a list of perfectly nested loops with bounds and steps
101 /// independent of any loop induction variable involved in the nest.
105 
106 /// Walk an affine.for to find a band to coalesce.
108 
109 /// Take the ParallelLoop and for each set of dimension indices, combine them
110 /// into a single dimension. combinedDimensions must contain each index into
111 /// loops exactly once.
112 void collapseParallelLoops(RewriterBase &rewriter, scf::ParallelOp loops,
113  ArrayRef<std::vector<unsigned>> combinedDimensions);
114 
115 /// Unrolls this for operation by the specified unroll factor. Returns failure
116 /// if the loop cannot be unrolled either due to restrictions or due to invalid
117 /// unroll factors. Requires positive loop bounds and step. If specified,
118 /// annotates the Ops in each unrolled iteration by applying `annotateFn`.
120  scf::ForOp forOp, uint64_t unrollFactor,
121  function_ref<void(unsigned, Operation *, OpBuilder)> annotateFn = nullptr);
122 
123 /// Transform a loop with a strictly positive step
124 /// for %i = %lb to %ub step %s
125 /// into a 0-based loop with step 1
126 /// for %ii = 0 to ceildiv(%ub - %lb, %s) step 1 {
127 /// %i = %ii * %s + %lb
128 /// Insert the induction variable remapping in the body of `inner`, which is
129 /// expected to be either `loop` or another loop perfectly nested under `loop`.
130 /// Insert the definition of new bounds immediate before `outer`, which is
131 /// expected to be either `loop` or its parent in the loop nest.
133  OpFoldResult lb, OpFoldResult ub,
134  OpFoldResult step);
135 
136 /// Get back the original induction variable values after loop normalization.
138  Value normalizedIv, OpFoldResult origLb,
139  OpFoldResult origStep);
140 
141 /// Tile a nest of standard for loops rooted at `rootForOp` by finding such
142 /// parametric tile sizes that the outer loops have a fixed number of iterations
143 /// as defined in `sizes`.
145 using TileLoops = std::pair<Loops, Loops>;
146 TileLoops extractFixedOuterLoops(scf::ForOp rootFOrOp, ArrayRef<int64_t> sizes);
147 
148 /// Performs tiling fo imperfectly nested loops (with interchange) by
149 /// strip-mining the `forOps` by `sizes` and sinking them, in their order of
150 /// occurrence in `forOps`, under each of the `targets`.
151 /// Returns the new AffineForOps, one per each of (`forOps`, `targets`) pair,
152 /// nested immediately under each of `targets`.
154  ArrayRef<scf::ForOp> targets);
155 
156 /// Performs tiling (with interchange) by strip-mining the `forOps` by `sizes`
157 /// and sinking them, in their order of occurrence in `forOps`, under `target`.
158 /// Returns the new AffineForOps, one per `forOps`, nested immediately under
159 /// `target`.
161  scf::ForOp target);
162 
163 /// Tile a nest of scf::ForOp loops rooted at `rootForOp` with the given
164 /// (parametric) sizes. Sizes are expected to be strictly positive values at
165 /// runtime. If more sizes than loops are provided, discard the trailing values
166 /// in sizes. Assumes the loop nest is permutable.
167 /// Returns the newly created intra-tile loops.
168 Loops tilePerfectlyNested(scf::ForOp rootForOp, ArrayRef<Value> sizes);
169 
170 /// Get perfectly nested sequence of loops starting at root of loop nest
171 /// (the first op being another AffineFor, and the second op - a terminator).
172 /// A loop is perfectly nested iff: the first op in the loop's body is another
173 /// AffineForOp, and the second op is a terminator).
175  scf::ForOp root);
176 
177 /// Given two scf.forall loops, `target` and `source`, fuses `target` into
178 /// `source`. Assumes that the given loops are siblings and are independent of
179 /// each other.
180 ///
181 /// This function does not perform any legality checks and simply fuses the
182 /// loops. The caller is responsible for ensuring that the loops are legal to
183 /// fuse.
184 scf::ForallOp fuseIndependentSiblingForallLoops(scf::ForallOp target,
185  scf::ForallOp source,
186  RewriterBase &rewriter);
187 
188 /// Given two scf.for loops, `target` and `source`, fuses `target` into
189 /// `source`. Assumes that the given loops are siblings and are independent of
190 /// each other.
191 ///
192 /// This function does not perform any legality checks and simply fuses the
193 /// loops. The caller is responsible for ensuring that the loops are legal to
194 /// fuse.
195 scf::ForOp fuseIndependentSiblingForLoops(scf::ForOp target, scf::ForOp source,
196  RewriterBase &rewriter);
197 
198 } // namespace mlir
199 
200 #endif // MLIR_DIALECT_SCF_UTILS_UTILS_H_
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:209
This class represents a single result from folding an operation.
Definition: OpDefinition.h:268
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:400
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Include the generated interface declarations.
void getPerfectlyNestedLoops(SmallVectorImpl< scf::ForOp > &nestedLoops, scf::ForOp root)
Get perfectly nested sequence of loops starting at root of loop nest (the first op being another Affi...
Definition: Utils.cpp:1018
LogicalResult loopUnrollByFactor(scf::ForOp forOp, uint64_t unrollFactor, function_ref< void(unsigned, Operation *, OpBuilder)> annotateFn=nullptr)
Unrolls this for operation by the specified unroll factor.
Definition: Utils.cpp:347
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:216
SmallVector< scf::ForOp > replaceLoopNestWithNewYields(RewriterBase &rewriter, MutableArrayRef< scf::ForOp > loopNest, ValueRange newIterOperands, const NewYieldValuesFn &newYieldValuesFn, bool replaceIterOperandsUsesInLoop=true)
Update a perfectly nested loop nest to yield new values from the innermost loop and propagating it up...
Definition: Utils.cpp:33
LogicalResult coalescePerfectlyNestedSCFForLoops(scf::ForOp op)
Walk an affine.for to find a band to coalesce.
Definition: Utils.cpp:687
std::pair< Loops, Loops > TileLoops
Definition: Utils.h:145
void collapseParallelLoops(RewriterBase &rewriter, 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:762
std::function< SmallVector< Value >(OpBuilder &b, Location loc, ArrayRef< BlockArgument > newBbArgs)> NewYieldValuesFn
A function that returns the additional yielded values during replaceWithAdditionalYields.
Loops tilePerfectlyNested(scf::ForOp rootForOp, ArrayRef< Value > sizes)
Tile a nest of scf::ForOp loops rooted at rootForOp with the given (parametric) sizes.
Definition: Utils.cpp:1006
bool getInnermostParallelLoops(Operation *rootOp, SmallVectorImpl< scf::ParallelOp > &result)
Get a list of innermost parallel loops contained in rootOp.
Definition: Utils.cpp:239
SmallVector< Loops, 8 > tile(ArrayRef< scf::ForOp > forOps, ArrayRef< Value > sizes, ArrayRef< scf::ForOp > targets)
Performs tiling fo imperfectly nested loops (with interchange) by strip-mining the forOps by sizes an...
Definition: Utils.cpp:983
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:112
void denormalizeInductionVariable(RewriterBase &rewriter, Location loc, Value normalizedIv, OpFoldResult origLb, OpFoldResult origStep)
Get back the original induction variable values after loop normalization.
Definition: Utils.cpp:510
scf::ForallOp fuseIndependentSiblingForallLoops(scf::ForallOp target, scf::ForallOp source, RewriterBase &rewriter)
Given two scf.forall loops, target and source, fuses target into source.
Definition: Utils.cpp:1065
LogicalResult coalesceLoops(MutableArrayRef< scf::ForOp > loops)
Replace a perfect nest of "for" loops with a single linearized loop.
Definition: Utils.cpp:679
scf::ForOp fuseIndependentSiblingForLoops(scf::ForOp target, scf::ForOp source, RewriterBase &rewriter)
Given two scf.for loops, target and source, fuses target into source.
Definition: Utils.cpp:1117
TileLoops extractFixedOuterLoops(scf::ForOp rootFOrOp, ArrayRef< int64_t > sizes)
Definition: Utils.cpp:1023
Range emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc, OpFoldResult lb, OpFoldResult ub, OpFoldResult step)
Transform a loop with a strictly positive step for i = lb to ub step s into a 0-based loop with step ...
Definition: Utils.cpp:467
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
Represents a range (offset, size, and stride) where each element of the triple may be dynamic or stat...