Skip to content

Commit 60b44d3

Browse files
[MLIR][Affine] Fix bug in simplifySemiAffine utility (#129200)
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
1 parent 9f37cdc commit 60b44d3

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

mlir/lib/IR/AffineExpr.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,11 @@ static AffineExpr simplifySemiAffine(AffineExpr expr, unsigned numDims,
597597
return getAffineBinaryOpExpr(expr.getKind(), sLHS, sRHS);
598598
if (expr.getKind() == AffineExprKind::Mod)
599599
return getAffineConstantExpr(0, expr.getContext());
600-
return symbolicDivide(sLHS, symbolPos, expr.getKind());
600+
AffineExpr simplifiedQuotient =
601+
symbolicDivide(sLHS, symbolPos, expr.getKind());
602+
return simplifiedQuotient
603+
? simplifiedQuotient
604+
: getAffineBinaryOpExpr(expr.getKind(), sLHS, sRHS);
601605
}
602606
}
603607
llvm_unreachable("Unknown AffineExpr");

mlir/test/Dialect/Affine/simplify-structures.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ func.func @simplify_zero_dim_map(%in : memref<f32>) -> f32 {
282282
// Tests the simplification of a semi-affine expression in various cases.
283283
// CHECK-DAG: #[[$map0:.*]] = affine_map<()[s0, s1] -> (-(s1 floordiv s0) + 2)>
284284
// CHECK-DAG: #[[$map1:.*]] = affine_map<()[s0, s1] -> (-(s1 floordiv s0) + 42)>
285+
// CHECK-DAG: #[[$FLOORDIV:.*]] = affine_map<()[s0] -> (1 floordiv s0)>
285286

286287
// Tests the simplification of a semi-affine expression with a modulo operation on a floordiv and multiplication.
287288
// CHECK-LABEL: func @semiaffine_mod
@@ -299,6 +300,16 @@ func.func @semiaffine_floordiv(%arg0: index, %arg1: index) -> index {
299300
return %a : index
300301
}
301302

303+
// The following semi-affine expression with nested floordiv cannot be simplified.
304+
// CHECK-LABEL: func @semiaffine_nested_floordiv
305+
// CHECK-SAME: %[[ARG0:.*]]: index
306+
func.func @semiaffine_nested_floordiv(%arg0: index) -> index {
307+
%a = affine.apply affine_map<()[s0] ->((s0 floordiv s0) floordiv s0)> ()[%arg0]
308+
return %a : index
309+
// CHECK: %[[RES:.*]] = affine.apply #[[$FLOORDIV]]()[%[[ARG0]]]
310+
// CHECK-NEXT: return %[[RES]] : index
311+
}
312+
302313
// Tests the simplification of a semi-affine expression with a ceildiv operation and a division of arith.constant 0 by a symbol.
303314
// CHECK-LABEL: func @semiaffine_ceildiv
304315
func.func @semiaffine_ceildiv(%arg0: index, %arg1: index) -> index {

0 commit comments

Comments
 (0)