MLIR 24.0.0git
FoldUtils.h
Go to the documentation of this file.
1//===- FoldUtils.h - Operation Fold 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 declares various operation folding utilities. These
10// utilities are intended to be used by passes to unify and simply their logic.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_TRANSFORMS_FOLDUTILS_H
15#define MLIR_TRANSFORMS_FOLDUTILS_H
16
17#include "mlir/IR/Builders.h"
18#include "mlir/IR/Dialect.h"
22
23namespace mlir {
24class Operation;
25class Value;
26
27//===--------------------------------------------------------------------===//
28// OperationFolder
29//===--------------------------------------------------------------------===//
30
31/// A utility class for folding operations, and unifying duplicated constants
32/// generated along the way.
34public:
36 : erasedFoldedLocation(UnknownLoc::get(ctx)), interfaces(ctx),
37 rewriter(ctx, listener) {}
38
39 /// Tries to perform folding on the given `op`, including unifying
40 /// deduplicated constants. If successful, replaces `op`'s uses with
41 /// folded results, and returns success. If the op was completely folded it is
42 /// erased. If it is just updated in place, `inPlaceUpdate` is set to true.
43 /// On success() and when in-place, the folder is invoked until
44 /// `maxIterations` is reached (default INT_MAX).
45 LogicalResult tryToFold(Operation *op, bool *inPlaceUpdate = nullptr,
46 int maxIterations = INT_MAX);
47
48 /// The effect of inserting a known constant on the IR.
50 /// The operation was retained without modifying the IR.
52 /// The operation was retained but moved to the insertion point.
54 /// The operation was replaced with an existing constant and erased.
56 };
57
58 /// Tries to fold a pre-existing constant operation. `constValue` represents
59 /// the value of the constant, and can be optionally passed if the value is
60 /// already known (e.g. if the constant was discovered by m_Constant). This is
61 /// purely an optimization opportunity for callers that already know the value
62 /// of the constant. Returns Replaced if an existing constant for `op` already
63 /// exists in the folder, in which case `op` is replaced and erased.
64 /// Otherwise, `op` is inserted into the folder (and hoisted if necessary),
65 /// returning Hoisted if it was moved or Unchanged if the IR was not modified.
67 Attribute constValue = {});
68
69 /// Notifies that the given constant `op` should be remove from this
70 /// OperationFolder's internal bookkeeping.
71 ///
72 /// Note: this method must be called if a constant op is to be deleted
73 /// externally to this OperationFolder. `op` must be a constant op.
74 void notifyRemoval(Operation *op);
75
76 /// Clear out any constants cached inside of the folder.
77 void clear();
78
79 /// Get or create a constant for use in the specified block. The constant may
80 /// be created in a parent block. On success this returns the constant
81 /// operation, nullptr otherwise.
82 Value getOrCreateConstant(Block *block, Dialect *dialect, Attribute value,
83 Type type);
84
85private:
86 /// This map keeps track of uniqued constants by dialect, attribute, and type.
87 /// A constant operation materializes an attribute with a type. Dialects may
88 /// generate different constants with the same input attribute and type, so we
89 /// also need to track per-dialect.
90 using ConstantMap =
92
93 /// Returns true if the given operation is an already folded constant that is
94 /// owned by this folder.
95 bool isFolderOwnedConstant(Operation *op) const;
96
97 /// Tries to perform folding on the given `op`. If successful, populates
98 /// `results` with the results of the folding.
99 /// On success() and when in-place, the folder is invoked until
100 /// `maxIterations` is reached (default INT_MAX).
101 LogicalResult tryToFold(Operation *op, SmallVectorImpl<Value> &results,
102 int maxIterations = INT_MAX);
103
104 /// Try to process a set of fold results. Populates `results` on success,
105 /// otherwise leaves it unchanged.
106 LogicalResult processFoldResults(Operation *op,
107 SmallVectorImpl<Value> &results,
108 ArrayRef<OpFoldResult> foldResults);
109
110 /// Try to get or create a new constant entry. On success this returns the
111 /// constant operation, nullptr otherwise.
112 Operation *tryGetOrCreateConstant(ConstantMap &uniquedConstants,
113 Dialect *dialect, Attribute value,
114 Type type, Location loc);
115
116 /// The location to overwrite with for folder-owned constants.
117 UnknownLoc erasedFoldedLocation;
118
119 /// A mapping between an insertion region and the constants that have been
120 /// created within it.
122
123 /// This map tracks all of the dialects that an operation is referenced by;
124 /// given that many dialects may generate the same constant.
126
127 /// A collection of dialect folder interfaces.
128 DialectInterfaceCollection<DialectFoldInterface> interfaces;
129
130 /// A rewriter that performs all IR modifications.
131 IRRewriter rewriter;
132};
133
134} // namespace mlir
135
136#endif // MLIR_TRANSFORMS_FOLDUTILS_H
Attributes are known-constant values of operations.
Definition Attributes.h:25
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:64
Value getOrCreateConstant(Block *block, Dialect *dialect, Attribute value, Type type)
Get or create a constant for use in the specified block.
void clear()
Clear out any constants cached inside of the folder.
ConstantInsertionResult
The effect of inserting a known constant on the IR.
Definition FoldUtils.h:49
@ Hoisted
The operation was retained but moved to the insertion point.
Definition FoldUtils.h:53
@ Unchanged
The operation was retained without modifying the IR.
Definition FoldUtils.h:51
@ Replaced
The operation was replaced with an existing constant and erased.
Definition FoldUtils.h:55
ConstantInsertionResult insertKnownConstant(Operation *op, Attribute constValue={})
Tries to fold a pre-existing constant operation.
LogicalResult tryToFold(Operation *op, bool *inPlaceUpdate=nullptr, int maxIterations=INT_MAX)
Tries to perform folding on the given op, including unifying deduplicated constants.
Definition FoldUtils.cpp:71
void notifyRemoval(Operation *op)
Notifies that the given constant op should be remove from this OperationFolder's internal bookkeeping...
OperationFolder(MLIRContext *ctx, OpBuilder::Listener *listener=nullptr)
Definition FoldUtils.h:35
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:123
This class represents a listener that may be used to hook into various actions within an OpBuilder.
Definition Builders.h:288