diff options
Diffstat (limited to 'docs/shader_validation.md')
| -rw-r--r-- | docs/shader_validation.md | 138 |
1 files changed, 131 insertions, 7 deletions
diff --git a/docs/shader_validation.md b/docs/shader_validation.md index d6399d74f..ddaafe738 100644 --- a/docs/shader_validation.md +++ b/docs/shader_validation.md | |||
| @@ -13,9 +13,11 @@ harness itself). The GPU-side runners are described below: `tools/valu_fuzz.py` | |||
| 13 | check that the module computes what the RDNA2 program computes. Those are different questions, and | 13 | check that the module computes what the RDNA2 program computes. Those are different questions, and |
| 14 | this repository already has cases where only the second question had a useful answer: | 14 | this repository already has cases where only the second question had a useful answer: |
| 15 | 15 | ||
| 16 | - All 26,503 shaders translate and pass `spirv-val` (Vulkan 1.2), yet the SDWA source/destination | 16 | - All 26,503 shaders translate and pass `spirv-val` (Vulkan 1.2) at `43d4703`, after `dcd8ecf` fixed |
| 17 | selects were silently ignored until 2026-11-20. That passed validation for 3,826 of the 13,926 | 17 | the ID-bound regression (#881); the intervening `dedd75c` run was 26,501, and `793b06f` before |
| 18 | translated shaders and produced wrong values (`docs/design.md`, "SDWA"). | 18 | #223's LLVM 23 source-field fix was 25,032. Yet the SDWA source/destination selects were silently |
| 19 | ignored until 2026-11-20: validation passed for 3,826 of the 13,926 shaders translated at the time | ||
| 20 | and produced wrong values (`docs/design.md`, "SDWA"). | ||
| 19 | - The compressed-export bug was found by a GPU test, not by validation: LLVM prints `v4, v4, v5, v5` | 21 | - The compressed-export bug was found by a GPU test, not by validation: LLVM prints `v4, v4, v5, v5` |
| 20 | for a compressed pair, and the translated shader exported `(r, g, r, g)`. | 22 | for a compressed pair, and the translated shader exported `(r, g, r, g)`. |
| 21 | - `v_cvt_pkrtz_f16_f32` with round-to-nearest overflowed a finite value to infinity; the GPU smoke | 23 | - `v_cvt_pkrtz_f16_f32` with round-to-nearest overflowed a finite value to infinity; the GPU smoke |
| @@ -255,6 +257,40 @@ RDNA4 (Radeon RX 9070, gfx1201, driver 26.11.2, `rocm-sdk-core` 10.0.0 clang): 1 | |||
| 255 | 500, 2000 each, and seed 500 with `--bfe-any`) gave 0 mismatches, so gfx1201 follows the same `s_bfe` rule. With | 257 | 500, 2000 each, and seed 500 with `--bfe-any`) gave 0 mismatches, so gfx1201 follows the same `s_bfe` rule. With |
| 256 | `--oracle translator,hw`, 200 of 200 programs matched (seed 1). | 258 | `--oracle translator,hw`, 200 of 200 programs matched (seed 1). |
| 257 | 259 | ||
| 260 | #### RDNA2 integer rows (`tools/int_hw_probe.py`, SHADER-137) | ||
| 261 | |||
| 262 | `tools/int_hw_probe.py` runs 87 hand-written cases across 19 integer groups (both wave32 and wave64) natively on | ||
| 263 | an AMD GPU through `tools/hw_oracle` and compares every output word against `tools/rdna2spirv.py` + | ||
| 264 | `tests/semantic/spirv_mini_interp.py` (with scalar ops run twice, SCC preset to 0 and 1, so SCC written vs kept is | ||
| 265 | observed directly). | ||
| 266 | |||
| 267 | Measured on an **AMD Radeon 680M (gfx1035, RDNA2)**, Windows 11 (`10.0.26300`), AMD driver `26.5.1` | ||
| 268 | (`32.0.21043.7012`, `amdhip64_6.dll`), conda-forge `clang 22.1.8` + `lld 23.1.6` (`--oracle both --wave both`): | ||
| 269 | **87 cases: 82 bit-exact matches, 5 known live translator divergences, 0 unexpected mismatches, 0 wave32/wave64 | ||
| 270 | differences** (plus `tools/salu_diff.py --oracle hw,translator --bfe-any --programs 500 --seed 1`: 500/500 match). | ||
| 271 | |||
| 272 | - **Matched RDNA2 silicon in both wave32 and wave64 (82/87):** `s_absdiff_i32`, `s_mul_hi_i32/u32`, `s_lshl1..4_add_u32` | ||
| 273 | (64-bit shift-out and add carry into SCC), `s_sext_i32_i8/i16`, `s_flbit_i32`, `s_cmov_b32`, `s_cmovk_i32`, | ||
| 274 | `s_pack_ll/lh/hh_b32_b16`, `s_bfe_u32/i32` past bit 31, `s_brev_b64`, `s_bfe_u64/i64`, `s_bitset0/1_b64`, | ||
| 275 | `s_flbit_i32_b64/i64`, `s_quadmask_b32/b64`, `s_bitreplicate_b64_b32`, all nine 64-bit scalar bitwise ops on scalar | ||
| 276 | data, `s_movrels_b32/b64`, `v_add/sub/subrev_nc_u32` / `v_add/sub_nc_i32` / `v_add/sub_nc_u16` with `clamp` and | ||
| 277 | `op_sel`, `v_mad_u64_u32` / `v_mad_i64_i32` (`sdst` bit 64 sign of exact 65-bit sum and inactive-EXEC masking), | ||
| 278 | `v_sad_u8`/`hi_u8`/`u16`/`u32`, `v_msad_u8`, `v_lerp_u8`, `v_qsad_pk_u16_u8`, `v_mqsad_pk_u16_u8`, `v_mqsad_u32_u8`, | ||
| 279 | `v_mul_hi_u32_u24`/`i32_i24`, `v_lshrrev_b64`/`v_ashrrev_i64`, `v_mad_u16`/`i16` (`clamp`, `op_sel`, untouched half | ||
| 280 | kept), `v_cvt_f16_u16/i16` and `v_cvt_u16/i16_f16` (`_e64` keeps high half; NaN → 0, ±inf saturates; note GFX10 | ||
| 281 | does not accept `op_sel` on these opcodes), `v_cvt_i32_f32` (control: all ±qNaN/±sNaN → 0), and | ||
| 282 | `v_cvt_pknorm_i16/u16_f32` (±qNaN → 0, ±inf saturation, RNE ties at ±0.5). | ||
| 283 | - **Live translator divergences confirmed on gfx1035 (5/87, pinned in `KNOWN_DIVERGENCES`):** | ||
| 284 | 1. **`v_add_co_u32`, `v_sub_co_u32`, `v_subrev_co_u32`, `v_add_co_ci_u32`, `v_sub_co_ci_u32`, `v_subrev_co_ci_u32` | ||
| 285 | with `clamp`:** hardware saturates `D` (`0xffffffff` on carry, `0x00000000` on borrow) while still reporting the | ||
| 286 | real carry/borrow (`1`) in `sdst`; the translator currently ignores `clamp` on these ops. | ||
| 287 | 2. **`v_cvt_rpi_i32_f32` precision:** hardware computes exact `floor(x + 0.5)` without binary32 rounding of `x + 0.5` | ||
| 288 | (`0x3effffff` → `0`, `0x4b000001` → `0x00800001`, `0x4b7fffff` → `0x00ffffff`), whereas the translator rounds | ||
| 289 | `x + 0.5` in f32 first (`1`, `0x00800002`, `0x01000000`). | ||
| 290 | 3. **`v_cvt_flr_i32_f32` / `v_cvt_rpi_i32_f32` on NaN:** hardware saturates by the NaN's sign bit (`0x7fc00000` → | ||
| 291 | `0x7fffffff`, `0xffc00000` → `0x80000000`), whereas `v_cvt_i32_f32` gives `0` for every NaN; the translator gives | ||
| 292 | `0` for both `flr` and `rpi`. | ||
| 293 | |||
| 258 | Two assembler facts matter when writing edge cases: | 294 | Two assembler facts matter when writing edge cases: |
| 259 | 295 | ||
| 260 | - An integer token in a float-operand position uses AMD's inline-constant encoding for values 0..64 | 296 | - An integer token in a float-operand position uses AMD's inline-constant encoding for values 0..64 |
| @@ -826,6 +862,22 @@ denormal, 2,810 other not judged. The four MODE settings read back in the kernel | |||
| 826 | `0x3c0`, so the float mode of the kernel descriptor reaches the hardware through that launcher as well. | 862 | `0x3c0`, so the float mode of the kernel descriptor reaches the hardware through that launcher as well. |
| 827 | `--negative-control` catches all six wrong models there (3 to 25 mismatches each). | 863 | `--negative-control` catches all six wrong models there (3 to 25 mismatches each). |
| 828 | 864 | ||
| 865 | The same seeds through the Windows HIP launcher (`hw_oracle.c`) on the integrated GPU of a Ryzen 7 9800X3D (gfx1036; | ||
| 866 | Windows 10 22H2, AMD driver 32.0.13036.4, `amdhip64_6.dll`, AMD clang 23.0.0git from rocm-sdk-core 10.0.0, main at | ||
| 867 | 41f44826) give the same counts in every column: 38,858 programs, **28,671 match, 0 mismatch, 56 known deviation** | ||
| 868 | (all `omod_neg_zero`), 4,224 skipped, 3,097 denormal, 2,810 other. `--negative-control` again catches all six (3 to | ||
| 869 | 25 mismatches). Two launchers on two machines and two operating systems agree on the hardware words. | ||
| 870 | |||
| 871 | `v_rcp_iflag_f32` (SHADER-136; added to the rcp / rsq / sqrt / exp / log family after the run above, so it is not in | ||
| 872 | its counts) on the same gfx1036 through the Windows launcher, `--ops v_rcp_iflag_f32 --programs 1000 --probes 24`, | ||
| 873 | seeds 1 to 6: 6,552 programs, **5,971 match, 0 mismatch, 35 known deviation** (`omod_neg_zero`), 279 denormal, 267 | ||
| 874 | other. It shares `v_rcp_f32`'s model, and the clamp and omod bits behave as on the other ops (summed over the six | ||
| 875 | seeds, per MODE setting flush / keep / no_dx10 / ieee): clamp makes a -0 result +0 in every mode (98 each) and a NaN | ||
| 876 | result +0 except with DX10_CLAMP off, where the NaN passes (169); omod is applied with IEEE off, including with f32 | ||
| 877 | denormals kept (461 each; like the other transcendental ops), and ignored with IEEE on (461); a -0 result becomes +0 | ||
| 878 | under omod (35 each, none with IEEE on); a signalling NaN passes unchanged with IEEE off and is quieted with IEEE on | ||
| 879 | (59); denormal operands read as zero and denormal results flush in every mode. | ||
| 880 | |||
| 829 | What the hardware answered where the model does not judge (gfx1150; the RDNA2 run above agrees where it is cited): | 881 | What the hardware answered where the model does not judge (gfx1150; the RDNA2 run above agrees where it is cited): |
| 830 | 882 | ||
| 831 | - **No model error found.** The model was right wherever it had an answer, in 30,577 programs; the min/max/med3 | 883 | - **No model error found.** The model was right wherever it had an answer, in 30,577 programs; the min/max/med3 |
| @@ -962,18 +1014,90 @@ side follows the same rule. Exit 1 means they differ, or a side follows neither | |||
| 962 | is unavailable. | 1014 | is unavailable. |
| 963 | 1015 | ||
| 964 | `tests/test_fma_mix_probe.py` checks the expected words against a derivation written out by hand. It also checks that | 1016 | `tests/test_fma_mix_probe.py` checks the expected words against a derivation written out by hand. It also checks that |
| 965 | the translator follows the double rule today, and that the compute shader gives the pixel shader's words on the | 1017 | the translator follows the single rounding rule, and that the compute shader gives the pixel shader's words on the |
| 966 | interpreter. If the hardware rounds once, the fix is to use `Gen.fma_mix_odd` in nearest mode as well. Round-to-odd at | 1018 | interpreter. Hardware measurement on AMD RDNA2 confirmed that the hardware rounds once, straight to f16, so the translator |
| 967 | f32 then narrows exactly. With that change the translator gives the single-rounded word on all seven inputs. | 1019 | uses `Gen.fma_mix_odd` in nearest mode as well. Round-to-odd at f32 then narrows exactly, giving the single-rounded word on |
| 1020 | all seven discriminating inputs. | ||
| 968 | 1021 | ||
| 969 | Results: | 1022 | Results: |
| 970 | 1023 | ||
| 971 | | GPU | Driver | `translator` | `vulkan` | `hw` | | 1024 | | GPU | Driver | `translator` | `vulkan` | `hw` | |
| 972 | |---|---|---|---|---| | 1025 | |---|---|---|---|---| |
| 973 | | NVIDIA GeForce RTX 5060 (Blackwell), Windows 11 | 621.24 | double | double, 8 of 8 words | not AMD | | 1026 | | NVIDIA GeForce RTX 5060 (Blackwell), Windows 11 | 621.24 | double | double, 8 of 8 words | not AMD | |
| 1027 | | AMD Radeon iGPU (RDNA2, gfx1036), Windows 11, HIP backend | 32.0.21044.64 | double | not run | **single**, 7 of 7 ties; control `0x4100` | | ||
| 974 | 1028 | ||
| 975 | The `hw` column is still empty: the question needs an RDNA GPU. On the RTX 5060, a translator copy changed to use | 1029 | RDNA2 rounds once. Every tie input gives the single-rounded word (A `0x3c01`, B `0x3c01`, C `0xbc01`, D `0x0001`, |
| 1030 | E `0x7bff`, F `0x3c01`, G `0x3c01`), so the translator is one f16 ulp off on exactly these inputs (SHADER-135; the | ||
| 1031 | translator is not changed here). On the RTX 5060, a translator copy changed to use | ||
| 976 | `fma_mix_odd` in nearest mode gave the single-rounded word for all seven inputs on the `vulkan` side as well. | 1032 | `fma_mix_odd` in nearest mode gave the single-rounded word for all seven inputs on the `vulkan` side as well. |
| 1033 | | NVIDIA GeForce RTX 5060 (Blackwell), Windows 11 | 621.24 | single | single, 8 of 8 words | not AMD | | ||
| 1034 | | AMD Radeon gfx1036 (RDNA2), Linux / Windows | | single | single, 8 of 8 words | single, 8 of 8 words | | ||
| 1035 | |||
| 1036 | Measurement on AMD RDNA2 (gfx1036) confirmed single rounding across all 7 discriminating cases (finding 7079901079). | ||
| 1037 | Using `fma_mix_odd` produces the exact hardware single-rounded words in both nearest-even and toward-zero modes. | ||
| 1038 | |||
| 1039 | ### f16 output modifiers, clamp and `v_fma_mix*` operands on RDNA2 (SHADER-135) | ||
| 1040 | |||
| 1041 | *Measured on an RDNA2 iGPU (gfx1036), AMD Windows driver 32.0.21044.64, HIP backend, AMD clang 23.0.0git, `main` | ||
| 1042 | a0acd33d.* Raw kernels written by hand, one lane each. The MODE comes from the kernel descriptor (`ieee`, | ||
| 1043 | `dx10_clamp`, `denorm_mode_32`, `denorm_mode_16_64`). Every program writes one word, starting from a destination of | ||
| 1044 | `0x5a5a5a5a`, so a half the op must not write shows up. 1,385 programs assembled, each run in wave32 and wave64, and | ||
| 1045 | the two wave sizes agree on every word. `tools/fma_mix_probe.py --oracle hw` (the section above) and | ||
| 1046 | `tools/f16_trans_sweep.py --ops v_exp_f16,v_log_f16` ran on the same GPU. | ||
| 1047 | |||
| 1048 | - **An omod on an f16 result acts only with IEEE 0 and f16 denormals flushed (`denorm_mode_16_64 = 0`).** In that | ||
| 1049 | MODE `mul:2`, `mul:4` and `div:2` scale the results of `v_add_f16`, `v_mul_f16`, `v_fma_f16`, `v_fmac_f16_e64`, | ||
| 1050 | `v_cvt_f16_f32_e64`, `v_exp_f16_e64` and `v_rcp_f16_e64` (`v_fmac_f16_e64` 0.75 · 1 + -0 `mul:4` gives `0x4200`). | ||
| 1051 | With `denorm_mode_16_64 = 3` or IEEE 1 they leave the result as it is (`0x3a00`), and `denorm_mode_32` makes no | ||
| 1052 | difference. The f32 control (`v_add_f32_e64 mul:2`) follows `denorm_mode_32` instead, as on gfx1150 above. Unlike | ||
| 1053 | the f32 transcendentals there, `v_exp_f16` and `v_rcp_f16` obey the same rule as the ALU ops. When the omod acts, a | ||
| 1054 | -0 result becomes +0 (`v_fmac_f16` -0 · 1 + -0 `mul:2` gives `0x0000`), and it comes before the clamp | ||
| 1055 | (`v_cvt_f16_f32_e64` 0.75 `clamp mul:2` gives `0x3c00`, not `0x3e00`). | ||
| 1056 | - **clamp** on `v_cvt_f16_f32_e64`, `v_fmac_f16_e64`, `v_fma_mixlo_f16` and `v_fma_mix_f32` clamps to [0, 1] in every | ||
| 1057 | MODE: -0.5 and -0 give +0, 3.0 and an overflow (1e5, 65504 · 2) give 1.0. A NaN gives +0 with DX10_CLAMP 1. With | ||
| 1058 | DX10_CLAMP 0 it stays the NaN the op gives without clamp. This is SHADER-186's f32 rule, here for the f16 results and | ||
| 1059 | the mix ops. | ||
| 1060 | - **Constants in `v_fma_mix*` operands.** A float inline constant is the f32 constant where `op_sel_hi` makes the | ||
| 1061 | operand f32, and the f16 constant with a zero high half where it makes it f16. `1.0`, `-1.0`, `0.5`, `2.0` and | ||
| 1062 | `-4.0` give their value either way. `0.15915494` gives `0x3e22f983` as f32 and `0x3118` as f16 (`0x3e230000` once | ||
| 1063 | widened). With `op_sel_hi = 1` and `op_sel = 1`, every float constant reads +0.0. An integer inline constant is its | ||
| 1064 | 32-bit integer. As an f32 operand, `1` and `64` are f32 denormals (+0 with `denorm_mode_32 = 0`) and `-1` is the NaN | ||
| 1065 | `0xffffffff`. As an f16 operand, `op_sel` picks a half: `1` gives `0x0001` low and +0 high, and `-1` gives `0xffff` | ||
| 1066 | in both. `op_sel` is ignored for an f32 operand. For gfx10, llvm-mc rejects a 32-bit literal in these ops ("invalid | ||
| 1067 | operand"). It encodes a literal equal to an f16 inline constant, such as `0x3c00`, as that constant (`0xf2`). | ||
| 1068 | - **Mix-op denormals follow the mode of their own width.** An f16 operand (`op_sel_hi = 1`) and an f16 result | ||
| 1069 | (`mixlo`/`mixhi`) become a zero of their sign with `denorm_mode_16_64 = 0`, and are kept with 3, whatever | ||
| 1070 | `denorm_mode_32` says. An f32 denormal is flushed with `denorm_mode_32 = 0` and kept with 3. | ||
| 1071 | - **Mix-op NaNs.** An f16 NaN operand widens with its payload (`0x7c01` → `0x7f802000`, `0xfe01` → `0xffc02000`). | ||
| 1072 | `v_fma_mix_f32` keeps a signalling NaN, f16 or f32, with IEEE 0, and quiets it with IEEE 1, payload kept | ||
| 1073 | (`0x7f800001` → `0x7fc00001`). `v_fma_mixlo_f16` quiets every NaN in every MODE and keeps the top payload bits | ||
| 1074 | (`0x7c01` → `0x7e01`, `0x7d55` → `0x7f55`, f32 `0x7fa00000` → `0x7f00`, f32 `0x7f800001` → `0x7e00`). | ||
| 1075 | - **`v_exp_f16` and `v_log_f16` give gfx1031's counts exactly**, on all 65,536 inputs in the five MODEs of the f16 | ||
| 1076 | transcendentals section: 0 ulp on every input that is not a NaN, with f16 denormals kept. With them flushed, 831 | ||
| 1077 | `v_exp_f16` results and 1,023 `v_log_f16` operands become zero. Signalling NaNs are quieted only with IEEE 1, and | ||
| 1078 | wave32 = wave64. | ||
| 1079 | |||
| 1080 | **The translator against these words.** Each program was translated as a pixel shader and run on | ||
| 1081 | `tests/semantic/spirv_mini_interp.py`, then compared with the hardware under the MODE the translator assumes for the | ||
| 1082 | game (IEEE 0, DX10_CLAMP 1, f32 denormals flushed, f16 denormals kept). It gives the same word for the float | ||
| 1083 | constants in every `op_sel`/`op_sel_hi` combination, for the integer constants as f16 halves, for clamp on values that | ||
| 1084 | are not NaN, and for the f16 denormals. It differs in five ways, none fixed here: | ||
| 1085 | |||
| 1086 | 1. **f16 omod.** The translator applies `mul:2`/`mul:4`/`div:2` to every f16 result. The hardware ignores them in this | ||
| 1087 | MODE (24 words: `v_add/mul/fma/fmac/exp/rcp_f16`). `v_cvt_f16_f32` drops its omod and clamp today, so its omod | ||
| 1088 | matches by accident; #718 would make it apply. The two assumptions can't both hold for the game. Either its shaders | ||
| 1089 | run with f16 denormals flushed, and keeping them is wrong, or every f16 omod in them does nothing. A corpus count | ||
| 1090 | of omod on f16 instructions would decide, since a compiler that emits one expects it to act (LLVM folds an omod only | ||
| 1091 | when "output denormals" are off, the `SIFoldOperands.cpp` comment quoted above). That count needs the game files. | ||
| 1092 | 2. **clamp of a NaN.** The interpreter refuses `FClamp` with a NaN operand, which GLSL.std.450 leaves undefined. The | ||
| 1093 | hardware gives +0 with DX10_CLAMP 1, for `v_fmac_f16`, `v_fma_mixlo_f16` and `v_fma_mix_f32` alike (SHADER-186). | ||
| 1094 | 3. **Mix-op rounding.** The hardware rounds once (the section above). | ||
| 1095 | 4. **f32 denormal operands of the mix ops.** They are not flushed with `denorm_mode_32 = 0`: `0x00000001`, `0x807fffff` | ||
| 1096 | and the integer constants `1` and `64` used as f32 give the denormal, where the hardware gives a zero of its sign | ||
| 1097 | (compare SHADER-118 for the mad ops). | ||
| 1098 | 5. **NaN payloads.** Where the hardware keeps the payload and, with IEEE 0, the signalling bit, the translator writes | ||
| 1099 | the canonical quiet NaN (`0x7fc00000`, `0x7e00`). For example, `v_fmac_f16` keeps `0x7c01`. This is the "not | ||
| 1100 | modelled yet" note of the f16 transcendentals row in `docs/design.md`, here for these ops too. | ||
| 977 | 1101 | ||
| 978 | ## Coverage plan and cost | 1102 | ## Coverage plan and cost |
| 979 | 1103 | ||