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.
103 
104 /// Take the ParallelLoop and for each set of dimension indices, combine them
105 /// into a single dimension. combinedDimensions must contain each index into
106 /// loops exactly once.
107 void collapseParallelLoops(scf::ParallelOp loops,
108  ArrayRef<std::vector<unsigned>> combinedDimensions);
109 
110 /// Unrolls this for operation by the specified unroll factor. Returns failure
111 /// if the loop cannot be unrolled either due to restrictions or due to invalid
112 /// unroll factors. Requires positive loop bounds and step. If specified,
113 /// annotates the Ops in each unrolled iteration by applying `annotateFn`.
115  scf::ForOp forOp, uint64_t unrollFactor,
116  function_ref<void(unsigned, Operation *, OpBuilder)> annotateFn = nullptr);
117 
118 /// Tile a nest of standard for loops rooted at `rootForOp` by finding such
119 /// parametric tile sizes that the outer loops have a fixed number of iterations
120 /// as defined in `sizes`.
122 using TileLoops = std::pair<Loops, Loops>;
123 TileLoops extractFixedOuterLoops(scf::ForOp rootFOrOp, ArrayRef<int64_t> sizes);
124 
125 /// Performs tiling fo imperfectly nested loops (with interchange) by
126 /// strip-mining the `forOps` by `sizes` and sinking them, in their order of
127 /// occurrence in `forOps`, under each of the `targets`.
128 /// Returns the new AffineForOps, one per each of (`forOps`, `targets`) pair,
129 /// nested immediately under each of `targets`.
131  ArrayRef<scf::ForOp> targets);
132 
133 /// Performs tiling (with interchange) by strip-mining the `forOps` by `sizes`
134 /// and sinking them, in their order of occurrence in `forOps`, under `target`.
135 /// Returns the new AffineForOps, one per `forOps`, nested immediately under
136 /// `target`.
138  scf::ForOp target);
139 
140 /// Tile a nest of scf::ForOp loops rooted at `rootForOp` with the given
141 /// (parametric) sizes. Sizes are expected to be strictly positive values at
142 /// runtime. If more sizes than loops are provided, discard the trailing values
143 /// in sizes. Assumes the loop nest is permutable.
144 /// Returns the newly created intra-tile loops.
145 Loops tilePerfectlyNested(scf::ForOp rootForOp, ArrayRef<Value> sizes);
146 
147 /// Get perfectly nested sequence of loops starting at root of loop nest
148 /// (the first op being another AffineFor, and the second op - a terminator).
149 /// A loop is perfectly nested iff: the first op in the loop's body is another
150 /// AffineForOp, and the second op is a terminator).
152  scf::ForOp root);
153 
154 /// Given two scf.forall loops, `target` and `source`, fuses `target` into
155 /// `source`. Assumes that the given loops are siblings and are independent of
156 /// each other.
157 ///
158 /// This function does not perform any legality checks and simply fuses the
159 /// loops. The caller is responsible for ensuring that the loops are legal to
160 /// fuse.
161 scf::ForallOp fuseIndependentSiblingForallLoops(scf::ForallOp target,
162  scf::ForallOp source,
163  RewriterBase &rewriter);
164 
165 } // namespace mlir
166 
167 #endif // MLIR_DIALECT_SCF_UTILS_UTILS_H_
This class helps build Operations.
Definition: Builders.h:209
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:399
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:860
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:355
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:224
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:41
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:608
std::pair< Loops, Loops > TileLoops
Definition: Utils.h:122
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:848
bool getInnermostParallelLoops(Operation *rootOp, SmallVectorImpl< scf::ParallelOp > &result)
Get a list of innermost parallel loops contained in rootOp.
Definition: Utils.cpp:247
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:825
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:120
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:907
LogicalResult coalesceLoops(MutableArrayRef< scf::ForOp > loops)
Replace a perfect nest of "for" loops with a single linearized loop.
Definition: Utils.cpp:551
TileLoops extractFixedOuterLoops(scf::ForOp rootFOrOp, ArrayRef< int64_t > sizes)
Definition: Utils.cpp:865
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26