Skip to content

Commit 3db6011

Browse files
[mlir][Transforms] Tighten replaceUsesOfBlockArgument (llvm#155227)
Improve the documentation of `replaceUsesOfBlockArgument` to clarify its semantics is rollback mode. Add an assertion to make sure that the same block argument is not replaced multiple times. That's an API violation and messes with the internal state of the conversion driver. This commit is in preparation of adding full support for `RewriterBase::replaceAllUsesWith`.
1 parent 704a10c commit 3db6011

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

mlir/include/mlir/Transforms/DialectConversion.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,12 @@ class ConversionPatternRewriter final : public PatternRewriter {
782782

783783
/// Replace all the uses of the block argument `from` with `to`. This
784784
/// function supports both 1:1 and 1:N replacements.
785+
///
786+
/// Note: If `allowPatternRollback` is set to "true", this function replaces
787+
/// all current and future uses of the block argument. This same block
788+
/// block argument must not be replaced multiple times. Uses are not replaced
789+
/// immediately but in a delayed fashion. Patterns may still see the original
790+
/// uses when inspecting IR.
785791
void replaceUsesOfBlockArgument(BlockArgument from, ValueRange to);
786792

787793
/// Return the converted value of 'key' with a type defined by the type

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,11 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
11321132
IRRewriter notifyingRewriter;
11331133

11341134
#ifndef NDEBUG
1135+
/// A set of replaced block arguments. This set is for debugging purposes
1136+
/// only and it is maintained only if `allowPatternRollback` is set to
1137+
/// "true".
1138+
DenseSet<BlockArgument> replacedArgs;
1139+
11351140
/// A set of operations that have pending updates. This tracking isn't
11361141
/// strictly necessary, and is thus only active during debug builds for extra
11371142
/// verification.
@@ -1892,6 +1897,19 @@ void ConversionPatternRewriterImpl::replaceUsesOfBlockArgument(
18921897
return;
18931898
}
18941899

1900+
#ifndef NDEBUG
1901+
// Make sure that a block argument is not replaced multiple times. In
1902+
// rollback mode, `replaceUsesOfBlockArgument` replaces not only all current
1903+
// uses of the given block argument, but also all future uses that may be
1904+
// introduced by future pattern applications. Therefore, it does not make
1905+
// sense to call `replaceUsesOfBlockArgument` multiple times with the same
1906+
// block argument. Doing so would overwrite the mapping and mess with the
1907+
// internal state of the dialect conversion driver.
1908+
assert(!replacedArgs.contains(from) &&
1909+
"attempting to replace a block argument that was already replaced");
1910+
replacedArgs.insert(from);
1911+
#endif // NDEBUG
1912+
18951913
appendRewrite<ReplaceBlockArgRewrite>(from.getOwner(), from, converter);
18961914
mapping.map(from, to);
18971915
}

0 commit comments

Comments
 (0)