Skip to content

Keep the selected FMA mode with each shader generator - #5269

Open
reviewbd wants to merge 7 commits into
ItsNotRexed:mainfrom
reviewbd:fix/fma-mode-per-generator
Open

reviewbd wants to merge 7 commits into
ItsNotRexed:mainfrom
reviewbd:fix/fma-mode-per-generator

Conversation

@reviewbd

@reviewbd reviewbd commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue. Closes #5267.

Depends on: #5094

What and why

Capture the selected mode when the generator is constructed. Instruction emission and shader_meta now use the same value even if a Python caller restores configuration or translates another shader before writing the first index entry. The CLI already sets its options before constructing each generator.

Tested

On Windows with CPython 3.13.9, 142 tests passed and two optional SPIR-V tests were skipped across test_fma_exact, test_fma_khr, test_shader_checks and test_run_gpu_tests. The regression checks all three generators after their translation options have been restored. The branch builds on commit 159426d of #5094 and uses its existing fma metadata field. Synthetic CPU tests only.

AI-assisted: yes

@ray3242

ray3242 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@reviewbd — your follow-up is a real defect: I independently reproduced it on #5094's head (translate in exact/khr with the scoped-helper pattern, restore globals, then index): both record fma=glsl; khr is saved by the capability but exact has none, so a metadata-trusting checker accepts split words. Your generator-bound approach in d2fe5dbf is the right fix.

Coordination, before either of us duplicates work: #5134 is a duplicate of my #5094 for the same item. I claimed TEST-1147 first at #5032 (comment); the coordinator ruled #5094 is the valid PR and asked you to pick another item at #5032 (comment); the finder @AmazingMoaaz passed first refusal to me at #5032 (comment). #5094 also carries two fixes found in review that #5134 lacks (run_gpu_tests.py strictness and recorded-mode-authoritative semantics).

