MLIR 24.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
18#include "mlir/Support/LLVM.h"
19#include "llvm/ADT/STLExtras.h"
20#include <optional>
21#include <tuple>
22
23namespace mlir {
24class Location;
25class Operation;
26class OpBuilder;
27class Region;
28class RewriterBase;
29class ValueRange;
30class Value;
31
32namespace func {
33class CallOp;
34class 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.
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.
68FailureOr<func::FuncOp>
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.
80LogicalResult 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.
89
90/// Replace a perfect nest of "for" loops with a single linearized loop. Assumes
91/// `loops` contains a list of perfectly nested loops with bounds and steps
92/// independent of any loop induction variable involved in the nest.
94LogicalResult coalesceLoops(RewriterBase &rewriter,
96
97/// Walk an affine.for to find a band to coalesce.
98LogicalResult coalescePerfectlyNestedSCFForLoops(scf::ForOp op);
99
100/// Take the ParallelLoop and for each set of dimension indices, combine them
101/// into a single dimension. combinedDimensions must contain each index into
102/// loops exactly once.
103void collapseParallelLoops(RewriterBase &rewriter, scf::ParallelOp loops,
104 ArrayRef<std::vector<unsigned>> combinedDimensions);
105
107 std::optional<scf::ForOp> mainLoopOp = std::nullopt;
108 std::optional<scf::ForOp> epilogueLoopOp = std::nullopt;
109};
110
111/// Unrolls this for operation by the specified unroll factor. Returns the
112/// unrolled main loop and the epilogue loop, if the loop is unrolled. Otherwise
113/// returns failure if the loop cannot be unrolled either due to restrictions or
114/// due to invalid unroll factors. Requires positive loop bounds and step. If
115/// specified, annotates the Ops in each unrolled iteration by applying
116/// `annotateFn`.
117/// If `shouldPromoteIfSingleIteration` is true, the function will promote the
118/// loop body up if this has turned into a single iteration loop.
119FailureOr<UnrolledLoopInfo> loopUnrollByFactor(
120 scf::ForOp forOp, uint64_t unrollFactor,
121 function_ref<void(unsigned, Operation *, OpBuilder)> annotateFn = nullptr,
122 bool shouldPromoteIfSingleIteration = true);
123
124/// Unrolls this loop completely.
125LogicalResult loopUnrollFull(scf::ForOp forOp);
126
127/// Unrolls and jams this `scf.for` operation by the specified unroll factor.
128/// Returns failure if the loop cannot be unrolled either due to restrictions or
129/// due to invalid unroll factors. In case of unroll factor of 1, the function
130/// bails out without doing anything (returns success). Currently, only constant
131/// trip count that are divided by the unroll factor is supported. Currently,
132/// for operations with results are not supported.
133LogicalResult loopUnrollJamByFactor(scf::ForOp forOp, uint64_t unrollFactor);
134
135/// Materialize bounds and step of a zero-based and unit-step loop derived by
136/// normalizing the specified bounds and step.
139 OpFoldResult step);
140
141/// Get back the original induction variable values after loop normalization.
143 Value normalizedIv, OpFoldResult origLb,
144 OpFoldResult origStep);
145
146/// Tile a nest of standard for loops rooted at `rootForOp` by finding such
147/// parametric tile sizes that the outer loops have a fixed number of iterations
148/// as defined in `sizes`.
150using TileLoops = std::pair<Loops, Loops>;
151TileLoops extractFixedOuterLoops(scf::ForOp rootFOrOp, ArrayRef<int64_t> sizes);
152
153/// Performs tiling fo imperfectly nested loops (with interchange) by
154/// strip-mining the `forOps` by `sizes` and sinking them, in their order of
155/// occurrence in `forOps`, under each of the `targets`.
156/// Returns the new AffineForOps, one per each of (`forOps`, `targets`) pair,
157/// nested immediately under each of `targets`.
159 ArrayRef<scf::ForOp> targets);
160
161/// Performs tiling (with interchange) by strip-mining the `forOps` by `sizes`
162/// and sinking them, in their order of occurrence in `forOps`, under `target`.
163/// Returns the new AffineForOps, one per `forOps`, nested immediately under
164/// `target`.
166 scf::ForOp target);
167
168/// Tile a nest of scf::ForOp loops rooted at `rootForOp` with the given
169/// (parametric) sizes. Sizes are expected to be strictly positive values at
170/// runtime. If more sizes than loops are provided, discard the trailing values
171/// in sizes. Assumes the loop nest is permutable.
172/// Returns the newly created intra-tile loops.
173Loops tilePerfectlyNested(scf::ForOp rootForOp, ArrayRef<Value> sizes);
174
175/// Get perfectly nested sequence of loops starting at root of loop nest
176/// (the first op being another AffineFor, and the second op - a terminator).
177/// A loop is perfectly nested iff: the first op in the loop's body is another
178/// AffineForOp, and the second op is a terminator).
180 scf::ForOp root);
181
182/// Given two scf.forall loops, `target` and `source`, fuses `target` into
183/// `source`. Assumes that the given loops are siblings and are independent of
184/// each other.
185///
186/// This function does not perform any legality checks and simply fuses the
187/// loops. The caller is responsible for ensuring that the loops are legal to
188/// fuse.
189scf::ForallOp fuseIndependentSiblingForallLoops(scf::ForallOp target,
190 scf::ForallOp source,
191 RewriterBase &rewriter);
192
193/// Given two scf.for loops, `target` and `source`, fuses `target` into
194/// `source`. Assumes that the given loops are siblings and are independent of
195/// each other.
196///
197/// This function does not perform any legality checks and simply fuses the
198/// loops. The caller is responsible for ensuring that the loops are legal to
199/// fuse.
200scf::ForOp fuseIndependentSiblingForLoops(scf::ForOp target, scf::ForOp source,
201 RewriterBase &rewriter);
202
203/// Normalize an `scf.forall` operation. Returns `failure()`if normalization
204/// fails.
205// On `success()` returns the
206/// newly created operation with all uses of the original operation replaced
207/// with results of the new operation.
208FailureOr<scf::ForallOp> normalizeForallOp(RewriterBase &rewriter,
209 scf::ForallOp forallOp);
210
211/// Check if the provided loops are perfectly nested for-loops. Perfect nesting
212/// means:
213/// 1. All loops are scf.for operations
214/// 2. Each outer loop's region iter args match the inner loop's init args
215/// 3. Each outer loop's yields match the inner loop's results
216/// 4. Each region iter arg and result has exactly one use
218
219/// Generate unrolled copies of an scf loop's 'loopBodyBlock', with 'iterArgs'
220/// and 'yieldedValues' as the block arguments and yielded values of the loop.
221/// The content of the loop body is replicated 'unrollFactor' times, calling
222/// 'ivRemapFn' to remap 'iv' for each unrolled body. If specified, annotates
223/// the Ops in each unrolled iteration using annotateFn. If provided,
224/// 'clonedToSrcOpsMap' is populated with the mappings from the cloned ops to
225/// the original op.
227 Block *loopBodyBlock, Value iv, uint64_t unrollFactor,
228 function_ref<Value(unsigned, Value, OpBuilder)> ivRemapFn,
229 function_ref<void(unsigned, Operation *, OpBuilder)> annotateFn,
230 ValueRange iterArgs, ValueRange yieldedValues,
231 IRMapping *clonedToSrcOpsMap = nullptr);
232
233/// Unroll this scf::Parallel loop by the specified unroll factors. Returns the
234/// unrolled loop if the unroll succeded; otherwise returns failure if the loop
235/// cannot be unrolled either due to restrictions or to invalid unroll factors.
236/// Requires positive loop bounds and step. If specified, annotates the Ops in
237/// each unrolled iteration by applying `annotateFn`.
238/// If provided, 'clonedToSrcOpsMap' is populated with the mappings from the
239/// cloned ops to the original op.
240FailureOr<scf::ParallelOp> parallelLoopUnrollByFactors(
241 scf::ParallelOp op, ArrayRef<uint64_t> unrollFactors,
242 RewriterBase &rewriter,
243 function_ref<void(unsigned, Operation *, OpBuilder)> annotateFn = nullptr,
244 IRMapping *clonedToSrcOpsMap = nullptr);
245
246/// Get constant loop bounds and steps for each of the induction variables of
247/// the given loop operation, if all the loop's ranges are constant. Each entry
248/// in the returned vector is a tuple (lowerBound, upperBound, step).
250getConstLoopBounds(mlir::LoopLikeOpInterface loopOp);
251
252/// Get constant trip counts for each of the induction variables of the given
253/// loop operation. If any of the loop's trip counts is not constant, return an
254/// empty vector.
256getConstLoopTripCounts(mlir::LoopLikeOpInterface loopOp);
257
258} // namespace mlir
259
260#endif // MLIR_DIALECT_SCF_UTILS_UTILS_H_
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
Block represents an ordered list of Operations.
Definition Block.h:33
This is a utility class for mapping one set of IR entities to another.
Definition IRMapping.h:26
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:210
This class represents a single result from folding an operation.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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...
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:389
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:1374
bool isPerfectlyNestedForLoops(MutableArrayRef< LoopLikeOpInterface > loops)
Check if the provided loops are perfectly nested for-loops.
Definition Utils.cpp:1581
FailureOr< UnrolledLoopInfo > loopUnrollByFactor(scf::ForOp forOp, uint64_t unrollFactor, function_ref< void(unsigned, Operation *, OpBuilder)> annotateFn=nullptr, bool shouldPromoteIfSingleIteration=true)
Unrolls this for operation by the specified unroll factor.
Definition Utils.cpp:368
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:218
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:36
std::function< SmallVector< Value >( OpBuilder &b, Location loc, ArrayRef< BlockArgument > newBbArgs)> NewYieldValuesFn
A function that returns the additional yielded values during replaceWithAdditionalYields.
LogicalResult coalescePerfectlyNestedSCFForLoops(scf::ForOp op)
Walk an affine.for to find a band to coalesce.
Definition Utils.cpp:1042
void generateUnrolledLoop(Block *loopBodyBlock, Value iv, uint64_t unrollFactor, function_ref< Value(unsigned, Value, OpBuilder)> ivRemapFn, function_ref< void(unsigned, Operation *, OpBuilder)> annotateFn, ValueRange iterArgs, ValueRange yieldedValues, IRMapping *clonedToSrcOpsMap=nullptr)
Generate unrolled copies of an scf loop's 'loopBodyBlock', with 'iterArgs' and 'yieldedValues' as the...
Definition Utils.cpp:295
LogicalResult loopUnrollFull(scf::ForOp forOp)
Unrolls this loop completely.
Definition Utils.cpp:523
llvm::SmallVector< llvm::APInt > getConstLoopTripCounts(mlir::LoopLikeOpInterface loopOp)
Get constant trip counts for each of the induction variables of the given loop operation.
Definition Utils.cpp:1639
std::pair< Loops, Loops > TileLoops
Definition Utils.h:150
llvm::SmallVector< std::tuple< int64_t, int64_t, int64_t > > getConstLoopBounds(mlir::LoopLikeOpInterface loopOp)
Get constant loop bounds and steps for each of the induction variables of the given loop operation,...
Definition Utils.cpp:1620
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:1117
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:1362
LogicalResult loopUnrollJamByFactor(scf::ForOp forOp, uint64_t unrollFactor)
Unrolls and jams this scf.for operation by the specified unroll factor.
Definition Utils.cpp:551
bool getInnermostParallelLoops(Operation *rootOp, SmallVectorImpl< scf::ParallelOp > &result)
Get a list of innermost parallel loops contained in rootOp.
Definition Utils.cpp:241
FailureOr< scf::ParallelOp > parallelLoopUnrollByFactors(scf::ParallelOp op, ArrayRef< uint64_t > unrollFactors, RewriterBase &rewriter, function_ref< void(unsigned, Operation *, OpBuilder)> annotateFn=nullptr, IRMapping *clonedToSrcOpsMap=nullptr)
Unroll this scf::Parallel loop by the specified unroll factors.
Definition Utils.cpp:1657
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:1341
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:115
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:805
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:1430
LogicalResult coalesceLoops(MutableArrayRef< scf::ForOp > loops)
Replace a perfect nest of "for" loops with a single linearized loop.
Definition Utils.cpp:1034
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:1483
llvm::function_ref< Fn > function_ref
Definition LLVM.h:147
TileLoops extractFixedOuterLoops(scf::ForOp rootFOrOp, ArrayRef< int64_t > sizes)
Definition Utils.cpp:1379
Range emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc, OpFoldResult lb, OpFoldResult ub, OpFoldResult step)
Materialize bounds and step of a zero-based and unit-step loop derived by normalizing the specified b...
Definition Utils.cpp:734
SmallVector< scf::ForOp, 8 > Loops
Tile a nest of standard for loops rooted at rootForOp by finding such parametric tile sizes that the ...
Definition Utils.h:149
FailureOr< scf::ForallOp > normalizeForallOp(RewriterBase &rewriter, scf::ForallOp forallOp)
Normalize an scf.forall operation.
Definition Utils.cpp:1536
Represents a range (offset, size, and stride) where each element of the triple may be dynamic or stat...
std::optional< scf::ForOp > epilogueLoopOp
Definition Utils.h:108
std::optional< scf::ForOp > mainLoopOp
Definition Utils.h:107