Skip to content

Commit 1624c84

Browse files
committed
Fix test expectation for affine.apply mod 1 (always returns 0)
1 parent 3c1596a commit 1624c84

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

mlir/lib/Dialect/Affine/Utils/Utils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ class AffineApplyExpander
7979
/// negative = a < 0 in
8080
/// select negative, remainder + b, remainder.
8181
///
82-
/// Special case for power of 2: use bitwise AND (x & (n-1)) for non-negative
82+
/// Special case for power-of-2 RHS: use bitwise AND (x & (n-1)) for non-negative
8383
/// x.
8484
Value visitModExpr(AffineBinaryOpExpr expr) {
8585
if (auto rhsConst = dyn_cast<AffineConstantExpr>(expr.getRHS())) {
86-
if (rhsConst.getValue() <= 0) {
86+
int64_t rhsValue = rhsConst.getValue();
87+
if (rhsValue <= 0) {
8788
emitError(loc, "modulo by non-positive value is not supported");
8889
return nullptr;
8990
}
9091

9192
// Special case: x mod n where n is a power of 2 can be optimized to x &
92-
// (n-1)
93-
int64_t rhsValue = rhsConst.getValue();
94-
if (rhsValue > 0 && (rhsValue & (rhsValue - 1)) == 0) {
93+
// (n-1).
94+
if ((rhsValue & (rhsValue - 1)) == 0) {
9595
auto lhs = visit(expr.getLHS());
9696
assert(lhs && "unexpected affine expr lowering failure");
9797

mlir/test/Conversion/AffineToStandard/lower-affine.mlir

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,18 @@ func.func @affine_parallel_with_reductions_i64(%arg0: memref<3x3xi64>, %arg1: me
928928
// CHECK: }
929929
// CHECK: }
930930

931-
#map_mod_8 = affine_map<(i) -> (i mod 8)>
932931
// CHECK-LABEL: func @affine_apply_mod_8
933932
func.func @affine_apply_mod_8(%arg0 : index) -> (index) {
934933
// CHECK-NEXT: %[[c7:.*]] = arith.constant 7 : index
935934
// CHECK-NEXT: %[[v0:.*]] = arith.andi %arg0, %[[c7]] : index
936-
%0 = affine.apply #map_mod_8 (%arg0)
935+
%0 = affine.apply affine_map<(i) -> (i mod 8)> (%arg0)
936+
return %0 : index
937+
}
938+
939+
// CHECK-LABEL: func @affine_apply_mod_1
940+
func.func @affine_apply_mod_1(%arg0 : index) -> (index) {
941+
// CHECK-NEXT: %[[c0:.*]] = arith.constant 0 : index
942+
// CHECK-NEXT: return %[[c0]] : index
943+
%0 = affine.apply affine_map<(i) -> (i mod 1)> (%arg0)
937944
return %0 : index
938945
}

0 commit comments

Comments
 (0)