diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-23 17:40:52 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-23 17:40:52 -0500 |
| commit | fecb7797c82814ded672aa9aff5f5709e7d4dfe8 (patch) | |
| tree | ce7d293db509a5ce61164952e19fdd6f227991cb | |
| parent | f48adad76119c45aed89ab612e46d87b10463cec (diff) | |
| parent | 4b6c2a2cca5cafd2b623a4f2a33c348424eee93b (diff) | |
Merge remote-tracking branch 'origin/pr/9003'
| -rw-r--r-- | docs/design.md | 2 | ||||
| -rw-r--r-- | tests/semantic/test_fma_mix_literal.py | 85 | ||||
| -rw-r--r-- | tools/rdna2spirv.py | 58 |
3 files changed, 143 insertions, 2 deletions
diff --git a/docs/design.md b/docs/design.md index 597acf6f9..159aecb6c 100644 --- a/docs/design.md +++ b/docs/design.md | |||
| @@ -271,7 +271,7 @@ How the generated SPIR-V expects the runtime to work: | |||
| 271 | | Xbox GPU addresses | Shaders build Xbox GPU virtual addresses, and some force the high dword to 0 (descriptor tables in the low 4 GB). Every memory access goes to **heap base + Xbox VA**, so the runtime maps the console's GPU address window into one host buffer-device-address allocation. | | 271 | | Xbox GPU addresses | Shaders build Xbox GPU virtual addresses, and some force the high dword to 0 (descriptor tables in the low 4 GB). Every memory access goes to **heap base + Xbox VA**, so the runtime maps the console's GPU address window into one host buffer-device-address allocation. | |
| 272 | | Code-embedded data | 155 VS/PS had "undecodable words". They're **constant tables inside the code range** (float values like 0x3EF7…, plus zeros), read through `s_getpc_b64` + `s_add_u32`/`s_addc_u32` + `s_load` (147 of 155). The translator re-anchors each instruction to its true FSSM offset by matching encodings, translates only code reachable from entry (a branch into data is rejected), and turns `s_getpc_b64` into code base + next-instruction offset. **The runtime uploads each shader's FSSM blob into the heap and passes that VA as the code base.** Re-anchoring compares bytes exactly, except that VOP3P `op_sel_hi[2]` (bit 14) is ignored on two-source packed ops: the compiler stores 0 there, and LLVM prints its default of 1. Unused VOP3/VOP3P `SRC1` (bits 49:41) and `SRC2` (bits 58:50) fields are also ignored when stored as 0 and printed as `0x80`: LLVM 23 re-encodes a source it doesn't read as inline constant 0 (one-source `v_rcp`/`v_log`/`v_sqrt_f32_e64`, two-source `v_sub_f32_e64`/`v_cmp_*_e64`/`v_mul_lo_u32`/`v_pk_mul_f16`). Both checks for clean listings and re-anchoring use the parsed operand count to apply this exception only to unused sources. A used source encoded as 0 reads `s0` and is never treated as inline zero. `tests/semantic/test_printed_encoding.py` checks used `SRC1`/`SRC2` and an unused field with synthetic listings. Confirmed by a count over the corpus with LLVM 23.1.5: without it, 1,471 of the 1,473 shaders with embedded data are rejected ("reachable instruction with normalized encoding"); with it, all 1,473 translate and pass `spirv-val`. Runs with LLVM 21 already translated all of them, so older LLVMs presumably print the stored 0 (not re-checked). Data words that decode with ignored fields set are printed re-encoded (e.g. `0xbe8d2242` as `s_rfe_b64` `0xbe802242`), so they can never match. Since the decoder consumed the same length, they are anchored in sequence and marked. A shader that can *reach* a marked word is rejected; none of the corpus does. All 155 now translate. A reachable instruction that falls through into words llvm-mc skipped (it printed no instruction there) is rejected too, as `fall-through into undecodable words`: until SHADER-813 the reachable path ended there without a word, so everything after the gap (exports, stores) was dropped from a module that still passed `spirv-val`. No VS/PS of the corpus has such a gap on a reachable path; in the FidelityFX FSPX programs it is the `v_dot4_i32_i8 ... op_sel` words LLVM 23 cannot decode (`docs/fspx.md`), so those programs are now refused instead of translated without their stores, until those words are decoded (#2038, #2323). `tests/test_rdna2spirv_decode.py` (`test_fall_through_into_undecodable_words_is_refused`). | | 272 | | Code-embedded data | 155 VS/PS had "undecodable words". They're **constant tables inside the code range** (float values like 0x3EF7…, plus zeros), read through `s_getpc_b64` + `s_add_u32`/`s_addc_u32` + `s_load` (147 of 155). The translator re-anchors each instruction to its true FSSM offset by matching encodings, translates only code reachable from entry (a branch into data is rejected), and turns `s_getpc_b64` into code base + next-instruction offset. **The runtime uploads each shader's FSSM blob into the heap and passes that VA as the code base.** Re-anchoring compares bytes exactly, except that VOP3P `op_sel_hi[2]` (bit 14) is ignored on two-source packed ops: the compiler stores 0 there, and LLVM prints its default of 1. Unused VOP3/VOP3P `SRC1` (bits 49:41) and `SRC2` (bits 58:50) fields are also ignored when stored as 0 and printed as `0x80`: LLVM 23 re-encodes a source it doesn't read as inline constant 0 (one-source `v_rcp`/`v_log`/`v_sqrt_f32_e64`, two-source `v_sub_f32_e64`/`v_cmp_*_e64`/`v_mul_lo_u32`/`v_pk_mul_f16`). Both checks for clean listings and re-anchoring use the parsed operand count to apply this exception only to unused sources. A used source encoded as 0 reads `s0` and is never treated as inline zero. `tests/semantic/test_printed_encoding.py` checks used `SRC1`/`SRC2` and an unused field with synthetic listings. Confirmed by a count over the corpus with LLVM 23.1.5: without it, 1,471 of the 1,473 shaders with embedded data are rejected ("reachable instruction with normalized encoding"); with it, all 1,473 translate and pass `spirv-val`. Runs with LLVM 21 already translated all of them, so older LLVMs presumably print the stored 0 (not re-checked). Data words that decode with ignored fields set are printed re-encoded (e.g. `0xbe8d2242` as `s_rfe_b64` `0xbe802242`), so they can never match. Since the decoder consumed the same length, they are anchored in sequence and marked. A shader that can *reach* a marked word is rejected; none of the corpus does. All 155 now translate. A reachable instruction that falls through into words llvm-mc skipped (it printed no instruction there) is rejected too, as `fall-through into undecodable words`: until SHADER-813 the reachable path ended there without a word, so everything after the gap (exports, stores) was dropped from a module that still passed `spirv-val`. No VS/PS of the corpus has such a gap on a reachable path; in the FidelityFX FSPX programs it is the `v_dot4_i32_i8 ... op_sel` words LLVM 23 cannot decode (`docs/fspx.md`), so those programs are now refused instead of translated without their stores, until those words are decoded (#2038, #2323). `tests/test_rdna2spirv_decode.py` (`test_fall_through_into_undecodable_words_is_refused`). | |
| 273 | | VOP3 opcode 0x140 | LLVM disassembly of the synthetic words in `tests/test_rdna2_decode.py` gives `v_mad_legacy_f32` on gfx1010 through gfx1013 and `v_fma_legacy_f32` on gfx1030. The direct decoder defaults to gfx1013, matching the disassembler CLI. Checked by the unit test `test_vop3_legacy_mad_fma_target` in `tests/test_rdna2_decode.py`, which decodes the same words directly and runs the Python disassembler backend for gfx1013 and gfx1030. | | 273 | | VOP3 opcode 0x140 | LLVM disassembly of the synthetic words in `tests/test_rdna2_decode.py` gives `v_mad_legacy_f32` on gfx1010 through gfx1013 and `v_fma_legacy_f32` on gfx1030. The direct decoder defaults to gfx1013, matching the disassembler CLI. Checked by the unit test `test_vop3_legacy_mad_fma_target` in `tests/test_rdna2_decode.py`, which decodes the same words directly and runs the Python disassembler backend for gfx1013 and gfx1030. | |
| 274 | | Printed encodings | A listing is LLVM's text for what it decoded, not the bytes it read, so the translator checks the two against each other: the re-anchoring above for listings with undecodable words, and, for every other listing whose header names its code range and whose blob sits next to it (`fssm/`, or `fspx/` for FSPX), each instruction's printed encoding against the bytes at its offset, with the same exceptions (`same_encoding`). A difference is `Unsupported`, naming the instruction, its offset and both byte strings, instead of a translation of the field as 0. The case that prompted it: llvm-mc's gfx1013 decoder (LLVM 21.1.8) drops the VOP3 `op_sel` bits (word 0, bits 14:11) of a 16-bit VOPC compare: `0a 08 a9 d4 01 05 02 00` disassembles as `v_cmp_lt_u16_e64 s10, v1, v2` and re-encodes with the bits cleared (its assembler rejects `op_sel` on VOPC for gfx1013 and gfx1030 alike). Counted over the 1.0.2.1 corpus: the 25,030 clean listings hold 30,335,381 instructions, and none differs from its bytes beyond those exceptions (754); the 166 16-bit VOPC compares (154 shaders, all `f16`) have `op_sel` clear. Hand-written listings, `run_gpu_tests.py`'s `code 0x0..0x0 (synthetic)` listings and a blob that doesn't hold the listing's code range (the tests' header-only FSSM) are not checked. `tests/semantic/test_printed_encoding.py` (a dropped VOPC `op_sel`, a later instruction, the SRC2 exception, and the unchecked cases). One difference is repaired instead of refused: a `v_fma_mix*` literal that llvm-mc prints cut to its low 16 bits (and re-encodes with a zero high half). llvm-mc 23.1.5 prints some of these literals in full as `lit(V)` (an f32 source, or a low half that is an inline constant; LLVM 21 printed the inline constant and dropped the literal word) and still re-encodes only the low half: `tools/shader_ir.py` reads `lit(V)` as V, and the same repair applies (5,733 such operands in the pipeline library, [shader_library.md](shader_library.md)). When the 8 instruction bytes match (`same_encoding` with all three sources counted as used, so a live `s0` can't pass as an inline 0) and the printed literal is the stored one's low half, every source the bytes encode as the literal (SRC field `0xff`) takes the stored 32 bits, so a source with `op_sel_hi` and `op_sel` set reads the high half instead of +0.0. Seen in one program of the FidelityFX DLL's `FSPX` code ([fspx.md](fspx.md)): src0 the low half, src2 the high half; the game's clean listings have no such difference (the count above). That the hardware gives op_sel's high half of the literal to that source is not measured; it is what Mesa's ACO compiler relies on (public: `optimize_constants()` in `src/amd/compiler/aco_optimizer.cpp`, Mesa `main` at `bb986060`, packs two constants as the two f16 halves of one literal, read through `v_fma_mix`'s `op_sel_hi` with the second source extracting the high half), and what the console compiler emitted here. `tools/fma_mix_literal_probe.py --oracle hw` measures it on an AMD GPU (the instruction's exact bytes through `tools/hw_oracle`; not run on one yet). `tests/semantic/test_fma_mix_literal.py` (the value through the interpreter, the re-anchoring path, an inline constant with the literal's printed value left alone, and the refused cases, a live source printed as inline 0 among them). | | 274 | | Printed encodings | A listing is LLVM's text for what it decoded, not the bytes it read, so the translator checks the two against each other: the re-anchoring above for listings with undecodable words, and, for every other listing whose header names its code range and whose blob sits next to it (`fssm/`, or `fspx/` for FSPX), each instruction's printed encoding against the bytes at its offset, with the same exceptions (`same_encoding`). A difference is `Unsupported`, naming the instruction, its offset and both byte strings, instead of a translation of the field as 0. The case that prompted it: llvm-mc's gfx1013 decoder (LLVM 21.1.8) drops the VOP3 `op_sel` bits (word 0, bits 14:11) of a 16-bit VOPC compare: `0a 08 a9 d4 01 05 02 00` disassembles as `v_cmp_lt_u16_e64 s10, v1, v2` and re-encodes with the bits cleared (its assembler rejects `op_sel` on VOPC for gfx1013 and gfx1030 alike). Counted over the 1.0.2.1 corpus: the 25,030 clean listings hold 30,335,381 instructions, and none differs from its bytes beyond those exceptions (754); the 166 16-bit VOPC compares (154 shaders, all `f16`) have `op_sel` clear. Hand-written listings, `run_gpu_tests.py`'s `code 0x0..0x0 (synthetic)` listings and a blob that doesn't hold the listing's code range (the tests' header-only FSSM) are not checked. `tests/semantic/test_printed_encoding.py` (a dropped VOPC `op_sel`, a later instruction, the SRC2 exception, and the unchecked cases). One difference is repaired instead of refused: a `v_fma_mix*` literal that llvm-mc prints cut to its low 16 bits (and re-encodes with a zero high half). llvm-mc 23.1.5 prints some of these literals in full as `lit(V)` (an f32 source, or a low half that is an inline constant; LLVM 21 printed the inline constant and dropped the literal word) and still re-encodes only the low half: `tools/shader_ir.py` reads `lit(V)` as V, and the same repair applies (5,733 such operands in the pipeline library, [shader_library.md](shader_library.md)). When the 8 instruction bytes match (`same_encoding` with all three sources counted as used, so a live `s0` can't pass as an inline 0) and the printed literal is the stored one's low half, every source the bytes encode as the literal (SRC field `0xff`) takes the stored 32 bits, so a source with `op_sel_hi` and `op_sel` set reads the high half instead of +0.0. Seen in one program of the FidelityFX DLL's `FSPX` code ([fspx.md](fspx.md)): src0 the low half, src2 the high half; the game's clean listings have no such difference (the count above). That the hardware gives op_sel's high half of the literal to that source is not measured; it is what Mesa's ACO compiler relies on (public: `optimize_constants()` in `src/amd/compiler/aco_optimizer.cpp`, Mesa `main` at `bb986060`, packs two constants as the two f16 halves of one literal, read through `v_fma_mix`'s `op_sel_hi` with the second source extracting the high half), and what the console compiler emitted here. `tools/fma_mix_literal_probe.py --oracle hw` measures it on an AMD GPU (the instruction's exact bytes through `tools/hw_oracle`; not run on one yet). `tests/semantic/test_fma_mix_literal.py` (the value through the interpreter, the re-anchoring path, an inline constant with the literal's printed value left alone, and the refused cases, a live source printed as inline 0 among them). A second shape is repaired too (`mix_inline_literal`): LLVM 21's form above. llvm-mc 21.1.8 (`-mcpu=gfx1013`) prints a `v_fma_mix*` literal whose low 16 bits are the f16 value of an inline constant as that constant and re-encodes the instruction in **8** bytes: every SRC field that held `0xff` holds the inline code, and the literal word is gone (`0x3f800000`, 1.0 as an f32 source, prints as `0`; `0x3c003800` as `0.5`, losing the high half 1.0). Swept with llvm-mc 21.1.8 over all 65,536 low halves under three high halves: exactly 90 low halves print this way (0..64, -1..-16, ±0.5, ±1.0, ±2.0, ±4.0, 1/(2π) as f16: `MIX_INLINE_F16`), whatever the high half; llvm-mc 23.1.6 prints all of them as `lit(V)`. The printed 8 bytes are 4 short of what the decoder read, so the later offsets of a clean listing were 4 low and its first such instruction was refused as a dropped field (one FSPX program, `tupacfan04`'s count in #8683). When every differing SRC field is `0xff` in the blob and the inline code of the stored literal's low half in the print, and the rest of the 8 bytes match exactly (`same_encoding`, all three sources live), each such source reads the stored 32 bits with the stored neg and abs (`NEG_HI`) bits, the instruction takes the 12 stored bytes, and the following offsets move up by 4 (also on the re-anchoring path). Any other difference stays `Unsupported`. CPU-checked by `tests/semantic/test_fma_mix_literal.py` (`FmaMixInlineLiteralTest`: an f32 source, two f16 halves, neg and abs, all 90 codes, both listing paths, the refused cases). | |
| 275 | | Global memory | `global_load/store_dword*`: address = (64-bit SGPR base + zero-extended 32-bit VGPR offset, or a 64-bit VGPR address) + the instruction offset, translated through the heap base. Inactive lanes don't access memory, and loads merge their previous values with OpPhi. The offset is the **signed 12-bit** byte value as llvm-mc prints it, -2048..2047 (`offset:-4` is the field 0xffc; llvm-mc rejects `offset:0xffc` and disassembles that field as `offset:-4`, so no repo path delivers the raw unsigned field), added to the address as is; a tool that printed the raw field (`0xffc`) would mean the same offset, and a value outside 12 bits is rejected. Measured on an AMD Radeon RX 6800 (gfx1030) running `global_load_dword` as a raw compute kernel, wave32 and wave64 alike, with the dword at byte n = 0x10000000+n and the pointer at 0x1000: offset 0 reads 0x10001000, -4 0x10000ffc, -2048 0x10000800, 2044 0x100017fc, -1024 0x10000c00. The translator used to subtract 0x1000 from an already negative value (4096 bytes too low). Checked by `tests/semantic/test_global_mem_offset.py` (both addressing modes, dword/x2/x4, byte/short loads and stores at -4, -2048, 2047 and 0) and the GPU test `runtime/test/shaders/cs_global_neg_offset.asm` (`vk_compute_test ... 40 dump`). | | 275 | | Global memory | `global_load/store_dword*`: address = (64-bit SGPR base + zero-extended 32-bit VGPR offset, or a 64-bit VGPR address) + the instruction offset, translated through the heap base. Inactive lanes don't access memory, and loads merge their previous values with OpPhi. The offset is the **signed 12-bit** byte value as llvm-mc prints it, -2048..2047 (`offset:-4` is the field 0xffc; llvm-mc rejects `offset:0xffc` and disassembles that field as `offset:-4`, so no repo path delivers the raw unsigned field), added to the address as is; a tool that printed the raw field (`0xffc`) would mean the same offset, and a value outside 12 bits is rejected. Measured on an AMD Radeon RX 6800 (gfx1030) running `global_load_dword` as a raw compute kernel, wave32 and wave64 alike, with the dword at byte n = 0x10000000+n and the pointer at 0x1000: offset 0 reads 0x10001000, -4 0x10000ffc, -2048 0x10000800, 2044 0x100017fc, -1024 0x10000c00. The translator used to subtract 0x1000 from an already negative value (4096 bytes too low). Checked by `tests/semantic/test_global_mem_offset.py` (both addressing modes, dword/x2/x4, byte/short loads and stores at -4, -2048, 2047 and 0) and the GPU test `runtime/test/shaders/cs_global_neg_offset.asm` (`vk_compute_test ... 40 dump`). | |
| 276 | | Global atomics (`global_atomic_*`, 32-bit) | `add`, `sub`, `smin/umin`, `smax/umax`, `and`, `or`, `xor`, `swap` and `cmpswap` at the `global_load/store` address (same `Gen.global_address`: saddr pair plus zero-extended vaddr, or a 64-bit vaddr with `off`, plus the signed 12-bit offset), as one `OpAtomic*` on the PhysicalStorageBuffer dword, device scope, guarded so inactive lanes don't touch memory. `fmin` and `fmax` (32-bit float) lower to a structured `OpAtomicCompareExchange` CAS loop using GLSL.std.450 `NMin`/`NMax` (no Vulkan float-atomic extension needed; NaN returns the non-NaN operand, `-0.0 < +0.0`). `inc` and `dec` (32-bit unsigned wrapping against `DATA`: `inc` gives `(M >= DATA) ? 0 : M + 1`, `dec` gives `(M == 0 \|\| M > DATA) ? DATA : M + 1`, `dec` gives `(M == 0 With `glc` the pre-op value lands in `vdst`, the first operand; without `glc` there is no `vdst`, and the first-read analysis counts operand 0 as the address (a source). `cmpswap` takes `vdata` as the pair {DATA, CMP}: `M = (M == CMP) ? DATA : M`, as `buffer_atomic_cmpswap`. Still `Unsupported`: the `_x2` 64-bit forms, `inc`/`dec`, the float atomics, and `slc`/`dlc`. `and/or/xor/swap_x2` work on a 64-bit element (two dwords, low first) as one 32-bit atomic per dword, with no Int64 atomics: a bitwise op leaves the same memory that way, since its bits are independent. **Approximation:** an exchange can tear, since two lanes swapping into one element at once can leave one's low dword with the other's high dword (A.lo, B.lo, B.hi, A.hi), and the returned pair comes from two reads, so another lane's 64-bit op on the same element between them can show up half done (as for `ds_and/or/xor_b64`). **Approximation:** the returned pair comes from two reads, so another lane's 64-bit op on the same element between them can show up half done (as for `ds_and/or/xor_b64`). Still `Unsupported`: the other `_x2` forms (`add/sub/min/max/fmin/fmax_x2` carry or compare across the dwords, and `cmpswap_x2` compares all 64 bits), `inc`/`dec`, `fcmpswap`, and `slc`/`dlc`. Until 2026-11-22 every global atomic was rejected. CPU-checked by `tests/semantic/test_global_atomics.py` (all ten ops on a value grid with and without `glc`, `cmpswap`, offsets from -2048 to 2044, `off` addressing, seeded values, rejected forms, the first-read analysis). CPU-checked by `tests/semantic/test_global_atomics.py` (all ten integer ops plus `fmin`/`fmax` on a value grid with and without `glc`, NaN/±0/±inf, multi-lane CAS contention, `cmpswap`, offsets from -2048 to 2044, `off` addressing, seeded values, rejected forms, the first-read analysis) and `tests/semantic/test_global_atomics_x2.py` (the four `_x2` forms with and without `glc`, an offset, seeded values, the carrying forms rejected), and validated with `spirv-val` (native and `--wave64-emulate`). **Not GPU-verified.** \|\| M > DATA) ? DATA : M - 1`, RDNA2 ISA) also lower to a structured `OpAtomicCompareExchange` CAS loop via `Gen.atomic_inc_dec_cas` for both `global_atomic_{inc,dec}` and MUBUF `buffer_atomic_{inc,dec}` (preserving MUBUF bounds checks: OOB/inactive lanes leave memory untouched and return 0 with `glc`). With `glc` the pre-op value lands in `vdst`, the first operand; without `glc` there is no `vdst`, and the first-read analysis counts operand 0 as the address (a source). `cmpswap` takes `vdata` as the pair {DATA, CMP}: `M = (M == CMP) ? DATA : M`, as `buffer_atomic_cmpswap`. `and/or/xor/swap_x2` work on a 64-bit element (two dwords, low first) as one 32-bit atomic per dword: a bitwise op or an exchange leaves the same memory that way, with no Int64 atomics. **Approximation:** the returned pair comes from two reads, so another lane's 64-bit op on the same element between them can show up half done (as for `ds_and/or/xor_b64`). Still `Unsupported`: the other `_x2` forms (`add/sub/min/max/inc/dec/fmin/fmax_x2` carry or compare across the dwords, and `cmpswap_x2` compares all 64 bits), `fcmpswap`, and `slc`/`dlc`. Until 2026-11-22 every global atomic was rejected. CPU-checked by `tests/semantic/test_global_atomics.py` (all ten integer ops plus `fmin`/`fmax` on a value grid with and without `glc`, NaN/±0/±inf, multi-lane CAS contention, `cmpswap`, offsets from -2048 to 2044, `off` addressing, seeded values, rejected forms, the first-read analysis), `tests/semantic/test_atomic_inc_dec.py` (`global_atomic_{inc,dec}` and `buffer_atomic_{inc,dec}` edge grid, wraparound, `glc` vs non-`glc`, signed offsets, `off` addressing, MUBUF bounds/OOB, inactive lanes, multi-lane CAS contention, first-read analysis, rejected `_x2`/`slc`/`dlc` forms), and `tests/semantic/test_global_atomics_x2.py` (the four `_x2` forms with and without `glc`, an offset, seeded values, the carrying forms rejected), and validated with `spirv-val` (native and `--wave64-emulate`). **Not GPU-verified.** | | 276 | | Global atomics (`global_atomic_*`, 32-bit) | `add`, `sub`, `smin/umin`, `smax/umax`, `and`, `or`, `xor`, `swap` and `cmpswap` at the `global_load/store` address (same `Gen.global_address`: saddr pair plus zero-extended vaddr, or a 64-bit vaddr with `off`, plus the signed 12-bit offset), as one `OpAtomic*` on the PhysicalStorageBuffer dword, device scope, guarded so inactive lanes don't touch memory. `fmin` and `fmax` (32-bit float) lower to a structured `OpAtomicCompareExchange` CAS loop using GLSL.std.450 `NMin`/`NMax` (no Vulkan float-atomic extension needed; NaN returns the non-NaN operand, `-0.0 < +0.0`). `inc` and `dec` (32-bit unsigned wrapping against `DATA`: `inc` gives `(M >= DATA) ? 0 : M + 1`, `dec` gives `(M == 0 \|\| M > DATA) ? DATA : M + 1`, `dec` gives `(M == 0 With `glc` the pre-op value lands in `vdst`, the first operand; without `glc` there is no `vdst`, and the first-read analysis counts operand 0 as the address (a source). `cmpswap` takes `vdata` as the pair {DATA, CMP}: `M = (M == CMP) ? DATA : M`, as `buffer_atomic_cmpswap`. Still `Unsupported`: the `_x2` 64-bit forms, `inc`/`dec`, the float atomics, and `slc`/`dlc`. `and/or/xor/swap_x2` work on a 64-bit element (two dwords, low first) as one 32-bit atomic per dword, with no Int64 atomics: a bitwise op leaves the same memory that way, since its bits are independent. **Approximation:** an exchange can tear, since two lanes swapping into one element at once can leave one's low dword with the other's high dword (A.lo, B.lo, B.hi, A.hi), and the returned pair comes from two reads, so another lane's 64-bit op on the same element between them can show up half done (as for `ds_and/or/xor_b64`). **Approximation:** the returned pair comes from two reads, so another lane's 64-bit op on the same element between them can show up half done (as for `ds_and/or/xor_b64`). Still `Unsupported`: the other `_x2` forms (`add/sub/min/max/fmin/fmax_x2` carry or compare across the dwords, and `cmpswap_x2` compares all 64 bits), `inc`/`dec`, `fcmpswap`, and `slc`/`dlc`. Until 2026-11-22 every global atomic was rejected. CPU-checked by `tests/semantic/test_global_atomics.py` (all ten ops on a value grid with and without `glc`, `cmpswap`, offsets from -2048 to 2044, `off` addressing, seeded values, rejected forms, the first-read analysis). CPU-checked by `tests/semantic/test_global_atomics.py` (all ten integer ops plus `fmin`/`fmax` on a value grid with and without `glc`, NaN/±0/±inf, multi-lane CAS contention, `cmpswap`, offsets from -2048 to 2044, `off` addressing, seeded values, rejected forms, the first-read analysis) and `tests/semantic/test_global_atomics_x2.py` (the four `_x2` forms with and without `glc`, an offset, seeded values, the carrying forms rejected), and validated with `spirv-val` (native and `--wave64-emulate`). **Not GPU-verified.** \|\| M > DATA) ? DATA : M - 1`, RDNA2 ISA) also lower to a structured `OpAtomicCompareExchange` CAS loop via `Gen.atomic_inc_dec_cas` for both `global_atomic_{inc,dec}` and MUBUF `buffer_atomic_{inc,dec}` (preserving MUBUF bounds checks: OOB/inactive lanes leave memory untouched and return 0 with `glc`). With `glc` the pre-op value lands in `vdst`, the first operand; without `glc` there is no `vdst`, and the first-read analysis counts operand 0 as the address (a source). `cmpswap` takes `vdata` as the pair {DATA, CMP}: `M = (M == CMP) ? DATA : M`, as `buffer_atomic_cmpswap`. `and/or/xor/swap_x2` work on a 64-bit element (two dwords, low first) as one 32-bit atomic per dword: a bitwise op or an exchange leaves the same memory that way, with no Int64 atomics. **Approximation:** the returned pair comes from two reads, so another lane's 64-bit op on the same element between them can show up half done (as for `ds_and/or/xor_b64`). Still `Unsupported`: the other `_x2` forms (`add/sub/min/max/inc/dec/fmin/fmax_x2` carry or compare across the dwords, and `cmpswap_x2` compares all 64 bits), `fcmpswap`, and `slc`/`dlc`. Until 2026-11-22 every global atomic was rejected. CPU-checked by `tests/semantic/test_global_atomics.py` (all ten integer ops plus `fmin`/`fmax` on a value grid with and without `glc`, NaN/±0/±inf, multi-lane CAS contention, `cmpswap`, offsets from -2048 to 2044, `off` addressing, seeded values, rejected forms, the first-read analysis), `tests/semantic/test_atomic_inc_dec.py` (`global_atomic_{inc,dec}` and `buffer_atomic_{inc,dec}` edge grid, wraparound, `glc` vs non-`glc`, signed offsets, `off` addressing, MUBUF bounds/OOB, inactive lanes, multi-lane CAS contention, first-read analysis, rejected `_x2`/`slc`/`dlc` forms), and `tests/semantic/test_global_atomics_x2.py` (the four `_x2` forms with and without `glc`, an offset, seeded values, the carrying forms rejected), and validated with `spirv-val` (native and `--wave64-emulate`). **Not GPU-verified.** | |
| 277 | | `global_atomic_csub`, `buffer_atomic_csub` (GFX10.3) | `tmp = M; M = (DATA > tmp) ? 0 : tmp - DATA`, unsigned: a subtract clamped at 0 that never wraps; with `glc` the old M comes back. Measured on gfx1036 by @Vonsek (#3193): 162/162 (memory, data) pairs over both forms. Same `OpAtomicCompareExchange` loop as `inc`/`dec` (`Gen.atomic_inc_dec_cas`), so exact under contention; operands, inactive lanes, buffer bounds and `glc` as `inc`/`dec`. Until 2026-11-22 both raised `Unsupported`. CPU-checked by `tests/semantic/test_atomic_csub.py` (a 9×9 value grid, no `glc`, buffer bounds, 32 lanes on one dword against 32 sequential subtracts, seeded values). The translation is not GPU-verified. | | 277 | | `global_atomic_csub`, `buffer_atomic_csub` (GFX10.3) | `tmp = M; M = (DATA > tmp) ? 0 : tmp - DATA`, unsigned: a subtract clamped at 0 that never wraps; with `glc` the old M comes back. Measured on gfx1036 by @Vonsek (#3193): 162/162 (memory, data) pairs over both forms. Same `OpAtomicCompareExchange` loop as `inc`/`dec` (`Gen.atomic_inc_dec_cas`), so exact under contention; operands, inactive lanes, buffer bounds and `glc` as `inc`/`dec`. Until 2026-11-22 both raised `Unsupported`. CPU-checked by `tests/semantic/test_atomic_csub.py` (a 9×9 value grid, no `glc`, buffer bounds, 32 lanes on one dword against 32 sequential subtracts, seeded values). The translation is not GPU-verified. | |
diff --git a/tests/semantic/test_fma_mix_literal.py b/tests/semantic/test_fma_mix_literal.py index c22efb48d..75608f4a9 100644 --- a/tests/semantic/test_fma_mix_literal.py +++ b/tests/semantic/test_fma_mix_literal.py | |||
| @@ -118,7 +118,7 @@ class FmaMixLiteralTest(unittest.TestCase): | |||
| 118 | def test_llvm23_lit_form_of_an_f32_source(self): | 118 | def test_llvm23_lit_form_of_an_f32_source(self): |
| 119 | # llvm-mc 23.1.5's own text and encoding for these bytes: the f32 source's literal 0x40400000 (3.0) prints | 119 | # llvm-mc 23.1.5's own text and encoding for these bytes: the f32 source's literal 0x40400000 (3.0) prints |
| 120 | # in full as lit(...), and the printed encoding keeps only its low half (0). LLVM 21 prints the inline 0 and | 120 | # in full as lit(...), and the printed encoding keeps only its low half (0). LLVM 21 prints the inline 0 and |
| 121 | # drops the literal word. fma(3.0, 2.0, 0) = 6.0. The pipeline library has 5,733 such operands (#4343). | 121 | # drops the literal word (FmaMixInlineLiteralTest). fma(3.0, 2.0, 0) = 6.0. The pipeline library has 5,733 such operands (#4343). |
| 122 | text = "v_fma_mix_f32 v1, lit(0x40400000), v2, 0" | 122 | text = "v_fma_mix_f32 v1, lit(0x40400000), v2, 0" |
| 123 | words = "01 00 20 cc ff 04 02 02" | 123 | words = "01 00 20 cc ff 04 02 02" |
| 124 | printed = program(0x00000000, words=words, text=text) | 124 | printed = program(0x00000000, words=words, text=text) |
| @@ -149,5 +149,88 @@ class FmaMixLiteralTest(unittest.TestCase): | |||
| 149 | blob(program(0x40400001, words=words, text=text))) | 149 | blob(program(0x40400001, words=words, text=text))) |
| 150 | 150 | ||
| 151 | 151 | ||
| 152 | def printed_and_stored(text, printed_words, stored_words, literal): | ||
| 153 | """(listing, blob) for llvm-mc 21's print of a 12-byte v_fma_mix*: an 8-byte printed encoding, while the blob | ||
| 154 | and the header's code range hold the stored 12 bytes (shader_disasm.py names the range it disassembled).""" | ||
| 155 | printed = [MOV, (text, printed_words), EXP, END] | ||
| 156 | stored = [MOV, (text, f"{stored_words} {lit(literal)}"), EXP, END] | ||
| 157 | data = listing(printed).decode("utf-8").split("\n", 1) | ||
| 158 | end = CODE_START + sum(len(bytes.fromhex(enc)) for _, enc in stored) | ||
| 159 | header = data[0].replace(data[0].split("code ")[1].split(" ")[0], f"0x{CODE_START:x}..0x{end:x}") | ||
| 160 | return (header + "\n" + data[1]).encode("utf-8"), blob(stored) | ||
| 161 | |||
| 162 | |||
| 163 | def with_invalid(data): | ||
| 164 | return data + b"\n; --- llvm-mc messages ---\n; <stdin>:1:1: warning: invalid instruction encoding\n" | ||
| 165 | |||
| 166 | |||
| 167 | class FmaMixInlineLiteralTest(unittest.TestCase): | ||
| 168 | """llvm-mc 21 prints a v_fma_mix* literal whose low half is an inline constant's f16 value as that constant, in | ||
| 169 | 8 bytes (the text and encodings below are llvm-mc 21.1.8's, -mcpu=gfx1013, for the stored bytes). The literal and | ||
| 170 | the 4 bytes come from the blob, and the instructions after it keep their offsets (EXP and s_endpgm are checked | ||
| 171 | against the blob 4 bytes further on).""" | ||
| 172 | |||
| 173 | def run_ps(self, data, fssm): | ||
| 174 | return mrt0_words(translate_listing(NAME, data, fssm=fssm)) | ||
| 175 | |||
| 176 | def both_paths(self, data, fssm, want): | ||
| 177 | self.assertEqual(self.run_ps(data, fssm), [f2u(want)] * 4) | ||
| 178 | self.assertEqual(self.run_ps(with_invalid(data), fssm), [f2u(want)] * 4) | ||
| 179 | |||
| 180 | def test_f32_source_keeps_its_high_half(self): | ||
| 181 | # Stored literal 0x40400000 (3.0) read as f32 by src0; llvm-mc 21 prints `0`. fma(3.0, 2.0, 0) = 6.0. | ||
| 182 | data, fssm = printed_and_stored("v_fma_mix_f32 v1, 0, v2, 0", "01 00 20 cc 80 04 02 02", | ||
| 183 | "01 00 20 cc ff 04 02 02", 0x40400000) | ||
| 184 | self.both_paths(data, fssm, 6.0) | ||
| 185 | # LLVM's text alone (no blob) is what the listing says: fma(0, 2.0, 0) = 0. | ||
| 186 | self.assertEqual(self.run_ps(data, None), [0] * 4) | ||
| 187 | |||
| 188 | def test_f16_halves_of_one_literal(self): | ||
| 189 | # 0x3c003800: src0 the low half 0.5, src2 the high half 1.0 (op_sel:[0,0,1] op_sel_hi:[1,0,1]); llvm-mc 21 | ||
| 190 | # prints 0.5 for both. fma(0.5, 2.0, 1.0) = 2.0, where the print would give 1.5. | ||
| 191 | data, fssm = printed_and_stored("v_fma_mix_f32 v1, 0.5, v2, 0.5 op_sel:[0,0,1] op_sel_hi:[1,0,1]", | ||
| 192 | "01 60 20 cc f0 04 c2 0b", "01 60 20 cc ff 04 fe 0b", 0x3C003800) | ||
| 193 | self.both_paths(data, fssm, 2.0) | ||
| 194 | |||
| 195 | def test_neg_and_abs_come_from_the_stored_bits(self): | ||
| 196 | data, fssm = printed_and_stored("v_fma_mix_f32 v1, neg(0), v2, 0", "01 00 20 cc 80 04 02 22", | ||
| 197 | "01 00 20 cc ff 04 02 22", 0x40400000) | ||
| 198 | self.both_paths(data, fssm, -6.0) | ||
| 199 | data, fssm = printed_and_stored("v_fma_mix_f32 v1, |0|, v2, 0", "01 01 20 cc 80 04 02 02", | ||
| 200 | "01 01 20 cc ff 04 02 02", 0xC0400000) | ||
| 201 | self.both_paths(data, fssm, 6.0) | ||
| 202 | |||
| 203 | def test_every_inline_code_maps_to_its_f16_bits(self): | ||
| 204 | self.assertEqual(len(rdna2spirv.MIX_INLINE_F16), 90) | ||
| 205 | for code, bits in rdna2spirv.MIX_INLINE_F16.items(): | ||
| 206 | stored = bytes.fromhex("0100 20cc ff04 0202") + struct.pack("<I", 0xABCD0000 | bits) | ||
| 207 | printed = bytes.fromhex("0100 20cc") + struct.pack("<I", 0x02020400 | code) | ||
| 208 | with self.subTest(code=hex(code)): | ||
| 209 | self.assertEqual(rdna2spirv.mix_inline_literal(stored, printed, "v_fma_mix_f32"), 0xABCD0000 | bits) | ||
| 210 | |||
| 211 | def test_a_low_half_that_is_not_the_printed_constant_is_refused(self): | ||
| 212 | data, fssm = printed_and_stored("v_fma_mix_f32 v1, 0, v2, 0", "01 00 20 cc 80 04 02 02", | ||
| 213 | "01 00 20 cc ff 04 02 02", 0x40400001) | ||
| 214 | with self.assertRaises(rdna2spirv.Unsupported) as cm: | ||
| 215 | self.run_ps(data, fssm) | ||
| 216 | self.assertIn("v_fma_mix_f32 at 0x104", str(cm.exception)) | ||
| 217 | |||
| 218 | def test_any_other_difference_is_still_refused(self): | ||
| 219 | # The stored instruction reads v3 as src1 (SRC1 field 0x103), not the printed v2. | ||
| 220 | data, fssm = printed_and_stored("v_fma_mix_f32 v1, 0, v2, 0", "01 00 20 cc 80 04 02 02", | ||
| 221 | "01 00 20 cc ff 06 02 02", 0x40400000) | ||
| 222 | with self.assertRaises(rdna2spirv.Unsupported) as cm: | ||
| 223 | self.run_ps(data, fssm) | ||
| 224 | self.assertIn("dropped", str(cm.exception)) | ||
| 225 | |||
| 226 | def test_only_v_fma_mix_and_only_literal_fields(self): | ||
| 227 | stored = bytes.fromhex("0100 20cc ff04 0202") + struct.pack("<I", 0x40400000) | ||
| 228 | printed = bytes.fromhex("0100 20cc 8004 0202") | ||
| 229 | self.assertIsNone(rdna2spirv.mix_inline_literal(stored, printed, "v_pk_fma_f16")) | ||
| 230 | self.assertIsNone(rdna2spirv.mix_inline_literal(stored[:8], printed, "v_fma_mix_f32")) | ||
| 231 | # A stored inline 0 (0x80) printed as 0: nothing to restore, and same_encoding already accepts it. | ||
| 232 | self.assertIsNone(rdna2spirv.mix_inline_literal(printed + bytes(4), printed, "v_fma_mix_f32")) | ||
| 233 | |||
| 234 | |||
| 152 | if __name__ == "__main__": | 235 | if __name__ == "__main__": |
| 153 | unittest.main() | 236 | unittest.main() |
diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index 421c79c90..363bd4c44 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py | |||
| @@ -16866,6 +16866,51 @@ def restore_mix_literal(ins, raw, literal): | |||
| 16866 | ins["args"][1 + i] = {**ins["args"][1 + i], "v": literal} | 16866 | ins["args"][1 + i] = {**ins["args"][1 + i], "v": literal} |
| 16867 | 16867 | ||
| 16868 | 16868 | ||
| 16869 | # The f16 bits each inline constant code stands for when llvm-mc 21 prints a v_fma_mix* literal as that constant: | ||
| 16870 | # 0..64, -1..-16, +-0.5, +-1.0, +-2.0, +-4.0 and 1/(2*pi). Measured with llvm-mc 21.1.8 (-mcpu=gfx1013) over all 65,536 | ||
| 16871 | # low halves of a literal under three high halves (0x0000, 0x3f80, 0xffff): exactly these 90 low halves print as the | ||
| 16872 | # inline constant, whatever the high half; every other literal prints in full. | ||
| 16873 | MIX_INLINE_F16 = {**{0x80 + n: n for n in range(65)}, **{0xC0 + n: -n & 0xFFFF for n in range(1, 17)}, | ||
| 16874 | 0xF0: 0x3800, 0xF1: 0xB800, 0xF2: 0x3C00, 0xF3: 0xBC00, 0xF4: 0x4000, 0xF5: 0xC000, | ||
| 16875 | 0xF6: 0x4400, 0xF7: 0xC400, 0xF8: 0x3118} | ||
| 16876 | |||
| 16877 | |||
| 16878 | def mix_inline_literal(raw, printed, op): | ||
| 16879 | """The 32-bit literal of a v_fma_mix* that llvm-mc 21 printed as an inline constant, else None. | ||
| 16880 | |||
| 16881 | llvm-mc 21's gfx1013 decoder reads a v_fma_mix* literal as 16 bits. When those low 16 bits are the f16 value of an | ||
| 16882 | inline constant (MIX_INLINE_F16), it prints that constant and re-encodes the instruction in 8 bytes: each SRC | ||
| 16883 | field that held 0xff (the literal) holds the inline code instead, and the literal word is gone (0x3f800000, 1.0 as | ||
| 16884 | an f32 source, prints as 0; 0x3c003800 as `0.5`). llvm-mc 23 prints lit(...) instead. ``raw`` is the 12 stored | ||
| 16885 | bytes at the instruction's offset, ``printed`` the 8-byte printed encoding. Only that change is accepted: every | ||
| 16886 | source field that differs must be 0xff in the blob and the inline code of the literal's low half in the print, | ||
| 16887 | and the rest of the 8 bytes must match exactly (same_encoding with all three sources live).""" | ||
| 16888 | if not op.startswith("v_fma_mix") or len(raw) != 12 or len(printed) != 8 or raw[3] != 0xCC: | ||
| 16889 | return None | ||
| 16890 | literal = int.from_bytes(raw[8:], "little") | ||
| 16891 | stored, shown = int.from_bytes(raw[4:8], "little"), int.from_bytes(printed[4:8], "little") | ||
| 16892 | patched = shown | ||
| 16893 | for i in range(3): | ||
| 16894 | field = (shown >> 9 * i) & 0x1FF | ||
| 16895 | if (stored >> 9 * i) & 0x1FF == 0xFF and field != 0xFF and MIX_INLINE_F16.get(field) == literal & 0xFFFF: | ||
| 16896 | patched = (patched & ~(0x1FF << 9 * i)) | (0xFF << 9 * i) | ||
| 16897 | if patched == shown or not same_encoding(raw[:8], printed[:4] + patched.to_bytes(4, "little"), op, 4): | ||
| 16898 | return None | ||
| 16899 | return literal | ||
| 16900 | |||
| 16901 | |||
| 16902 | def restore_mix_inline_literal(ins, raw, literal): | ||
| 16903 | """Give ``ins`` the stored instruction mix_inline_literal accepted: every source the blob encodes as the literal | ||
| 16904 | (SRC field 0xff) reads the 32-bit ``literal`` with the stored neg (NEG) and abs (NEG_HI, as abs on v_fma_mix*) | ||
| 16905 | bits, and the encoding is the 12 stored bytes, so the next instruction starts after them.""" | ||
| 16906 | w0, w1 = int.from_bytes(raw[:4], "little"), int.from_bytes(raw[4:8], "little") | ||
| 16907 | for i in range(3): | ||
| 16908 | if (w1 >> 9 * i) & 0x1FF == 0xFF: | ||
| 16909 | ins["args"][1 + i] = {"k": "imm", "v": literal, "neg": bool((w1 >> (29 + i)) & 1), | ||
| 16910 | "abs": bool((w0 >> (8 + i)) & 1)} | ||
| 16911 | ins["enc"] = raw.hex() | ||
| 16912 | |||
| 16913 | |||
| 16869 | def output_signature(path, name): | 16914 | def output_signature(path, name): |
| 16870 | """OSG1 elements as (register, system value), one per register (first element wins).""" | 16915 | """OSG1 elements as (register, system value), one per register (first element wins).""" |
| 16871 | d = shader_container(path, name) | 16916 | d = shader_container(path, name) |
| @@ -17036,7 +17081,9 @@ def check_printed_encodings(path, info): | |||
| 17036 | blob = native_blob(path, info["name"]) | 17081 | blob = native_blob(path, info["name"]) |
| 17037 | if int(m.group(3), 16) > len(blob): | 17082 | if int(m.group(3), 16) > len(blob): |
| 17038 | return | 17083 | return |
| 17084 | shift = 0 # bytes the decoder read past the printed encodings so far (mix_inline_literal) | ||
| 17039 | for ins in info["insns"]: | 17085 | for ins in info["insns"]: |
| 17086 | ins["pc"] += shift | ||
| 17040 | if not ins.get("enc"): | 17087 | if not ins.get("enc"): |
| 17041 | continue | 17088 | continue |
| 17042 | enc = bytes.fromhex(ins["enc"]) | 17089 | enc = bytes.fromhex(ins["enc"]) |
| @@ -17046,6 +17093,12 @@ def check_printed_encodings(path, info): | |||
| 17046 | if literal is not None: | 17093 | if literal is not None: |
| 17047 | restore_mix_literal(ins, raw, literal) | 17094 | restore_mix_literal(ins, raw, literal) |
| 17048 | continue | 17095 | continue |
| 17096 | raw12 = blob[ins["pc"]:ins["pc"] + 12] | ||
| 17097 | literal = mix_inline_literal(raw12, enc, base_op(ins["op"])) | ||
| 17098 | if literal is not None: | ||
| 17099 | restore_mix_inline_literal(ins, raw12, literal) | ||
| 17100 | shift += 4 | ||
| 17101 | continue | ||
| 17049 | raise Unsupported(f"{ins['op']} at 0x{ins['pc']:x}: the blob holds {raw.hex()}, LLVM printed " | 17102 | raise Unsupported(f"{ins['op']} at 0x{ins['pc']:x}: the blob holds {raw.hex()}, LLVM printed " |
| 17050 | f"{enc.hex()} (a field the disassembler dropped)") | 17103 | f"{enc.hex()} (a field the disassembler dropped)") |
| 17051 | 17104 | ||
| @@ -17102,6 +17155,11 @@ def translate(path): | |||
| 17102 | if literal is not None: | 17155 | if literal is not None: |
| 17103 | restore_mix_literal(ins, blob[at:at + len(enc)], literal) | 17156 | restore_mix_literal(ins, blob[at:at + len(enc)], literal) |
| 17104 | break | 17157 | break |
| 17158 | literal = mix_inline_literal(blob[at:at + 12], enc, base_op(op)) | ||
| 17159 | if literal is not None: | ||
| 17160 | restore_mix_inline_literal(ins, blob[at:at + 12], literal) | ||
| 17161 | enc = blob[at:at + 12] # the 12 bytes the decoder read: the next one starts after them | ||
| 17162 | break | ||
| 17105 | at += 4 | 17163 | at += 4 |
| 17106 | skipped += 4 | 17164 | skipped += 4 |
| 17107 | if skipped > 4096 or at >= len(blob): | 17165 | if skipped > 4096 or at >= len(blob): |