fix(recompiler): widen constants of 64-bit operands instead of repeating them #39

Merged
Pacuka merged 1 commit from upstream-pr/39 into main 2026-09-28 05:22:02 +00:00
Pacuka commented 2026-09-27 17:00:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-09-27T17:00:16Z.


Problem

TranslationContext::readU32Pair builds a 64-bit source from two 32-bit reads, taking the high half from offsetOperand(operand, 1). For registers that is the next register. For a literal or an inline constant, offsetOperand returns the same operand, so the constant ended up in both halves: the literal L was read as L:L instead of 0:L.

Found in The Smurfs Dreams (UE). A compute shader writes the args of an indirect draw and rounds a count up to a multiple of 21:

v_add_nc_u32   v4, 20, v3
v_mad_u64_u32  v[2:3], vcc_lo, 0xc30c30c3, v4, 0xc30c30c3
v_lshrrev_b32  v2, 4, v3          ; instances = (n + 20) / 21

With the addend read as 0xc30c30c3c30c30c3, the draw got 0x0c30c30d instances instead of 0. On RDNA2 that kept the gfx ring busy past the amdgpu lockup timeout, and a MODE1 GPU reset followed.

Fix

The fix only applies when the operand is a constant:

  • a literal is zero-extended (the high half is 0), which matches LLVM's AMDGPU disassembler. Only f64 operands put the literal in the high half, and the translator has no f64 ALU path.
  • an integer inline constant is sign-extended, so -1 becomes all ones
  • a float inline constant becomes the double of the same value

Register operands behave as before.

The same function backs readMask, so a 64-bit mask constant also changes. For example, s_and_saveexec_b64 s[14:15], 1 in a wave64 shader enabled lanes 0 and 32. Now it enables only lane 0.

Testing

  • Full build plus libs on Linux; ctest passes 36/36.
  • The Smurfs Dreams on a local branch: before this change it always hit a GPU reset at frame 34. With the change, the args record reads {63, 0, 0, 0}, the draw is skipped as intended, and there has been no reset or GPU fault in a 240 s run.
  • Not covered: the float inline constant branch. I haven't seen a shader that uses one in a 64-bit integer operation, so it follows the ISA rule and is untested. There is no unit test either: the recompiler has no translation-level test harness yet.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4655377972 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/39). Originally posted by [Pacuka](https://github.com/Pacuka) on 2026-09-27T17:00:16Z. --- ## Problem `TranslationContext::readU32Pair` builds a 64-bit source from two 32-bit reads, taking the high half from `offsetOperand(operand, 1)`. For registers that is the next register. For a literal or an inline constant, `offsetOperand` returns the same operand, so the constant ended up in **both** halves: the literal `L` was read as `L:L` instead of `0:L`. Found in The Smurfs Dreams (UE). A compute shader writes the args of an indirect draw and rounds a count up to a multiple of 21: ``` v_add_nc_u32 v4, 20, v3 v_mad_u64_u32 v[2:3], vcc_lo, 0xc30c30c3, v4, 0xc30c30c3 v_lshrrev_b32 v2, 4, v3 ; instances = (n + 20) / 21 ``` With the addend read as `0xc30c30c3c30c30c3`, the draw got `0x0c30c30d` instances instead of 0. On RDNA2 that kept the gfx ring busy past the amdgpu lockup timeout, and a MODE1 GPU reset followed. ## Fix The fix only applies when the operand is a constant: - a **literal** is zero-extended (the high half is 0), which matches LLVM's AMDGPU disassembler. Only f64 operands put the literal in the high half, and the translator has no f64 ALU path. - an **integer inline constant** is sign-extended, so `-1` becomes all ones - a **float inline constant** becomes the double of the same value Register operands behave as before. The same function backs `readMask`, so a 64-bit mask constant also changes. For example, `s_and_saveexec_b64 s[14:15], 1` in a wave64 shader enabled lanes 0 **and 32**. Now it enables only lane 0. ## Testing - Full build plus `libs` on Linux; `ctest` passes 36/36. - The Smurfs Dreams on a local branch: before this change it always hit a GPU reset at frame 34. With the change, the args record reads `{63, 0, 0, 0}`, the draw is skipped as intended, and there has been no reset or GPU fault in a 240 s run. - Not covered: the float inline constant branch. I haven't seen a shader that uses one in a 64-bit integer operation, so it follows the ISA rule and is untested. There is no unit test either: the recompiler has no translation-level test harness yet. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
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!39
No description provided.