shader: 32-bit literals for signed 64-bit integer operands are zero-extended, hardware sign-extends them #10771

Closed
opened 2026-10-24 23:44:18 +00:00 by TechLevelZero · 0 comments
TechLevelZero commented 2026-10-24 23:44:18 +00:00 (Migrated from github.com)
Contributor

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, 0 is 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 every LiteralConstant. #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-oracle and through the recompiler on Vulkan, with the results compared word by word.

AnyPS5 commit
main c8d34aa8f4.

System

  • Hardware: the integrated Radeon Graphics of a Ryzen 7 9700X (gfx1036, RDNA2) through tools/hw-oracle, ROCm 7.1.1, wave32, IEEE 0, DX10 clamp 1, FLOAT_MODE 0xc0.
  • AnyPS5: an execution-test-style runner with the same prologue and epilogue as the oracle template, on an RX 9070 XT (RADV, Mesa 26.0.8), ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1.

The PS5's gfx1013 is not measured.

Steps to reproduce
body.s:

  v_cmp_lt_i64_e64 s24, 0x80000000, 0
  v_cndmask_b32_e64 v10, 0, 1, s24
  v_ashrrev_i64 v[12:13], 0, 0x80000000
echo "0 0 0 0" > rows.txt
python3 tools/hw-oracle/hw_oracle.py body.s rows.txt --ieee 0 --denorm32 0 --denorm16 3 --dx10-clamp 1 --round32 0 --round16 0 --fp16-overflow 0 --outs 4
00000001 00000000 80000000 ffffffff

The recompiler gives 00000000 00000000 80000000 00000000 for the same words (v10 false, and a zero high half in v13).

Log
Each row is the literal 0x80000000 against 0 (or as the shift or add source). It shows which high half each side uses:

Instruction Operand type Hardware AnyPS5
v_cmp_lt_i64, v_cmp_gt_i64, v_cmp_le_i64 (both operand orders) i64 sign-extended zero-extended
v_ashrrev_i64 v[10:11], 0, 0x80000000 i64 ffffffff:80000000 00000000:80000000
v_mad_i64_i32 v[10:11], s24, 0, 0, 0x80000000 (src2) i64 ffffffff:80000000 00000000:80000000
s_ashr_i64 s[24:25], 0x80000000, 0 i64 ffffffff:80000000 00000000:80000000
s_bfe_i64 (src0), s_flbit_i32_i64 i64 sign-extended zero-extended
v_cmp_eq_i64, v_cmp_ne_i64 against a register pair holding the sign-extended value i64 sign-extended zero-extended
v_cmp_lt_u64, v_cmp_gt_u64, v_cmp_eq_u64 u64 zero-extended zero-extended (match)
v_lshrrev_b64, v_lshlrev_b64 b64 zero-extended zero-extended (match)
v_mad_u64_u32 (src2) u64 zero-extended zero-extended (match)
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_b64 b64 zero-extended zero-extended (match)
v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8 (64-bit sources) b64 zero-extended zero-extended (match)
v_add_f64 v[10:11], 0, 0xbff00000 f64 literal in the high half literal in the high half (match)

For 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_b64 and the always-false/always-true compares can't tell the two apart.

