Skip to content

[compiler] Preserve expression bindings when coalescing device variables - #4749

Merged
choijon5 merged 1 commit into
mainfrom
choijon5/helion-b200/shared/1
Nov 28, 2026
Merged

choijon5 merged 1 commit into
mainfrom
choijon5/helion-b200/shared/1

Conversation

@choijon5

@choijon5 choijon5 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[compiler] Preserve expression bindings when coalescing device variables

Coalescing an outer variable could change 2 * 3 to 7 * 3 in a closed
lambda, or turn an outer 7 into a local 2. The same name can appear as
both a bound and a free read in a single generated device expression.

Keep lambda arguments and comprehension targets local. Defaults and
the first iterable use their defining scope. For a real free-to-bound
collision, rename the comprehension target or give the lambda a getter
so it retains its keyword arguments and observes later outer assignments.
Plain code takes one iterative walk; closed callback shapes stay intact.

Helion templates may splice the same AST node twice. Detach affected
expression occurrences before changing outer names, preserve source
metadata and FX-owned argument lists, and process replacements without
recursing into either the scoped body or an unrelated deep expression.
This is an internal expression transform. The kernel's own variables
still coalesce; outlined named functions explicitly rewrite their own
formals before passing their body to this pass. Use the returned value
for a bare lambda, which may need a wrapper.

Testing: scripts/run_tests.py passed 351 focused CuTe CPU tests (2
skipped), 40 Triton CPU, 37 Metal CPU (3 skipped), and the full local
Pallas interpreter module (269 passed, 44 skipped). These include 13
direct regressions, cross-scope templates, immutable FX arguments and
1,500 nested operators inside and beside a callback. Four of six initial
semantic cases failed on main; the first expression-scope correction
passed 344 CuTe, 33 Triton, 30 Metal and 269 Pallas, with the same skips.
A device-denied SDK 4.8.1 compile of the corrected callback and a direct
late load emitted identical complete SM100a ELF and instructions.
./lint.sh check on the three touched files, including Pyrefly, passed.
No physical GPU lane was run in this standalone check.

Integrated validation before the direct-ConfigSpec target lookup and
shared test-loader changes: scripts/run_tests.py passed the CuTe lanes
(26,206 CPU, 16,138 GPU on B200 3/5), the full Triton lanes
(4,900 CPU, 3,395 GPU on B200 6), and the Pallas interpreter suite
(269 passed, 44 skipped). CuTe used nightly Torch and CuTe DSL 4.8.1;
Triton used CI Torch 2.14.1/Triton 3.8. The production compiler was
identical in these runs. The Triton run includes a later test-only
fix to isolate the fake SM103 target from real devices, also checked
with the affected test module on CuTe and Triton.

autoreview passed: claude, codex

Coalescing an outer variable could change 2 * 3 to 7 * 3 in a closed
lambda, or turn an outer 7 into a local 2. The same name can appear as
both a bound and a free read in a single generated device expression.

Keep lambda arguments and comprehension targets local. Defaults and
the first iterable use their defining scope. For a real free-to-bound
collision, rename the comprehension target or give the lambda a getter
so it retains its keyword arguments and observes later outer assignments.
Plain code takes one iterative walk; closed callback shapes stay intact.

Helion templates may splice the same AST node twice. Detach affected
expression occurrences before changing outer names, preserve source
metadata and FX-owned argument lists, and process replacements without
recursing into either the scoped body or an unrelated deep expression.
This is an internal expression transform. The kernel's own variables
still coalesce; outlined named functions explicitly rewrite their own
formals before passing their body to this pass. Use the returned value
for a bare lambda, which may need a wrapper.

Testing: scripts/run_tests.py passed 351 focused CuTe CPU tests (2
skipped), 40 Triton CPU, 37 Metal CPU (3 skipped), and the full local
Pallas interpreter module (269 passed, 44 skipped). These include 13
direct regressions, cross-scope templates, immutable FX arguments and
1,500 nested operators inside and beside a callback. Four of six initial
semantic cases failed on main; the first expression-scope correction
passed 344 CuTe, 33 Triton, 30 Metal and 269 Pallas, with the same skips.
A device-denied SDK 4.8.1 compile of the corrected callback and a direct
late load emitted identical complete SM100a ELF and instructions.
./lint.sh check on the three touched files, including Pyrefly, passed.
No physical GPU lane was run in this standalone check.

Integrated validation before the direct-ConfigSpec target lookup and
shared test-loader changes: scripts/run_tests.py passed the CuTe lanes
(26,206 CPU, 16,138 GPU on B200 3/5), the full Triton lanes
(4,900 CPU, 3,395 GPU on B200 6), and the Pallas interpreter suite
(269 passed, 44 skipped). CuTe used nightly Torch and CuTe DSL 4.8.1;
Triton used CI Torch 2.14.1/Triton 3.8. The production compiler was
identical in these runs. The Triton run includes a later test-only
fix to isolate the fake SM103 target from real devices, also checked
with the affected test module on CuTe and Triton.

autoreview passed: claude, codex

stack-info: PR: #4749, branch: choijon5/helion-b200/shared/1
@choijon5
choijon5 marked this pull request as draft November 26, 2026 17:02
@choijon5
choijon5 force-pushed the choijon5/helion-b200/shared/1 branch from 3d5fd78 to 8500749 Compare November 26, 2026 17:03
@choijon5
choijon5 marked this pull request as ready for review November 26, 2026 17:04
@choijon5
choijon5 requested a review from jansel November 27, 2026 16:31
@choijon5
choijon5 merged commit 854fbf8 into main Nov 28, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants