Skip to content

[MLIR][Affine] Fix bug in simplifySemiAffine utility #129200

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

Conversation

arnab-polymage
Copy link
Contributor

@arnab-polymage arnab-polymage commented Feb 28, 2025

Whenever symbolicDivide returns nullptr when called from inside simplifySemiAffine we substitute the result with the original expression (expr). nullptr simply indicates that the floordiv expression cannot be simplified further.

Fixes: #122231

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir:affine mlir labels Feb 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 28, 2025

@llvm/pr-subscribers-mlir
@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir-affine

Author: Arnab Dutta (arnab-polymage)

Changes

Whenever symbolicDivide returns nullptr when called from inside simplifySemiAffine we substitute the result with the original expression (expr). nullptr simply indicates that the floordiv expression cannot be simplified further.


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

2 Files Affected:

  • (modified) mlir/lib/IR/AffineExpr.cpp (+2-1)
  • (modified) mlir/test/Dialect/Affine/simplify-structures.mlir (+11)
diff --git a/mlir/lib/IR/AffineExpr.cpp b/mlir/lib/IR/AffineExpr.cpp
index 59df0cd6833db..0077a869c014e 100644
--- a/mlir/lib/IR/AffineExpr.cpp
+++ b/mlir/lib/IR/AffineExpr.cpp
@@ -597,7 +597,8 @@ static AffineExpr simplifySemiAffine(AffineExpr expr, unsigned numDims,
       return getAffineBinaryOpExpr(expr.getKind(), sLHS, sRHS);
     if (expr.getKind() == AffineExprKind::Mod)
       return getAffineConstantExpr(0, expr.getContext());
-    return symbolicDivide(sLHS, symbolPos, expr.getKind());
+    AffineExpr simplifiedQuotient = symbolicDivide(sLHS, symbolPos, expr.getKind());
+    return simplifiedQuotient ? simplifiedQuotient : expr;
   }
   }
   llvm_unreachable("Unknown AffineExpr");
diff --git a/mlir/test/Dialect/Affine/simplify-structures.mlir b/mlir/test/Dialect/Affine/simplify-structures.mlir
index d1f34f20fa5da..723bd366e6ed4 100644
--- a/mlir/test/Dialect/Affine/simplify-structures.mlir
+++ b/mlir/test/Dialect/Affine/simplify-structures.mlir
@@ -282,6 +282,7 @@ func.func @simplify_zero_dim_map(%in : memref<f32>) -> f32 {
 // Tests the simplification of a semi-affine expression in various cases.
 // CHECK-DAG: #[[$map0:.*]] = affine_map<()[s0, s1] -> (-(s1 floordiv s0) + 2)>
 // CHECK-DAG: #[[$map1:.*]] = affine_map<()[s0, s1] -> (-(s1 floordiv s0) + 42)>
+// CHECK-DAG: #[[$NESTED_FLOORDIV:.*]] = affine_map<()[s0] -> ((s0 floordiv s0) floordiv s0)>
 
 // Tests the simplification of a semi-affine expression with a modulo operation on a floordiv and multiplication.
 // CHECK-LABEL: func @semiaffine_mod
@@ -299,6 +300,16 @@ func.func @semiaffine_floordiv(%arg0: index, %arg1: index) -> index {
   return %a : index
 }
 
+// The following semi-affine expression with nested floordiv cannot be simplified.
+// CHECK-LABEL: func @semiaffine_floordiv_no_simplification
+// CHECK-SAME:  %[[ARG0:.*]]: index
+func.func @semiaffine_floordiv_no_simplification(%arg0: index) -> index {
+  %a = affine.apply affine_map<()[s0] ->((s0 floordiv s0) floordiv s0)> ()[%arg0]
+  return %a : index
+  // CHECK:       %[[RES:.*]] = affine.apply #[[$NESTED_FLOORDIV]]()[%[[ARG0]]]
+  // CHECK-NEXT:  return %[[RES]] : index
+}
+
 // Tests the simplification of a semi-affine expression with a ceildiv operation and a division of arith.constant 0 by a symbol.
 // CHECK-LABEL: func @semiaffine_ceildiv
 func.func @semiaffine_ceildiv(%arg0: index, %arg1: index) -> index {

@arnab-polymage arnab-polymage changed the title Fix bug in simplifySemiAffine utility [MLIR][Affine] Fix bug in simplifySemiAffine utility Feb 28, 2025
@arnab-polymage arnab-polymage force-pushed the ornib/semi_affine_simplification_bug branch from 4ddb183 to 778064a Compare February 28, 2025 06:49
Copy link

github-actions bot commented Feb 28, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@arnab-polymage arnab-polymage force-pushed the ornib/semi_affine_simplification_bug branch 3 times, most recently from 64f4680 to 83c034d Compare February 28, 2025 08:26
Whenever `symbolicDivide` returns nullptr when called from inside
`simplifySemiAffine` we substitute the result with the original
expression with simplified lhs and rhs. nullptr simply indicates
that the floordiv expression cannot be simplified further.
@arnab-polymage arnab-polymage force-pushed the ornib/semi_affine_simplification_bug branch from 83c034d to 686ba5f Compare February 28, 2025 08:28
@bondhugula bondhugula merged commit 60b44d3 into llvm:main Mar 1, 2025
11 checks passed
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
Whenever `symbolicDivide` returns nullptr when called from inside
`simplifySemiAffine` we substitute the result with the original
expression (`expr`). nullptr simply indicates that the floordiv
expression cannot be simplified further.

Fixes: llvm#122231
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:affine mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Mlir] --canonicalize crashes in Casting.h:656
3 participants