Skip to content

[mlir][Transforms] Dialect conversion: Erase materialized constants instead of rollback #136489

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
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
8 changes: 6 additions & 2 deletions mlir/include/mlir/IR/Builders.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,13 @@ class OpBuilder : public Builder {

/// Attempts to fold the given operation and places new results within
/// `results`. Returns success if the operation was folded, failure otherwise.
/// If the fold was in-place, `results` will not be filled.
/// If the fold was in-place, `results` will not be filled. Optionally, newly
/// materialized constant operations can be returned to the caller.
///
/// Note: This function does not erase the operation on a successful fold.
LogicalResult tryFold(Operation *op, SmallVectorImpl<Value> &results);
LogicalResult
tryFold(Operation *op, SmallVectorImpl<Value> &results,
SmallVectorImpl<Operation *> *materializedConstants = nullptr);

/// Creates a deep copy of the specified operation, remapping any operands
/// that use values outside of the operation using the map that is provided
Expand Down
9 changes: 7 additions & 2 deletions mlir/lib/IR/Builders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,9 @@ Operation *OpBuilder::create(Location loc, StringAttr opName,
return create(state);
}

LogicalResult OpBuilder::tryFold(Operation *op,
SmallVectorImpl<Value> &results) {
LogicalResult
OpBuilder::tryFold(Operation *op, SmallVectorImpl<Value> &results,
SmallVectorImpl<Operation *> *materializedConstants) {
assert(results.empty() && "expected empty results");
ResultRange opResults = op->getResults();

Expand Down Expand Up @@ -528,6 +529,10 @@ LogicalResult OpBuilder::tryFold(Operation *op,
for (Operation *cst : generatedConstants)
insert(cst);

// Return materialized constant operations.
if (materializedConstants)
*materializedConstants = std::move(generatedConstants);

return success();
}

Expand Down
21 changes: 9 additions & 12 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2090,37 +2090,34 @@ LogicalResult
OperationLegalizer::legalizeWithFold(Operation *op,
ConversionPatternRewriter &rewriter) {
auto &rewriterImpl = rewriter.getImpl();
RewriterState curState = rewriterImpl.getCurrentState();

LLVM_DEBUG({
rewriterImpl.logger.startLine() << "* Fold {\n";
rewriterImpl.logger.indent();
});

// Try to fold the operation.
SmallVector<Value, 2> replacementValues;
SmallVector<Operation *, 2> newOps;
rewriter.setInsertionPoint(op);
if (failed(rewriter.tryFold(op, replacementValues))) {
if (failed(rewriter.tryFold(op, replacementValues, &newOps))) {
LLVM_DEBUG(logFailure(rewriterImpl.logger, "unable to fold"));
return failure();
}

// An empty list of replacement values indicates that the fold was in-place.
// As the operation changed, a new legalization needs to be attempted.
if (replacementValues.empty())
return legalize(op, rewriter);

// Recursively legalize any new constant operations.
for (unsigned i = curState.numRewrites, e = rewriterImpl.rewrites.size();
i != e; ++i) {
auto *createOp =
dyn_cast<CreateOperationRewrite>(rewriterImpl.rewrites[i].get());
if (!createOp)
continue;
if (failed(legalize(createOp->getOperation(), rewriter))) {
for (Operation *newOp : newOps) {
if (failed(legalize(newOp, rewriter))) {
LLVM_DEBUG(logFailure(rewriterImpl.logger,
"failed to legalize generated constant '{0}'",
createOp->getOperation()->getName()));
rewriterImpl.resetState(curState);
newOp->getName()));
// Legalization failed: erase all materialized constants.
for (Operation *op : newOps)
rewriter.eraseOp(op);
return failure();
}
}
Expand Down
Loading