Skip to content

[lang] Keep custom reduce and scan callback bodies self-contained - #103

Closed
azazhu wants to merge 9 commits into
NVIDIA:mainfrom
azazhu:azazhu/bug-reduce-scan-isolation
Closed

[lang] Keep custom reduce and scan callback bodies self-contained#103
azazhu wants to merge 9 commits into
NVIDIA:mainfrom
azazhu:azazhu/bug-reduce-scan-isolation

Conversation

@azazhu

@azazhu azazhu commented Sep 2, 2026

Copy link
Copy Markdown

Classification: bug fix with a documented behavior change.

Description

Custom ct.reduce() / ct.scan() callbacks are pure combine functions over their block arguments. Until now the body emitted for a callback could depend on the enclosing scope: loop-invariant code motion hoisted constants out of the body, and constants captured from the kernel were referenced from outside the body. This change keeps callback bodies self-contained, so they rely on nothing but their block arguments and constants materialized inside the body.

  • Treat reduce/scan bodies as LICM barriers. Hoisting the whole operation out of a loop still works.
  • Rematerialize captured scalar compile-time constants inside each callback body.
  • Require every value inside a callback to be a scalar (0-d) tile, and reject captures of runtime values with a compile-time error that names the offending variable. Note: capturing runtime values in these callbacks previously compiled; it is no longer supported.
  • Verify after the final passes that callback SSA uses are region-local and definition-ordered.

Testing

  • python -m pytest -q test/test_reduction.py test/test_scan.py test/test_code_motion.py
    • 450 passed, 72 skipped, 4 xfailed
  • python -m flake8 on all changed Python files
  • git diff --check main..HEAD

Checklist

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

azazhu and others added 9 commits September 2, 2026 12:30
Signed-off-by: feiwen zhu <mzhu@nvidia.com>
Signed-off-by: feiwen zhu <mzhu@nvidia.com>
Signed-off-by: feiwen zhu <mzhu@nvidia.com>
Signed-off-by: feiwen zhu <mzhu@nvidia.com>
Signed-off-by: feiwen zhu <mzhu@nvidia.com>
Return the source-level names of captured values from the pre-pass and
hand them to the legalization pass, instead of stashing them on the
IRContext and importing a private helper from _compile.py. Key them by the
value that survives Assign elimination. Also document why reduce/scan
bodies are LICM barriers and why capture legalization must run after
constant materialization, and name the callback's parameters explicitly in
the runtime-capture error.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: feiwen zhu <mzhu@nvidia.com>
Check, right after a callback body is built, that every value inside it
is a 0-d tile. This gives one consistent diagnostic for non-scalar
constants whether they are captured from the enclosing scope or created
inside the callback, and it replaces the capture-only shape check in the
legalization pass. Document the restriction in the ct.reduce()/ct.scan()
docstrings.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: feiwen zhu <mzhu@nvidia.com>
The kernels checking that a whole reduce/scan can be hoisted stored tiles
whose rank did not match the array; give them matching ranks and launch
them so the hoisted result is verified. Cover scan in the LICM barrier
test as well. The scan constant-capture test used identity 0 together with
'(a + b) % 2', which is not an identity for arbitrary inputs; restrict the
inputs to {0, 1} so the reference does not depend on where the identity is
combined.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: feiwen zhu <mzhu@nvidia.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: feiwen zhu <mzhu@nvidia.com>
@azazhu azazhu changed the title [lang] Isolate custom reduce and scan callback bodies [lang] Keep custom reduce and scan callback bodies self-contained Sep 2, 2026
@haijieg

haijieg commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

@azazhu i will close this PR since this issue should be fixed in tileiras.

@haijieg haijieg closed this Sep 4, 2026
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.

2 participants