-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Lower affine modulo by powers of two using bitwise AND #146311
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,12 +78,27 @@ class AffineApplyExpander | |
/// let remainder = srem a, b; | ||
/// negative = a < 0 in | ||
/// select negative, remainder + b, remainder. | ||
/// | ||
/// Special case for power of 2: use bitwise AND (x & (n-1)) for non-negative | ||
/// x. | ||
Value visitModExpr(AffineBinaryOpExpr expr) { | ||
if (auto rhsConst = dyn_cast<AffineConstantExpr>(expr.getRHS())) { | ||
if (rhsConst.getValue() <= 0) { | ||
emitError(loc, "modulo by non-positive value is not supported"); | ||
return nullptr; | ||
} | ||
|
||
// Special case: x mod n where n is a power of 2 can be optimized to x & | ||
// (n-1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Terminate all comments with a full stop. LLVM style. |
||
int64_t rhsValue = rhsConst.getValue(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this assignment to above L88 to avoid multiple calls to getValue(). |
||
if (rhsValue > 0 && (rhsValue & (rhsValue - 1)) == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But we've already returned for all rhs values <= 0 at L90. |
||
auto lhs = visit(expr.getLHS()); | ||
assert(lhs && "unexpected affine expr lowering failure"); | ||
|
||
Value maskCst = | ||
builder.create<arith::ConstantIndexOp>(loc, rhsValue - 1); | ||
return builder.create<arith::AndIOp>(loc, lhs, maskCst); | ||
} | ||
} | ||
|
||
auto lhs = visit(expr.getLHS()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -927,3 +927,12 @@ func.func @affine_parallel_with_reductions_i64(%arg0: memref<3x3xi64>, %arg1: me | |
// CHECK: scf.reduce.return %[[RES]] : i64 | ||
// CHECK: } | ||
// CHECK: } | ||
|
||
#map_mod_8 = affine_map<(i) -> (i mod 8)> | ||
// CHECK-LABEL: func @affine_apply_mod_8 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also check if using |
||
func.func @affine_apply_mod_8(%arg0 : index) -> (index) { | ||
// CHECK-NEXT: %[[c7:.*]] = arith.constant 7 : index | ||
// CHECK-NEXT: %[[v0:.*]] = arith.andi %arg0, %[[c7]] : index | ||
%0 = affine.apply #map_mod_8 (%arg0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can put the map inline for better readability. |
||
return %0 : index | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
power of 2 RHS