Skip to content

feat(unroll): recognize an exit test that adds a constant to the counter - #1321

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
midagedev:fix/unroll-offset-guard
Sep 24, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
midagedev:fix/unroll-offset-guard

Conversation

@midagedev

@midagedev midagedev commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

#[unroll] now recognizes a counted loop whose exit test adds or subtracts a constant from the counter, including tile walks:

#[unroll]
while d + TILE <= WIDTH {
    // Process one tile.
    d += TILE;
}

Previously this was reported as having no recognized counter. The analysis now records the offset and normalizes constant limits. Full unrolling refuses counter/test wraparound; partial unrolling guards complete groups and retains the source loop for the remainder. WGMMA counted-loop matching still rejects offset guards.

Coverage includes guard recognition, exact full-unroll results, fixed-width guard behavior across signed/unsigned boundaries, and GPU tile-walk/full/partial examples.

Verified at 12d61bca: 585 host tests across mir-transforms, mir-lower, and dialect-mir; strict Clippy; B200/sm_100 LLVM runs of unroll_smoke and unroll_bounds_check; smoke memcheck/synccheck each with 0 errors. The libNVVM GPU paths were not rerun. No speedup has been measured.

At 12d61bca, hosted tests, lint/format checks, guards, docs/book and example compilation passed. CodeQL was still running at the final review check.

midagedev and others added 2 commits September 23, 2026 23:28
`while i + c <op> n` (a tile walk: stop while a whole tile still fits) was
not unrolled, and the warning said the loop had no induction variable. The
guard match only accepted the counter itself on one side of the compare.

analyze_guard now also matches `i + c` / `c + i` / `i - c` on exactly one
side and records iv_offset; a constant bound is normalized to n - c with
checked arithmetic. Full unroll skips when i + c can wrap at the first or
last test; partial unroll adds one no-wrap compare to its group guard. The
WGMMA counted-loop matchers keep refusing an offset test. A loop with a
counter but an unrecognized exit test gets its own warning.

Tests: induction and unroll host tests for both sides, negative offsets,
overflowing limits, high-bit unsigned constants, both-sides-variable, and
the two wrap cases; an unroll_smoke kernel with exact expected values.

Signed-off-by: midagedev <[email protected]>
nihalpasham
nihalpasham previously approved these changes Sep 24, 2026

@nihalpasham nihalpasham 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.

The offset handling looks sound: full unrolling checks the counter and tested expression through the exit test, while partial unrolling sends wrapping groups back to the original loop.

I added behavior checks for the generated guard across signed/unsigned boundaries, plus full-unroll result checks for descending and high-bit cases. At 12d61bca, the three affected host suites passed (585 tests) and strict Clippy was clean. unroll_smoke and unroll_bounds_check passed on B200; smoke memcheck and synccheck each reported 0 errors.

GPU verification used the LLVM path. The libNVVM paths and full repository/hosted checks have not been rerun here.

@nihalpasham nihalpasham added enhancement New feature or request IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) optional-opts Opt-in optimization passes for the oxide-owned opt registry (off by default) labels Sep 24, 2026

@nihalpasham nihalpasham 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.

The offset handling looks sound: full unrolling checks the counter and tested expression through the exit test, while partial unrolling sends wrapping groups back to the original loop.

I added behavior checks for the generated guard across signed/unsigned boundaries, plus full-unroll result checks for descending and high-bit cases. At 12d61bca, the three affected host suites passed (585 tests) and strict Clippy was clean. unroll_smoke and unroll_bounds_check passed on B200; smoke memcheck and synccheck each reported 0 errors.

GPU verification used the LLVM path. The libNVVM paths have not been rerun here.

At 12d61bca, hosted tests, lint/format checks, guards, docs/book and example compilation passed.

@nihalpasham
nihalpasham merged commit 8206f6e into NVIDIA:main Sep 24, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) optional-opts Opt-in optimization passes for the oxide-owned opt registry (off by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants