Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ How the generated SPIR-V expects the runtime to work:
| User data | **Push constants, 144 bytes:** dwords 0–31 = initial s0–s31 (the user SGPRs the console driver loads from the root signature: descriptor-table pointers, constants and so on), dwords 32–33 = the 64-bit **heap base**, dwords 34–35 = the **code base**: the Xbox VA where the runtime uploaded this shader's whole FSSM blob. `--mesh-compute` modules add dwords 36–37, the output buffer address (152 bytes; see "Mesh shaders as compute"). `--pc-tail-ubo` keeps only dwords 0–31 here (128 bytes) and moves the rest to a uniform block (see "Push-constant tail as a uniform block"). The runtime has to reproduce the console driver's root-signature-to-user-SGPR mapping; that comes from the exe or driver behaviour. **Mesh shaders** read their root SGPRs through the graphics root window: s16..s31 come from dwords 8..23 (where a VS, PS or GS reads s8..s23), s8..s15 are 0, s0..s7 dwords 0..7; the index records `"root_window": {"sgpr": 16, "dword": 8}` (D3D-448-GRAPHICS, `docs/d3d12_vulkan.md` 3.5). Checked by `tests/semantic/test_ms_root_window.py` (MeshEXT and `--mesh-compute`, CPU interpreter) and `d3d12_mesh_test` (RX 9070 XT). |
| Direct resource heap | In the matched Prop/Tree BatchCull compute signature the two explicit tables and scalar constant occupy s4–s6. Bind the resource heap's uint32 arena-relative byte address at s7 (push-constant byte 28). The program supplies a zero high word and scales the resource index by 32 bytes. Other stages and signatures consume different SGPRs. See [descriptor heaps and the bindless model](d3d12_vulkan.md#34-descriptor-heaps-and-the-bindless-model) for the evidence and graphics examples. |
| 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. |
| 64-bit addresses without `Int64` | The translator builds every PhysicalStorageBuffer address from one idiom: widen a dword, shift the high word by 32, OR, add or subtract (64-bit constants included), AND a mask, take a word back (narrow to 32 bits, or shift right by 32), compare two such values (the NUM_RECORDS bound), convert to a pointer. `lower_addr64` runs on the assembled module: when a shader's only 64-bit integers are that idiom it becomes (low, high) pairs of 32-bit words, `OpIAddCarry` / `OpISubBorrow` carrying into the high word, pair compares high word first, and a `uvec2` bitcast to the pointer (as `GL_EXT_buffer_reference_uvec2`), and the module drops `OpCapability Int64`, so `index.json` no longer lists `shaderInt64` for it. Shifts by 1..31 either way become the two words' shifts with the bits that cross between them (`host_addr`'s 48-bit sign extension shifts left and then arithmetically right by 16); a left shift by a 32-bit value (the swizzled buffer offset's element-size log, a V# field) becomes the same on `k & 31` with the words moved up once `k >= 32`; a 64-bit multiply (the full `index * stride` of the swizzled offset and its 32-bit range check) becomes the low words' product with its high word from the four 16-bit half products, plus each low word times the other's high word. A shader with any other 64-bit integer use keeps `Int64`: right shifts by a variable amount, a 64-bit `OpPhi` or variable; so do dynamic-V# scalar-buffer loads and 64-bit data ops. `--int64-addresses` turns the lowering off. Checked by `tests/semantic/test_addr64_lowering.py`: both forms read the same word in `spirv_mini_interp` with a plain sum, a low-word carry and a 64-bit wrap, and dropping the carry is caught; on the GPU by the whole `runtime/test/shaders` suite (103 of its 117 memory-touching shaders drop `Int64`), the D3D12 fixture tests, and the reports on PR #2463 (NVIDIA Turing to Blackwell, AMD RDNA2 to RDNA4 on both drivers, Intel UHD and Arc A770 without `shaderInt64`, SwiftShader). Motivation: Intel drivers report `shaderInt64` false while supporting `bufferDeviceAddress` (#313, #1825). |
| 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 (when the llvm-mc messages give one offset per rejected word, `shader_ir.parse_file`'s `invalid_at`, those words are skipped exactly and nothing is searched forward: a forward search let a data word printed as a copy of a later instruction take that instruction's place and lose every one after it; counted on the 1.0.2.1 pipeline library with #327's vcc-tuple decoding, 36 compute programs that stopped at "cannot re-anchor" now translate and pass `spirv-val`, none lost; `tests/semantic/test_reanchor_llvm_offsets.py`), 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`). The exact re-anchoring path tries the same repair as the forward search for a `v_dot4_i32_i8` with an inline-integer S1 whose stored op_sel_hi (5) llvm-mc prints as the default (7), `restore_dot4_inline_sel`, before it marks the word: without it such a program is refused as "reachable instruction with normalized encoding" whenever its listing also reports rejected words. Counted on the 29 FidelityFX DLL programs (1.0.2.1, LLVM 21.1.8): 22 translate without it, 24 with it, all 24 pass `spirv-val`; `tests/semantic/test_reanchor_dot4_inline_sel.py`. |
| 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. |
| 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). A third shape needs no blob: llvm-mc 21.1.8 also prints such a literal cut to its low 16 bits in the text while its printed encoding keeps all 32, so the printed-encoding check passes and the text alone read the high half as +0.0 (an f16 source reads 0 instead of its high half, an f32 source a denormal instead of its value). `restore_text_cut_mix_literal` takes the literal from the printed encoding, on the clean and the re-anchoring paths. Counted on one work folder's listings of 1.0.2.1: 47,910 instructions in 1,218 pipeline-library programs and 6 in one FSPX program, none in the game's own shaders. CPU-checked by `test_text_cut_but_printed_encoding_whole` (with and without a blob) and `test_text_immediate_other_than_the_low_half_is_left_alone`. |
Expand Down
14 changes: 14 additions & 0 deletions tests/semantic/spirv_mini_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,20 @@ def sval(x):
values[res] = val(a[2]) if val(a[1]) else val(a[3])
elif op == "OpCopyObject":
values[res] = val(a[1])
elif op == "OpBitcast" and a[0] in ptr_types:
# A (low, high) pair of 32-bit words bitcast to a PhysicalStorageBuffer pointer (rdna2spirv.lower_addr64,
# as GL_EXT_buffer_reference_uvec2): the same address OpConvertUToPtr of the 64-bit value would give.
v = val(a[1])
vt = types.t.get(id_type.get(a[1]))
if not (vt and vt[0] == "vec" and vt[2] == 2 and types.t.get(vt[1], ())[:2] == ("int", 32)):
raise UnsupportedConstruct("OpBitcast to a pointer from a type other than a 2-component 32-bit "
"integer vector")
if not (isinstance(v, tuple) and len(v) == 2 and all(isinstance(x, int) and not isinstance(x, bool)
for x in v)):
raise UnsupportedConstruct("OpBitcast to a pointer from something other than two 32-bit words")
if ptr_types[a[0]][0] != "PhysicalStorageBuffer":
raise UnsupportedConstruct("OpBitcast to a pointer that is not PhysicalStorageBuffer")
values[res] = pointers[res] = ("psb", (v[0] & MASK) | ((v[1] & MASK) << 32))
elif op == "OpBitcast":
k, v = types.t[a[0]], val(a[1])
source = types.t.get(id_type.get(a[1]))
Expand Down
9 changes: 9 additions & 0 deletions tests/semantic/spirv_wave_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,15 @@ def eval(self, lane, res, op, a):
raise UnsupportedConstruct("nested OpCompositeInsert")
v[int(a[3])] = self.raw(lane, a[1])
return tuple(v)
if op == "OpBitcast" and a[0] in m.ptr_types:
# A (low, high) uint pair bitcast to a buffer pointer (rdna2spirv.lower_addr64): the address
# OpConvertUToPtr of the 64-bit value would give.
cls, pointee = m.ptr_types[a[0]]
if cls != "PhysicalStorageBuffer":
raise UnsupportedConstruct(f"OpBitcast to storage class {cls}")
lo, hi = (int(x) for x in self.val(lane, a[1]))
lane.pointers[res] = ("psb", (lo & 0xFFFFFFFF) | ((hi & 0xFFFFFFFF) << 32), pointee)
return None
if op == "OpBitcast":
k, v = m.types[a[0]], self.val(lane, a[1])
if k[0] == "float" and isinstance(v, int) and not isinstance(v, bool):
Expand Down
Loading
Loading