Skip to content

Shaders: abs / neg on the shift-add and align ops act on the sign bit - #2020

Open
upipibi wants to merge 2 commits into
Marvisak:mainfrom
upipibi:fix/int-sign-mods
Open

upipibi wants to merge 2 commits into
Marvisak:mainfrom
upipibi:fix/int-sign-mods

Conversation

@upipibi

@upipibi upipibi commented Nov 25, 2026

Copy link
Copy Markdown

VOP3 abs and neg on v_lshl_add_u32, v_add_lshl_u32, v_alignbit_b32 and v_alignbyte_b32 stopped translation with "integer operand modifiers". RDNA2 applies them to a source's bit 31, as for a float: abs clears it and neg flips it. There is no integer negate or absolute value.

Sources: boykopovar/AnyPS5#25964 measured every abs/neg combination on the two shift-adds on gfx1036 (RDNA2) and gfx1201 (RDNA4), 196/196 words matching on each. boykopovar/AnyPS5#25799 measured the align ops on gfx1036: on src1 the bit reaches the result, and on src0 and the shift amount it can't change it. xcl-dev/xcl#2058 is the same fix in XCL, where the modifiers were silently dropped.

What is in it

  • emit/lower.rs, src: for those four opcodes, abs and neg become x & 0x7fffffff and x ^ 0x80000000 on the source. Every other integer operation still returns "integer operand modifiers" when it has them, since only these four are measured.
  • emit/valu_tests.rs: shift_add_and_align_take_sign_bit_modifiers covers neg on src2 of v_lshl_add_u32, abs on src0 of v_add_lshl_u32, and neg and abs on src1 of both align ops. Each case is built so the modifier can't be shifted out.

Tested on Windows 11, RTX 3070 (Ampere, driver 616.56), debug, on main 940d32f:

Not tested: AMD hardware, a title.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

VOP3 abs and neg on v_lshl_add_u32, v_add_lshl_u32, v_alignbit_b32 and
v_alignbyte_b32 stopped translation as integer operand modifiers. RDNA2
applies them to a source's bit 31, as on a float: abs clears it, neg
flips it (boykopovar/AnyPS5#25964, all combinations on gfx1036 and
gfx1201; #25799 for the align ops). Other integer operations still
refuse them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremygold02

Copy link
Copy Markdown
Contributor

Triage verdict — Ready for maintainer decision; one useful test addition (ea7dff7)

Evidence

  • src limits the sign-bit handling to the four measured opcodes. For those, it clears bit 31 before flipping it, consistent with the published shift-add and align measurements. Other integer opcodes still reject these modifiers.

Checks and limits

  • The exact-head Windows check passed. The author reports that the targeted test ran on hardware and WARP skipped the wave32 shader. I reviewed the diff and the published measurements without rerunning the GPU test.

Next step

  • Keep this scoped to the measured opcodes. A combined ABS+NEG case on the same source would pin the operation order in the local fixture. The current cases set one modifier at a time, so they would also pass if those two operations were accidentally reversed.

This verification was AI-assisted.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

144 passed / 0 failed. abs/neg on shift-add and align ops act on the sign bit — ALU semantics fix for the packed shift-add path.

Two cases set both modifiers on the same source (add_lshl src0, lshl_add
src2): abs clears bit 31, then neg sets it. They fail if the two are applied
the other way round (checked by swapping them locally).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Author

Added the combined case in eed34ee. Two rows now set ABS and NEG on the same source: v_add_lshl_u32 with -|src0| and v_lshl_add_u32 with -|src2| on 0x8000_0001. Both must give bit 31 set. With the two operations swapped locally (neg, then abs), the first row fails on hardware (0x00000004, want 0x80000004), so the order is pinned now. RTX 3070 (616.56): the test passes, and victory-shader gives 245 passed with only #1744's 3 NVIDIA failures, the same as main.

AI-assisted (Claude Code).

@Calvix2k

Copy link
Copy Markdown
Contributor

Verified on Windows 10 19045 (RX 7800 XT), head eed34ee plus the device-skip patch from #1991 (this host has no SM 6.6 device, so the probes would otherwise fail for an unrelated reason):

cargo test -p victory-shader --lib — 248 passed, 0 failed, 3 ignored, including abs and neg together on one shift-add source give -|x|.

On the semantics: treating abs as clearing bit 31 and neg as flipping it is the right model for these four ops if the hardware does them as sign-bit operations rather than two's complement — and the order (abs then neg, so both set gives -|x|) matches the float convention the comment cites. Restricting it to the four names and keeping the refusal for the other integer ops is the conservative half, which I would keep.

The GPU-valued cases skip here and run in CI; no unrelated failure in the suite on this tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants