Repository navigation
codegen: preserve i32 range bounds when narrowing loop counters - #4411
Conversation
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
left a comment
There was a problem hiding this comment.
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
-O3withopt -mcpu=nativeon x86_64. Theforloop now has noabortcall, and LLVM vectorizes it like thewhileloop. This is true with both code generators. - I removed only
overflow<nsw>from thetruncin the output. Then theabortcall 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::MAXandi32::MIN..(i32::MIN + 3)) and au32loop above 2^31 give the correct sums at-O0and-O3, with both code generators. counted_loops_become_affine_loops.vxandemits_verifiable_affine_for_looppass.
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
|
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
|
Added the missing files |
hiraditya
left a comment
There was a problem hiding this comment.
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.mlrandcounted_loops_become_affine_loops.vxpass.- I removed
overflow<nsw>from the flat code generator. ThenCAST,OPTandVECTORfail: theabortcall comes back, and LLVM does not vectorize the loop. - I removed it from the legacy code generator. Then
LEGACYfails.
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
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