diff options
| author | hugo <63666848+hugo0024@users.noreply.github.com> | 2026-11-25 17:38:41 +0800 |
|---|---|---|
| committer | hugo <63666848+hugo0024@users.noreply.github.com> | 2026-11-28 12:44:48 +0800 |
| commit | 420116dbbcf78d75d8f0893c8cd8bcfbfb0bcdab (patch) | |
| tree | 4c6eca585f1983014b197fe8508f8bfefcf315c9 | |
| parent | ef8fbf09f3ba00460e1c08102b096b76702a70b6 (diff) | |
tests: cs_fma_fused takes its f16 operands at run time, like its f32 ones (TEST-FMA-FUSED-F16-RUNTIME)
The fixture reads the operands of its f32 multiply-adds from s4..s7 so that a driver has to
compute them at run time. The three operands of its v_fma_f16 were literals, which made the
whole f16 multiply-add a compile-time constant. MoltenVK 1.4.2 gives the wrong constant: word
7 read 0x0000 on an Apple M5 (0x3c00 in the docs' row) where 0x7351 is expected, the
cs_fma_fused failure listed in docs/testing.md and in the macOS job's EXPECTED_FAILURES. The
same translation with any one of the three operands taken at run time gives 0x7351.
The triple is now the s4 word XOR a literal each, so the test checks the translator's
round-to-odd sequence as the GPU runs it. The translator is not changed.
tests/semantic/test_fma_fused_fixture.py runs the fixture in the interpreter against the eight
words of its header and checks that no multiply-add in it reads a register only literals fed.
The Known problems row says what is left: an f16 multiply-add with only constant operands.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
| -rw-r--r-- | docs/testing.md | 2 | ||||
| -rw-r--r-- | runtime/test/shaders/cs_fma_fused.asm | 10 | ||||
| -rw-r--r-- | tests/semantic/README.md | 4 | ||||
| -rw-r--r-- | tests/semantic/test_fma_fused_fixture.py | 96 |
4 files changed, 108 insertions, 4 deletions
diff --git a/docs/testing.md b/docs/testing.md index 62f5ced44..6a399740c 100644 --- a/docs/testing.md +++ b/docs/testing.md | |||
| @@ -796,7 +796,7 @@ game's device ignores it`, so leaving them set cannot slow a game down (`run_tin | |||
| 796 | | On a Mac, a translated shader's GPU time is 1.5 to 16 times what it would be without the precise-arithmetic overhead | MoltenVK's SPIRV-Cross turns every `NoContraction` float add, subtract and multiply into a call to an `[[clang::optnone]]` helper that Metal does not inline. MoltenVK 1.4.2 and `main` up to at least `5189f8de` are affected (SHADER-234, [design notes](design.md#translator-status-and-runtime-contract)) | Expected with these MoltenVK versions, and the output is still exact. Before you time shaders on a Mac, build MoltenVK with SPIRV-Cross `98cbb7c0` or newer in `ExternalRevisions/SPIRV-Cross_repo_revision` (KhronosGroup/MoltenVK#2917 moves it to `57e4efcc`), and point `LEONIDA_VULKAN_LOADER` at its `libMoltenVK.dylib` | | 796 | | On a Mac, a translated shader's GPU time is 1.5 to 16 times what it would be without the precise-arithmetic overhead | MoltenVK's SPIRV-Cross turns every `NoContraction` float add, subtract and multiply into a call to an `[[clang::optnone]]` helper that Metal does not inline. MoltenVK 1.4.2 and `main` up to at least `5189f8de` are affected (SHADER-234, [design notes](design.md#translator-status-and-runtime-contract)) | Expected with these MoltenVK versions, and the output is still exact. Before you time shaders on a Mac, build MoltenVK with SPIRV-Cross `98cbb7c0` or newer in `ExternalRevisions/SPIRV-Cross_repo_revision` (KhronosGroup/MoltenVK#2917 moves it to `57e4efcc`), and point `LEONIDA_VULKAN_LOADER` at its `libMoltenVK.dylib` | |
| 797 | | `d3d12_descriptor_test` reports that the set had room for fewer than 5001 2D views and 2100 samplers, and the device logs that the bindless set holds fewer samplers than a full shader-visible sampler heap | The device's descriptor limits, or its `vkGetDescriptorSetLayoutSupport`, allow no bigger bindless set ([d3d12_vulkan.md](d3d12_vulkan.md) section 5). MoltenVK's answer is ignored, since it rejects every layout of `maxPerSetDescriptors` (1212) or more although it runs the full set | Expected where the limits are low. The test still checks the last index of each array and that the next view and sampler are refused | | 797 | | `d3d12_descriptor_test` reports that the set had room for fewer than 5001 2D views and 2100 samplers, and the device logs that the bindless set holds fewer samplers than a full shader-visible sampler heap | The device's descriptor limits, or its `vkGetDescriptorSetLayoutSupport`, allow no bigger bindless set ([d3d12_vulkan.md](d3d12_vulkan.md) section 5). MoltenVK's answer is ignored, since it rejects every layout of `maxPerSetDescriptors` (1212) or more although it runs the full set | Expected where the limits are low. The test still checks the last index of each array and that the next view and sampler are refused | |
| 798 | | `cs_dpp_ops` fails on a Mac with MoltenVK 1.4.2: `out[0..7] = 100 100 11 ...`, want `100 10 11 ...` | MoltenVK 1.4.2's SPIRV-Cross moves the DPP lowering's subgroup ballot and shuffle into a short-circuiting `&&` and `?:`, so only some lanes run them and lane 1 sees its source lane 0 as inactive. SPIRV-Cross `5c372ffb` (in MoltenVK `main`) keeps them out of `?:`; a ballot on the right of `&&` is still moved there in SPIRV-Cross `57e4efcc` | Fixed in the translator (SHADER-529, #3227): it stores the ballot and the shuffle to variables of their own, so the MSL that MoltenVK 1.4.2 generates (`MVK_CONFIG_DEBUG=1`) runs each as a statement in every lane. Translate again with current `main`; MoltenVK `main` and KosmicKrisp are no longer needed for it. A pass alone does not show the fix: an Apple M5 Pro (macOS 26.5.1) returns the right words with 1.4.2 even when the ballot is on the right of `&&` | | 798 | | `cs_dpp_ops` fails on a Mac with MoltenVK 1.4.2: `out[0..7] = 100 100 11 ...`, want `100 10 11 ...` | MoltenVK 1.4.2's SPIRV-Cross moves the DPP lowering's subgroup ballot and shuffle into a short-circuiting `&&` and `?:`, so only some lanes run them and lane 1 sees its source lane 0 as inactive. SPIRV-Cross `5c372ffb` (in MoltenVK `main`) keeps them out of `?:`; a ballot on the right of `&&` is still moved there in SPIRV-Cross `57e4efcc` | Fixed in the translator (SHADER-529, #3227): it stores the ballot and the shuffle to variables of their own, so the MSL that MoltenVK 1.4.2 generates (`MVK_CONFIG_DEBUG=1`) runs each as a statement in every lane. Translate again with current `main`; MoltenVK `main` and KosmicKrisp are no longer needed for it. A pass alone does not show the fix: an Apple M5 Pro (macOS 26.5.1) returns the right words with 1.4.2 even when the ballot is on the right of `&&` | |
| 799 | | `check.py`'s `cs_fma_fused` fails on a Mac with MoltenVK 1.4.2: word 7 reads `15360` (`0x3c00`), want `29521` (`0x7351`) | The f16 multiply-add (round to odd). The MSL of 1.4.2 and of MoltenVK `main` differ only in SPIRV-Cross's precise-arithmetic helpers (`[[clang::optnone]]` and `fma(l, r, 0)` against `math_mode(safe)` and `fma(l, r, -0.0)`, the SHADER-182 / SHADER-234 change) | Expected with MoltenVK 1.4.2; it passes with MoltenVK `main` | | 799 | | On a Mac with MoltenVK 1.4.2, an f16 multiply-add whose three operands are all constants gives a wrong constant: while `cs_fma_fused` had literals there, its word 7 read `15360` (`0x3c00`), or `0` on an Apple M5 and M5 Pro, want `29521` (`0x7351`) | The f16 multiply-add (round to odd), evaluated at compile time. With any one of the three operands taken at run time the same translation gives `0x7351` (Apple M5). The MSL of 1.4.2 and of MoltenVK `main` differ only in SPIRV-Cross's precise-arithmetic helpers (`[[clang::optnone]]` and `fma(l, r, 0)` against `math_mode(safe)` and `fma(l, r, -0.0)`, the SHADER-182 / SHADER-234 change) | `cs_fma_fused` takes its f16 operands at run time since TEST-FMA-FUSED-F16-RUNTIME, as it does its f32 ones, and passes with MoltenVK 1.4.2 on an Apple M5. The all-literal form passes with MoltenVK `main` | |
| 800 | | `ps_imgatomic` fails in about two of three runs on a Mac with MoltenVK 1.4.2: 1 to 6 of the 256 image-atomic texels are wrong while the render target is exact, for example `2D umax texel 48: got 0x00000d45, want 0x00000dc7` | MoltenVK 1.4.2 lowers the four `OpAtomic*` on image texels to Metal's texture atomics (`atomic_fetch_max`, `atomic_fetch_min` and `atomic_fetch_or` on `access::read_write` textures), and the texels read back the way a read-modify-write that is not atomic between two groups of fragment threads leaves them. A wrong `umax` texel always holds the id of one of its own 64 pixels, the largest of only some of them: on an Apple M5 it was the last pixel of the texel's first four rows in 43 of 90 cases, with all 32 larger ids gone. A wrong `umin` texel mirrors that (the first pixel of its last four rows in 39 of 48). The `or` image did not differ in 150 runs there. It is not the translator's first-lane broadcast of the descriptor index (54 of 60 runs fail with the index used directly) and not the readback (42 of 60 fail with the copies in a submission of their own after the draw). Measured on an Apple M5 (macOS 27.2.1, 101 of 150 runs fail) and an M5 Pro (#543) | Expected with MoltenVK 1.4.2. KosmicKrisp passed the same module and binary 10 times of 10 on the M5 Pro (#543). The macOS CI job does not reach it: on the runner's paravirtual device the test skips because its bindless set does not fit the descriptor limits. Do not weaken the test's expected texels | | 800 | | `ps_imgatomic` fails in about two of three runs on a Mac with MoltenVK 1.4.2: 1 to 6 of the 256 image-atomic texels are wrong while the render target is exact, for example `2D umax texel 48: got 0x00000d45, want 0x00000dc7` | MoltenVK 1.4.2 lowers the four `OpAtomic*` on image texels to Metal's texture atomics (`atomic_fetch_max`, `atomic_fetch_min` and `atomic_fetch_or` on `access::read_write` textures), and the texels read back the way a read-modify-write that is not atomic between two groups of fragment threads leaves them. A wrong `umax` texel always holds the id of one of its own 64 pixels, the largest of only some of them: on an Apple M5 it was the last pixel of the texel's first four rows in 43 of 90 cases, with all 32 larger ids gone. A wrong `umin` texel mirrors that (the first pixel of its last four rows in 39 of 48). The `or` image did not differ in 150 runs there. It is not the translator's first-lane broadcast of the descriptor index (54 of 60 runs fail with the index used directly) and not the readback (42 of 60 fail with the copies in a submission of their own after the draw). Measured on an Apple M5 (macOS 27.2.1, 101 of 150 runs fail) and an M5 Pro (#543) | Expected with MoltenVK 1.4.2. KosmicKrisp passed the same module and binary 10 times of 10 on the M5 Pro (#543). The macOS CI job does not reach it: on the runner's paravirtual device the test skips because its bindless set does not fit the descriptor limits. Do not weaken the test's expected texels | |
| 801 | | `gs_tri` and `ms_tri` fail on a Mac | MoltenVK has no geometry stage and no `VK_EXT_mesh_shader` | Current `main` reports them as skipped | | 801 | | `gs_tri` and `ms_tri` fail on a Mac | MoltenVK has no geometry stage and no `VK_EXT_mesh_shader` | Current `main` reports them as skipped | |
| 802 | | With `LEONIDA_W64_EMULATE=1` on llvmpipe, the `ms_*` tests fail. Measured with Mesa 26.3.1: on x86_64 at 256-bit vectors (the default there) all six abort with "LLVM ERROR: Cannot emit physreg copy instruction"; on arm64 (128-bit) `ms_tri` draws nothing (the frame stays at the clear color); on x86_64 at 128 bits (`LP_NATIVE_VECTOR_WIDTH=128`) `ms_tri` happened to draw correctly, but with Mesa 26.0.3 `ms_wave_info_s3` and `ms_v5_workgroup` draw nothing there (4 of 6 pass, [measured by @TreXito](https://github.com/ItsNotRexed/Leonida/pull/3838#issuecomment-7086696759)) ([measured runs](https://github.com/ItsNotRexed/Leonida/issues/3127#issuecomment-7086469679)) | An llvmpipe bug: a mesh shader's runtime `CullPrimitiveEXT` is stored through an invalid `bitcast <N x i1> to <N x float>` (TEST-782, #3127; see "Without a GPU: Mesa llvmpipe") | Run the mesh tests on llvmpipe without `LEONIDA_W64_EMULATE`, or add `LEONIDA_MESH_COMPUTE=1` (the compute fallback): with it all 6 `ms_*` pass emulated on llvmpipe (x86_64, 256-bit) | | 802 | | With `LEONIDA_W64_EMULATE=1` on llvmpipe, the `ms_*` tests fail. Measured with Mesa 26.3.1: on x86_64 at 256-bit vectors (the default there) all six abort with "LLVM ERROR: Cannot emit physreg copy instruction"; on arm64 (128-bit) `ms_tri` draws nothing (the frame stays at the clear color); on x86_64 at 128 bits (`LP_NATIVE_VECTOR_WIDTH=128`) `ms_tri` happened to draw correctly, but with Mesa 26.0.3 `ms_wave_info_s3` and `ms_v5_workgroup` draw nothing there (4 of 6 pass, [measured by @TreXito](https://github.com/ItsNotRexed/Leonida/pull/3838#issuecomment-7086696759)) ([measured runs](https://github.com/ItsNotRexed/Leonida/issues/3127#issuecomment-7086469679)) | An llvmpipe bug: a mesh shader's runtime `CullPrimitiveEXT` is stored through an invalid `bitcast <N x i1> to <N x float>` (TEST-782, #3127; see "Without a GPU: Mesa llvmpipe") | Run the mesh tests on llvmpipe without `LEONIDA_W64_EMULATE`, or add `LEONIDA_MESH_COMPUTE=1` (the compute fallback): with it all 6 `ms_*` pass emulated on llvmpipe (x86_64, 256-bit) | |
diff --git a/runtime/test/shaders/cs_fma_fused.asm b/runtime/test/shaders/cs_fma_fused.asm index 31282c47a..198ca6137 100644 --- a/runtime/test/shaders/cs_fma_fused.asm +++ b/runtime/test/shaders/cs_fma_fused.asm | |||
| @@ -21,6 +21,10 @@ | |||
| 21 | ; --fma glsl and --fma khr. | 21 | ; --fma glsl and --fma khr. |
| 22 | ; The operands come in through s4..s7 (VKC_SGPRS=0x3f800800,0x3f800001,0x3f7ffffe,0xbf800002: a, 1+2^-23, 1-2^-23, | 22 | ; The operands come in through s4..s7 (VKC_SGPRS=0x3f800800,0x3f800001,0x3f7ffffe,0xbf800002: a, 1+2^-23, 1-2^-23, |
| 23 | ; -(1+2^-22)), so a driver cannot fold the multiply-adds at compile time; only the fmaak/fmamk constants are literals. | 23 | ; -(1+2^-22)), so a driver cannot fold the multiply-adds at compile time; only the fmaak/fmamk constants are literals. |
| 24 | ; The f16 triple is a XOR a literal each (0x3f800800 ^ 0x3f8064e1 = 0x6ce1, ^ 0x3f804a00 = 0x4200, ^ 0x3f808804 = | ||
| 25 | ; 0x8004) for the same reason (TEST-FMA-FUSED-F16-RUNTIME). As three literals the whole f16 multiply-add was a | ||
| 26 | ; compile-time constant, and MoltenVK 1.4.2 gave the wrong one: word 7 read 0x0000 on an Apple M5 (0x3c00 in an | ||
| 27 | ; earlier report), while the same translation with any one of the three taken at run time gave 0x7351. | ||
| 24 | ; Run: VKC_SGPRS=0x3f800800,0x3f800001,0x3f7ffffe,0xbf800002 vk_compute_test cs_fma_fused.spv 1 dump | 28 | ; Run: VKC_SGPRS=0x3f800800,0x3f800001,0x3f7ffffe,0xbf800002 vk_compute_test cs_fma_fused.spv 1 dump |
| 25 | ; out[0..7] = 33800000 a8800000 3a000400 33800000 33800000 33800000 28800000 00007351 (hex) | 29 | ; out[0..7] = 33800000 a8800000 3a000400 33800000 33800000 33800000 28800000 00007351 (hex) |
| 26 | ; group: 1,1,1 | 30 | ; group: 1,1,1 |
| @@ -47,9 +51,9 @@ buffer_store_dword v12, v20, s[0:3], 0 offen offset:20 | |||
| 47 | v_mov_b32_e32 v13, s7 | 51 | v_mov_b32_e32 v13, s7 |
| 48 | v_fma_f32 v14, v5, v5, v13 | 52 | v_fma_f32 v14, v5, v5, v13 |
| 49 | buffer_store_dword v14, v20, s[0:3], 0 offen offset:24 | 53 | buffer_store_dword v14, v20, s[0:3], 0 offen offset:24 |
| 50 | v_mov_b32_e32 v15, 0x6ce1 | 54 | v_xor_b32_e32 v15, 0x3f8064e1, v0 |
| 51 | v_mov_b32_e32 v16, 0x4200 | 55 | v_xor_b32_e32 v16, 0x3f804a00, v0 |
| 52 | v_mov_b32_e32 v17, 0x8004 | 56 | v_xor_b32_e32 v17, 0x3f808804, v0 |
| 53 | v_fma_f16 v18, v15, v16, v17 | 57 | v_fma_f16 v18, v15, v16, v17 |
| 54 | v_and_b32_e32 v18, 0xffff, v18 | 58 | v_and_b32_e32 v18, 0xffff, v18 |
| 55 | buffer_store_dword v18, v20, s[0:3], 0 offen offset:28 | 59 | buffer_store_dword v18, v20, s[0:3], 0 offen offset:28 |
diff --git a/tests/semantic/README.md b/tests/semantic/README.md index 5360ab244..e53b0d3a4 100644 --- a/tests/semantic/README.md +++ b/tests/semantic/README.md | |||
| @@ -176,6 +176,10 @@ instruction family per file; the list below describes some of them, not every fi | |||
| 176 | modifiers on `v_cvt_f32_ubyte*`/`u32`/`f16` and `v_rcp_iflag_f32`), found by `tools/f32_fuzz.py` (FUZZ-3), and | 176 | modifiers on `v_cvt_f32_ubyte*`/`u32`/`f16` and `v_rcp_iflag_f32`), found by `tools/f32_fuzz.py` (FUZZ-3), and |
| 177 | tests the interpreter's float operations against GLSL.std.450 (signed zeros, undefined inputs, a single-rounding | 177 | tests the interpreter's float operations against GLSL.std.450 (signed zeros, undefined inputs, a single-rounding |
| 178 | `Fma`). | 178 | `Fma`). |
| 179 | - `test_fma_fused_fixture.py` (TEST-FMA-FUSED-F16-RUNTIME) runs the GPU test `cs_fma_fused` in the interpreter with | ||
| 180 | the four operand words of its header and expects the header's eight words, and checks that every register one of | ||
| 181 | its multiply-adds reads comes from `s4..s7`: with three literal operands the f16 one was a compile-time constant, | ||
| 182 | which MoltenVK 1.4.2 gets wrong (`docs/testing.md`, Known problems). | ||
| 179 | - `test_fixtures.py` checks `tests/semantic/fixtures/disasm/*.s` equals what `regen_fixtures.py` makes from | 183 | - `test_fixtures.py` checks `tests/semantic/fixtures/disasm/*.s` equals what `regen_fixtures.py` makes from |
| 180 | `runtime/test/shaders/*.asm` (`python tests/semantic/regen_fixtures.py` rewrites them). | 184 | `runtime/test/shaders/*.asm` (`python tests/semantic/regen_fixtures.py` rewrites them). |
| 181 | - `test_dpp_eager_subgroup.py` (SHADER-529) checks that no DPP ballot or shuffle is left where SPIRV-Cross | 185 | - `test_dpp_eager_subgroup.py` (SHADER-529) checks that no DPP ballot or shuffle is left where SPIRV-Cross |
diff --git a/tests/semantic/test_fma_fused_fixture.py b/tests/semantic/test_fma_fused_fixture.py new file mode 100644 index 000000000..901f0109b --- /dev/null +++ b/tests/semantic/test_fma_fused_fixture.py | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | """runtime/test/shaders/cs_fma_fused.asm: its eight words, and no multiply-add a compiler can fold | ||
| 2 | (TEST-FMA-FUSED-F16-RUNTIME). | ||
| 3 | |||
| 4 | The fixture takes the operands of its f32 multiply-adds from s4..s7, so that a driver has to compute them at run | ||
| 5 | time. The three operands of its v_fma_f16 were literals. That made the whole f16 multiply-add a compile-time | ||
| 6 | constant, and MoltenVK 1.4.2 gave the wrong one: word 7 read 0x0000 on an Apple M5 (0x3c00 in an earlier report) | ||
| 7 | instead of 0x7351, the cs_fma_fused row of docs/testing.md, while the same translation with any one of the three | ||
| 8 | operands taken at run time gave 0x7351. The triple is now s4 XOR a literal each. | ||
| 9 | |||
| 10 | Checked here, without a GPU: the translated fixture gives the eight words of its header in the interpreter (so the | ||
| 11 | new operands are the triple the header derives 0x7351 from), and every register a multiply-add reads comes from | ||
| 12 | s4..s7, so a literal operand cannot come back unnoticed. What a driver does with the module is the GPU test's | ||
| 13 | business (runtime/test/shaders/check.py, scripts/run_gpu_tests.py). | ||
| 14 | |||
| 15 | Standard library only; no GPU, no LLVM, no SPIRV-Tools, no game files. | ||
| 16 | """ | ||
| 17 | import os | ||
| 18 | import re | ||
| 19 | import struct | ||
| 20 | import sys | ||
| 21 | import unittest | ||
| 22 | |||
| 23 | HERE = os.path.dirname(os.path.abspath(__file__)) | ||
| 24 | sys.path.insert(0, HERE) | ||
| 25 | |||
| 26 | import support # noqa: E402 (puts tools/ on the path) | ||
| 27 | import spirv_mini_interp as si # noqa: E402 | ||
| 28 | |||
| 29 | FIXTURE = os.path.join(HERE, "..", "..", "runtime", "test", "shaders", "cs_fma_fused.asm") | ||
| 30 | VSHARP = [0, 0, 0x1000, 0x31016FAC] # s[0:3], the output buffer | ||
| 31 | REG = re.compile(r"(?<![\w.])([vs])(\d+)\b") | ||
| 32 | |||
| 33 | |||
| 34 | def read_fixture(): | ||
| 35 | """(instruction lines, {header field: text}) of the fixture.""" | ||
| 36 | with open(FIXTURE, encoding="utf-8") as f: | ||
| 37 | text = f.read() | ||
| 38 | header = dict(re.findall(r"^; (group|rsrc2|Run|out\[0\.\.7\]):? =? ?(.+)$", text, re.M)) | ||
| 39 | lines = [ln.strip() for ln in text.splitlines() if ln.strip() and not ln.lstrip().startswith(";")] | ||
| 40 | return lines, header | ||
| 41 | |||
| 42 | |||
| 43 | def run_time_registers(lines): | ||
| 44 | """{line index: the VGPR sources of a multiply-add that are not run-time values}, by a pass over the listing: a | ||
| 45 | VGPR is a run-time value when the instruction that wrote it read s4..s7 or another run-time VGPR.""" | ||
| 46 | run_time, literal_only = set(), {} | ||
| 47 | for i, line in enumerate(lines): | ||
| 48 | op, _, rest = line.partition(" ") | ||
| 49 | regs = REG.findall(rest) | ||
| 50 | if not op.startswith("v_") or not regs or regs[0][0] != "v": | ||
| 51 | continue # stores and the like write no VGPR | ||
| 52 | dst, sources = int(regs[0][1]), regs[1:] | ||
| 53 | if op.startswith(("v_fma", "v_fmac")): | ||
| 54 | if op.startswith("v_fmac"): | ||
| 55 | sources = regs # d += a * b reads its destination as well | ||
| 56 | literal_only[i] = [f"v{n}" for kind, n in sources if kind == "v" and int(n) not in run_time] | ||
| 57 | fed = any(kind == "s" and 4 <= int(n) <= 7 or kind == "v" and int(n) in run_time for kind, n in sources) | ||
| 58 | (run_time.add if fed else run_time.discard)(dst) | ||
| 59 | return literal_only | ||
| 60 | |||
| 61 | |||
| 62 | class FmaFusedFixture(unittest.TestCase): | ||
| 63 | @classmethod | ||
| 64 | def setUpClass(cls): | ||
| 65 | cls.lines, cls.header = read_fixture() | ||
| 66 | |||
| 67 | def test_the_eight_words_of_the_header(self): | ||
| 68 | operands = [int(w, 16) for w in re.search(r"VKC_SGPRS=([0-9a-fx,]+)", self.header["Run"]).group(1).split(",")] | ||
| 69 | want = [int(w, 16) for w in self.header["out[0..7]"].split("(")[0].split()] | ||
| 70 | self.assertEqual((len(operands), len(want), want[7]), (4, 8, 0x7351)) | ||
| 71 | group = [int(v) for v in self.header["group"].split(",")] | ||
| 72 | fssm = bytearray(0x148) | ||
| 73 | struct.pack_into("<4I", fssm, 0x138, int(self.header["rsrc2"], 0), *group) | ||
| 74 | spv = support.translate_lines("cs_fma_fused", self.lines, fssm=bytes(fssm)) | ||
| 75 | memory = si.run_workgroup( | ||
| 76 | spv, 1, 32, inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i}, | ||
| 77 | push_constants=VSHARP + operands + [0] * 24, memory={}) | ||
| 78 | self.assertEqual([memory.get(4 * k) for k in range(8)], want) | ||
| 79 | |||
| 80 | def test_every_multiply_add_reads_run_time_registers_only(self): | ||
| 81 | literal_only = run_time_registers(self.lines) | ||
| 82 | self.assertEqual(len(literal_only), 8) # seven f32 forms and the f16 one | ||
| 83 | for i, regs in literal_only.items(): | ||
| 84 | self.assertEqual(regs, [], f"`{self.lines[i]}` reads {regs}, which only literals fed: a compiler can " | ||
| 85 | "fold it, and MoltenVK 1.4.2 folds the f16 form to the wrong word") | ||
| 86 | |||
| 87 | def test_the_pass_sees_a_literal_operand(self): | ||
| 88 | # The f16 lines as they were: three literal moves in front of the multiply-add. | ||
| 89 | old = ["v_mov_b32_e32 v0, s4", "v_mov_b32_e32 v15, 0x6ce1", "v_mov_b32_e32 v16, 0x4200", | ||
| 90 | "v_mov_b32_e32 v17, 0x8004", "v_fma_f16 v18, v15, v16, v17", "v_fma_f32 v4, v0, v0, -1.0", | ||
| 91 | "v_mov_b32_e32 v0, 0", "v_fmac_f32_e32 v4, v0, v0"] | ||
| 92 | self.assertEqual(run_time_registers(old), {4: ["v15", "v16", "v17"], 5: [], 7: ["v0", "v0"]}) | ||
| 93 | |||
| 94 | |||
| 95 | if __name__ == "__main__": | ||
| 96 | unittest.main() | ||