[MLIR][Math] Move exponent threshold check before IR creation in PowIStrengthReduction#188955
Merged
Merged
Conversation
…StrengthReduction PowIStrengthReduction::matchAndRewrite was creating the `one` constant (using complex::ConstantOp::create for complex::PowiOp) before the threshold check that guards whether the rewrite is profitable. When the exponent exceeds the threshold, the pattern returned failure() after IR was already modified, violating MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS. Fix: reorder so the abs(exponent) computation and threshold check occur before any IR creation. Assisted-by: Claude Code Fix a failure present with MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS=ON.
Member
|
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-math Author: Mehdi Amini (joker-eph) ChangesPowIStrengthReduction::matchAndRewrite was creating the Fix: reorder so the abs(exponent) computation and threshold check occur before any IR creation. Assisted-by: Claude Code Full diff: https://github.com/llvm/llvm-project/pull/188955.diff 1 Files Affected:
diff --git a/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp b/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp
index ff5f7f685903f..7ce7b2153ffb4 100644
--- a/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp
+++ b/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp
@@ -166,6 +166,18 @@ PowIStrengthReduction<PowIOpTy, DivOpTy, MulOpTy>::matchAndRewrite(
else
return failure();
+ // Compute abs(exponent) and check the threshold before creating any IR,
+ // so that returning failure() here does not violate the pattern API contract.
+ bool exponentIsNegative = false;
+ if (exponentValue < 0) {
+ exponentIsNegative = true;
+ exponentValue *= -1;
+ }
+
+ // Bail out if `abs(exponent)` exceeds the threshold (exponent==0 is free).
+ if (exponentValue != 0 && exponentValue > exponentThreshold)
+ return failure();
+
// Maybe broadcasts scalar value into vector type compatible with `op`.
auto bcast = [&loc, &op, &rewriter](Value value) -> Value {
if (auto vec = dyn_cast<VectorType>(op.getType()))
@@ -196,16 +208,6 @@ PowIStrengthReduction<PowIOpTy, DivOpTy, MulOpTy>::matchAndRewrite(
return success();
}
- bool exponentIsNegative = false;
- if (exponentValue < 0) {
- exponentIsNegative = true;
- exponentValue *= -1;
- }
-
- // Bail out if `abs(exponent)` exceeds the threshold.
- if (exponentValue > exponentThreshold)
- return failure();
-
Value result = base;
// Transform to naive sequence of multiplications:
// * For positive exponent case replace:
|
matthias-springer
approved these changes
Apr 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PowIStrengthReduction::matchAndRewrite was creating the
oneconstant (using complex::ConstantOp::create for complex::PowiOp) before the threshold check that guards whether the rewrite is profitable. When the exponent exceeds the threshold, the pattern returned failure() after IR was already modified, violating MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS.Fix: reorder so the abs(exponent) computation and threshold check occur before any IR creation.
Assisted-by: Claude Code
Fix a failure present with MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS=ON.