So I am not implementing the generator-bound fix in #5094 — it's yours, as claimed. Could you retarget #5269 onto #5094 instead of #5134 (Depends on: #5094)? Note the field name differs: #5134 uses fma_mode, #5094 uses fma; please keep #5094's semantics (recorded field alone decides strictness; capability/env only as fallback for old indexes) and its two tests. If you'd rather carry TEST-1147 through #5134, say so and let the coordinator rule — but please don't have both PRs carry the same fix. AI-assisted (OpenCode), synthetic shaders, no game data.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Head d2fe5dbfb. git diff --check clean.

  • python -I -m pytest tests/semantic/test_fma_exact.py tests/test_shader_checks.py tests/test_shader_meta.py tests/test_spirv_stubs.py -q -> 122 passed, 1089 subtests passed (nothing skipped; this collection is 122, not the body's 127).
  • Negative control for the regression: restoring tools/rdna2spirv.py from c53013b52 -> SUBFAILED(mode='exact') and SUBFAILED(mode='khr') in test_index_keeps_the_mode_used_for_each_translation, 1 passed; with the PR -> all pass. The test restores the globals before shader_meta, which is exactly the failure mode fixed by snapshotting fma_mode in Gen.__init__.
  • python -I -m pytest tests -k fma -q -> 287 passed, 19 skipped, 2641 subtests passed; tests/semantic/test_fma_khr.py tests/test_fma_mix_probe.py (not in the body's list) -> 27 passed.
  • GPU (RTX 5070 Ti, build from main): --filter cs_fma_fused -> PASS 8/8 fused exact (GLSL Fma); with LEONIDA_FMA=exact -> PASS 8/8 fused exact (exact emulation). The new shader_fma_mode(index...) dispatch is exercised on both the lenient and strict paths.
  • Fallback ordering is safe: an index without fma_mode still yields features shaderFmaFloat32 -> khr else LEONIDA_FMA/glsl, i.e. the old judgement; a recorded mode overrides a conflicting env var, which is right because the SPIR-V cannot change after translation. shader_features() has no remaining callers.

Verdict: merge. Single behaviour change beyond the snapshot is that --fma exact is now judged strictly (8/8 fused exact (exact emulation)), which is the intent.

@ray3242

ray3242 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@reviewbd — verified your force-push: #5269 now contains all of #5094's commits (b327ef46…159426db) plus your d7f07338, the body says Depends on: #5094, and your diff uses this PR's fma field ("fma": g.fma_mode) with the generator capturing the mode at construction — my check.py / run_gpu_tests.py fixes are untouched. That's exactly the chain @AmazingMoaaz and I proposed.

Independent check on your new head d7f07338 (Windows 11, Python 3.12, python -I): test_shader_checks.py 82/82 OK, test_run_gpu_tests.py 43/43 OK, unittest discover -s tests/semantic -p test_fma_exact.py 10 tests OK (1 optional skip), git diff --check clean. No game data, no GPU needed. Credit for #5267 stays with you; nothing further from me on this fix. AI-assisted (OpenCode).

@Atomic258

Copy link
Copy Markdown

Tested d7f0733 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_shader_checks.py -> Ran 82 tests, OK
  • Negative control (tools/rdna2spirv.py swapped to upstream/main): also OK, control does not differentiate here
  • Only the Python part tested.

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Windows CPU review at d7f0733 against direct parent #5094 (159426d). For each of glsl, exact and khr, all 24 cached shaders translated and validated under Vulkan 1.2 on both revisions. Assembly, binaries and index.json matched byte for byte in each mode. In a synthetic same-process check, three generators were constructed under different modes, then emitted code and wrote metadata under changed settings. All three head generators retained their construction mode in both assembly and persisted metadata. All three parent generators instead followed the mode active at each operation. AI-assisted; 12 pixel and 12 vertex shaders in the CLI slice.

@milas6669

Copy link
Copy Markdown

Tested d7f07338 on Windows 11, NVIDIA GeForce RTX 3070 (driver 32.0.16.2124), MSVC 14.44.

Unit and semantic tests, on the branch:

python -I tests/test_run_gpu_tests.py     -> OK
python -I tests/test_shader_checks.py     -> OK
python -I -m unittest discover -s tests/semantic -p "test_fma_exact.py"  -> Ran 10 tests, OK

Negative control (the point of the change). On main 3faac557, with this branch's tests/semantic/test_fma_exact.py but main's tools/rdna2spirv.py:

ERROR: test_index_keeps_the_mode_used_for_each_translation (mode='exact')  KeyError: 'fma'
ERROR: test_index_keeps_the_mode_used_for_each_translation (mode='khr')    KeyError: 'fma'
ERROR: test_index_keeps_the_mode_used_for_each_translation (mode='glsl')   KeyError: 'fma'
FAILED (errors=3)

The same file on d7f07338 passes, so the new case fails before the fix and passes after it, in all three modes.

GPU side, this machine:

python -I scripts/run_gpu_tests.py --build runtime/build --filter fma --require-pass
3 shaders: 3 translated, 3 pass spirv-val
  PASS  cs_fma_fused          8/8 fused exact (GLSL Fma)
  PASS  ps_fma_literal        4096/4096 pixels exact
  PASS  ps_fmac_f16_mods      4096/4096 pixels exact
3/3 passed

So the per-generator mode also holds on the hardware: the fused shader is still bit-exact on Ampere and the two pixel fixtures are exact.

AI-assisted. No game data.

@lunarminus

Copy link
Copy Markdown

Tested d7f07338 merged onto main d0835dd9 on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass

  • The head already contains main up to 98fa503f, so the merge onto d0835dd9 was a fast-forward. The tree tested is d7f07338 itself: TEST-1147: record the FMA mode in shader metadata and honor it in check.py #5094's five commits plus d7f07338. Neither control below separates Keep the selected FMA mode with each shader generator #5269 from TEST-1147: record the FMA mode in shader metadata and honor it in check.py #5094.
  • The runtime was built fresh in the worktree (cmake --build, RelWithDebInfo, exit 0).
  • CPU tests, with LEONIDA_AMD_CLANG pointing at an AMDGPU-capable clang:
    • python -I tests/test_shader_checks.py: 82 tests OK.
    • python -I tests/test_run_gpu_tests.py: 43 tests OK.
    • python -m unittest discover -s tests/semantic -p 'test_fma*.py': 19 tests OK, 0 skipped, including test_index_keeps_the_mode_used_for_each_translation.
    • git diff --check is clean.
  • scripts/run_gpu_tests.py, default FMA mode:
    • native wave64 (LEONIDA_W64_EMULATE unset): 202/217 passed, 15 skipped.
    • LEONIDA_W64_EMULATE=1: 202/217 passed, 15 skipped.
  • Per-test comparison with d0835dd9 without the PR (196/210 in both modes): no test changed its PASS/FAIL/SKIP word. The only differences are 7 tests that d0835dd9 does not have and that came in with main: cs_smem_sconst_off, ps_far_branch, ps_sample_c, ps_sample_c_b, ps_texture_b_cl and ps_texture_cl PASS, ps_m2_literal SKIP.
  • Against the PR's own base 98fa503f (earlier run on this machine, 202/217 in both modes) there are 0 per-test differences in either mode.
  • cs_fma_fused in default glsl mode: PASS in both modes, "3/7 f32 fused, 4/7 split (GLSL Fma …), f16 exact", so RADV splits the GLSL Fma here. spirv/index.json records "fma": "glsl" for all 221 shaders.
  • Extra runs of --filter cs_fma_fused with native wave64:
    • LEONIDA_FMA=exact: the index records "fma": "exact" and the strict judge passes, "8/8 fused exact (single-rounded FMA: OpFmaKHR or --fma exact)".
    • LEONIDA_FMA=khr: the index records "khr" plus the FMAKHR capability, and the test passes 8/8.
    • 98fa503f with LEONIDA_FMA=exact: also 8/8, but judged on the old lenient "GLSL Fma" path.
  • On this driver --fma exact gives single-rounded words, so the case where the strict judge rejects split words cannot be reproduced on real hardware here. It is covered only by the PR's synthetic tests.

AI-assisted (Claude Code).

@sovljanskii

Copy link
Copy Markdown
Contributor

AI-assisted. Independent run on an Apple M4 (macOS, MoltenVK 1.4.2) at d7f07338, the force-pushed head that contains #5094 through 159426db, merged onto main 3faac557 without conflicts. The runtime was built from main, because the branch changes no C++.

Corpus slice. Over --shard 0/64 of my own copy (415 shaders), both main and this branch translate 415 of 415, and all 415 pass spirv-val. All 415 .spv and all 415 .spvasm files are byte-identical with main. The index differs only by the new fma field, which is "glsl" in all 415 entries.

cs_fma_fused in each mode. I ran run_gpu_tests.py --filter cs_fma_fused with LEONIDA_FMA set to each mode:

LEONIDA_FMA main this PR
glsl FAIL word 7 (f16): got 0x00000000, want 0x00007351 same
exact PASS 8/8 fused exact (GLSL Fma) PASS 8/8 fused exact (single-rounded FMA: OpFmaKHR or --fma exact)
khr FAIL word 7: got 0x00000000, want fused 0x00007351 (OpFmaKHR) same failure, new label

The exact build passes the strict judging on this GPU. main judged that build with the lenient GLSL rule. The word-7 failure is the known MoltenVK 1.4.2 one recorded in #166, and it happens on main in the same modes, so this branch neither causes it nor hides it.

GPU suite. The full suite gives 152/217 passed and 58 skipped. Every row has the same status and detail as on main.

Unit tests.

@reviewbd

Copy link
Copy Markdown
Author

@ray3242 Thanks for checking the stack. I merged main at 561b65a into #5269 in f173caa. The PR still builds on #5094 and uses its fma field. Both runners use the recorded mode when present and retain the capability/environment fallback for older indexes. The merge also keeps main's separate diagnostics for exact emulation and OpFmaKHR.

I ran the two FMA semantic suites, both shader test runners, the metadata tests and the SPIR-V stub tests on Windows with CPython 3.13.9. Of 194 tests, 191 passed and 3 were skipped. This was a CPU-only run. AI-assisted.

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.

FMA index metadata follows later option changes when translation and indexing are separated

8 participants