fix(shader): VOP3 op_sel on v_alignbit/alignbyte_b32 and 16-bit max3/min3/med3 #504

Merged
adriadam10 merged 1 commit from upstream-pr/504 into main 2026-10-05 08:32:14 +00:00
adriadam10 commented 2026-10-04 10:42:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T10:42:01Z.


What

op_sel on the VOP3 forms of v_alignbit_b32, v_alignbyte_b32, v_max3_i16, v_max3_u16, v_min3_i16, v_min3_u16 and v_med3_u16 threw VOP3 source modifiers are not implemented. It is now decoded:

  • v_max3/min3_{i16,u16}, v_med3_u16: the same selectors v_med3_i16 already uses (op_sel[0..2] take the high half of each source, op_sel[3] writes the high half of the destination and keeps the low half).
  • v_alignbit_b32, v_alignbyte_b32: op_sel[1:0] choose the byte of src2 that holds the shift, op_sel[2] has no effect, and op_sel[3] writes the low 16 bits of the result into the high half and zeroes the low half. This is not in the RDNA2 ISA doc, so it comes from the hardware (below). It reuses the SDWA byte selector on src2 and WORD_1 + UNUSED_PAD on the destination.

Also fixed in v_alignbyte_b32: the shift came from src2 & 31 and gave 0 or src0 for values >= 4. The RDNA2 ISA says ({S0, S1} >> (8 * S2.u[1:0])), and the hardware agrees.

clamp, abs and neg on these ops still throw. Integer clamp is a separate batch.

Behaviour measured on HW (RDNA2, gfx1035)

  • All 16 op_sel values on each of the 8 ops (including v_med3_i16), 512 random rows, prior destination value seeded: 65536 of 65536 results match the model above.
  • v_alignbit_b32 with src2 = 0x05060708: op_sel 0/1/2/3 shift by 8/7/6/5; op_sel[2] alone gives the same result as 0; op_sel[3] → 0xfeba0000.
  • v_alignbyte_b32 with src2 = 0x1d0b1704: op_sel 0/1/2/3 shift by 0/3/3/1 bytes (byte & 3).

Tested

Two new execution tests, with 32 rows each and expected values from the hardware (the Python model matches every cell):

  • agc_driver_vop3_align_op_sel_tests: alignbit/alignbyte with 8 op_sel values each. Rows are picked so each selected byte covers shift 0, < 16, >= 16 and >= 32 (alignbit), and every byte & 3 with the byte < 4 and >= 4 (alignbyte).
  • agc_driver_vop3_integer16_ternary_op_sel_tests: 16 max3/min3/med3 i16/u16 variants with mixed op_sel. Rows cover each source winning, ties and negative halves.

Both pass on RADV and lavapipe. The tests fail without the fix: the old src2 & 31 on alignbyte, dropping the src2 byte select, selecting by half instead of by byte, keeping the low half on op_sel[3], dropping the destination select, and ignoring source/destination op_sel on the 16-bit ops all fail. clamp still throws (decoder probe). The full Release ctest passes except agc_driver_pixel_helper_lanes, which also fails on main on local 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4733714649 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/504). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T10:42:01Z. --- ### What `op_sel` on the VOP3 forms of `v_alignbit_b32`, `v_alignbyte_b32`, `v_max3_i16`, `v_max3_u16`, `v_min3_i16`, `v_min3_u16` and `v_med3_u16` threw `VOP3 source modifiers are not implemented`. It is now decoded: - `v_max3/min3_{i16,u16}`, `v_med3_u16`: the same selectors `v_med3_i16` already uses (`op_sel[0..2]` take the high half of each source, `op_sel[3]` writes the high half of the destination and keeps the low half). - `v_alignbit_b32`, `v_alignbyte_b32`: `op_sel[1:0]` choose the byte of src2 that holds the shift, `op_sel[2]` has no effect, and `op_sel[3]` writes the low 16 bits of the result into the high half and zeroes the low half. This is not in the RDNA2 ISA doc, so it comes from the hardware (below). It reuses the SDWA byte selector on src2 and `WORD_1` + `UNUSED_PAD` on the destination. Also fixed in `v_alignbyte_b32`: the shift came from `src2 & 31` and gave 0 or `src0` for values >= 4. The RDNA2 ISA says `({S0, S1} >> (8 * S2.u[1:0]))`, and the hardware agrees. `clamp`, `abs` and `neg` on these ops still throw. Integer clamp is a separate batch. #### Behaviour measured on HW (RDNA2, gfx1035) - All 16 `op_sel` values on each of the 8 ops (including `v_med3_i16`), 512 random rows, prior destination value seeded: 65536 of 65536 results match the model above. - `v_alignbit_b32` with src2 = `0x05060708`: `op_sel` 0/1/2/3 shift by 8/7/6/5; `op_sel[2]` alone gives the same result as 0; `op_sel[3]` → `0xfeba0000`. - `v_alignbyte_b32` with src2 = `0x1d0b1704`: `op_sel` 0/1/2/3 shift by 0/3/3/1 bytes (`byte & 3`). ### Tested Two new execution tests, with 32 rows each and expected values from the hardware (the Python model matches every cell): - `agc_driver_vop3_align_op_sel_tests`: alignbit/alignbyte with 8 `op_sel` values each. Rows are picked so each selected byte covers shift 0, < 16, >= 16 and >= 32 (alignbit), and every `byte & 3` with the byte < 4 and >= 4 (alignbyte). - `agc_driver_vop3_integer16_ternary_op_sel_tests`: 16 max3/min3/med3 i16/u16 variants with mixed `op_sel`. Rows cover each source winning, ties and negative halves. Both pass on RADV and lavapipe. The tests fail without the fix: the old `src2 & 31` on alignbyte, dropping the src2 byte select, selecting by half instead of by byte, keeping the low half on `op_sel[3]`, dropping the destination select, and ignoring source/destination `op_sel` on the 16-bit ops all fail. `clamp` still throws (decoder probe). The full Release ctest passes except `agc_driver_pixel_helper_lanes`, which also fails on `main` on local RADV. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 21:07:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:07:58Z.


