Shaders: lower v_mad_i64_i32; signed 64-bit operands sign-extend a literal #85

Open
Ingenioeren wants to merge 1 commit from upstream-pr/85 into main
Ingenioeren commented 2026-11-25 10:11:20 +00:00 (Migrated from github.com)
First-time contributor

Imported from Zettrox/ztrx. Originally posted by Ingenioeren on 2026-11-25T09:25:17Z.


What

  • v_mad_i64_i32 is lowered. It was refused (Unsupported { what: "v_mad_i64_i32" }). It now shares v_mad_u64_u32's lowering, with its factors sign-extended. Its carry-out mask is left missing, because the unsigned sum's carry is not the signed instruction's.
  • A 32-bit literal in a signed 64-bit VALU operand is sign-extended. Lower::src64 zero-extended every literal. Per the RDNA 2 ISA, §6.2.1 "Literal Expansion to 64 bits" ("64-bit signed integer: sign extended to 64 bits"), that is wrong for v_ashrrev_i64's source, v_cmp_*_i64 and v_mad_i64_i32's S2.i64 addend. v_mad_u64_u32 and the *_u64 compares still zero-extend.

Why

Upstream VIctory lowers v_mad_i64_i32 and has the same literal rule (#447 there, plus the v_mad_i64_i32 addend case being fixed in Marvisak/VIctory#2253). ztrx's history diverged before those changes, so it still refuses the instruction and gets the sign wrong. For example, v_ashrrev_i64 v[5:6], 4, -100 currently produces a high dword of 0 instead of 0xffffffff.

How tested

I added a new synthetic test, emit::valu_tests::signed_64_bit_operands_sign_extend_their_literal, which builds GFX10 code in the test and uses no game shaders. It covers:

  • v_mad_i64_i32: low and high dwords with register operands (signed factors and 0x80000000²);

  • v_mad_i64_i32 / v_mad_u64_u32 with a literal -100 addend;

  • v_ashrrev_i64 with a literal;

  • v_cmp_eq_i64 / v_cmp_lt_i64 with a literal.

  • cargo test --locked --offline -p victory-shader --lib signed_64_bit_operands:

    • before: fails with lower: Unsupported { offset: 40, what: "v_mad_i64_i32" };
    • with only the lowering: fails with v_mad_i64_i32 -100 hi [0, 0, 0, 0] = 0x0, want 0xffffffff;
    • with the lowering but only v_mad_i64_i32 in the literal rule: fails with v_ashrrev_i64 hi ... = 0x0, want 0xffffffff;
    • after: passes on both the hardware adapter and WARP.
  • cargo test --locked --offline -p victory-shader: 209 passed, 2 failed. The two failures (emit::rect::tests::the_rectangle_stage_is_a_geometry_shader and rectangles_are_drawn_whole, "Container part 'Pipeline State Validation' does not match") also fail on main on this PC with its local dxil.dll, so they are unrelated.

  • cargo clippy --locked --offline -p victory-shader --all-targets: no warnings.

  • cargo run -p xtask -- policy --staged: asset policy: 2 files ok.

Not tested: the test hardware was an AMD RX 6750 XT plus WARP, not an NVIDIA card. This is a code-level translation fix, so it should not depend on the vendor, but there was no NVIDIA run and no game run.

AI assistance: prepared with Claude Code, reviewed by Ingenioeren.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Zettrox/ztrx:issue:6601291909 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/85). Originally posted by [Ingenioeren](https://github.com/Ingenioeren) on 2026-11-25T09:25:17Z. --- ## What - **`v_mad_i64_i32` is lowered.** It was refused (`Unsupported { what: "v_mad_i64_i32" }`). It now shares `v_mad_u64_u32`'s lowering, with its factors sign-extended. Its carry-out mask is left missing, because the unsigned sum's carry is not the signed instruction's. - **A 32-bit literal in a signed 64-bit VALU operand is sign-extended.** `Lower::src64` zero-extended every literal. Per the RDNA 2 ISA, §6.2.1 "Literal Expansion to 64 bits" ("64-bit signed integer: sign extended to 64 bits"), that is wrong for `v_ashrrev_i64`'s source, `v_cmp_*_i64` and `v_mad_i64_i32`'s `S2.i64` addend. `v_mad_u64_u32` and the `*_u64` compares still zero-extend. ## Why Upstream VIctory lowers `v_mad_i64_i32` and has the same literal rule (#447 there, plus the `v_mad_i64_i32` addend case being fixed in Marvisak/VIctory#2253). ztrx's history diverged before those changes, so it still refuses the instruction and gets the sign wrong. For example, `v_ashrrev_i64 v[5:6], 4, -100` currently produces a high dword of 0 instead of `0xffffffff`. ## How tested I added a new synthetic test, `emit::valu_tests::signed_64_bit_operands_sign_extend_their_literal`, which builds GFX10 code in the test and uses no game shaders. It covers: - `v_mad_i64_i32`: low and high dwords with register operands (signed factors and `0x80000000²`); - `v_mad_i64_i32` / `v_mad_u64_u32` with a literal `-100` addend; - `v_ashrrev_i64` with a literal; - `v_cmp_eq_i64` / `v_cmp_lt_i64` with a literal. - `cargo test --locked --offline -p victory-shader --lib signed_64_bit_operands`: - **before:** fails with `lower: Unsupported { offset: 40, what: "v_mad_i64_i32" }`; - **with only the lowering:** fails with `v_mad_i64_i32 -100 hi [0, 0, 0, 0] = 0x0, want 0xffffffff`; - **with the lowering but only `v_mad_i64_i32` in the literal rule:** fails with `v_ashrrev_i64 hi ... = 0x0, want 0xffffffff`; - **after:** passes on both the hardware adapter and WARP. - `cargo test --locked --offline -p victory-shader`: 209 passed, 2 failed. The two failures (`emit::rect::tests::the_rectangle_stage_is_a_geometry_shader` and `rectangles_are_drawn_whole`, "Container part 'Pipeline State Validation' does not match") also fail on `main` on this PC with its local `dxil.dll`, so they are unrelated. - `cargo clippy --locked --offline -p victory-shader --all-targets`: no warnings. - `cargo run -p xtask -- policy --staged`: `asset policy: 2 files ok`. **Not tested:** the test hardware was an AMD RX 6750 XT plus WARP, not an NVIDIA card. This is a code-level translation fix, so it should not depend on the vendor, but there was no NVIDIA run and no game run. AI assistance: prepared with Claude Code, reviewed by Ingenioeren. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/85:upstream-pr/85'
git switch 'upstream-pr/85'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/85'
git switch 'upstream-pr/85'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/85'
git switch 'upstream-pr/85'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/85'
git switch 'main'
git merge --squash 'upstream-pr/85'
git switch 'main'
git merge --ff-only 'upstream-pr/85'
git switch 'main'
git merge 'upstream-pr/85'
git push origin 'main'
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
Zettrox/ztrx!85
No description provided.