MLIR 23.0.0git
RegionUtils.h
Go to the documentation of this file.
1//===- RegionUtils.h - Region-related transformation 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#ifndef MLIR_TRANSFORMS_REGIONUTILS_H_
10#define MLIR_TRANSFORMS_REGIONUTILS_H_
11
12#include "mlir/IR/Region.h"
13#include "mlir/IR/Value.h"
14#include "mlir/IR/ValueRange.h"
15
16#include "llvm/ADT/SetVector.h"
17
18namespace mlir {
19class DominanceInfo;
20class RewriterBase;
21
22/// Check if all values in the provided range are defined above the `limit`
23/// region. That is, if they are defined in a region that is a proper ancestor
24/// of `limit`.
25template <typename Range>
26bool areValuesDefinedAbove(Range values, Region &limit) {
27 for (Value v : values)
28 if (!v.getParentRegion()->isProperAncestor(&limit))
29 return false;
30 return true;
31}
32
33/// Replace all uses of `orig` within the given region with `replacement`.
34void replaceAllUsesInRegionWith(Value orig, Value replacement, Region &region);
35
36/// Calls `callback` for each use of a value within `region` or its descendants
37/// that was defined at the ancestors of the `limit`.
38void visitUsedValuesDefinedAbove(Region &region, Region &limit,
39 function_ref<void(OpOperand *)> callback);
40
41/// Calls `callback` for each use of a value within any of the regions provided
42/// that was defined in one of the ancestors.
43void visitUsedValuesDefinedAbove(MutableArrayRef<Region> regions,
44 function_ref<void(OpOperand *)> callback);
45
46/// Fill `values` with a list of values defined at the ancestors of the `limit`
47/// region and used within `region` or its descendants.
48void getUsedValuesDefinedAbove(Region &region, Region &limit,
49 SetVector<Value> &values);
50
51/// Fill `values` with a list of values used within any of the regions provided
52/// but defined in one of the ancestors.
53void getUsedValuesDefinedAbove(MutableArrayRef<Region> regions,
54 SetVector<Value> &values);
55
56/// Make a region isolated from above
57/// - Capture the values that are defined above the region and used within it.
58/// - Append to the entry block arguments that represent the captured values
59/// (one per captured value).
60/// - Replace all uses within the region of the captured values with the
61/// newly added arguments.
62/// - `cloneOperationIntoRegion` is a callback that allows caller to specify
63/// if the operation defining an `OpOperand` needs to be cloned into the
64/// region. Then the operands of this operation become part of the captured
65/// values set (unless the operations that define the operands themeselves
66/// are to be cloned). The cloned operations are added to the entry block
67/// of the region.
68/// Return the set of captured values for the operation.
69SmallVector<Value> makeRegionIsolatedFromAbove(
70 RewriterBase &rewriter, Region &region,
71 llvm::function_ref<bool(Operation *)> cloneOperationIntoRegion =
72 [](Operation *) { return false; });
73
74/// Move the operation dependencies (producers) of `op` before `insertionPoint`,
75/// so that `op` itself can subsequently be moved. This includes transitive
76/// dependencies. Supports movement within the same block or from nested regions
77/// to an outer block.
78///
79/// The following conditions cause the move to fail:
80/// - `insertionPoint` does not dominate `op`.
81/// - Movement across an isolated-from-above region boundary.
82/// - A dependency uses a block argument that wouldn't dominate
83/// `insertionPoint`.
84/// - `insertionPoint` is itself a dependency of `op` (cycle).
85/// - Any side-effecting operations in the dependency chain pessimistically
86/// blocks movement.
87LogicalResult moveOperationDependencies(RewriterBase &rewriter, Operation *op,
88 Operation *insertionPoint,
89 DominanceInfo &dominance);
90LogicalResult moveOperationDependencies(RewriterBase &rewriter, Operation *op,
91 Operation *insertionPoint);
92
93/// Move definitions of `values` (and their transitive dependencies) before
94/// `insertionPoint`. Supports movement within the same block or from nested
95/// regions to an outer block.
96///
97/// This is all-or-nothing: either all definitions are moved, or none are.
98///
99/// The following conditions cause the move to fail:
100/// - Any value is a block argument (cannot be moved).
101/// - Any side-effecting operations in the dependency chain.
102/// - Movement across an isolated-from-above region boundary.
103/// - A dependency uses a block argument that wouldn't dominate
104/// `insertionPoint`.
105/// - `insertionPoint` is itself a dependency (cycle).
106LogicalResult moveValueDefinitions(RewriterBase &rewriter, ValueRange values,
107 Operation *insertionPoint,
108 DominanceInfo &dominance);
109LogicalResult moveValueDefinitions(RewriterBase &rewriter, ValueRange values,
110 Operation *insertionPoint);
111
112/// Run a set of structural simplifications over the given regions. This
113/// includes transformations like unreachable block elimination, dead argument
114/// elimination, as well as some other DCE. This function returns success if any
115/// of the regions were simplified, failure otherwise. The provided rewriter is
116/// used to notify callers of operation and block deletion.
117/// Structurally similar blocks will be merged if the `mergeBlock` argument is
118/// true. Note this can lead to merged blocks with extra arguments.
119LogicalResult simplifyRegions(RewriterBase &rewriter,
121 bool mergeBlocks = true);
122
123/// Erase the unreachable blocks within the provided regions. Returns success
124/// if any blocks were erased, failure otherwise.
125LogicalResult eraseUnreachableBlocks(RewriterBase &rewriter,
127
128/// This function returns success if any operations or arguments were deleted,
129/// failure otherwise.
130LogicalResult runRegionDCE(RewriterBase &rewriter,
132
133} // namespace mlir
134
135#endif // MLIR_TRANSFORMS_REGIONUTILS_H_
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be the output argument nBegin is set to its * replacement(set to `begin` if no invalidation happens). Since outgoing *copies could have been inserted at `end`
A class for computing basic dominance information.
Definition Dominance.h:140
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
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:387
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 replaceAllUsesInRegionWith(Value orig, Value replacement, Region &region)
Replace all uses of orig within the given region with replacement.
LogicalResult moveOperationDependencies(RewriterBase &rewriter, Operation *op, Operation *insertionPoint, DominanceInfo &dominance)
Move the operation dependencies (producers) of op before insertionPoint, so that op itself can subseq...
LogicalResult eraseUnreachableBlocks(RewriterBase &rewriter, MutableArrayRef< Region > regions)
Erase the unreachable blocks within the provided regions.
llvm::SetVector< T, Vector, Set, N > SetVector
Definition LLVM.h:123
SmallVector< Value > makeRegionIsolatedFromAbove(RewriterBase &rewriter, Region &region, llvm::function_ref< bool(Operation *)> cloneOperationIntoRegion=[](Operation *) { return false;})
Make a region isolated from above.
void getUsedValuesDefinedAbove(Region &region, Region &limit, SetVector< Value > &values)
Fill values with a list of values defined at the ancestors of the limit region and used within region...
LogicalResult runRegionDCE(RewriterBase &rewriter, MutableArrayRef< Region > regions)
This function returns success if any operations or arguments were deleted, failure otherwise.
bool areValuesDefinedAbove(Range values, Region &limit)
Check if all values in the provided range are defined above the limit region.
Definition RegionUtils.h:26
LogicalResult simplifyRegions(RewriterBase &rewriter, MutableArrayRef< Region > regions, bool mergeBlocks=true)
Run a set of structural simplifications over the given regions.
LogicalResult moveValueDefinitions(RewriterBase &rewriter, ValueRange values, Operation *insertionPoint, DominanceInfo &dominance)
Move definitions of values (and their transitive dependencies) before insertionPoint.
void visitUsedValuesDefinedAbove(Region &region, Region &limit, function_ref< void(OpOperand *)> callback)
Calls callback for each use of a value within region or its descendants that was defined at the ances...
llvm::function_ref< Fn > function_ref
Definition LLVM.h:144
Represents a range (offset, size, and stride) where each element of the triple may be dynamic or stat...