Thanks. I checked this against the RDNA2 ISA (V_ALIGNBYTE_B32 shifts by 8 * S2.u[1:0], so main's src2 & 31 was wrong) and LLVM's VOP3Instructions.td. LLVM defines V_ALIGNBIT_B32_opsel/V_ALIGNBYTE_B32_opsel for gfx10 with the note "Hardware uses opsel[1:0] to byte-select src2", which matches your capture, and llvm-mc for gfx1013 accepts every op_sel form in the test. The 16-bit max3/min3/med3 path is the same one med3_i16 already used, and I reproduced all 512 expected cells of the align test from your model.

One small question: the same LLVM comment says "Other opsel bits are ignored", while your capture has op_sel[3] moving the low half into the high half and zeroing the low half. I'm fine going with the hardware, since no compiler emits that bit here. Could you confirm that the op_sel[3] rows came straight from the gfx1035 capture and weren't modelled?

The alignbyte change touches a path existing shaders already use, so I'll run it through my PPSA21564 check before merging. Otherwise it's ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984395925 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/504#issuecomment-5984395925). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:07:58Z. --- Thanks. I checked this against the RDNA2 ISA (`V_ALIGNBYTE_B32` shifts by `8 * S2.u[1:0]`, so main's `src2 & 31` was wrong) and LLVM's VOP3Instructions.td. LLVM defines `V_ALIGNBIT_B32_opsel`/`V_ALIGNBYTE_B32_opsel` for gfx10 with the note "Hardware uses opsel[1:0] to byte-select src2", which matches your capture, and llvm-mc for gfx1013 accepts every op_sel form in the test. The 16-bit max3/min3/med3 path is the same one med3_i16 already used, and I reproduced all 512 expected cells of the align test from your model. One small question: the same LLVM comment says "Other opsel bits are ignored", while your capture has `op_sel[3]` moving the low half into the high half and zeroing the low half. I'm fine going with the hardware, since no compiler emits that bit here. Could you confirm that the `op_sel[3]` rows came straight from the gfx1035 capture and weren't modelled? The alignbyte change touches a path existing shaders already use, so I'll run it through my PPSA21564 check before merging. Otherwise it's ready to merge from my side.
adriadam10 commented 2026-10-04 21:08:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T21:08:40Z.


Yes, they came straight from the gfx1035 capture. The generator stores the oracle output as Expected and only asserts that the model matches it, so the model never fills a cell. The test has three alignbit and three alignbyte forms with op_sel[3] set (op_sel 8, 9 and 15). In the full sweep (all 16 op_sel values, 512 rows) every one of the 8 op_sel[3] forms per op gave the same result: the low 16 bits of the shifted value moved to the high half and the low half zeroed, whatever the destination held before. So on this hardware the bit is not ignored for these two ops, even though LLVM never sets it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984401656 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/504#issuecomment-5984401656). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T21:08:40Z. --- Yes, they came straight from the gfx1035 capture. The generator stores the oracle output as `Expected` and only asserts that the model matches it, so the model never fills a cell. The test has three alignbit and three alignbyte forms with `op_sel[3]` set (`op_sel` 8, 9 and 15). In the full sweep (all 16 `op_sel` values, 512 rows) every one of the 8 `op_sel[3]` forms per op gave the same result: the low 16 bits of the shifted value moved to the high half and the low half zeroed, whatever the destination held before. So on this hardware the bit is not ignored for these two ops, even though LLVM never sets it.
oneandonlydean commented 2026-10-04 22:38:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:38:11Z.


Thanks for confirming. Since the generator only checks the model against the captured Expected values, the op_sel[3] rows are hardware results, and it's good to know the bit is honoured on these two ops even though LLVM never sets it.

agc_driver_vop3_align_op_sel and agc_driver_vop3_integer16_ternary_op_sel still pass on my NVIDIA machine on current main, and fail on main's code.

I'll run it through my PPSA21564 check next and merge it after that if it's clean.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985220390 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/504#issuecomment-5985220390). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:38:11Z. --- Thanks for confirming. Since the generator only checks the model against the captured `Expected` values, the `op_sel[3]` rows are hardware results, and it's good to know the bit is honoured on these two ops even though LLVM never sets it. `agc_driver_vop3_align_op_sel` and `agc_driver_vop3_integer16_ternary_op_sel` still pass on my NVIDIA machine on current main, and fail on main's code. I'll run it through my PPSA21564 check next and merge it after that if it's clean.
oneandonlydean commented 2026-10-05 08:31:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T08:31:17Z.


Gated on PPSA21564: no visual or FPS change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5990921115 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/504#issuecomment-5990921115). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T08:31:17Z. --- Gated on PPSA21564: no visual or FPS change.
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!504
No description provided.