feat: 16-bit integer VALU (VOP3 16-bit ops and packed VOP3P) in the RDNA 2 interpreter and HLSL — 1,178/1,490 #232
No reviewers
Labels
No labels
accessibility
bloqué : binaire console
bug
chargeur
console-dll
cpu
documentation
duplicate
enhancement
good first issue
gpu
help wanted
invalid
performance
question
runtime-gdk
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/LooperSalty--AnyXboxX!232
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/232/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@KainanP on GitHub · 2026-11-25 20:05 UTC · original
KainanP:feat/int16-valu→main· +832 −40 · 19 files · 1 commits · merged 2026-11-26 00:15 UTCChange
The reference interpreter and the HLSL recompiler now run the 16-bit integer VALU: 40 encodings that until now raised
Untranslated/Untranslatable.v_add/sub_nc_{u16,i16},v_mul_lo_u16,v_lshlrev_b16,v_lshrrev_b16,v_ashrrev_i16,v_max/min_{u16,i16},v_mad_{u16,i16},v_min3/max3/med3_{u16,i16}(20)op_sel[0-2]readS[31:16],op_sel[3]writesD[31:16], the other half of D is kept; results wrap to 16 bits;clampsaturates add/sub/mad to [0, 0xFFFF] or [-32768, 32767]v_mad_u32_u16,v_mad_i32_i16op_sel[0-1]pick the source halves,clampsaturates the 32-bit sum;op_sel[2]/op_sel[3](32-bit S2 and D) refusedv_cvt_pk_u16_u32,v_cvt_pk_i16_i32,v_add_nc_i32,v_sub_nc_i32D = {sat16(S1), sat16(S0)}; 32-bit wrap,clampsaturates signedv_pk_{add,sub,mad}_{u16,i16},v_pk_mul_lo_u16,v_pk_{max,min}_{u16,i16},v_pk_lshlrev_b16,v_pk_lshrrev_b16,v_pk_ashrrev_i16(14)op_sel, half 1 fromop_sel_hi;clampsaturates add/sub/mad per half, no-op on max/minA constant source is its 32-bit value split like a register: inline integers sign-extended (
-16=0xFFF0/0xFFFF), inline floats their float32 bits (1.0=0x0000/0x3F80), a literal its whole dword (LLVM, below).Refused by name (rule 3), in both the interpreter and HLSL: VOP3
abs/neg/omodand VOP3Pneg_lo/neg_hion these integer ops;clampwhere it is not implemented (v_mul_lo_u16andv_pk_mul_lo_u16, the 16-bit shifts and VOP3 min/max, min3/max3/med3, the packs, the packed shifts);op_sel[2]/op_sel[3]onv_mad_u32_u16/v_mad_i32_i16;op_selon the packs and onv_add/sub_nc_i32. The select bits of the third source of a two-source op are ignored (no third operand is read; LLVM itself writesop_sel_hi[2] = 0there).New files:
emu/rdna2_exec_int16.py(interpreter, refusal rules shared with HLSL) andemu/rdna2_hlsl_int16.py(integer-only HLSL, no 64-bit types, so it runs on Intel UHD too).Driver finding (NVIDIA): the first HLSL version wrote the signed 16-bit saturating add/sub as
clamp(Sext16(a) ± Sext16(b), -32768, 32767), masked to 16 bits when stored in its half. On the RTX 4060 Laptop GPU the sign-extended high half (0xFFFF) reached the destination in 10 and 9 of 32 lanes ofv_add_nc_i16 clamp/v_sub_nc_i16 clamp(e.g.0xFFFFAAABwhere the interpreter gives0x0001AAAB); WARP and Intel UHD were right. It is now computed from the sign bits inuint(AddSatI16), and the parity test covers it. (v_mad_i16 clampandv_cvt_pk_i16_i32, which use the same clamp, were right.)Coverage, recounted on the tree merged with main
e921268: interpreter 1,208 → 1,248 of 1,490 encodings (README 81% → 84%, fromrdna2_exec.coverage()); test count 1400+ → 1500+ (1,517 collected). Same change in the 9 READMEs.The int16 helpers (
AddSatI16,AddSatI32…) are now also part of the graphics HLSL (rdna2_hlsl_graphics.py), so a vertex or pixel shader using them compiles. A vertex-shader test runs the helper-backed ops on the GPU and compares them with the interpreter. Same change in the 9 READMEs.Verification
Branch rebased on main
e921268(head40aae0f). Tests were run on the same tree rebased on9a06443;e921268only adds launcher/docs changes.HLSL path on the other two adapters (the two new files plus
test_rdna2_exec_dot.py,test_rdna2_vop3p.py,test_rdna2_alu_parity.py,test_rdna2_exec.py,test_rdna2_exec_vop3_more.py,test_rdna2_hlsl_wave64.py,test_rdna2_exec_cmp16.py,test_rdna2_hlsl.py, including the new vertex-shader test):Without the
rdna2_hlsl_graphics.pychange, dxc rejects the vertex shader of the new graphics test (AddSatI16undefined); with it, the shader compiles. The skips of the full run are the usual ones on this machine (Rust crates and TomBox DLLs not built, no Vulkan SDK forspirv-as).Oracle and test data
test_int16_ops_match_interpreter_on_gpu(wave32) and..._in_wave64: 174 instructions, every encoding of the family with itsop_sel/op_sel_hi/clampvariants plus inline integer, inline float and literal sources, run under a partial EXEC (0x0F0FF0F3;0xF00F0FF0for the upper 32 lanes) on per-lane sources whose halves are boundary values (0x0000 0x0001 0x0002 0x000F 0x0011 0x7FFE 0x7FFF 0x8000 0x8001 0xFFFE 0xFFFF 0x1234 0xAAAA 0x4000 0xC000, one lane in eight random), each destination preset to a per-lane old value.test_parity_program_results_vary_by_laneguards that results vary per lane and that inactive lanes keep their old value.tests/test_rdna2_exec_int16.py, arithmetic in the comments): 95 tests covering wrap and saturation at both ends of each type, shift counts masked to 4 bits, high-half selects and the destination half, the 32-bit forms, constants, inactive lanes, and every refused form (with the HLSL refusal). None of the expected values is a hardware measurement: they come from the ISA pseudocode and the LLVM cross-checks named in the comments.emu.rdna2_exec_int16does not exist) and each of the 40 encodings raisesinstruction non traduite.test_rdna2_exec_dot.pynow usesv_pk_add_f16as its example of a still-refused packed op.Sources (labelled per
docs/research/rdna2-sources.md; printed page numbers of the archived PDF, SHA-25622aec03f…4795):v_add/sub_nc_i32, and forV_MAD_U16/V_MAD_I16"If op_sel[3] is 1: Result is written to 16 MSBs of destination VGPR and lo 16 bits are preserved". §13.3 tables 82 and 86 (p. 258, 263): VOP3 OPSEL "Operand select for 16-bit data. 0 = select low half, 1 = select high half",[14]= dest; VOP3P OPSEL / OPSEL_HI. §6.2.5 table 21 (p. 44): opcodes usable with OPSEL. §6.5 (p. 48): integer clamp "clamps the result to the largest and smallest representable value", so a max or min, whose result is one of its in-range inputs, is unchanged.lib/Target/AMDGPU/GCNSubtarget.cppzeroesHigh16BitsOfDest: "On gfx10, all 16-bit instructions preserve the high bits" (the kept half of D).test/CodeGen/AMDGPU/{uaddsat,usubsat,saddsat,ssubsat}.llselect the saturating i16, v2i16 and i32 add/sub tov_add/sub_nc_{u16,i16},v_pk_add/sub_{u16,i16}andv_add/sub_nc_i32withclampon GFX10.ssubsatusesv_sub_nc_i16 … clamp, so the saturation is signed, although the PDF says "unsigned 16-bit integer domain".lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cppgetInlineEncodingV216: for (packed) 16-bit instructions "integer encodings (-16 .. 64) are always produced as sign-extended 32-bit values", float encodings "for UI16 instructions: corresponding single-precision float value".test/CodeGen/AMDGPU/add.v2i16.ll(GFX10) encodes<123, 456>as the literal0x1c8007band<0, 16256>as1.0.nir_opcodes.pypack_uint_2x16/pack_sint_2x16"Convert two (un)signed integers into a packed (un)signed short (clamp is applied)", which ACO (aco_select_nir_alu.cpp) emits asv_cvt_pk_u16_u32/v_cvt_pk_i16_i32. No code copied.Assumptions left open (measurement request: akimfomin/PortBridge#84, sections A, B and E)
v_cvt_pk_u16_u32/v_cvt_pk_i16_i32saturate (Mesa/ACO), e.g.v_cvt_pk_u16_u32(0x10000, 0x1234)=0x1234FFFF.op_selon a VOP3 16-bit op gives its high half, as LLVM'sadd.v2i16.llshows for the packed form.v_add/sub_nc_i32andv_mad_u32_u16/v_mad_i32_i16, and clamp as a no-op onv_pk_max/min, follow from §6.5 and LLVM rather than from a measurement.clamponv_mul_lo_u16/v_pk_mul_lo_u16(the PDF saysV_MUL_LO_U16"Supports saturation", but whether that saturates the full product is not stated), the 16-bit shifts, VOP3 min/max/min3/max3/med3, the packs and the packed shifts;op_sel[2]/op_sel[3]onv_mad_u32_u16/v_mad_i32_i16.v_sad_u16is left out of this PR.Checklist
E_NOTIMPL), never a fake success. (Unmeasured forms raiseUntranslated/Untranslatablenaming the instruction and the modifier.)test_readme_sync.pychecks it).Made with AI assistance: Claude Code with Claude Opus 5.5, for KainanP.
🤖 Generated with Claude Code
- VOP3 16-bit ops: v_add/sub_nc_{u16,i16}, v_mul_lo_u16, v_lshlrev_b16, v_lshrrev_b16, v_ashrrev_i16, v_max/min_{u16,i16}, v_mad_{u16,i16}, v_min3/max3/med3_{u16,i16}. op_sel[0-2] read the high half of a source, op_sel[3] writes D[31:16], the other half of D is kept; clamp saturates add/sub/mad to the type's range. - v_mad_u32_u16 / v_mad_i32_i16: op_sel[0-1] pick the source halves, clamp saturates the 32-bit sum; op_sel[2]/op_sel[3] (32-bit S2 and D) refused. - v_cvt_pk_u16_u32 / v_cvt_pk_i16_i32 (saturating packs) and v_add/sub_nc_i32 (clamp saturates signed). - Packed VOP3P: v_pk_{add,sub,mad}_{u16,i16}, v_pk_mul_lo_u16, v_pk_{max,min}_{u16,i16}, v_pk_{lshlrev,lshrrev}_b16, v_pk_ashrrev_i16; op_sel / op_sel_hi pick each half, clamp saturates add/sub/mad and is a no-op on max/min. - Constants are their 32-bit value split like a register (inline integers sign-extended, inline floats their float32 bits, a literal its whole dword). Sources: AMD RDNA 2 ISA (November 2020) sections 12.10 and 12.12 (pseudocode, "Supports saturation", op_sel[3] on v_mad_u16/i16 preserving the other half), 13.3 tables 82 and 86 (OPSEL), 6.2.5 (opcodes usable with OPSEL) and 6.5 (integer clamp saturates to the representable range). Cross-checks from LLVM 20.1.8: GCNSubtarget.cpp ("On gfx10, all 16-bit instructions preserve the high bits"), the uaddsat/usubsat/saddsat/ssubsat CodeGen tests (GFX10 selects the saturating add/sub to these ops with clamp, v_sub_nc_i16 signed), AMDGPUBaseInfo.cpp getInlineEncodingV216 and add.v2i16.ll (constants). The two packs follow Mesa's pack_uint_2x16 / pack_sint_2x16 ("clamp is applied"), which ACO emits as these opcodes; assumed, to be measured (akimfomin/PortBridge#84), like a 32-bit literal read through op_sel on a VOP3 16-bit op. Refused by name: VOP3 abs/neg/omod and VOP3P neg_lo/neg_hi on these integer ops; clamp on v_mul_lo_u16, v_pk_mul_lo_u16, the 16-bit shifts, VOP3 min/max, min3/max3/med3, the packs and the packed shifts; op_sel[2]/[3] on v_mad_u32_u16 / v_mad_i32_i16; op_sel on the packs and on v_add/sub_nc_i32. HLSL: integer-only expressions; the signed 16-bit saturating add/sub is computed from sign bits because clamp(Sext16(a) + Sext16(b)) then a 16-bit mask let the sign-extended high half reach the destination on an RTX 4060 Laptop GPU (WARP and Intel UHD were right). The int16 helpers (AddSatI16, AddSatI32...) are part of the compute, wave64 and graphics (vertex/pixel) HLSL; a vertex shader test runs the helper-backed ops on the GPU. Interpreter coverage 1,208 -> 1,248 of 1,490 encodings (README 81% -> 84%), test count 1400+ -> 1500+. New tests in tests/test_rdna2_exec_int16.py (hand-worked expectations, refusals) and tests/test_rdna2_hlsl_int16.py (byte-for-byte GPU parity in wave32 and wave64 under a partial EXEC). Co-Authored-By: Claude Opus 5.5 <[email protected]>@LooperSalty on GitHub · 2026-11-25 21:03 UTC · original
Thanks, the 16-bit VALU and packed VOP3P work is solid (96 tests fail without the change, README count matches).
The docstrings cite measurements from the Leonida project (ubiquitarianism/leonida, docs/design.md and docs/shader_validation.md at 9f9a7c5f). That repository was taken down on 2026-11-24 after a DMCA notice, and rule 2 of AGENTS.md only allows public sources (AMD's RDNA 2 ISA, LLVM's tests, Microsoft Learn, compatibly licensed code) and measurements from our own runs. Please remove these Leonida citations and, where a behaviour still needs a source, cite the AMD ISA section or an LLVM test instead (or re-measure it with our own tooling and say how it was obtained).
@KainanP on GitHub · 2026-11-25 20:17 UTC · original · edited 2026-11-25 21:35 UTC
Measurement request for the open assumptions above (pack saturation, op_sel on a 32-bit literal, unmeasured 16-bit
clamp): akimfomin/PortBridge#84. (Edited 2026-11-25: a citation of the deleted Leonida docs removed;v_sad_u16is not part of this PR.)@LooperSalty on GitHub · 2026-11-25 21:45 UTC · original
Follow-up after the review:
rdna2_exec_int16.py:29-31attributes measurements to the Leonida project; please remove that attribution (cite the AMD ISA or LLVM, or label the behaviour as assumed). Then rebase on main and recount the README RDNA 2 line withrdna2_exec.coverage()on the merged tree. The rest looks good (tests fail without the change).@KainanP on GitHub · 2026-11-25 22:54 UTC · original
Thanks. Updated and rebased on main
e921268(one commit,40aae0f):v_mad_u16/i16, plus LLVM 20.1.8GCNSubtarget.cpp, "On gfx10, all 16-bit instructions preserve the high bits".uaddsat/usubsat/saddsat/ssubsat.llon GFX10 (ssubsat→v_sub_nc_i16 … clamp, so the saturation is signed).AMDGPUBaseInfo.cppgetInlineEncodingV216andadd.v2i16.ll(GFX10).v_pk_mul_lo_u16(the PDF marksV_MUL_LO_U16"Supports saturation", which contradicts a no-op), andop_sel[2]/op_sel[3]onv_mad_u32_u16/v_mad_i32_i16. Still assumed: the pack saturation (Mesa/ACO) and a 32-bit literal under VOP3 op_sel. All of these are in akimfomin/PortBridge#84 (sections A, B, E).rdna2_hlsl_graphics.pydid not include the int16 helpers, so a vertex or pixel shader using e.g.v_add_nc_i16 … clampcalled an undefinedAddSatI16(dxc rejects that HLSL). A new vertex-shader test runs those ops on the GPU against the interpreter.Checks: full suite on the RTX 4060 (1499 passed, 17 skipped); int16 and parity files on WARP (253 passed) and Intel UHD (253 passed); ruff clean.
AI use: Claude Code (Claude Opus 5.5).