This is an automated email from the ASF dual-hosted git repository.
mshr-h pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/main by this push:
new bfebeeed0a [FIX][S-TIR] Preserve loop variable types when eliminating
unit-extent loops (#20999)
bfebeeed0a is described below
commit bfebeeed0af6d4f6cc8c97c281fa189d120daa82
Author: Xu <[email protected]>
AuthorDate: Thu Nov 19 15:52:05 2026 +0800
[FIX][S-TIR] Preserve loop variable types when eliminating unit-extent
loops (#20999)
LoopPartition replaces unit-extent loop variables with an int32 zero,
regardless of the original variable type. For int64 loops, this can
produce addition expressions with mismatched int32/int64 operands.
Construct the replacement zero using the loop variable's type.
Add int32/int64 regression tests that check unit loops are eliminated
without introducing mismatched addition operands. The int64 case fails
before the fix and passes afterward.
The immediate substitution error reported in #20879 was not reproduced
on this checkout; instead, the pass produced expressions with mismatched
operand types. The regression test checks this type inconsistency.
Fixes #20879.
---
src/s_tir/transform/loop_partition.cc | 2 +-
.../test_s_tir_transform_loop_partition.py | 26 ++++++++++++++++++++++
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/src/s_tir/transform/loop_partition.cc
b/src/s_tir/transform/loop_partition.cc
index b3b7c30f97..0edc13e205 100644
--- a/src/s_tir/transform/loop_partition.cc
+++ b/src/s_tir/transform/loop_partition.cc
@@ -936,7 +936,7 @@ inline Stmt LoopPartitioner::MakeFor(const ffi::Object*
node, PrimExpr extent, S
// If the loop extent is 1, do not create the loop anymore
auto f_substitute =
[loop_var = for_node->var](const Var& var) ->
ffi::Expected<ffi::UnchangedOr<ffi::Any>> {
- if (var.same_as(loop_var)) return ffi::Any(IntImm::Int32(0));
+ if (var.same_as(loop_var)) return
ffi::Any(IntImm(var->ty.as_or_throw<PrimType>(), 0));
return ffi::Unchanged();
};
return ffi::StructuralMap<ffi::WalkOrder::kPreOrder>(body,
f_substitute).as_or_throw<Stmt>();
diff --git
a/tests/python/s_tir/transform/test_s_tir_transform_loop_partition.py
b/tests/python/s_tir/transform/test_s_tir_transform_loop_partition.py
index bb680f8f5b..6b589a629f 100644
--- a/tests/python/s_tir/transform/test_s_tir_transform_loop_partition.py
+++ b/tests/python/s_tir/transform/test_s_tir_transform_loop_partition.py
@@ -23,6 +23,7 @@ import tvm
import tvm_tirx
import tvm_tirx.testing
from tvm.s_tir import script as Ts
+from tvm_tirx.ir import prim
from tvm_tirx.ir.module import IRModule
from tvm_tirx.script import tirx as T
@@ -103,6 +104,31 @@ def test_condition_EQ():
assert not any(collect_visit(stmt[0], lambda x: isinstance(x,
tvm_tirx.tirx.Select)))
[email protected]("index_dtype", ["int32", "int64"])
+def test_single_point_partition_preserves_index_type(index_dtype):
+ @Ts.function
+ def before(A: T.Tensor((2,), "float32"), B: T.Tensor((2,), "float32")):
+ for i in T.serial(T.Cast(index_dtype, 2)):
+ if T.likely(i < T.Cast(index_dtype, 1)):
+ B[i] = A[i]
+ else:
+ B[i] = A[i]
+
+ mod = IRModule({"main": before})
+ with tvm_tirx.ir.transform.PassContext(
+ config={"s_tir.LoopPartition": {"partition_const_loop": True}}
+ ):
+ mod = tvm.s_tir.transform.LoopPartition()(mod)
+
+ body = mod["main"].body
+ assert not any(collect_visit(body, lambda node: isinstance(node,
tvm_tirx.ir.For)))
+
+ def check_add(expr):
+ assert expr.a.ty == expr.b.ty, f"Mismatched addition types:
{expr.a.ty} and {expr.b.ty}"
+
+ tvm_ffi.structural_walk(body, (prim.Add, check_add))
+
+
def test_everything_during_deduction():
@Ts.function
def func(m: T.int64, n: T.int64):