diff --git a/docs/experiments.md b/docs/experiments.md index 8b6fe1c158..19bf986bec 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -6127,11 +6127,11 @@ What a 16-bit result leaves in the high half of its destination is not measured `v_cvt_f32_u32_e64` and `v_cvt_f32_i32_e64` (issue #360, stage 2) lower the same conversion with the e64 clamp modifier, bit 15 of the raw encoding's first dword: the plain e64 spelling emits exactly the e32 lowering, and with `clamp` the result is wrapped in HLSL `saturate(...)`, so the same word pairs and nearest-even references cover both encodings, ties included. omod is explicitly rejected rather than lowered, per the issue's fallback option: LLVM 15.0.7's assembler rejects every probed omod syntax on these forms — `omod:1`, `omod:2`, `omod:3`, `omod(2)`, `mul:2`, and `clamp` combined with `omod:2` — and also on `v_add_f32_e64` and `v_mul_f32_e64`, and the disassembler never prints omod for the probed raw encodings with both 2-bit fields swept over values 1 to 3, so an omod-carrying program cannot round-trip through the assembler. abs and neg on the integer source (`|s44|`, `-s44`) are refused as well; the assembler refuses them too. The clamp draws pair (0, 1), (2, 0xFFFFFFFF) and (0x80000000, 0x7FFFFFFF), expecting min(1.0, max(0.0, .)) of the u32f/i32f reference for each word: rows (0, 0, 1, 1), (1, 1, 1, 0) — 0xFFFFFFFF is 4294967295.0 unsigned, which rounds to 4294967296.0, but -1.0 signed — and (1, 0, 1, 1), where 0x7FFFFFFF rounds to 2147483648.0 before clamping. Two negative controls run on every backend: dropping `clamp` from the program exports u32 of 2 as 2.0 against the clamped 1.0 reference still on disk, and swapping the unsigned and signed conversions under clamp on the 0xFFFFFFFF/0x80000000 pair exchanges 1.0 and 0.0 on every channel. The rounding assumption is unchanged: the clamp rows contain no tie words and every conversion either is exact or clamps to exactly 0.0 or 1.0, so they do not depend on it, while the plain e64 rows keep the stage-1 tie coverage under the recorded nearest-even reference. Binary and draw fixtures additionally verify SGPR, VGPR and literal operands across negative and positive integers. Mutating `v_cvt_f32_i32` to `v_cvt_f32_u32` or dropping `clamp` produces a pixel mismatch. The reproduction commands are unchanged. -`v_cvt_u32_f32_e32` and `v_cvt_i32_f32_e32` convert one binary32 value to a 32-bit word. Only the plain e32 encoding with a VGPR, an SGPR or a hex literal source is lowered, through the shared helpers in `src/ffx_float_to_int.hlsli`, emitted once before `main`; the signed path is restricted to the entry mask; the result word is held as its bit pattern in a `precise float` local, like a `v_mov_b32` literal, and the draws read it back through the integer export. A float export of such a word is translated the same way, but no draw checks it, so whether a float render target keeps subnormal or NaN bit patterns there is untested. Decimal inline constants are refused because `fp_operand` reads them as bit patterns and the f32 reading has not been checked against the ISA. e64 spellings, source modifiers, clamp and omod suffixes, DPP and SDWA variants, `v_cvt_rpi_i32_f32`, `v_cvt_flr_i32_f32` and a pending scalar load are refused. The rule is the one in issue #360, which cites the RDNA 2 ISA; it was not re-checked against the ISA document and not measured on the console (`F32_TRUNC_ASSUMPTION`): results truncate toward zero, NaN gives 0, and out-of-range values saturate (u32: x <= 0 gives 0, x >= 2^32 gives 0xFFFFFFFF; i32: x <= -2^31 gives 0x80000000, x >= 2^31 gives 0x7FFFFFFF). Truncation is assumed to ignore MODE.FP_ROUND. The input is exact in every denormal mode, since a subnormal truncates to 0; result words 0x00000001 to 0x007FFFFF are binary32 subnormal bit patterns in a `precise float`, and the harness compiles with `-denorm preserve`. Whether an FTZ build would flush them is not claimed and not tested, and a later float instruction that reads such a result would follow the pipeline's `FLOAT_MODE`. The draws move literal words with `v_mov_b32_e32`, convert each unsigned and signed, and export through the lossless `R32G32B32A32_UINT` readback. They cover 18 words in 9 pairs: both zeros, 1.5, -2.75, -0.5, -1.0, a quiet NaN and a negative NaN with payload, both infinities, 2^32, 4294967040, 2^31, 2147483520, -2^31, the next binary32 below -2^31 (the issue's -2^31-1 is not representable and rounds to -2^31), the smallest subnormal and 0.99999994; a further draw reads 3.75, -3.75, 1e10 and -1e10 from SGPRs. A program that swaps the two conversions on -0.5 and -1.0 must report a mismatch. Measured on WARP (driver 10.0.26100.9818) and an NVIDIA GeForce RTX 3080 (driver 32.0.16.2124): every row passes on both. Dev-time mutants (not in CI): replacing the unsigned helper's guards with a bare `uint(x)` fails on both WARP and the RTX 3080, so the saturation guards matter. The helpers apply the cast only to an operand that is made NaN-free and clamped to the largest binary32 below 2^32 or 2^31, because an out-of-range or NaN cast is undefined in HLSL and poison in LLVM. NaN fails every guard compare, so it reaches the cast as 0. Dropping that NaN replacement from both helpers fails on both adapters: the NaN rows then export 0x80000000 from the signed conversion instead of 0. The LLVM 15 round trip (`pixel_aot_check.py binary`) runs in the Ubuntu CI job and also passed locally with LLVM 15.0.7 on Windows. Reproduce with `ctest --preset dev --no-tests=error -R '^pixel_aot_'`. +`v_cvt_u32_f32_e32` and `v_cvt_i32_f32_e32` convert one binary32 value to a 32-bit word. Only the plain e32 encoding with a VGPR, an SGPR or a hex literal source is lowered, through the shared helpers in `src/ffx_float_to_int.hlsli`, emitted once before `main`; the signed path is restricted to the entry mask; the result word is held as its bit pattern in a `precise float` local, like a `v_mov_b32` literal, and the draws read it back through the integer export. A float export of such a word is translated the same way, but no draw checks it, so whether a float render target keeps subnormal or NaN bit patterns there is untested. Decimal inline constants are refused because `fp_operand` reads them as bit patterns and the f32 reading has not been checked against the ISA. e64 spellings, source modifiers, clamp and omod suffixes, DPP and SDWA variants and a pending scalar load are refused (`v_cvt_rpi_i32_f32` and `v_cvt_flr_i32_f32` are described below). The rule is the one in issue #360, which cites the RDNA 2 ISA; it was not re-checked against the ISA document and not measured on the console (`F32_TRUNC_ASSUMPTION`): results truncate toward zero, NaN gives 0, and out-of-range values saturate (u32: x <= 0 gives 0, x >= 2^32 gives 0xFFFFFFFF; i32: x <= -2^31 gives 0x80000000, x >= 2^31 gives 0x7FFFFFFF). Truncation is assumed to ignore MODE.FP_ROUND. The input is exact in every denormal mode, since a subnormal truncates to 0; result words 0x00000001 to 0x007FFFFF are binary32 subnormal bit patterns in a `precise float`, and the harness compiles with `-denorm preserve`. Whether an FTZ build would flush them is not claimed and not tested, and a later float instruction that reads such a result would follow the pipeline's `FLOAT_MODE`. The draws move literal words with `v_mov_b32_e32`, convert each unsigned and signed, and export through the lossless `R32G32B32A32_UINT` readback. They cover 18 words in 9 pairs: both zeros, 1.5, -2.75, -0.5, -1.0, a quiet NaN and a negative NaN with payload, both infinities, 2^32, 4294967040, 2^31, 2147483520, -2^31, the next binary32 below -2^31 (the issue's -2^31-1 is not representable and rounds to -2^31), the smallest subnormal and 0.99999994; a further draw reads 3.75, -3.75, 1e10 and -1e10 from SGPRs. A program that swaps the two conversions on -0.5 and -1.0 must report a mismatch. Measured on WARP (driver 10.0.26100.9818) and an NVIDIA GeForce RTX 3080 (driver 32.0.16.2124): every row passes on both. Dev-time mutants (not in CI): replacing the unsigned helper's guards with a bare `uint(x)` fails on both WARP and the RTX 3080, so the saturation guards matter. The helpers apply the cast only to an operand that is made NaN-free and clamped to the largest binary32 below 2^32 or 2^31, because an out-of-range or NaN cast is undefined in HLSL and poison in LLVM. NaN fails every guard compare, so it reaches the cast as 0. Dropping that NaN replacement from both helpers fails on both adapters: the NaN rows then export 0x80000000 from the signed conversion instead of 0. The LLVM 15 round trip (`pixel_aot_check.py binary`) runs in the Ubuntu CI job and also passed locally with LLVM 15.0.7 on Windows. Reproduce with `ctest --preset dev --no-tests=error -R '^pixel_aot_'`. -`v_cvt_u32_f32_e64` and `v_cvt_i32_f32_e64` extend the truncating float-to-word lowering to VOP3 with optional source `abs` and `neg` modifiers (`|src|`, `abs(src)`, `-src`, `neg(src)`, `-|src|`, `-abs(src)`, `neg(abs(src))`, `neg(|src|)`) on VGPR, SGPR or hex literal sources under the entry mask. Source modifiers operate bitwise on the raw binary32 word (`& 0x7fffffffu` for `abs`, then `^ 0x80000000u` for `neg`) before calling the same shared `cvt_u32_f32` and `cvt_i32_f32` helpers from `src/ffx_float_to_int.hlsli`, so plain e64 emits identical HLSL to e32 and NaN/infinity/subnormal inputs avoid FP modifier quirks. `clamp`, `omod`, decimal/float literals, bare `-0x...` hex negation, reversed modifiers (`|-src|`), DPP/SDWA variants, pending scalar/sample reads, WQM mask execution, and `v_cvt_flr_i32_f32` / `v_cvt_rpi_i32_f32` remain rejected. The binary round-trip and lossless `R32G32B32A32_UINT` draws (`check_cvt_float_int_e64_draw`) cover plain e64 parity across all 18 `CVT_FLOAT_WORDS` (SGPR and VGPR), five modifier sets across fractions, saturation endpoints (`-(2^32-256)`, `-2^31`, `-2^32`, `2^31-128`), `±Inf`, `-0.0`, quiet/signaling NaNs and `-min_subnormal`, bitwise single-bit flip negative controls, a plain `u32`/`i32` swap control, and shader-mutation negative controls dropping each `abs` and `neg` modifier individually. Reproduce with `ctest --preset dev --no-tests=error -R '^pixel_aot_cvt_float_int_e64_'`. +`v_cvt_u32_f32_e64` and `v_cvt_i32_f32_e64` extend the truncating float-to-word lowering to VOP3 with optional source `abs` and `neg` modifiers (`|src|`, `abs(src)`, `-src`, `neg(src)`, `-|src|`, `-abs(src)`, `neg(abs(src))`, `neg(|src|)`) on VGPR, SGPR or hex literal sources under the entry mask. Source modifiers operate bitwise on the raw binary32 word (`& 0x7fffffffu` for `abs`, then `^ 0x80000000u` for `neg`) before calling the same shared `cvt_u32_f32` and `cvt_i32_f32` helpers from `src/ffx_float_to_int.hlsli`, so plain e64 emits identical HLSL to e32 and NaN/infinity/subnormal inputs avoid FP modifier quirks. `clamp`, `omod`, decimal/float literals, bare `-0x...` hex negation, reversed modifiers (`|-src|`), DPP/SDWA variants, pending scalar/sample reads and WQM mask execution remain rejected. The binary round-trip and lossless `R32G32B32A32_UINT` draws (`check_cvt_float_int_e64_draw`) cover plain e64 parity across all 18 `CVT_FLOAT_WORDS` (SGPR and VGPR), five modifier sets across fractions, saturation endpoints (`-(2^32-256)`, `-2^31`, `-2^32`, `2^31-128`), `±Inf`, `-0.0`, quiet/signaling NaNs and `-min_subnormal`, bitwise single-bit flip negative controls, a plain `u32`/`i32` swap control, and shader-mutation negative controls dropping each `abs` and `neg` modifier individually. Reproduce with `ctest --preset dev --no-tests=error -R '^pixel_aot_cvt_float_int_e64_'`. -`v_cvt_flr_i32_f32` and `v_cvt_rpi_i32_f32` lower in both VOP1 (`_e32`) and VOP3 (`_e64`, with optional bitwise source `abs` and `neg` modifiers) under the entry mask, through lazy HLSL helpers (`cvt_flr_i32_f32` and `cvt_rpi_i32_f32`), **restricted to inputs statically proven finite and in `[-2^31, 2^31)` after source modifiers** (hex literals and constant-initialized SGPR/VGPR moves). Because the AMD RDNA 2 VOP1 table states only the finite rounding formulas and denormal support for opcodes 12 and 13 without an exceptional-input rule like opcodes 7 and 8, arbitrary runtime inputs (constant-buffer SGPRs, interpolated/sample/computed VGPRs) and any NaN, infinity, or out-of-range constant (including post-modifier overflow such as `|0xcf000000|`) are explicitly rejected, and `tools/scan_unsupported_shaders.py` leaves them unsupported for generic title scans until a native RDNA reference establishes exceptional-input behavior. `cvt_rpi_i32_f32` evaluates `floor(x) + ((x - floor(x)) >= 0.5f ? 1.0f : 0.0f)` rather than naive `floor(x + 0.5f)` so binary32 inputs one ULP below `0.5` (`0x3effffff`) stay `0` and large odd integers (`2^23 + 1 = 8388609.0f`) do not round up to even. `clamp`, `omod`, decimal/float literals, bare `-0x...`, reversed modifiers, DPP/SDWA spellings, pending scalar/sample reads and WQM mask execution remain rejected. Binary round-trip and lossless `R32G32B32A32_UINT` draws (`check_cvt_flr_rpi_draw`) test signed zeros, positive/negative halves and fractions, single-ULP half boundaries (`0x3effffff`, `0xbf000001`, `0xbeffffff`), `8388609.0f`, `-min_subnormal`, in-range i32 bounds (`2^31 - 128`, `-2^31`), post-modifier in-range `neg(0x4f000000)`, bitwise 1-bit flip controls, `flr`/`rpi`/`i32` swap controls, and modifier-drop mutations. Reproduce with `ctest --preset dev --no-tests=error -R '^pixel_aot_cvt_flr_rpi_'`. +`v_cvt_flr_i32_f32` and `v_cvt_rpi_i32_f32` lower in both VOP1 (`_e32`) and VOP3 (`_e64`, with optional bitwise source `abs` and `neg` modifiers) under the entry mask, through lazy HLSL helpers (`cvt_flr_i32_f32` and `cvt_rpi_i32_f32`), for any VGPR, SGPR or hex literal source, including runtime inputs (constant-buffer SGPRs, interpolated, sampled or computed VGPRs). `flr` is `floor(x)` and `rpi` the floor of the exact `x + 0.5`: `cvt_rpi_i32_f32` evaluates `floor(x) + ((x - floor(x)) >= 0.5f ? 1.0f : 0.0f)` rather than naive `floor(x + 0.5f)` so binary32 inputs one ULP below `0.5` (`0x3effffff`) stay `0` and large odd integers (`2^23 + 1 = 8388609.0f`) do not round up to even. The AMD RDNA 2 VOP1 table gives only these finite formulas for opcodes 12 and 13, so exceptional inputs follow native raw-word measurements (issue #1772, which cites an RDNA 2 gfx1036 APU and an RDNA 4 gfx1201 card through inline assembly): a NaN gives `0x7fffffff` when its sign bit is clear and `0x80000000` when it is set, quiet or signalling (unlike `v_cvt_i32_f32`, where a NaN gives 0), and both infinities and out-of-range values saturate to `0x7fffffff` and `0x80000000`. Each helper replaces a NaN word before any float operation, clamps the rounded value to `[-2^31, 2^31 - 128]` (2^31 - 1 is not a binary32) and selects `0x7fffffff` for `x >= 2^31`, so no HLSL cast reads a NaN or an out-of-range value; finite in-range results are unchanged. Before issue #1772 the translator refused runtime inputs and any NaN, infinity or out-of-range constant, and `tools/scan_unsupported_shaders.py` left both opcodes unsupported; the scanner now accepts the same e32 and e64 spellings as `v_cvt_i32_f32`. The same rows were read back natively on an AMD Radeon RX 6600 (gfx1032, driver 32.0.21045.29003, OpenCL 3652.0) with `v_cvt_flr_i32_f32` and `v_cvt_rpi_i32_f32` as inline assembly in an OpenCL kernel, under all four MODE.IEEE and MODE.DX10_CLAMP settings with f32 denormals flushed or kept (MODE read back after each write): every NaN, infinity, out-of-range, half and 2^23 + 1 row matched the table in `CVT_FLR_RPI_EXCEPTIONAL` in all eight modes. Only negative subnormals depend on the mode: `flr` of `0x80000001` and `0x807fffff` gives `0xffffffff` with f32 denormals kept and `0` with them flushed. The draws assume kept denormals (`F32_DENORM_MODE`), like the rest of the harness, and the console's FLOAT_MODE is not measured. None of these adapters is gfx1013. The undefined casts that the old helpers used gave 0 for every NaN and saturated the other rows on both WARP and the RX 6600 (D3D12 draws with a dev-time copy of the old helpers), so before this change the two adapters agreed with each other but not with the native NaN results. `clamp`, `omod`, decimal/float literals, bare `-0x...`, reversed modifiers, DPP/SDWA spellings, pending scalar/sample reads and WQM mask execution remain rejected. Binary round-trip and lossless `R32G32B32A32_UINT` draws (`check_cvt_flr_rpi_draw`) test signed zeros, positive/negative halves and fractions, single-ULP half boundaries (`0x3effffff`, `0xbf000001`, `0xbeffffff`), `8388609.0f`, `-min_subnormal`, in-range i32 bounds (`2^31 - 128`, `-2^31`), post-modifier in-range `neg(0x4f000000)`, bitwise 1-bit flip controls, `flr`/`rpi`/`i32` swap controls, and modifier-drop mutations. The exceptional rows run from the constant buffer, so DXC cannot fold them, in e32 and e64 (24 words each, NaNs of both signs with quiet, signalling and full payloads, both infinities, `±2^31`, `±(2^31 + 256)`, `±2^63`, `±FLT_MAX`, `±0.5`, `0.49999997`, `±(2^23 + 1)` and `2^24 - 1`), through e64 `abs` and `neg` on NaNs and infinities, and as literal constants. Negative controls: a case that expects 0 for NaN, dropping `abs` or `neg` on a NaN source, and three helper mutants (the NaN sign select dropped, the sign select reversed, the `2^31` saturation dropped) must each report a mismatch. Measured on WARP (Microsoft Basic Render Driver, 10.0.26100.9818) and an AMD Radeon RX 6600 (1002:73ff, driver 32.0.21045.29003), Windows 11 build 26300.9974, D3D12 debug layer enabled: all rows pass on both. Reproduce with `ctest --preset dev --no-tests=error -R '^pixel_aot_cvt_flr_rpi_'`. The same binary and draw checks cover `v_trunc_f32` in e32 and e64 forms. The lowerer emits HLSL `trunc`, which rounds toward zero and returns a floating-point value. With two constant sets, the RTX 5080 and WARP draws matched all four channels of 4,096 pixels per draw against the CPU reference. The fixture covers both signs, signed zero, source negation and absolute value, and both clamp bounds. Replacing truncation with floor changed the first pixel on both adapters. Removing the upper clamp changed alpha from 1 to 2 in the first pixel on WARP. The source-built fixture verifies desktop readback with the bindings above. Execution with a title's own pipeline state remains unverified. diff --git a/tests/pixel_aot_check.py b/tests/pixel_aot_check.py index f96bf9d161..6951c3a0bf 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -15706,25 +15706,31 @@ def _check_controls_inline(directory): # Post-modifier in-range control: -(+2^31) = -2^31 is exact in signed i32. translate(with_constants(["v_cvt_flr_i32_f32_e64 v12, neg(0x4f000000)"])) translate(with_constants(["v_cvt_rpi_i32_f32_e64 v12, -|0x4f000000|"])) - # Explicit refusals for unproven runtime inputs and exceptional/out-of-range constants (including after modifiers). - for unproven in ("v_cvt_flr_i32_f32_e32 v12, s44", "v_cvt_rpi_i32_f32_e64 v12, -s44", - "v_cvt_flr_i32_f32_e32 v12, v12", "v_cvt_rpi_i32_f32_e64 v12, |v12|"): - expect_value_error(translate, with_constants([unproven]), "Unproven runtime input") - expect_value_error(translate_int, uncompressed_export(["v_mov_b32_e32 v18, s44", "v_cvt_flr_i32_f32_e32 v12, v18"]), - "Unproven runtime input") - expect_value_error(translate_int, uncompressed_export(["v_mov_b32_e32 v18, 0x3f800000", "v_add_f32_e32 v18, v18, v18", - "v_cvt_rpi_i32_f32_e32 v12, v18"]), - "Unproven runtime input") - for bad_const in ("v_cvt_flr_i32_f32_e32 v12, 0x7fc00000", "v_cvt_rpi_i32_f32_e32 v12, 0xff800001", - "v_cvt_flr_i32_f32_e32 v12, 0x7f800000", "v_cvt_rpi_i32_f32_e32 v12, 0xff800000", - "v_cvt_flr_i32_f32_e32 v12, 0x4f000000", "v_cvt_rpi_i32_f32_e32 v12, 0xcf000001", - "v_cvt_flr_i32_f32_e64 v12, |0xcf000000|", "v_cvt_rpi_i32_f32_e64 v12, neg(0xcf000000)", - "v_cvt_flr_i32_f32_e64 v12, -|0x7f800000|"): - expect_value_error(translate, with_constants([bad_const]), "Unproven exceptional or out-of-range input") - expect_value_error(translate_int, uncompressed_export(["s_mov_b32 s44, 0xcf000000", "v_cvt_flr_i32_f32_e64 v12, |s44|"]), - "Unproven exceptional or out-of-range input") - expect_value_error(translate_int, uncompressed_export(["v_mov_b32_e32 v18, 0x4f000000", "v_cvt_rpi_i32_f32_e32 v12, v18"]), - "Unproven exceptional or out-of-range input") + # Runtime inputs and exceptional or out-of-range constants (also after modifiers) lower through the same helpers, + # which define NaN, infinities and overflow (issue #1772); the raw word reaches the helper unchanged. + for runtime, call in (("v_cvt_flr_i32_f32_e32 v12, s44", "cvt_flr_i32_f32(asuint(params[0].w))"), + ("v_cvt_rpi_i32_f32_e64 v12, -s44", "cvt_rpi_i32_f32((asuint(params[0].w) ^ 0x80000000u))"), + ("v_cvt_flr_i32_f32_e32 v12, v12", "cvt_flr_i32_f32(asuint(sample16.x))"), + ("v_cvt_rpi_i32_f32_e64 v12, |v12|", "cvt_rpi_i32_f32((asuint(sample16.x) & 0x7fffffffu))")): + hlsl = translate(with_constants([runtime])) + assert call in hlsl.split("float4 main", 1)[1], (runtime, hlsl) + assert hlsl.count(f"uint {call.split('(', 1)[0]}(uint bits)") == 1, runtime + for body in (["v_mov_b32_e32 v18, s44", "v_cvt_flr_i32_f32_e32 v12, v18"], + ["v_mov_b32_e32 v18, 0x3f800000", "v_add_f32_e32 v18, v18, v18", "v_cvt_rpi_i32_f32_e32 v12, v18"], + ["s_mov_b32 s44, 0xcf000000", "v_cvt_flr_i32_f32_e64 v12, |s44|"], + ["v_mov_b32_e32 v18, 0x4f000000", "v_cvt_rpi_i32_f32_e32 v12, v18"]): + assert "_i32_f32(" in translate_int(uncompressed_export(body)).split("uint4 main", 1)[1], body + for exceptional in ("v_cvt_flr_i32_f32_e32 v12, 0x7fc00000", "v_cvt_rpi_i32_f32_e32 v12, 0xff800001", + "v_cvt_flr_i32_f32_e32 v12, 0x7f800000", "v_cvt_rpi_i32_f32_e32 v12, 0xff800000", + "v_cvt_flr_i32_f32_e32 v12, 0x4f000000", "v_cvt_rpi_i32_f32_e32 v12, 0xcf000001", + "v_cvt_flr_i32_f32_e64 v12, |0xcf000000|", "v_cvt_rpi_i32_f32_e64 v12, neg(0xcf000000)", + "v_cvt_flr_i32_f32_e64 v12, -|0x7f800000|"): + assert "_i32_f32(" in translate(with_constants([exceptional])).split("float4 main", 1)[1], exceptional + # No cast in either helper reads a NaN or an out-of-range value: the NaN word is replaced and the value clamped. + for helper in ("cvt_flr_i32_f32", "cvt_rpi_i32_f32"): + text = pixel_aot.SALU_HELPERS[helper] + assert "float x = asfloat(nan ? 0u : bits);" in text and "return nan ? 0x7fffffffu + (bits >> 31) : word;" in text + assert len(re.findall(r"\bint\(", text)) == 1 and "asuint(int(r))" in text and "2147483520.0f)" in text, helper # e64 source modifiers apply bitwise sign-bit ops before the shared helper, never FP abs/neg. e64_mod_hlsl = translate_int(uncompressed_export(CVT_FLOAT_E64_I32_MOD_BODY)) assert e64_mod_hlsl.count("uint cvt_i32_f32(uint bits)") == 1 @@ -19918,22 +19924,62 @@ def make_case(name, input_words, expected_words): raise AssertionError(f"Modifier mutation ({label}) escaped detection") +def cvt_flr_rpi_saturate(word, integer): + """Saturate an exact integer result to i32; a NaN takes its sign bit (issue #1772, RDNA 2 gfx1036 and RDNA 4).""" + if integer is None: + return 0x80000000 if word >> 31 else 0x7FFFFFFF + return min(max(integer, -2**31), 2**31 - 1) & 0xFFFFFFFF + + def cvt_flr_i32_ref(word): - """Reference for finite in-range v_cvt_flr_i32_f32: floor(x) in [-2^31, 2^31).""" + """Reference for v_cvt_flr_i32_f32: floor(x), saturated to i32 (infinities included); NaN by sign bit.""" x = struct.unpack(" 0 else -2**31 - 1) + return cvt_flr_rpi_saturate(word, None if math.isnan(x) else math.floor(x)) def cvt_rpi_i32_ref(word): - """Reference for finite in-range v_cvt_rpi_i32_f32: floor(x + 0.5) in [-2^31, 2^31).""" + """Reference for v_cvt_rpi_i32_f32: floor of the exact x + 0.5 (binary64 holds it), saturated; NaN by sign bit.""" x = struct.unpack(" 0 else -2**31 - 1) + return cvt_flr_rpi_saturate(word, None if math.isnan(x) else math.floor(x + 0.5)) + + +# Hand-worked (word, flr, rpi) rows. Rows without "by rule" repeat raw words measured natively on RDNA 2 gfx1036 +# and RDNA 4 (the sources cited in issue #1772); rows marked "by rule" apply its saturation rule or, for 2^31 - 128, +# the finite formula. Every row also matches a native gfx1032 readback (docs/experiments.md). None is gfx1013. +CVT_FLR_RPI_EXCEPTIONAL = ( + (0x7fc00000, 0x7fffffff, 0x7fffffff), # +qNaN + (0xffc00000, 0x80000000, 0x80000000), # -qNaN + (0x7f800001, 0x7fffffff, 0x7fffffff), # +sNaN + (0xff800001, 0x80000000, 0x80000000), # -sNaN + (0x7fffffff, 0x7fffffff, 0x7fffffff), # +NaN, all payload bits set + (0xffffffff, 0x80000000, 0x80000000), # -NaN, all payload bits set + (0x7f800000, 0x7fffffff, 0x7fffffff), # +inf + (0xff800000, 0x80000000, 0x80000000), # -inf + (0x4f000000, 0x7fffffff, 0x7fffffff), # 2^31 + (0xcf000000, 0x80000000, 0x80000000), # -2^31, in range + (0x4f000001, 0x7fffffff, 0x7fffffff), # 2^31 + 256, by rule + (0xcf000001, 0x80000000, 0x80000000), # -2^31 - 256, by rule + (0x4effffff, 0x7fffff80, 0x7fffff80), # 2^31 - 128, the largest in-range binary32, by rule + (0x5f000000, 0x7fffffff, 0x7fffffff), # 2^63, by rule + (0xdf000000, 0x80000000, 0x80000000), # -2^63, by rule + (0x7f7fffff, 0x7fffffff, 0x7fffffff), # largest finite binary32, by rule + (0xff7fffff, 0x80000000, 0x80000000), # most negative finite binary32, by rule + (0x3effffff, 0x00000000, 0x00000000), # 0.49999997 + (0x3f000000, 0x00000000, 0x00000001), # 0.5 + (0xbf000000, 0xffffffff, 0x00000000), # -0.5 + (0xbf000001, 0xffffffff, 0xffffffff), # -0.50000006 + (0x4b000001, 0x00800001, 0x00800001), # 2^23 + 1 + (0xcb000001, 0xff7fffff, 0xff7fffff), # -(2^23 + 1) + (0x4b7fffff, 0x00ffffff, 0x00ffffff), # 2^24 - 1 +) def cvt_flr_rpi_pair_body(w0, w1, *, e64): - """Build a proven-constant SGPR/VGPR shader converting two finite in-range binary32 words.""" + """Build a constant-source SGPR/VGPR shader converting two binary32 words with flr and rpi.""" suf = "_e64" if e64 else "_e32" return [ f"s_mov_b32 s44, {w0:#010x}", @@ -20062,6 +20108,87 @@ def make_case(name, expected_words): if wrong.returncode != 1 or "pixel_mismatch" not in wrong.stderr: raise AssertionError(f"{kind} modifier mutation ({label}) escaped detection") + # Exceptional inputs (issue #1772): NaN by sign bit, saturated infinities and out-of-range values. + for word, flr, rpi in CVT_FLR_RPI_EXCEPTIONAL: + assert (cvt_flr_i32_ref(word), cvt_rpi_i32_ref(word)) == (flr, rpi), hex(word) + + def make_runtime_case(name, input_words, expected_words): + path = directory / f"cvt_flr_rpi_{name}.case" + constants = bytearray(256) + struct.pack_into("<4I", constants, 12, *input_words) + for word in expected_words: + assert struct.unpack("> 31) : word;", "return word;", nan_case), + ("reverse the NaN sign select", "0x7fffffffu + (bits >> 31)", "0x80000000u - (bits >> 31)", nan_case), + ("drop the 2^31 saturation", "x >= 2147483648.0f ? 0x7fffffffu : ", "", inf_case), + ): + assert runtime_hlsl.count(old) == 2, label + source.write_text(runtime_hlsl.replace(old, new), encoding="ascii") + compile_shader(compiler, source, shader) + wrong = subprocess.run([renderer, backend, "--check-pixel-case", shader, case], capture_output=True, text=True) + if wrong.returncode != 1 or "pixel_mismatch" not in wrong.stderr: + raise AssertionError(f"Helper mutation ({label}) escaped detection") + + # Runtime e64 modifiers flip the sign of a NaN or an infinity before the conversion. + runtime_mod_sets = ( + (0xffc00000, 0x7fc00000, 0x7f800001, 0xffffffff), # |-qNaN|, -(+qNaN), -|+sNaN|, -(-NaN) + (0xff800000, 0x7f800000, 0x4f000001, 0xcf000001), # |-inf|, -(+inf), -|2^31+256|, -(-2^31-256) + ) + for kind, ref_fn in (("flr_i32", cvt_flr_i32_ref), ("rpi_i32", cvt_rpi_i32_ref)): + body = cvt_flr_rpi_mod_body(kind, (0, 0, 0, 0))[4:] + assert not any(line.startswith("s_mov_b32") for line in body) + source.write_text(translate_int(uncompressed_export(body)), encoding="ascii") + compile_shader(compiler, source, shader) + cases = [] + for idx, words in enumerate(runtime_mod_sets): + expected = tuple(ref_fn(apply_f32_mod(w, abs_mod=a, neg_mod=n)) for w, (a, n) in zip(words, chan_mods)) + cases.append(make_runtime_case(f"runtime_{kind}_mod_{idx}", words, expected)) + run_pixel_batch(renderer, backend, shader, [(case, "pass") for case in cases]) + for old, new, label in mutations[:2]: + mutated = [line.replace(old, new) for line in body] + assert mutated != body, label + source.write_text(translate_int(uncompressed_export(mutated)), encoding="ascii") + compile_shader(compiler, source, shader) + wrong = subprocess.run([renderer, backend, "--check-pixel-case", shader, cases[0]], + capture_output=True, text=True) + if wrong.returncode != 1 or "pixel_mismatch" not in wrong.stderr: + raise AssertionError(f"{kind} runtime modifier mutation ({label}) escaped detection") + + # Constant sources: exceptional literal words that DXC may fold at compile time. + for idx, (w0, w1) in enumerate(((0x7fc00000, 0xffc00000), (0x7f800000, 0xff800000), + (0x4f000001, 0xcf000001), (0x4f000000, 0xff7fffff))): + source.write_text(translate_int(uncompressed_export(cvt_flr_rpi_pair_body(w0, w1, e64=False))), + encoding="ascii") + compile_shader(compiler, source, shader) + expected = (cvt_flr_i32_ref(w0), cvt_rpi_i32_ref(w0), cvt_flr_i32_ref(w1), cvt_rpi_i32_ref(w1)) + run_pixel_batch(renderer, backend, shader, [(make_case(f"const_exceptional_{idx}", expected), "pass")]) + def check_int_minmax_draws(directory, compiler, renderer, backend): source = directory / "int_minmax.hlsl" diff --git a/tests/rdna2_tools_check.py b/tests/rdna2_tools_check.py index 6f4ebe57b4..04a7719e6e 100644 --- a/tests/rdna2_tools_check.py +++ b/tests/rdna2_tools_check.py @@ -4800,22 +4800,28 @@ def test_non_packed_int16_ternary_forms(self): self.assertFalse(is_instruction_supported(line), line) def test_float_to_word_conversions_e32_and_e64(self): - """v_cvt_u32_f32 and v_cvt_i32_f32 accept plain e32 and e64 with optional source abs/neg; - v_cvt_flr_i32_f32 and v_cvt_rpi_i32_f32 stay out of generic scanner support until arbitrary runtime - exceptional inputs are backed by a native RDNA reference.""" + """v_cvt_u32_f32, v_cvt_i32_f32, v_cvt_flr_i32_f32 and v_cvt_rpi_i32_f32 accept plain e32 and e64 with + optional source abs/neg, on runtime registers too: pixel_aot defines NaN, infinities and overflow for + flr and rpi (issue #1772).""" for line in ("v_cvt_u32_f32_e32 v12, v13", "v_cvt_i32_f32_e32 v12, s44", "v_cvt_u32_f32_e32 v12, 0x40400000", "v_cvt_u32_f32_e64 v12, v13", "v_cvt_i32_f32_e64 v12, s44", "v_cvt_u32_f32_e64 v12, |s44|", "v_cvt_i32_f32_e64 v12, -s44", "v_cvt_i32_f32_e64 v12, -|v13|", "v_cvt_u32_f32_e64 v12, abs(v13)", "v_cvt_i32_f32_e64 v12, neg(0x40400000)", - "v_cvt_u32_f32_e64 v12, |0xc0300000|"): + "v_cvt_u32_f32_e64 v12, |0xc0300000|", + "v_cvt_flr_i32_f32_e32 v12, v13", "v_cvt_rpi_i32_f32_e32 v12, s44", + "v_cvt_flr_i32_f32_e32 v12, 0x7fc00000", "v_cvt_rpi_i32_f32_e64 v12, -v13", + "v_cvt_flr_i32_f32_e64 v12, -|s44|", "v_cvt_rpi_i32_f32_e64 v12, neg(0x4f000000)"): self.assertTrue(is_instruction_supported(line), line) for line in ("v_cvt_u32_f32_e64 v12, s44 clamp", "v_cvt_i32_f32_e64 v12, s44 omod:2", "v_cvt_i32_f32_e32 v12, 1", "v_cvt_u32_f32_e32 v12, 0.5", "v_cvt_u32_f32_e64 v12, 0.5", "v_cvt_i32_f32_e64 v12, -1", "v_cvt_i32_f32_e64 v12, -0x40400000", "v_cvt_u32_f32_e64 v12, |-s44|", "v_cvt_u32_f32_e32 v12, v13", "v_cvt_u32_f32_e64 v12, v13", - "v_cvt_flr_i32_f32_e32 v12, v13", "v_cvt_rpi_i32_f32_e64 v12, -v13"): + "v_cvt_flr_i32_f32_e64 v12, s44 clamp", "v_cvt_rpi_i32_f32_e64 v12, s44 omod:2", + "v_cvt_flr_i32_f32_e32 v12, 0.5", "v_cvt_rpi_i32_f32_e64 v12, |-s44|", + "v_cvt_flr_i32_f32_dpp v12, v13 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf", + "v_cvt_rpi_i32_f32_sdwa v12, v13 dst_sel:DWORD src0_sel:DWORD"): self.assertFalse(is_instruction_supported(line), line) def test_disasm_cvt_flr_and_rpi_e32_and_e64(self): diff --git a/tools/pixel_aot.py b/tools/pixel_aot.py index 6a970b3e9c..5cb8eb8e26 100644 --- a/tools/pixel_aot.py +++ b/tools/pixel_aot.py @@ -1264,17 +1264,28 @@ def check_vopc_e32_dst(enc, dst): " return hi;\n" "}" ), + # v_cvt_flr_i32_f32 is floor(x) and v_cvt_rpi_i32_f32 the floor of the exact x + 0.5 (x - floor(x) is exact). + # Exceptional inputs follow the native RDNA 2 (gfx1036) and RDNA 4 raw words cited in issue #1772, not gfx1013: + # a NaN gives 0x7fffffff with its sign bit clear and 0x80000000 with it set, quiet or signalling, and both + # infinities and out-of-range values saturate to the i32 limits. The NaN is replaced before any float operation + # and the rounded value is clamped to [-2^31, 2^31 - 128] (2^31 - 1 is not a binary32), so every cast is in range. "cvt_flr_i32_f32": ( "uint cvt_flr_i32_f32(uint bits) {\n" - " float x = asfloat(bits);\n" - " return asuint(int(floor(x)));\n" + " bool nan = (bits & 0x7fffffffu) > 0x7f800000u;\n" + " float x = asfloat(nan ? 0u : bits);\n" + " float r = clamp(floor(x), -2147483648.0f, 2147483520.0f);\n" + " uint word = x >= 2147483648.0f ? 0x7fffffffu : asuint(int(r));\n" + " return nan ? 0x7fffffffu + (bits >> 31) : word;\n" "}" ), "cvt_rpi_i32_f32": ( "uint cvt_rpi_i32_f32(uint bits) {\n" - " float x = asfloat(bits);\n" + " bool nan = (bits & 0x7fffffffu) > 0x7f800000u;\n" + " float x = asfloat(nan ? 0u : bits);\n" " float f = floor(x);\n" - " return asuint(int(f + ((x - f) >= 0.5f ? 1.0f : 0.0f)));\n" + " float r = clamp(f + ((x - f) >= 0.5f ? 1.0f : 0.0f), -2147483648.0f, 2147483520.0f);\n" + " uint word = x >= 2147483648.0f ? 0x7fffffffu : asuint(int(r));\n" + " return nan ? 0x7fffffffu + (bits >> 31) : word;\n" "}" ), } @@ -1932,37 +1943,6 @@ def half16_arith_source(token, scalar, vector, pending, sample_dest, known_ssa_b return word, bits -def require_proven_i32_f32_range(base, has_abs, has_neg, scalar, vector, known_ssa_bits, kind): - """Require a statically known binary32 input that is finite and in [-2^31, 2^31) after modifiers. - - AMD RDNA 2 VOP1 opcodes 12 and 13 (v_cvt_rpi_i32_f32 and v_cvt_flr_i32_f32) document the finite - rounding formulas without an exceptional-input rule for NaN, infinities, or i32 overflow. Reject - unproven runtime inputs and out-of-range or non-finite constants until a native RDNA reference - establishes exceptional behavior. - """ - if base.startswith("0x"): - raw_bits = int(base, 16) - elif base.startswith("s"): - val = scalar.get(base) - if not isinstance(val, int): - raise ValueError(f"Unproven runtime input {base} at v_cvt_{kind}_f32") - raw_bits = val & 0xffffffff - else: - ssa = vector.get(base) - if not isinstance(ssa, str) or ssa not in known_ssa_bits: - raise ValueError(f"Unproven runtime input {base} at v_cvt_{kind}_f32") - raw_bits = known_ssa_bits[ssa] - eff_bits = raw_bits - if has_abs: - eff_bits &= 0x7fffffff - if has_neg: - eff_bits ^= 0x80000000 - x, = struct.unpack(" 255 or (base.startswith(("v", "s")) and @@ -6136,8 +6113,6 @@ def sample_coord(reg): if base.startswith("v") and base not in vector: raise ValueError(f"Unbound pixel input {base} at v_cvt_{kind}_f32_e64") operand = fp_operand(base, scalar, vector, pending, sample_dest) - if kind in ("flr_i32", "rpi_i32"): - require_proven_i32_f32_range(base, has_abs, has_neg, scalar, vector, known_ssa_bits, kind) bits = f"asuint({operand})" if has_abs: bits = f"({bits} & 0x7fffffffu)" diff --git a/tools/scan_unsupported_shaders.py b/tools/scan_unsupported_shaders.py index 7de7ff4b33..68b8e03d88 100644 --- a/tools/scan_unsupported_shaders.py +++ b/tools/scan_unsupported_shaders.py @@ -167,8 +167,8 @@ def lower_rejection_causes(results: list[dict]) -> dict: r"v_cvt_f32_f16(?:_e32|_e64)?\s+v\d+,\s+-?(?:\|(?:[vs]\d+|0x[0-9a-f]{1,8}|\d+)\||abs\((?:[vs]\d+|0x[0-9a-f]{1,8}|\d+)\)|[vs]\d+|0x[0-9a-f]{1,8}|\d+)(?:\s+clamp)?", r"v_cvt_f32_f16_sdwa\s+v\d+,\s+-?(?:\|[vs]\d+\||[vs]\d+)(?:\s+clamp)?\s+dst_sel:DWORD\s+dst_unused:UNUSED_(?:PAD|PRESERVE)\s+src0_sel:WORD_[01]", r"v_cvt_f16_f32(?:_e32|_e64)?\s+v\d+,\s+[^,\s]+", - r"v_cvt_i32_f32_e32 v\d+, (?:[vs]\d+|0x[0-9a-f]{1,8})", - rf"v_cvt_(?:i32|u32)_f32_e64 v\d+, {CVT_F32_E64_SOURCE}", + r"v_cvt_(?:i32|flr_i32|rpi_i32)_f32_e32 v\d+, (?:[vs]\d+|0x[0-9a-f]{1,8})", + rf"v_cvt_(?:i32|u32|flr_i32|rpi_i32)_f32_e64 v\d+, {CVT_F32_E64_SOURCE}", r"v_cvt_f32_ubyte[0-3](?:_e32|_e64)?\s+v\d+,\s+(?:[vs]\d+|0x[0-9a-f]{1,8}|\d+)(?:\s+clamp)?", r"v_cvt_off_f32_i4(?:_e32|_e64)?\s+v\d+,\s+(?:[vs]\d+|0x[0-9a-f]{1,8}|\d+)(?:\s+clamp)?", r"v_cvt_f32_(?:u32|i32)_e32\s+v\d+,\s+(?:[vs]\d+|0x[0-9a-f]{1,8}|-?\d+)",