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 3c9fc462 [Fix] Preserve unsigned comparison semantics in 
simplification (#13)
3c9fc462 is described below

commit 3c9fc462b5c818961bf29f2f0d0ca9e90f7bf009
Author: Tianqi Chen <[email protected]>
AuthorDate: Wed Oct 21 13:55:45 2026 -0400

    [Fix] Preserve unsigned comparison semantics in simplification (#13)
    
    Unsigned ordering cannot be reduced to subtraction because unsigned
    subtraction may underflow. Keep unsigned comparisons on the direct
    rewrite path instead of canonicalizing them through a difference.
    
    Fixes apache/tvm-tirx#9.
---
 src/sym/canonical_simplify.cc               | 4 ++++
 tests/python/sym/test_canonical_simplify.py | 5 +++++
 2 files changed, 9 insertions(+)

diff --git a/src/sym/canonical_simplify.cc b/src/sym/canonical_simplify.cc
index 20e9d45b..afbb1538 100644
--- a/src/sym/canonical_simplify.cc
+++ b/src/sym/canonical_simplify.cc
@@ -1390,6 +1390,10 @@ UnchangedOr<PrimExpr> 
CanonicalSimplifier::Impl::Mutate_(const prim::CastNode* o
 
 UnchangedOr<PrimExpr> CanonicalSimplifier::Impl::Mutate_(const prim::LTNode* 
op,
                                                          InplaceMode 
inplace_mode) {
+  // Unsigned subtraction does not preserve comparison ordering.
+  if (op->a->ty.as_or_throw<PrimType>().MatchesCode(DLDataTypeCode::kDLUInt)) {
+    return Rewriter::Mutate_(op, inplace_mode);
+  }
   // First convert a < b into a - b < 0
   PrimExpr difference = op->a - op->b;
   PrimExpr expr = DirectMutate(difference, 
inplace_mode).ValueOrUnchanged(std::move(difference));
diff --git a/tests/python/sym/test_canonical_simplify.py 
b/tests/python/sym/test_canonical_simplify.py
index f8a38295..5a9ac36f 100644
--- a/tests/python/sym/test_canonical_simplify.py
+++ b/tests/python/sym/test_canonical_simplify.py
@@ -411,6 +411,11 @@ def test_floormod_two():
     ck.verify(flm(x * 10 + 1 + y * 2 + 2, 2), 1)
 
 
+def test_simplify_unsigned_comparison():
+    ck = CanonicalChecker()
+    ck.verify(tirx.Cast("uint8", T.uint8(1)) > tirx.Cast("uint8", T.uint8(0)), 
T.bool(True))
+
+
 def test_simplify_le():
     ck = CanonicalChecker()
     # Case 1. Ignore the extra expr if it's small than the division number

Reply via email to