startup: gfx2hlsl folds a loop counter to its value before the loop, so translated loops run to the 65,536-iteration guard #2270

Closed
opened 2026-11-25 09:28:20 +00:00 by lukaslw-lab · 0 comments
lukaslw-lab commented 2026-11-25 09:28:20 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-25T09:28:20Z.


Commit or build

main 07c1c20f.

Reproduction

A source-built program, no shader machine code needed:

s_mov_b32 s14, 0
s_add_u32 s14, s14, 1          ; loop head (target of the branch below)
v_cvt_f32_i32_e32 v13, s14
s_cmp_eq_u32 s14, s2
s_cbranch_scc0 <loop head>
s_endpgm

Translate it with gfx2hlsl.Stage.translate() (the harness of tests/startup_gfx2hlsl_readfirstlane_uniform_check.py).

Expected behavior

Inside the loop the counter is read as the register: s14 = s14 + 0x00000001u; and v13 = U(float(int(s14)));.

Observed behavior

The value from before the loop is folded into the loop body:

s14 = 0x00000000u + 0x00000001u;
v13 = U(float(int(0x00000000u)));

The counter is 1 on every pass, so the exit test only fires for a trip count of 1. Any other loop runs until the dispatcher guard (guard < 65536), and everything computed from the counter (sample offsets, buffer strides) is frozen at the first iteration's value.

Cause: at a branch target Stage.translate clears the uniformity proofs (uniform_v, since_join, scc_uniform) but keeps the ('const', n) entries in self.sym. A forward branch has the same problem: after a join, a register written only on the fall-through path is still folded to its earlier value.

Effect in a local startup run (Windows 10 19045, RTX 3080 Ti, -Render -Translate, post-confirmation load):

Before the load During the load
Presents per second ~30 (paced) ~2.5
GPU time per present (direct queue) 3.5–5 ms 330–440 ms
  • A one-frame PIX capture and per-draw GPU timestamps both put ~99% of the frame in three translated full-screen pixel passes: one at about 324 ms and four at about 16 ms each. A 4K full-screen pass in the same frame takes 0.14 ms.
  • All three translated pixel shaders contain a loop whose counter is folded as above.
  • In this run's shader cache, 17 of 171 translated pipelines contain a loop, and all 17 are affected.

Environment

Windows 10 22H2 (19045), Python 3.13, RTX 3080 Ti. The reproduction above is pure Python and needs no device.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6601345449 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/2278). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-25T09:28:20Z. --- ### Commit or build `main` `07c1c20f`. ### Reproduction A source-built program, no shader machine code needed: ``` s_mov_b32 s14, 0 s_add_u32 s14, s14, 1 ; loop head (target of the branch below) v_cvt_f32_i32_e32 v13, s14 s_cmp_eq_u32 s14, s2 s_cbranch_scc0 <loop head> s_endpgm ``` Translate it with `gfx2hlsl.Stage.translate()` (the harness of `tests/startup_gfx2hlsl_readfirstlane_uniform_check.py`). ### Expected behavior Inside the loop the counter is read as the register: `s14 = s14 + 0x00000001u;` and `v13 = U(float(int(s14)));`. ### Observed behavior The value from before the loop is folded into the loop body: ```hlsl s14 = 0x00000000u + 0x00000001u; v13 = U(float(int(0x00000000u))); ``` The counter is 1 on every pass, so the exit test only fires for a trip count of 1. Any other loop runs until the dispatcher guard (`guard < 65536`), and everything computed from the counter (sample offsets, buffer strides) is frozen at the first iteration's value. Cause: at a branch target `Stage.translate` clears the uniformity proofs (`uniform_v`, `since_join`, `scc_uniform`) but keeps the `('const', n)` entries in `self.sym`. A forward branch has the same problem: after a join, a register written only on the fall-through path is still folded to its earlier value. **Effect in a local startup run** (Windows 10 19045, RTX 3080 Ti, `-Render -Translate`, post-confirmation load): | | Before the load | During the load | |---|---|---| | Presents per second | ~30 (paced) | ~2.5 | | GPU time per present (direct queue) | 3.5–5 ms | 330–440 ms | - A one-frame PIX capture and per-draw GPU timestamps both put ~99% of the frame in three translated full-screen pixel passes: one at about 324 ms and four at about 16 ms each. A 4K full-screen pass in the same frame takes 0.14 ms. - All three translated pixel shaders contain a loop whose counter is folded as above. - In this run's shader cache, 17 of 171 translated pipelines contain a loop, and all 17 are affected. ### Environment Windows 10 22H2 (19045), Python 3.13, RTX 3080 Ti. The reproduction above is pure Python and needs no device. AI-assisted (Claude Code).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows#2270
No description provided.