Skip to content

Conversation

@matthias-springer
Copy link
Member

@matthias-springer matthias-springer commented Jul 12, 2025

When an operation is folded to an attribute, the attribute must be materialized as a constant operation. That operation must then be legalized. If such a legalization fails, the entire folding is rolled back. This is not supported in a One-Shot Dialect Conversion. (Support for rolling back foldings could be added at a later point of time.)

This commit improves the allowPatternRollback flag handling, such that a fatal error is reported when a folder is attempted to be rolled back.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Jul 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 12, 2025

@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir

Author: Matthias Springer (matthias-springer)

Changes

When an operation is folded to an attribute, the attribute must be materialized as a constant operation. That operation must then be legalized. If such a legalization fails, the entire folding is rolled back. This is not supported in a One-Shot Dialect Conversion.

This commit improves the allowPatternRollback flag handling, such that a fatal error is reported when a folder is attempted to be rolled back.


Full diff: https://github.com/llvm/llvm-project/pull/148394.diff

1 Files Affected:

  • (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+7)
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 9f71129d39d09..437dbcfea5288 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -2176,6 +2176,7 @@ OperationLegalizer::legalizeWithFold(Operation *op,
   (void)rewriterImpl;
 
   // Try to fold the operation.
+  StringRef opName = op->getName().getStringRef();
   SmallVector<Value, 2> replacementValues;
   SmallVector<Operation *, 2> newOps;
   rewriter.setInsertionPoint(op);
@@ -2195,6 +2196,12 @@ OperationLegalizer::legalizeWithFold(Operation *op,
       LLVM_DEBUG(logFailure(rewriterImpl.logger,
                             "failed to legalize generated constant '{0}'",
                             newOp->getName()));
+      if (!config.allowPatternRollback) {
+        // Rolling back a folder is like rolling back a pattern.
+        llvm::report_fatal_error(
+            "op '" + opName +
+            "' folder rollback of IR modifications requested");
+      }
       // Legalization failed: erase all materialized constants.
       for (Operation *op : newOps)
         rewriter.eraseOp(op);

@matthias-springer matthias-springer marked this pull request as draft July 12, 2025 19:13
@matthias-springer matthias-springer changed the title [mlir][Transforms] Dialect Conversion: allowPatternRollback to check foldings [mlir][Transforms] WIP: Dialect Conversion: allowPatternRollback to check foldings Jul 12, 2025
@matthias-springer matthias-springer changed the title [mlir][Transforms] WIP: Dialect Conversion: allowPatternRollback to check foldings [mlir][Transforms] Dialect Conversion: allowPatternRollback to check foldings Jul 12, 2025
@matthias-springer matthias-springer marked this pull request as ready for review July 12, 2025 19:31
@matthias-springer matthias-springer merged commit 58c0bd1 into main Jul 13, 2025
14 checks passed
@matthias-springer matthias-springer deleted the users/matthias-springer/folder_rollback branch July 13, 2025 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mlir:core MLIR Core Infrastructure mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants