-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Arith] Use ConstIntBound to remove negative numerator when lowering #13724
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
Lunderberg
merged 4 commits into
apache:main
from
Lunderberg:offset_negative_floormod_floordiv
Jan 10, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4b99ea0
[Arith] Use ConstIntBound to remove negative numerator when lowering
Lunderberg 2d0a7cf
Add check to avoid -INT32_MIN
Lunderberg a56f5c3
Updated to use `tvm::min_value(DataType)`
Lunderberg c392b3e
Added derivation for floordiv/floormod in terms of truncdiv/trundmod
Lunderberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| #include <tvm/tir/op.h> | ||
| #include <tvm/tir/transform.h> | ||
|
|
||
| #include <limits> | ||
| #include <unordered_set> | ||
|
|
||
| #include "../../arith/ir_mutator_with_analyzer.h" | ||
|
|
@@ -112,20 +113,63 @@ class IntrinInjecter : public tvm::arith::IRMutatorWithAnalyzer { | |
| // Common path, positive divisor | ||
| if (analyzer_->CanProveGreaterEqual(op->a, 0) || analyzer_->CanProveGreaterEqual(e, 0)) { | ||
| return truncdiv(op->a, op->b); | ||
| } | ||
|
|
||
| // If the numerator's lower bound is known, express the floordiv | ||
| // in terms of truncdiv using only positive operands. | ||
| arith::ConstIntBound const_int_bound = analyzer_->const_int_bound(op->a); | ||
| if (const_int_bound->min_value != arith::ConstIntBound::kNegInf && | ||
| const_int_bound->min_value < 0 && | ||
| const_int_bound->min_value > Downcast<IntImm>(tvm::min_value(op->a->dtype))->value) { | ||
| // The goal is to write floordiv(a,b) in terms of truncdiv, without using | ||
| // negative operands. | ||
| // | ||
| // For any integer c | ||
| // | ||
| // floordiv(a,b) == floordiv(a + b*c - b*c, b) | ||
| // == floordiv(a + b*c, b) - c | ||
| // | ||
| // Choosing `c = ceildiv(-a_min, b)`. This can be rewritten in terms of | ||
| // truncdiv as follows. | ||
| // | ||
| // c == ceildiv(-a_min,b) | ||
| // == floordiv(-a_min + (b-1), b) | ||
| // == truncdiv(-a_min + (b-1), b) | ||
| // | ||
| // When substituted into `a + b*c`, this results in a positive argument. | ||
| // | ||
| // a + b*c | ||
| // == a + b*ceildiv(-a_min,b) | ||
| // == a - b*floordiv(a_min,b) | ||
| // >= a - b*floordiv(a,b) | ||
| // == floormod(a, b) | ||
| // >= 0 | ||
| // | ||
| // Since the argument is positive, this allows floordiv to be written as | ||
| // followed. | ||
| // | ||
| // floordiv(a,b) | ||
| // == floordiv(a + b*c, b) - c | ||
| // == truncdiv(a + b*c, b) - c | ||
| IntImm min(op->a->dtype, const_int_bound->min_value); | ||
| PrimExpr ceildiv = truncdiv((op->b - 1) - min, op->b); | ||
| PrimExpr offset_numerator = analyzer_->Simplify(op->a + op->b * ceildiv); | ||
| return truncdiv(offset_numerator, op->b) - ceildiv; | ||
| } | ||
|
|
||
| DLOG(INFO) << "LowerFloorDiv: Cannot decide the sign of divident"; | ||
| PrimExpr rdiv = truncdiv(op->a, op->b); | ||
| PrimExpr rmod = truncmod(op->a, op->b); | ||
| // condition on b >= 0. | ||
| // truncmod(a, b) < 0 will implies ceildiv, | ||
| // So we need to correct these cases. | ||
| if ((dtype == DataType::Int(32) || dtype == DataType::Int(64)) && support_bitwise_op_) { | ||
| // equivalent to rdiv + (rmod >= 0 ? 0: -1); | ||
| return rdiv + (rmod >> make_const(dtype, dtype.bits() - 1)); | ||
| } else { | ||
| DLOG(INFO) << "LowerFloorDiv: Cannot decide the sign of divident"; | ||
| PrimExpr rdiv = truncdiv(op->a, op->b); | ||
| PrimExpr rmod = truncmod(op->a, op->b); | ||
| // condition on b >= 0. | ||
| // truncmod(a, b) < 0 will implies ceildiv, | ||
| // So we need to correct these cases. | ||
| if ((dtype == DataType::Int(32) || dtype == DataType::Int(64)) && support_bitwise_op_) { | ||
| // equivalent to rdiv + (rmod >= 0 ? 0: -1); | ||
| return rdiv + (rmod >> make_const(dtype, dtype.bits() - 1)); | ||
| } else { | ||
| return tir::Select(rmod >= 0, rdiv, rdiv - make_const(dtype, 1)); | ||
| } | ||
| return tir::Select(rmod >= 0, rdiv, rdiv - make_const(dtype, 1)); | ||
| } | ||
|
|
||
| } else { | ||
| if (dtype.is_float()) { | ||
| // floor(a / b) | ||
|
|
@@ -165,21 +209,63 @@ class IntrinInjecter : public tvm::arith::IRMutatorWithAnalyzer { | |
| // Common pass, positive divisor | ||
| if (analyzer_->CanProveGreaterEqual(op->a, 0)) { | ||
| return truncmod(op->a, op->b); | ||
| } | ||
|
|
||
| // If the numerator's lower bound is known, express the floormod | ||
| // in terms of truncmod using only positive operands. | ||
| arith::ConstIntBound const_int_bound = analyzer_->const_int_bound(op->a); | ||
| if (const_int_bound->min_value != arith::ConstIntBound::kNegInf && | ||
| const_int_bound->min_value < 0 && | ||
| const_int_bound->min_value > Downcast<IntImm>(tvm::min_value(op->a->dtype))->value) { | ||
| // The goal is to write floormod(a,b) in terms of truncdiv and truncmod, | ||
| // without using negative operands. | ||
| // | ||
| // For any integer c | ||
| // | ||
| // floormod(a, b) == floormod(a + b*c, b) | ||
| // | ||
| // Choosing `c = ceildiv(-a_min, b)`. This can be rewritten in terms of | ||
| // truncdiv as follows. | ||
| // | ||
| // c == ceildiv(-a_min,b) | ||
| // == floordiv(-a_min + (b-1), b) | ||
| // == truncdiv(-a_min + (b-1), b) | ||
| // | ||
| // When substituted into `a + b*c`, this results in a positive argument. | ||
| // | ||
| // a + b*c | ||
| // == a + b*ceildiv(-a_min,b) | ||
| // == a - b*floordiv(a_min,b) | ||
| // >= a - b*floordiv(a,b) | ||
| // == floormod(a, b) | ||
| // >= 0 | ||
| // | ||
| // Since the argument is positive, this allows floordiv to be written as | ||
| // followed. | ||
| // | ||
| // floormod(a,b) | ||
| // == floormod(a + b*c, b) | ||
| // == truncmod(a + b*c, b) | ||
| IntImm min(op->a->dtype, const_int_bound->min_value); | ||
| PrimExpr ceildiv = truncdiv(-min + (op->b - 1), op->b); | ||
| PrimExpr offset_numerator = analyzer_->Simplify(op->a + op->b * ceildiv); | ||
| return truncmod(offset_numerator, op->b); | ||
|
Contributor
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. Right?
Contributor
Author
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. Yup, that derivation is correct, and I've added a comment here as well. |
||
| } | ||
|
|
||
| DLOG(INFO) << "LowerFloorMod: Cannot decide the sign of divident"; | ||
| // NOTE:condition on b >= 0. | ||
| // mod(a, b) < 0 will imply we are doing ceildiv, | ||
| // So we need to correct these cases. | ||
| PrimExpr rmod = truncmod(op->a, op->b); | ||
| if ((dtype == DataType::Int(32) || dtype == DataType::Int(64)) && support_bitwise_op_) { | ||
| // (rmod >> shift) & b | ||
| // -> (rmod >= 0 ? 0: -1) & b | ||
| // -> rmod >= 0 ? 0 : b | ||
| return rmod + (op->b & (rmod >> make_const(dtype, dtype.bits() - 1))); | ||
| } else { | ||
| DLOG(INFO) << "LowerFloorMod: Cannot decide the sign of divident"; | ||
| // NOTE:condition on b >= 0. | ||
| // mod(a, b) < 0 will imply we are doing ceildiv, | ||
| // So we need to correct these cases. | ||
| PrimExpr rmod = truncmod(op->a, op->b); | ||
| if ((dtype == DataType::Int(32) || dtype == DataType::Int(64)) && support_bitwise_op_) { | ||
| // (rmod >> shift) & b | ||
| // -> (rmod >= 0 ? 0: -1) & b | ||
| // -> rmod >= 0 ? 0 : b | ||
| return rmod + (op->b & (rmod >> make_const(dtype, dtype.bits() - 1))); | ||
| } else { | ||
| return tir::Select(rmod >= 0, rmod, rmod + op->b); | ||
| } | ||
| return tir::Select(rmod >= 0, rmod, rmod + op->b); | ||
| } | ||
|
|
||
| } else { | ||
| if (dtype.is_float()) { | ||
| // a - floor(a / b) * b | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
IIUC, the rationale is:
Right?
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.
Yup, that is the rationale. I've added the derivation in a comment, which I probably should have done from the start.