Skip to content

Commit 1311306

Browse files
committed
[mlir][AttrTypeReplacer] Make attribute dictionary replacement optional
This provides an optimization opportunity for clients that don't want/need to recurse attribute dictionaries.
1 parent 03d07e1 commit 1311306

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

mlir/include/mlir/IR/SubElementInterfaces.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ class AttrTypeReplacer {
3131
// Application
3232
//===--------------------------------------------------------------------===//
3333

34-
/// Replace the elements within the given operation. By default this includes
35-
/// the attributes within the operation. If `replaceLocs` is true, this also
36-
/// updates its location, the locations of any nested block arguments. If
37-
/// `replaceTypes` is true, this also updates the result types of the
38-
/// operation, and the types of any nested block arguments.
39-
void replaceElementsIn(Operation *op, bool replaceLocs = false,
40-
bool replaceTypes = false);
34+
/// Replace the elements within the given operation. If `replaceAttrs` is
35+
/// true, this updates the attribute dictionary of the operation. If
36+
/// `replaceLocs` is true, this also updates its location, and the locations
37+
/// of any nested block arguments. If `replaceTypes` is true, this also
38+
/// updates the result types of the operation, and the types of any nested
39+
/// block arguments.
40+
void replaceElementsIn(Operation *op, bool replaceAttrs = true,
41+
bool replaceLocs = false, bool replaceTypes = false);
4142

4243
/// Replace the given attribute/type, and recursively replace any sub
4344
/// elements. Returns either the new attribute/type, or nullptr in the case of

mlir/lib/IR/SubElementInterfaces.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,20 @@ void SubElementTypeInterface::walkSubElements(
9595
/// AttrTypeReplacer
9696
//===----------------------------------------------------------------------===//
9797

98-
void AttrTypeReplacer::replaceElementsIn(Operation *op, bool replaceLocs,
99-
bool replaceTypes) {
98+
void AttrTypeReplacer::replaceElementsIn(Operation *op, bool replaceAttrs,
99+
bool replaceLocs, bool replaceTypes) {
100100
// Functor that replaces the given element if the new value is different,
101101
// otherwise returns nullptr.
102102
auto replaceIfDifferent = [&](auto element) {
103103
auto replacement = replace(element);
104104
return (replacement && replacement != element) ? replacement : nullptr;
105105
};
106-
// Check the attribute dictionary for replacements.
107-
if (auto newAttrs = replaceIfDifferent(op->getAttrDictionary()))
108-
op->setAttrs(cast<DictionaryAttr>(newAttrs));
106+
107+
// Update the attribute dictionary.
108+
if (replaceAttrs) {
109+
if (auto newAttrs = replaceIfDifferent(op->getAttrDictionary()))
110+
op->setAttrs(cast<DictionaryAttr>(newAttrs));
111+
}
109112

110113
// If we aren't updating locations or types, we're done.
111114
if (!replaceTypes && !replaceLocs)

0 commit comments

Comments
 (0)