Skip to content

fix(shader): s_lshlN_add SCC and s_bfe_[iu]32 fields past bit 31 - #525

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
adriadam10:test/scalar-alu
Oct 5, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
adriadam10:test/scalar-alu

Conversation

@adriadam10

Copy link
Copy Markdown
Collaborator

What

Execution test for 38 SOP2 opcodes that no test ran yet, plus fixes for two bugs it found:

  • s_lshl1/2/3/4_add_u32 never wrote SCC. On hardware SCC = 1 when the 64-bit (S0 << N) + S1 doesn't fit in 32 bits, so bits shifted out of S0 count as well as the add carry.
  • s_bfe_i32 / s_bfe_u32 used OpBitFieldSExtract/OpBitFieldUExtract with count = min(width, 32 - offset). That input is valid SPIR-V (offset + count <= 32), but RADV (Mesa 26.2.4) returns the field as if the width were width & 31 when it runs past bit 31. Example: s_bfe_i32 of 0xbde5c099, offset 23, width 37 gives 0xfffffffb on RADV and 0xffffff7b on hardware and lavapipe. The field is now built from shifts (extractBits32: shift right by the offset, then shift left and back right by 32 - min(width, 32), with width 0 giving 0), the way sBfeU64 already does it. Every shift amount stays below 32.

Opcodes in the test: s_add_i32, s_sub_i32, s_subb_u32, s_min/max_{i32,u32}, s_cselect_b64, s_or/xor/andn2/orn2/nand/nor/xnor_b64, s_xor/andn2/orn2/nor/xnor_b32, s_lshl_b64, s_lshr_b32/b64, s_ashr_i32/i64, s_bfm_b32/b64, s_bfe_i32/u32/u64, s_absdiff_i32, s_lshl1..4_add_u32, s_pack_{ll,lh,hh}_b32_b16, s_mul_hi_{u32,i32}. s_bfe_i64 is left out because #234 adds it.

Behaviour measured on HW

Radeon 660M (gfx1035, same SALU as gfx1013), 2048 rows of edge and random operands, all 38 opcodes, 100% match with a Python model:

  • s_absdiff_i32: absolute value of the wrapped 32-bit difference, so 0x43892dfc vs 0x960d5a8f gives 0x52842c93.
  • s_bfe_i32: the field is taken from the arithmetic shift of S0, so a width past bit 31 repeats the sign bit.
  • s_lshlN_add_u32: SCC comes from the unmasked shift, as described above.
  • s_bfm_*, s_pack_*, s_mul_hi_* and s_cselect_b64 leave SCC unchanged.

The test runs one wave of 32 lanes. A loop reads each lane's operands into SGPRs with v_readlane_b32, runs the opcodes, and writes the results and SCC back with v_writelane_b32. SCC is packed with s_addc_u32 acc, acc, acc after each op. Each lane stores 64 dwords, compared against the hardware values on 32 rows picked to cover SCC 0/1, zero and negative results, shift amounts >= 32 and bfe widths 0, < 32 and > 32 - offset.

Tested

Linux, agc_driver_scalar_sop2_tests on RADV (Radeon 660M), local lavapipe and CI's lavapipe (Mesa 25.2.8): passes. Without the fix it fails: on RADV and lavapipe for the SCC part, on RADV only for the bfe part (lavapipe handles the native extract right). Each piece of the bfe construction (arithmetic first shift, min(width, 32), width-0 select) and of the SCC (shifted-out bits, carry) was mutated separately and the test caught every mutation. Full Release ctest: all pass except agc_driver_pixel_helper_lanes, which also fails on main on this RADV.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

s_lshl1..4_add_u32 never wrote SCC; hardware sets it when the shifted-out
bits or the add carry are non-zero. s_bfe_i32/u32 went through
OpBitField{S,U}Extract, which RADV gets wrong when the width runs past
bit 31; build the field from shifts instead.

Adds an execution test for 38 SOP2 opcodes with hardware-captured results.

Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10

Copy link
Copy Markdown
Collaborator Author

Rebased on current main: the only Windows failure was guest_audio_in_no_device, fixed by #532. No other changes.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks. I re-checked the rebased head; the commit is the same as before and merges cleanly onto current main.

The SCC fix matches the RDNA2 ISA pseudocode for S_LSHLn_ADD_U32 (64-bit sum, SCC = sum >= 2^32) and LLVM's Defs = [SCC] on these ops, and (S0 >> (32 - N)) != 0 || carry is exactly that overflow.

On the bfe half: main's BitField{S,U}Extract(src, offset, min(width, 32 - offset)) was already correct for valid SPIR-V, so this works around the RADV result rather than changing semantics. extractBits32 gives the same value for every input and keeps all shift amounts below 32, so I'm happy to take it. On my machine the new test fails on main because of the SCC rows, and passes with the change.

s_bfe is on a very hot path in every title, so I'll run it through my PPSA21564 check before merging. After that it's ready to merge from my side.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Gated on PPSA21564: no visual or FPS change.

@oneandonlydean
oneandonlydean merged commit 5ad0c99 into boykopovar:main Oct 5, 2026
3 checks passed
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 5, 2026
Takes upstream's merged versions of our boykopovar#586 boykopovar#611 boykopovar#612 boykopovar#505 boykopovar#490 boykopovar#619
boykopovar#503 boykopovar#627 boykopovar#628 boykopovar#618 and drops astrobot's local copies, plus gate-batch-3
(boykopovar#469 boykopovar#500 boykopovar#504 boykopovar#509 boykopovar#510 boykopovar#514 boykopovar#525 boykopovar#526 boykopovar#528), round o/p (boykopovar#617 boykopovar#620
boykopovar#621 boykopovar#622 boykopovar#623 boykopovar#624 boykopovar#501 boykopovar#508 boykopovar#629 boykopovar#630 boykopovar#631).

Conflicts:
- SpirvAnalysis.hpp/.cpp, SpirvBufferAccess.cpp: upstream's boykopovar#586/boykopovar#612.
  The 64-bit LDS atomics keep the full array (f27d6e5 no longer needed).
- SpirvMemoryInstructions.cpp: upstream's constant slot plus offset;
  astrobot's split-wave append/consume kept.
- GuestTextureResource.cpp, TextureTiling.cpp and their tests: upstream's
  boykopovar#490/boykopovar#503 (astrobot had no other change there).
- MaskedSelect.cpp test, ShaderMemory.cpp LDS test: upstream's.
- State.cpp: upstream's boykopovar#505 export count and boykopovar#469 null pixel program;
  astrobot's depth export rejection and depth target model kept.
- Pipeline.cpp: upstream's pipeline key; astrobot's depth attachment and
  blend check (a trailing unused attachment stays valid, as
  colorGapPipelineTests expects).
- Draw.cpp, Recorder.cpp, ShaderResources.cpp: astrobot's (renderPassKey
  with depth, moved sample functions, residency maintenance), which
  already hold the merged boykopovar#505/boykopovar#628/boykopovar#490 changes.
- ShaderInputState.cpp: both register sets.
- State.hpp: the duplicate exportIndex member from the auto-merge removed.
- CMakeLists.txt: astrobot's test list (a superset of upstream's).
- tests/Graphics.cpp: upstream's CompactedExportTests replaces astrobot's
  inline copy; tests/Recorder.cpp: upstream's viewPastLastMipTests added.
@adriadam10
adriadam10 deleted the test/scalar-alu branch October 6, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants