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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion mlir/lib/IR/AffineExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,11 @@ 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
: getAffineBinaryOpExpr(expr.getKind(), sLHS, sRHS);
}
}
llvm_unreachable("Unknown AffineExpr");
Expand Down
11 changes: 11 additions & 0 deletions mlir/test/Dialect/Affine/simplify-structures.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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: #[[$FLOORDIV:.*]] = affine_map<()[s0] -> (1 floordiv s0)>

// Tests the simplification of a semi-affine expression with a modulo operation on a floordiv and multiplication.
// CHECK-LABEL: func @semiaffine_mod
Expand All @@ -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_nested_floordiv
// CHECK-SAME: %[[ARG0:.*]]: index
func.func @semiaffine_nested_floordiv(%arg0: index) -> index {
%a = affine.apply affine_map<()[s0] ->((s0 floordiv s0) floordiv s0)> ()[%arg0]
return %a : index
// CHECK: %[[RES:.*]] = affine.apply #[[$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 {
Expand Down