Skip to content

codegen: preserve i32 range bounds when narrowing loop counters - #4411

Merged
hiraditya merged 3 commits into
vx-lang:mainfrom
minzyfox:3269/i32-range-checks
Dec 2, 2026
Merged

hiraditya merged 3 commits into
vx-lang:mainfrom
minzyfox:3269/i32-range-checks

Conversation

@minzyfox

Copy link
Copy Markdown
Contributor

Mark signed i32 loop-counter narrowing as exact in both code generators. This lets LLVM remove redundant checks and vectorize the loop.

Fold exact counter conversions before raising affine accesses, preserving loop fusion. Guard against unflagged truncations and earlier casts that lose bits.

Add tests for check removal, vectorization, conversion guards, and signed and unsigned edge values.

Validation: 1,046 library tests, focused regressions, and flat-backend coverage passed. Four GPU optimization failures also reproduce on main.

Fixes: #3269

Mark signed i32 loop-counter narrowing as exact in both code generators.
This lets LLVM remove redundant checks and vectorize the loop.

Fold exact counter conversions before raising affine accesses, preserving
loop fusion. Guard against unflagged truncations and earlier casts that
lose bits.

Add tests for check removal, vectorization, conversion guards, and signed
and unsigned edge values.

Validation: 1,046 library tests, focused regressions, and flat-backend
coverage passed. Four GPU optimization failures also reproduce on main.

Fixes: vx-lang#3269

@hiraditya hiraditya left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. The change is correct, but the tests in the description are not in the PR. The diff changes only one CHECK line and one unit test. The description names tests for check removal, vectorization, conversion guards, and signed and unsigned edge values. Maybe the new files were not added with git add. Please push them.

What I checked, with the PR merged into current main:

  • The example of #3269, at -O3 with opt -mcpu=native on x86_64. The for loop now has no abort call, and LLVM vectorizes it like the while loop. This is true with both code generators.
  • I removed only overflow<nsw> from the trunc in the output. Then the abort call came back, and LLVM did not vectorize the loop. So the flag makes the change.
  • Loops at the edges of i32 ((i32::MAX - 3)..i32::MAX and i32::MIN..(i32::MIN + 3)) and a u32 loop above 2^31 give the correct sums at -O0 and -O3, with both code generators.
  • counted_loops_become_affine_loops.vx and emits_verifiable_affine_for_loop pass.

A test for #3269 could compile its example at -O3 and check that the for loop has no call of abort. With overflow<nsw> removed, that check fails.

Generated With Claude Code

@minzyfox

minzyfox commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Yikes. Didn't notice that some files weren't added. Will update.

Add the missing regression fixtures for redundant check removal,
vectorization, and safe counter conversion folding. Check signed and
unsigned edge values at -O0 and -O3 with both code generators.

Validation: all 13 fixture commands, targeted mutation checks, and
flat-backend coverage passed.

Issue: vx-lang#3269
@minzyfox

minzyfox commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Added the missing files

@hiraditya hiraditya left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. The new tests are in, and they cover the change. I merged the branch into current main (70cc938).

  • i32_range_checks_are_removed.vx, exact_counter_casts_recover_the_index.mlr and counted_loops_become_affine_loops.vx pass.
  • I removed overflow<nsw> from the flat code generator. Then CAST, OPT and VECTOR fail: the abort call comes back, and LLVM does not vectorize the loop.
  • I removed it from the legacy code generator. Then LEGACY fails.

Nit: with the legacy code generator, OPT and VECTOR pass on main too. The legacy output of main has no abort in sum_counts, and LLVM vectorizes it. So only the LEGACY check tests the legacy part of this change. Please say so in the test header, or remove the two legacy opt RUN lines.

CI passed.

Generated With Claude Code

Explain that only the LEGACY check tests the legacy code generator change.
The legacy OPT and VECTOR checks also pass without the change.

Issue: vx-lang#3269
@hiraditya
hiraditya merged commit 08150f1 into vx-lang:main Dec 2, 2026
7 checks passed
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.

A for loop over an i32 range keeps checks that a while loop removes, and is not vectorized

2 participants