wrongtest-intellif commented on code in PR #13724:
URL: https://github.com/apache/tvm/pull/13724#discussion_r1065295308
##########
src/tir/transforms/lower_intrin.cc:
##########
@@ -112,20 +113,33 @@ 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 > -(1LL << (op->a->dtype.bits() - 1))) {
Review Comment:
The meaning of bound `-(1LL << (op->a->dtype.bits() - 1)))` is sign free or
not? Could we use `min_value` interface of op.h here?
##########
src/tir/transforms/lower_intrin.cc:
##########
@@ -112,20 +113,33 @@ 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 > -(1LL << (op->a->dtype.bits() - 1))) {
+ 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;
Review Comment:
IIUC, the rationale is:
```
a // b =>
(a + -min - -min) // b =>
(a + (x*b-y) - (x*b-y)) // b => ( where x is ceildiv(-min, b), x >= 0, y >=
0 )
(a + x*b) // b - x =>
(a + x*b) / b - x ( since a + x*b >= a + -min >= 0 )
```
Right?
##########
src/tir/transforms/lower_intrin.cc:
##########
@@ -165,21 +179,34 @@ 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 > -(1LL << (op->a->dtype.bits() - 1))) {
+ 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);
Review Comment:
```
floormod(a, b) =>
floormod(a + -min - -min, b) =>
floormod(a + (x*b-y) - (x*b-y)), b) => ( where x is ceildiv(-min, b), x >=
0, y >= 0 )
floormod(a + x*b, b) =>
(a + x*b) % b ( since a + x*b >= a + -min >= 0 )
```
Right?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]