You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2023/01/10 04:19:31 UTC

[GitHub] [tvm] wrongtest-intellif commented on a diff in pull request #13724: [Arith] Use ConstIntBound to remove negative numerator when lowering

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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org