Skip to content

[mlir] Don't assert when simplifying certain AffineExprs #78855

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 2 commits into from
Mar 19, 2024

Conversation

ubfx
Copy link
Member

@ubfx ubfx commented Jan 20, 2024

Currently, simplifyMul() asserts that either lhs or rhs is symbolic or constant. This method is called by the overloaded * operator for AffineExprs which leads to a crash when building a multiplication expression where neither operand is symbolic or constant.
This patch returns a nullptr from simplifyMul() to signal that the expression could not be simplified instead.

Fix #75770

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Jan 20, 2024
@ubfx ubfx requested a review from ftynse January 20, 2024 18:09
@llvmbot
Copy link
Member

llvmbot commented Jan 20, 2024

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Felix Schneider (ubfx)

Changes

Currently, simplifyMul() asserts that either lhs or rhs is symbolic or constant. This method is called by the overloaded * operator for AffineExprs which leads to a crash when building a multiplication expression where neither operand is symbolic or constant.
This patch returns a nullptr from simplifyMul() to signal that the expression could not be simplified instead.

Fix #75770


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

1 Files Affected:

  • (modified) mlir/lib/IR/AffineExpr.cpp (+2-1)
diff --git a/mlir/lib/IR/AffineExpr.cpp b/mlir/lib/IR/AffineExpr.cpp
index a90b264a8edd26..77c8fcc96d0ea0 100644
--- a/mlir/lib/IR/AffineExpr.cpp
+++ b/mlir/lib/IR/AffineExpr.cpp
@@ -774,7 +774,8 @@ static AffineExpr simplifyMul(AffineExpr lhs, AffineExpr rhs) {
     return getAffineConstantExpr(lhsConst.getValue() * rhsConst.getValue(),
                                  lhs.getContext());
 
-  assert(lhs.isSymbolicOrConstant() || rhs.isSymbolicOrConstant());
+  if (!lhs.isSymbolicOrConstant() && !rhs.isSymbolicOrConstant())
+    return nullptr;
 
   // Canonicalize the mul expression so that the constant/symbolic term is the
   // RHS. If both the lhs and rhs are symbolic, swap them if the lhs is a

@ubfx ubfx requested review from ftynse and removed request for ftynse March 6, 2024 13:28
Copy link
Member

@ftynse ftynse left a comment

Choose a reason for hiding this comment

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

Is there a way to test this? Perhaps in a unit test?

Currently, `simplifyMul()` asserts that either `lhs` or `rhs` is symbolic or constant. This method is called by the overloaded `*` operator for `AffineExpr`s which leads to a crash when building a multiplication expression where neither operand is symbolic or constant.
This patch returns a `nullptr` from `simplifyMul()` to signal that the expression could not be simplified instead.
@ubfx ubfx force-pushed the simplify-mul-crash branch from ba7b34b to b452fa8 Compare March 19, 2024 07:09
@ubfx ubfx force-pushed the simplify-mul-crash branch from b452fa8 to 1c7fcab Compare March 19, 2024 07:23
@ubfx
Copy link
Member Author

ubfx commented Mar 19, 2024

@ftynse Like this? Or were you thinking a more thorough test with walking the resulting AffineExpr. Although this might get more difficult because we would have to anticipate all possible rearrangements and simplifications.
This test would have caught the linked issue because simplifyMul() would have crashed on d0*d1

@ftynse
Copy link
Member

ftynse commented Mar 19, 2024

Good enough. I usually add the minimized test case that triggered the assertion in the first place, this lets us catch an eventual regression.

@ubfx ubfx merged commit a4b2363 into llvm:main Mar 19, 2024
@ubfx ubfx deleted the simplify-mul-crash branch March 19, 2024 09:43
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
Currently, `simplifyMul()` asserts that either `lhs` or `rhs` is
symbolic or constant. This method is called by the overloaded `*`
operator for `AffineExpr`s which leads to a crash when building a
multiplication expression where neither operand is symbolic or constant.
This patch returns a `nullptr` from `simplifyMul()` to signal that the
expression could not be simplified instead.

Fix llvm#75770
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
3 participants