Skip to content

[mlir][Transforms] Improve replaceOpWithMultiple API #132608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion mlir/include/mlir/Transforms/DialectConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,18 @@ class ConversionPatternRewriter final : public PatternRewriter {

/// Replace the given operation with the new value ranges. The number of op
/// results and value ranges must match. The given operation is erased.
void replaceOpWithMultiple(Operation *op, ArrayRef<ValueRange> newValues);
void replaceOpWithMultiple(Operation *op,
SmallVector<SmallVector<Value>> &&newValues);
template <typename RangeT = ValueRange>
void replaceOpWithMultiple(Operation *op, ArrayRef<RangeT> newValues) {
replaceOpWithMultiple(op,
llvm::to_vector_of<SmallVector<Value>>(newValues));
}
template <typename RangeT>
void replaceOpWithMultiple(Operation *op, RangeT &&newValues) {
replaceOpWithMultiple(op,
ArrayRef(llvm::to_vector_of<ValueRange>(newValues)));
}

/// PatternRewriter hook for erasing a dead operation. The uses of this
/// operation *must* be made dead by the end of the conversion process,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,7 @@ class SparseCallConverter : public OpConversionPattern<func::CallOp> {
}

assert(packedResultVals.size() == op.getNumResults());
rewriter.replaceOpWithMultiple(
op, llvm::to_vector_of<ValueRange>(packedResultVals));
rewriter.replaceOpWithMultiple(op, std::move(packedResultVals));
return success();
}
};
Expand Down
33 changes: 17 additions & 16 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ struct ConversionValueMapping {
}
}

void map(Value oldVal, SmallVector<Value> &&newVal) {
map(ValueVector{oldVal}, ValueVector(std::move(newVal)));
}

/// Drop the last mapping for the given values.
void erase(const ValueVector &value) { mapping.erase(value); }

Expand Down Expand Up @@ -946,7 +950,8 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
OpBuilder::InsertPoint previous) override;

/// Notifies that an op is about to be replaced with the given values.
void notifyOpReplaced(Operation *op, ArrayRef<ValueRange> newValues);
void notifyOpReplaced(Operation *op,
SmallVector<SmallVector<Value>> &&newValues);

/// Notifies that a block is about to be erased.
void notifyBlockIsBeingErased(Block *block);
Expand Down Expand Up @@ -1519,7 +1524,7 @@ void ConversionPatternRewriterImpl::notifyOperationInserted(
}

void ConversionPatternRewriterImpl::notifyOpReplaced(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is also a sink function i.e. needs to at the very least take SmallVector<SmallVector<Value>>&& such that we can move repl below into map

Operation *op, ArrayRef<ValueRange> newValues) {
Operation *op, SmallVector<SmallVector<Value>> &&newValues) {
assert(newValues.size() == op->getNumResults());
assert(!ignoredOps.contains(op) && "operation was already replaced");

Expand Down Expand Up @@ -1561,7 +1566,7 @@ void ConversionPatternRewriterImpl::notifyOpReplaced(
// Remap result to replacement value.
if (repl.empty())
continue;
mapping.map(result, repl);
mapping.map(static_cast<Value>(result), std::move(repl));
}

appendRewrite<ReplaceOperationRewrite>(op, currentTypeConverter);
Expand Down Expand Up @@ -1639,35 +1644,31 @@ void ConversionPatternRewriter::replaceOp(Operation *op, ValueRange newValues) {
impl->logger.startLine()
<< "** Replace : '" << op->getName() << "'(" << op << ")\n";
});
SmallVector<ValueRange> newVals;
for (size_t i = 0; i < newValues.size(); ++i) {
if (newValues[i]) {
newVals.push_back(newValues.slice(i, 1));
} else {
newVals.push_back(ValueRange());
}
}
impl->notifyOpReplaced(op, newVals);
SmallVector<SmallVector<Value>> newVals =
llvm::map_to_vector(newValues, [](Value v) -> SmallVector<Value> {
return v ? SmallVector<Value>{v} : SmallVector<Value>();
});
impl->notifyOpReplaced(op, std::move(newVals));
}

void ConversionPatternRewriter::replaceOpWithMultiple(
Operation *op, ArrayRef<ValueRange> newValues) {
Operation *op, SmallVector<SmallVector<Value>> &&newValues) {
assert(op->getNumResults() == newValues.size() &&
"incorrect # of replacement values");
LLVM_DEBUG({
impl->logger.startLine()
<< "** Replace : '" << op->getName() << "'(" << op << ")\n";
});
impl->notifyOpReplaced(op, newValues);
impl->notifyOpReplaced(op, std::move(newValues));
}

void ConversionPatternRewriter::eraseOp(Operation *op) {
LLVM_DEBUG({
impl->logger.startLine()
<< "** Erase : '" << op->getName() << "'(" << op << ")\n";
});
SmallVector<ValueRange> nullRepls(op->getNumResults(), {});
impl->notifyOpReplaced(op, nullRepls);
SmallVector<SmallVector<Value>> nullRepls(op->getNumResults(), {});
impl->notifyOpReplaced(op, std::move(nullRepls));
}

void ConversionPatternRewriter::eraseBlock(Block *block) {
Expand Down
23 changes: 23 additions & 0 deletions mlir/test/lib/Dialect/Test/TestPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,29 @@ class TestMultiple1ToNReplacement : public ConversionPattern {
}
};

/// Test unambiguous overload resolution of replaceOpWithMultiple. This
/// function is just to trigger compiler errors. It is never executed.
[[maybe_unused]] void testReplaceOpWithMultipleOverloads(
ConversionPatternRewriter &rewriter, Operation *op, ArrayRef<ValueRange> r1,
SmallVector<ValueRange> r2, ArrayRef<SmallVector<Value>> r3,
SmallVector<SmallVector<Value>> r4, ArrayRef<ArrayRef<Value>> r5,
SmallVector<ArrayRef<Value>> r6, SmallVector<SmallVector<Value>> &&r7,
Value v, ValueRange vr, ArrayRef<Value> ar) {
rewriter.replaceOpWithMultiple(op, r1);
rewriter.replaceOpWithMultiple(op, r2);
rewriter.replaceOpWithMultiple(op, r3);
rewriter.replaceOpWithMultiple(op, r4);
rewriter.replaceOpWithMultiple(op, r5);
rewriter.replaceOpWithMultiple(op, r6);
rewriter.replaceOpWithMultiple(op, std::move(r7));
rewriter.replaceOpWithMultiple(op, {vr});
rewriter.replaceOpWithMultiple(op, {ar});
rewriter.replaceOpWithMultiple(op, {{v}});
rewriter.replaceOpWithMultiple(op, {{v, v}});
rewriter.replaceOpWithMultiple(op, {{v, v}, vr});
rewriter.replaceOpWithMultiple(op, {{v, v}, ar});
rewriter.replaceOpWithMultiple(op, {ar, {v, v}, vr});
}
} // namespace

namespace {
Expand Down