Skip to content

[BugFix] Align default (C) tirx.round lowering to ties-to-even - #20131

Merged
tlopex merged 2 commits into
apache:mainfrom
LngelKyo:fix/c-round-ties-to-even
Sep 1, 2026
Merged

[BugFix] Align default (C) tirx.round lowering to ties-to-even#20131
tlopex merged 2 commits into
apache:mainfrom
LngelKyo:fix/c-round-ties-to-even

Conversation

@LngelKyo

Copy link
Copy Markdown
Contributor

#19368 aligned tir.round to ties-to-even "across all backends" and moved LLVM, CUDA, NVPTX, ROCm, Hexagon, Metal, OpenCL, SPIR-V and WebGPU. It did not touch src/target/intrin_rule.cc, so the default.FLowerIntrinsic rule — which the c target uses — still lowers through FloatSuffix and emits round/roundf, i.e. ties away from zero.

This disagrees with:

Why CI did not catch it. test_target_codegen_c_host.py::test_round compiles with target="c" and asserts against np.round, which is ties-to-even — so the intended semantics was already encoded. It passes only because its input is np.random.rand, which does not produce exact midpoints (0 occurrences in 2,048,000 sampled values). The ties-to-even test added by #19368 (test_tir_intrin.py::test_round_ties_to_even) only runs target="llvm", so no test covered the C path's tie behaviour.

The first commit adds the midpoint vector — reused verbatim from test_round_ties_to_even — to the existing C-host test and it fails:

E   Mismatch at indices:
E    [0]: 1.0 (ACTUAL), 0.0 (DESIRED)
E    [2]: 3.0 (ACTUAL), 2.0 (DESIRED)
E    [4]: -1.0 (ACTUAL), -0.0 (DESIRED)
E    [6]: -3.0 (ACTUAL), -2.0 (DESIRED)

The second commit renames round→nearbyint before the float suffix, mirroring the existing CUDA rule, and it passes.

One note for reviewers: nearbyint honours the current floating-point environment and is ties-to-even under the default FE_TONEAREST. That is the same guarantee LLVM, CUDA, Metal and OpenCL already rely on after #19368; this PR does not introduce a new assumption.

np.random.rand never produces exact midpoints, so the existing input
cannot distinguish ties-to-even (np.round, constant folding, and every
other backend) from ties-away-from-zero (C round/roundf). Reuse the
midpoint vector from test_round_ties_to_even verbatim. This test fails
against current main.
tirx.round is ties-to-even, and constant folding implements it with
std::nearbyint, but the default.FLowerIntrinsic rule lowered it through
FloatSuffix to the C library round()/roundf(), which is ties away from
zero. Rename to nearbyint before the float suffix is applied, mirroring
the existing CUDA rule. Fixes the C target's disagreement with the
constant folder and with every other backend after apache#19368.
@LngelKyo

Copy link
Copy Markdown
Contributor Author

@swjng @tlopex — cc, since this completes the backend sweep from #19368.

CI has not been triggered on this PR (no check runs); I believe it needs approval as this is my first contribution here. Happy to rebase or adjust if anything is needed.

@yongwww yongwww closed this Aug 17, 2026
@yongwww yongwww reopened this Aug 17, 2026

@tlopex tlopex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks so much for the fix!

@tlopex
tlopex merged commit 2a2b293 into apache:main Sep 1, 2026
5 checks passed
LngelKyo added a commit to LngelKyo/tvm that referenced this pull request Sep 1, 2026
test_round_ties_to_even was pinned to target="llvm". The shape of the
bug apache#19368 left in the C host (fixed by apache#20131) was that a backend could
implement tirx.round without ties-to-even and no test would notice: the
ties behaviour was only exercised on llvm. The C host passed everything
for four months because of exactly this.

Parametrize the test over every backend that registers tirx.round
(llvm, c, cuda, nvptx, rocm, metal, opencl, vulkan, webgpu, hexagon).
The target-list convention (gpu-marked pytest.params for device
targets) follows test_clz in the same file; "c" is always runnable, so
it is the one target exempt from the device_enabled skip, as in
test_codegen_loop_step. The midpoint vector is the one apache#19368 added.
LngelKyo added a commit to LngelKyo/tvm that referenced this pull request Sep 1, 2026
apache#20131 left a 3-line wrapped call in test_round that ruff-format
(line-length = 100) rejoins into one line, so pre-commit run
--all-files fails on every open PR. Reformat the line; no other
change. Reproduce with: uvx ruff@0.12.3 format --check
tests/python/codegen/test_target_codegen_c_host.py
LngelKyo added a commit to LngelKyo/tvm that referenced this pull request Sep 1, 2026
test_round_ties_to_even was pinned to target="llvm". The shape of the
bug apache#19368 left in the C host (fixed by apache#20131) was that a backend could
implement tirx.round without ties-to-even and no test would notice: the
ties behaviour was only exercised on llvm. The C host passed everything
for four months because of exactly this.