What was checked

  • All results repeat in 3 of 3 runs, and every lane is the same.
  • Inline constants are unaffected: they are already sign-extended (#39).
  • The first case came from a random program, v_cmp_ge_i64_e64 s32, 8, 0xff00ff00. It is true on hardware and false in AnyPS5.
  • Not checked: v_cmpx_*_i64 (they change EXEC, which this harness doesn't use; they share the translation path of the v_cmp_*_i64 and get the same fix), the PS5's gfx1013 and RDNA1.

A fix with an execution test built from these measurements is on TechLevelZero/AnyPS5 branch fix/signed-64-bit-literal-sign-extension. It sign-extends the literal in readU32Pair for 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)

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6018888003 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/10771). Originally posted by [TechLevelZero](https://github.com/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, 0` is 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 every `LiteralConstant`. #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-oracle` and through the recompiler on Vulkan, with the results compared word by word. **AnyPS5 commit** `main` c8d34aa8f4. **System** - Hardware: the integrated Radeon Graphics of a Ryzen 7 9700X (gfx1036, RDNA2) through `tools/hw-oracle`, ROCm 7.1.1, wave32, IEEE 0, DX10 clamp 1, `FLOAT_MODE` 0xc0. - AnyPS5: an execution-test-style runner with the same prologue and epilogue as the oracle template, on an RX 9070 XT (RADV, Mesa 26.0.8), `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`. The PS5's gfx1013 is not measured. **Steps to reproduce** `body.s`: ``` v_cmp_lt_i64_e64 s24, 0x80000000, 0 v_cndmask_b32_e64 v10, 0, 1, s24 v_ashrrev_i64 v[12:13], 0, 0x80000000 ``` ``` echo "0 0 0 0" > rows.txt python3 tools/hw-oracle/hw_oracle.py body.s rows.txt --ieee 0 --denorm32 0 --denorm16 3 --dx10-clamp 1 --round32 0 --round16 0 --fp16-overflow 0 --outs 4 00000001 00000000 80000000 ffffffff ``` The recompiler gives `00000000 00000000 80000000 00000000` for the same words (`v10` false, and a zero high half in `v13`). **Log** Each row is the literal `0x80000000` against 0 (or as the shift or add source). It shows which high half each side uses: | Instruction | Operand type | Hardware | AnyPS5 | | --- | --- | --- | --- | | `v_cmp_lt_i64`, `v_cmp_gt_i64`, `v_cmp_le_i64` (both operand orders) | i64 | sign-extended | zero-extended | | `v_ashrrev_i64 v[10:11], 0, 0x80000000` | i64 | `ffffffff:80000000` | `00000000:80000000` | | `v_mad_i64_i32 v[10:11], s24, 0, 0, 0x80000000` (src2) | i64 | `ffffffff:80000000` | `00000000:80000000` | | `s_ashr_i64 s[24:25], 0x80000000, 0` | i64 | `ffffffff:80000000` | `00000000:80000000` | | `s_bfe_i64` (src0), `s_flbit_i32_i64` | i64 | sign-extended | zero-extended | | `v_cmp_eq_i64`, `v_cmp_ne_i64` against a register pair holding the sign-extended value | i64 | sign-extended | zero-extended | | `v_cmp_lt_u64`, `v_cmp_gt_u64`, `v_cmp_eq_u64` | u64 | zero-extended | zero-extended (match) | | `v_lshrrev_b64`, `v_lshlrev_b64` | b64 | zero-extended | zero-extended (match) | | `v_mad_u64_u32` (src2) | u64 | zero-extended | zero-extended (match) | | `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_b64` | b64 | zero-extended | zero-extended (match) | | `v_qsad_pk_u16_u8`, `v_mqsad_pk_u16_u8` (64-bit sources) | b64 | zero-extended | zero-extended (match) | | `v_add_f64 v[10:11], 0, 0xbff00000` | f64 | literal in the high half | literal in the high half (match) | For 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_b64` and the always-false/always-true compares can't tell the two apart. **What was checked** - All results repeat in 3 of 3 runs, and every lane is the same. - Inline constants are unaffected: they are already sign-extended (#39). - The first case came from a random program, `v_cmp_ge_i64_e64 s32, 8, 0xff00ff00`. It is true on hardware and false in AnyPS5. - Not checked: `v_cmpx_*_i64` (they change EXEC, which this harness doesn't use; they share the translation path of the `v_cmp_*_i64` and get the same fix), the PS5's gfx1013 and RDNA1. A fix with an execution test built from these measurements is on `TechLevelZero/AnyPS5` branch [`fix/signed-64-bit-literal-sign-extension`](https://github.com/TechLevelZero/AnyPS5/tree/fix/signed-64-bit-literal-sign-extension). It sign-extends the literal in `readU32Pair` for 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)
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#10771
No description provided.