shader: 32-bit literals for signed 64-bit integer operands are zero-extended, hardware sign-extends them #10771
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
Imported from boykopovar/AnyPS5. Originally posted by TechLevelZero on 2026-10-24T23:44:18Z.
What happens
When a 32-bit literal is the source of a signed 64-bit integer operand, AnyPS5 reads it with a zero high half. RDNA2 hardware copies its sign bit into the high half instead. For example,
v_cmp_lt_i64_e64 s24, 0x80000000, 0is true on hardware (0xffffffff80000000 < 0) and false in AnyPS5 (0x0000000080000000 < 0).TranslationContext::readU32Pair(core/shader/recompiler/Translation/src/TranslationContext/SubwordPacking.cpp:17) returns{literal, 0}for everyLiteralConstant. #39 chose zero-extension to match LLVM's disassembler. The measurement below shows that this holds for unsigned and bitwise operands but not for signed ones.Title, test or build step
Shader recompiler. Found by differential testing: random RDNA programs run on RDNA2 hardware through
tools/hw-oracleand through the recompiler on Vulkan, with the results compared word by word.AnyPS5 commit
mainc8d34aa8f4.System
tools/hw-oracle, ROCm 7.1.1, wave32, IEEE 0, DX10 clamp 1,FLOAT_MODE0xc0.ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1.The PS5's gfx1013 is not measured.
Steps to reproduce
body.s:The recompiler gives
00000000 00000000 80000000 00000000for the same words (v10false, and a zero high half inv13).Log
Each row is the literal
0x80000000against 0 (or as the shift or add source). It shows which high half each side uses:v_cmp_lt_i64,v_cmp_gt_i64,v_cmp_le_i64(both operand orders)v_ashrrev_i64 v[10:11], 0, 0x80000000ffffffff:8000000000000000:80000000v_mad_i64_i32 v[10:11], s24, 0, 0, 0x80000000(src2)ffffffff:8000000000000000:80000000s_ashr_i64 s[24:25], 0x80000000, 0ffffffff:8000000000000000:80000000s_bfe_i64(src0),s_flbit_i32_i64v_cmp_eq_i64,v_cmp_ne_i64against a register pair holding the sign-extended valuev_cmp_lt_u64,v_cmp_gt_u64,v_cmp_eq_u64v_lshrrev_b64,v_lshlrev_b64v_mad_u64_u32(src2)s_and_b64,s_andn2_b64,s_xnor_b64,s_not_b64,s_mov_b64,s_cselect_b64,s_lshl_b64,s_lshr_b64,s_brev_b64,s_bitcmp1_b64,s_bcnt1_i32_b64,s_flbit_i32_b64v_qsad_pk_u16_u8,v_mqsad_pk_u16_u8(64-bit sources)v_add_f64 v[10:11], 0, 0xbff00000For each row, the same instruction was also run with the literal replaced by an SGPR pair holding the zero-extended and then the sign-extended value. The literal's result matched exactly one of the two, and that is the column shown.
s_cmp_eq_u64,s_cmp_lg_u64,s_ff1_i32_b64and the always-false/always-true compares can't tell the two apart.What was checked
v_cmp_ge_i64_e64 s32, 8, 0xff00ff00. It is true on hardware and false in AnyPS5.v_cmpx_*_i64(they change EXEC, which this harness doesn't use; they share the translation path of thev_cmp_*_i64and get the same fix), the PS5's gfx1013 and RDNA1.A fix with an execution test built from these measurements is on
TechLevelZero/AnyPS5branchfix/signed-64-bit-literal-sign-extension. It sign-extends the literal inreadU32Pairfor the opcodes above, and the rest keep zero-extension. I'll open it as a pull request once my first one is merged (open-PR limit).AI-assisted: yes (Claude Code)