MLIR 24.0.0git
LoopInvariantCodeMotionUtils.h
Go to the documentation of this file.
1//===- LoopInvariantCodeMotionUtils.h - LICM Utils --------------*- 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#ifndef MLIR_TRANSFORMS_LOOPINVARIANTCODEMOTIONUTILS_H
10#define MLIR_TRANSFORMS_LOOPINVARIANTCODEMOTIONUTILS_H
11
12#include "mlir/Support/LLVM.h"
13
14#include "llvm/ADT/SmallVector.h"
15
16namespace mlir {
17
18class LoopLikeOpInterface;
19class Operation;
20class Region;
21class RewriterBase;
22class Value;
23
24/// Given a list of regions, perform loop-invariant code motion. An operation is
25/// loop-invariant if it depends only of values defined outside of the loop.
26/// LICM moves these operations out of the loop body so that they are not
27/// computed more than once.
28///
29/// Example:
30///
31/// ```mlir
32/// affine.for %arg0 = 0 to 10 {
33/// affine.for %arg1 = 0 to 10 {
34/// %v0 = arith.addi %arg0, %arg0 : i32
35/// %v1 = arith.addi %v0, %arg1 : i32
36/// }
37/// }
38/// ```
39///
40/// After LICM:
41///
42/// ```mlir
43/// affine.for %arg0 = 0 to 10 {
44/// %v0 = arith.addi %arg0, %arg0 : i32
45/// affine.for %arg1 = 0 to 10 {
46/// %v1 = arith.addi %v0, %arg1 : i32
47/// }
48/// }
49/// ```
50///
51/// Users must supply three callbacks.
52///
53/// - `isDefinedOutsideRegion` returns true if the given value is invariant with
54/// respect to the given region. A common implementation might be:
55/// `value.getParentRegion()->isProperAncestor(region)`.
56/// - `shouldMoveOutOfRegion` returns true if the provided operation can be
57/// moved of the given region, e.g. if it is side-effect free.
58/// - `moveOutOfRegion` moves the operation out of the given region. A common
59/// implementation might be: `op->moveBefore(region->getParentOp())`.
60///
61/// An operation is moved if all of its operands satisfy
62/// `isDefinedOutsideRegion` and it satisfies `shouldMoveOutOfRegion`.
63///
64/// Returns the number of operations moved.
66 ArrayRef<Region *> regions,
67 function_ref<bool(Value, Region *)> isDefinedOutsideRegion,
68 function_ref<bool(Operation *, Region *)> shouldMoveOutOfRegion,
69 function_ref<void(Operation *, Region *)> moveOutOfRegion);
70
71/// Move side-effect free loop invariant code out of a loop-like op using
72/// methods provided by the interface. This includes code in control-flow
73/// regions nested in the loop body, see
74/// `moveLoopInvariantCodeFromNestedRegions`.
75size_t moveLoopInvariantCode(LoopLikeOpInterface loopLike);
76
77/// Move loop invariant code out of the control-flow regions nested in the body
78/// of a loop-like op, e.g., out of an `scf.if` that stays in the loop:
79///
80/// ```mlir
81/// scf.for %i = %lb to %ub step %step {
82/// scf.if %cond {
83/// %v = arith.muli %a, %b : i32 // Moved before the loop.
84/// memref.store %v, %m[%i] : memref<?xi32>
85/// }
86/// }
87/// ```
88///
89/// Only regions of `RegionBranchOpInterface` ops are inspected, and nested
90/// loops are skipped. A nested region is not guaranteed to execute, so only
91/// speculatable ops without memory effects are moved.
92///
93/// Returns the number of operations moved.
94size_t moveLoopInvariantCodeFromNestedRegions(LoopLikeOpInterface loopLike);
95
96/// Hoist loop-invariant tensor subsets (subset extraction and subset insertion
97/// ops) from loop-like ops. Extraction ops are moved before the loop. Insertion
98/// ops are moved after the loop. The loop body operates on newly added region
99/// iter_args (one per extraction-insertion pair).
100///
101/// A subset extraction op (`SubsetExtractionOpInterface`) extracts from a
102/// tensor value at a subset. The result of the op may have an arbitrary type,
103/// i.e., not necessarily a tensor type. Example: "tensor.extract_slice".
104///
105/// A subset insertion op (`SubsetInsertionOpInterface`) inserts into a tensor
106/// value ("destination") at a subset. Example: "tensor.insert_slice".
107///
108/// Matching extraction-insertion subset ops can be hoisted from a loop if there
109/// are no other ops within the loop that operate on the same or on an
110/// overlapping subset. In particular, non-subset ops can prevent hoisting
111/// because the analysis does not know what subset they operate on.
112///
113/// Example:
114/// ```
115/// %r = scf.for ... iter_args(%t = %a) -> (tensor<?xf32>) {
116/// %0 = tensor.extract_slice %t[0][5][1] : tensor<?xf32> to tensor<5xf32>
117/// %1 = "test.foo"(%0) : (tensor<5xf32>) -> (tensor<5xf32>)
118/// %2 = tensor.insert_slice %1 into %t[0][5][1]
119/// : tensor<5xf32> into tensor<?xf32>
120/// scf.yield %2 : tensor<?xf32>
121/// }
122/// ```
123/// Is rewritten to:
124/// ```
125/// %0 = tensor.extract_slice %a[0][5][1] : tensor<?xf32> to tensor<5xf32>
126/// %new_loop:2 = scf.for ... iter_args(%t = %a, %h = %0) -> (tensor<?xf32>) {
127/// %1 = "test.foo"(%h) : (tensor<5xf32>) -> (tensor<5xf32>)
128/// scf.yield %t, %2 : tensor<?xf32>, tensor<5xf32>
129/// }
130/// %r = tensor.insert_slice %new_loop#1 into %new_loop#0
131/// : tensor<5xf32> into tensor<?xf32>
132/// ```
133LoopLikeOpInterface hoistLoopInvariantSubsets(RewriterBase &rewriter,
134 LoopLikeOpInterface loopLike);
135
136} // end namespace mlir
137
138#endif // MLIR_TRANSFORMS_LOOPINVARIANTCODEMOTIONUTILS_H
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 represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Include the generated interface declarations.
LoopLikeOpInterface hoistLoopInvariantSubsets(RewriterBase &rewriter, LoopLikeOpInterface loopLike)
Hoist loop-invariant tensor subsets (subset extraction and subset insertion ops) from loop-like ops.
size_t moveLoopInvariantCodeFromNestedRegions(LoopLikeOpInterface loopLike)
Move loop invariant code out of the control-flow regions nested in the body of a loop-like op,...
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155
size_t moveLoopInvariantCode(ArrayRef< Region * > regions, function_ref< bool(Value, Region *)> isDefinedOutsideRegion, function_ref< bool(Operation *, Region *)> shouldMoveOutOfRegion, function_ref< void(Operation *, Region *)> moveOutOfRegion)
Given a list of regions, perform loop-invariant code motion.