This is an automated email from the ASF dual-hosted git repository.
spectrometerHBH 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 ed9fa19aa [FIX][Sym] Use integer arithmetic for constant signed right
shifts in Z3 (#261)
ed9fa19aa is described below
commit ed9fa19aa5126c69a92d580c6162fda3a73d92cd
Author: Lei Wang <[email protected]>
AuthorDate: Wed Nov 25 11:18:31 2026 +0800
[FIX][Sym] Use integer arithmetic for constant signed right shifts in Z3
(#261)
## Problem
`Z3Prover` models scalar index arithmetic over unbounded Z3 integers,
but `RShiftObj` previously always converted operands through bit-vectors
(`int2bv` -> `bvashr` -> `bv2int`). When a constant signed right shift
such as `(n + 4095) >> 12` appears in a range constraint, even
pure-integer proof queries in the same scope inherit mixed `Int` +
`BitVec` assertions and frequently exhaust the default deterministic
`rlimit` budget (even `(x >> 1) * 2 <= x` in
`test_z3_shift_right_halves` previously required `set_z3_rlimit(0)`).
## Approach
For a signed scalar `RShiftObj` shifted right by a constant `k` with `0
<= k < w <= 64` (where `w = max(bits(a), bits(b))`), encode the shift
directly in Z3 integer arithmetic:
1. Wrap the unbounded integer operand to its signed `w`-bit
two's-complement value: `s = ((a + 2^(w-1)) mod 2^w) - 2^(w-1)`
(preserving overflow wraparound on intermediate sums such as `INT_MAX +
1`).
2. Divide by `2^k` using Z3's Euclidean integer division (`s / 2^k`),
which rounds toward negative infinity for positive divisors and matches
arithmetic right shift.
`test_z3_shift_right_halves` now passes under the default `rlimit`, and
`test_z3_constant_signed_right_shift` verifies SMT-LIB2 output
(`bvashr`-free) and exact wraparound/sign behavior across `int8`,
`int32`, and `int64` (`shift = 63`). All 1,050 tests in
`tests/python/sym/` pass.
---
src/sym/z3_prover.cc | 21 ++++++++++++++++++++-
tests/python/sym/test_z3.py | 30 ++++++++++++++++++++++++++++--
2 files changed, 48 insertions(+), 3 deletions(-)
diff --git a/src/sym/z3_prover.cc b/src/sym/z3_prover.cc
index 623447171..27732d846 100644
--- a/src/sym/z3_prover.cc
+++ b/src/sym/z3_prover.cc
@@ -38,6 +38,7 @@
#include <algorithm>
#include <climits>
+#include <cstdint>
#include <map>
#include <memory>
#include <optional>
@@ -901,7 +902,25 @@ class Z3Prover::Impl final :
tvm::ExprFunctor<z3::expr(const Expr&)> {
return VisitShiftOp(z3::shl, op, op->a, op->b);
}
z3::expr Dispatch_(const prim::RShiftObj* op) override {
- return VisitShiftOp(z3::ashr, op, op->a, op->b);
+ const PrimExpr& a = op->a;
+ const PrimExpr& b = op->b;
+ if (IsZ3SupportedExpr(a.get()) && IsZ3SupportedExpr(b.get())) {
+ unsigned bit_width = std::max(a.ty().bits(), b.ty().bits());
+ std::optional<int64_t> shift_amount;
+ if (auto b_imm = b.as<IntImm>(); b_imm.has_value()) {
+ shift_amount = (*b_imm)->value.as<int64_t>();
+ }
+ if (a.ty().MatchesCode(DLDataTypeCode::kDLInt) &&
shift_amount.has_value() &&
+ 0 <= *shift_amount && *shift_amount < bit_width && bit_width <= 64) {
+ // Both powers of two fit in uint64_t. Wrap the unbounded input to its
+ // signed value before dividing to model an arithmetic right shift.
+ z3::expr a_expr = VisitInt(a);
+ z3::expr half_range = ctx->int_val(uint64_t{1} << (bit_width - 1));
+ z3::expr signed_value = z3::mod(a_expr + half_range, 2 * half_range) -
half_range;
+ return signed_value / ctx->int_val(uint64_t{1} << *shift_amount);
+ }
+ }
+ return VisitShiftOp(z3::ashr, op, a, b);
}
z3::expr Dispatch_(const CallObj* op) override {
diff --git a/tests/python/sym/test_z3.py b/tests/python/sym/test_z3.py
index 42c2e0424..eddd7bb6f 100644
--- a/tests/python/sym/test_z3.py
+++ b/tests/python/sym/test_z3.py
@@ -648,11 +648,37 @@ def test_z3_shift_right_halves():
x = tirx.Var("x", "int32")
analyzer_context.append(tvm_tirx.sym.RangeConstraint(x,
tvm_tirx.ir.Range(0, 256)))
analyzer.sync_constraint_context(analyzer_context)
- analyzer.set_z3_rlimit(0)
- # For non-negative x, (x >> 1) * 2 <= x.
+ # Constant signed right shifts use integer arithmetic and stay within the
default rlimit.
assert analyzer.can_prove(tirx.shift_right(x, tvm_tirx.ir.IntImm("int32",
1)) * 2 <= x, SB)
[email protected]("bits, shift", [(8, 1), (32, 12), (64, 63)])
+def test_z3_constant_signed_right_shift(bits, shift):
+ z3 = pytest.importorskip("z3")
+ analyzer = Analyzer()
+ _require_z3(analyzer)
+
+ dtype = f"int{bits}"
+ x = tirx.Var("x", dtype)
+ result = tirx.Var("result", dtype)
+ shifted = tirx.shift_right(
+ x + tvm_tirx.ir.IntImm(dtype, 1),
+ tvm_tirx.ir.IntImm(dtype, shift),
+ )
+ # The dump asserts the negation of its argument, so it defines result ==
shifted.
+ smt = analyzer.get_smtlib2(result != shifted)
+ assert "bvashr" not in smt
+ solver = z3.Solver()
+ solver.from_string(smt)
+
+ highest = (1 << (bits - 1)) - 1
+ lowest = -(1 << (bits - 1))
+ for value, expected in [(highest, lowest >> shift), (-2, -1), (0, 1 >>
shift)]:
+ at_value = z3.Int("x") == value
+ assert solver.check(at_value, z3.Int("result") == expected) == z3.sat
+ assert solver.check(at_value, z3.Int("result") != expected) == z3.unsat
+
+
def test_z3_shift_left_lower_bound():
analyzer = Analyzer()
analyzer_context = tvm_tirx.sym.ConstraintContext()