This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-tirx.git


The following commit(s) were added to refs/heads/main by this push:
     new fef2d09e1 [FIX] Preserve division semantics in canonical comparisons 
(#141)
fef2d09e1 is described below

commit fef2d09e14c0274dabe6de7ab1efd56204f1dc49
Author: Tianqi Chen <[email protected]>
AuthorDate: Sat Oct 31 17:54:39 2026 -0400

    [FIX] Preserve division semantics in canonical comparisons (#141)
    
    Canonical comparison simplification reconstructs split remainders with
    floor operations. Require positive split factors and floor-compatible
    division, or a proven nonnegative index, so truncating remainders keep
    their semantics when the index may be negative.
    
    Fixes https://github.com/apache/tvm-tirx/issues/137.
---
 src/sym/canonical_simplify.cc               | 11 ++++++++---
 tests/python/sym/test_canonical_simplify.py |  9 +++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/sym/canonical_simplify.cc b/src/sym/canonical_simplify.cc
index d7ca8ecfe..4bb452f36 100644
--- a/src/sym/canonical_simplify.cc
+++ b/src/sym/canonical_simplify.cc
@@ -1437,9 +1437,14 @@ UnchangedOr<PrimExpr> 
CanonicalSimplifier::Impl::Mutate_(const prim::LTObj* op,
       PrimExpr normalized = divisible->Normalize() < IntImm(dtype, 0);
       return DirectMutate(normalized, 
inplace_mode).ValueOrUnchanged(std::move(normalized));
     } else if (extra->args.size() == 1 && extra->args[0]->scale == 1 &&
-               extra->args[0]->upper_factor.has_value() &&
-               *extra->args[0]->upper_factor % (gcd * 
extra->args[0]->lower_factor) == 0) {
-      // Case 2. xn == ((yn % m) // L), scale = +1, m % (d*L) == 0.
+               extra->args[0]->upper_factor.has_value() && 
*extra->args[0]->upper_factor > 0 &&
+               extra->args[0]->lower_factor > 0 &&
+               *extra->args[0]->upper_factor % (gcd * 
extra->args[0]->lower_factor) == 0 &&
+               (extra->args[0]->DivModeCompatibleTo(kFloorDiv) ||
+                analyzer_->CanProveGreaterEqual(extra->args[0]->index, 0))) {
+      // Case 2. xn == ((yn % m) // L), scale = +1, m, L > 0, m % (d*L) == 0.
+      // The reconstruction uses floor operations. For truncation, require
+      // a nonnegative index with positive split factors.
       // S + xn < 0 with S divisible by d  ⇔  S/d + xn // d < 0, because
       // xn % d ∈ [0, d) lets us drop the remainder via the Case 1 argument,
       // and xn // d = (yn // (d*L)) % (m/(d*L)).
diff --git a/tests/python/sym/test_canonical_simplify.py 
b/tests/python/sym/test_canonical_simplify.py
index fc18e5af0..53064d80d 100644
--- a/tests/python/sym/test_canonical_simplify.py
+++ b/tests/python/sym/test_canonical_simplify.py
@@ -557,5 +557,14 @@ def test_simplify_le_negative_scale_extra():
     ck.verify(expr_true, tvm_tirx.tirx.const(True, "bool"))
 
 
+def test_simplify_lt_truncmod():
+    ck = CanonicalChecker()
+    x, z = tirx.Var("x", "int32"), tirx.Var("z", "int32")
+    # With z=-2,x=0 this is true; reconstructing with floor operations makes
+    # it false. The index has no nonnegative bound to make the modes agree.
+    expr = tirx.truncmod(z, 12) < x * 3
+    ck.verify(expr, expr)
+
+
 if __name__ == "__main__":
     tvm_tirx.testing.main()

Reply via email to