Skip to content

Fix loop split point overflow when the bound is the index type maximum - #128

Open
vxnuaj wants to merge 1 commit into
NVIDIA:mainfrom
vxnuaj:fix-loop-split-max-overflow
Open

vxnuaj wants to merge 1 commit into
NVIDIA:mainfrom
vxnuaj:fix-loop-split-max-overflow

Conversation

@vxnuaj

@vxnuaj vxnuaj commented Nov 19, 2026

Copy link
Copy Markdown

Description

Fixes a miscompilation in the loop split pass. For if i > c and if i <= c the pass splits the loop at c + 1. When c is the maximum value of the loop index type, the addition wraps to the type minimum and every iteration takes the wrong branch. The kernel then returns incorrect results with no error.

This change saturates the split point at the type maximum instead. That is correct because the loop stop can never exceed the maximum, so for <= the first loop covers the whole range and for > the second loop is empty. The saturation is emitted as where(c == max, max, c + 1), so the result does not depend on how the wrapped c + 1 is interpreted. Splits for < and >= do not add one and are unchanged.

Regression tests cover both predicates at c equal to -1, 0, 1, 3, 4, INT32_MAX minus 1 and INT32_MAX with a 4 iteration loop, checked against a Python reference.

Tested on an NVIDIA L4 (sm_89) with CUDA 13.5 by building cuTile from source at main and at this branch, then running test/test_loop_split.py, test/test_control_flow.py and test/test_range.py with -m "not benchmark and not use_mlir".

  • main gives 254 passed, 2 failed (the new INT32_MAX cases), 2 xfailed
  • this branch gives 256 passed, 2 xfailed

flake8 and scripts/check_license.py pass.

closes #127

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@gbonik

gbonik commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Thank you for the patch! It looks correct but I'm a bit worried about quality of codegen: using comparison+select may not be ideal. To the best of my knowledge, there is no way to express saturating arithmetic directly in TileIR, unfortunately. I suspect that the next best option may be using min:

adjusted_split_value = min(split_value, max_value - 1) + 1

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.

[BUG] Loop split gives wrong results for i > c and i <= c when c is the index type maximum

2 participants