Parametrize the test over the two host targets the regression can recur
on ("c" and "llvm"), following the test_codegen_loop_step convention;
device backends have their own lowering rules and belong in their own
codegen tests. The midpoint vector is the one apache#19368 added.
LngelKyo added a commit to LngelKyo/tvm that referenced this pull request Sep 1, 2026
apache#20131 left a 3-line wrapped call in test_round that ruff-format
(line-length = 100) rejoins into one line, so pre-commit run
--all-files fails on every open PR. Reformat the line; no other
change. Reproduce with: uvx ruff@0.12.3 format --check
tests/python/codegen/test_target_codegen_c_host.py
LngelKyo added a commit to LngelKyo/tvm that referenced this pull request Sep 1, 2026
test_round_ties_to_even was pinned to target="llvm". The shape of the
bug apache#19368 left in the C host (fixed by apache#20131) was that a backend could
implement tirx.round without ties-to-even and no test would notice: the
ties behaviour was only exercised on llvm. The C host passed everything
for four months because of exactly this.

Parametrize the test over the two host targets the regression can recur
on ("c" and "llvm"), following the test_codegen_loop_step convention;
device backends have their own lowering rules and belong in their own
codegen tests. The midpoint vector is the one apache#19368 added.
LngelKyo added a commit to LngelKyo/tvm that referenced this pull request Sep 1, 2026
apache#20131 left a 3-line wrapped call in test_round that ruff-format
(line-length = 100) rejoins into one line, so pre-commit run
--all-files fails on every open PR. Reformat the line; no other
change. Reproduce with: uvx ruff@0.12.3 format --check
tests/python/codegen/test_target_codegen_c_host.py
LngelKyo added a commit to LngelKyo/tvm that referenced this pull request Sep 1, 2026
test_round_ties_to_even was pinned to target="llvm". The shape of the
bug apache#19368 left in the C host (fixed by apache#20131) was that a backend could
implement tirx.round without ties-to-even and no test would notice: the
ties behaviour was only exercised on llvm. The C host passed everything
for four months because of exactly this.

Parametrize the test over the two host targets the regression can recur
on ("c" and "llvm"), following the test_codegen_loop_step convention;
device backends have their own lowering rules and belong in their own
codegen tests. The midpoint vector is the one apache#19368 added.
tlopex pushed a commit that referenced this pull request Sep 1, 2026
#20252)

#19368 aligned `tir.round` to ties-to-even across LLVM, CUDA, NVPTX,
ROCm, Hexagon, Metal, OpenCL, SPIR-V and WebGPU, and #20131 fixed the C
host — but the shape of that bug can recur: `test_round_ties_to_even`
(tests/python/tirx-base/test_tir_intrin.py) is pinned to
`target="llvm"`, so the C host's ties behaviour is not exercised
anywhere. The C host passed every test for four months for exactly this
reason.

**What this PR does**

Parametrize `test_round_ties_to_even` over the two host targets the
regression can recur on — `c` and `llvm` — following the
`test_codegen_loop_step` convention. Device backends have their own
lowering rules and belong in their own codegen tests. The midpoint
vector is the one #19368 added, reused verbatim.

**Verification.** On a local build: reverting the C rule to its
pre-#20131 ties-away-from-zero form makes `test_round_ties_to_even[c]`
fail with the expected midpoint mismatches (`0.5 → 1.0 vs 0.0`, `2.5 →
3.0 vs 2.0`, `−0.5 → −1.0 vs −0.0`), while `[llvm]` still passes — i.e.
the test now catches the class of bug that #20131 fixed. With the rule
restored, both targets pass. Formatting checked with ruff format/ruff
check — both the CI-pinned ruff 0.12.3 and 0.15.14 pass clean on the
touched file.

**Second commit** is docs-only: it pins the floating-point-environment
assumption from the #20131 discussion into `src/target/intrin_rule.cc`.
nearbyint is ties-to-even under the default `FE_TONEAREST`, and the same
holds for the LLVM rule, which lowers to `llvm.nearbyint` —
rounding-mode sensitive, like nearbyint itself (`llvm.roundeven` is the
mode-independent one). Every other backend that registers `tirx.round` —
cuda, nvptx, rocm, hexagon, metal, opencl, vulkan, webgpu — emits code
for a separate device whose rounding mode is fixed at RNE, so
`fesetround()` in the host process cannot reach it.

**Third commit** is a one-line CI restore: #20131 left a 3-line wrapped
call in `test_target_codegen_c_host.py` that ruff-format rejoins under
`line-length = 100`, so `pre-commit run --all-files` fails on every open
PR. This PR carries the rejoin so that merging it leaves main
lint-green. Reproduce: `uvx ruff@0.12.3 format --check
tests/python/codegen/test_target_codegen_c_host.py`.

---------

Co-authored-by: LngelKyo <lngel.kyo@a2o-labs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants