Shaders: a signed 64-bit operand sign-extends its 32-bit literal #447
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!447
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/447/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Dufftwt on GitHub · 2026-11-24 03:31 UTC · original
Dufftwt:fix/signed-64-bit-literals→main· +43 −1 · 2 files · 1 commits · merged 2026-11-25 00:16 UTCWhat's wrong
A literal (the 32-bit constant word that follows an instruction) can feed a 64-bit operand. The hardware widens it to 64 bits:
i64);u64,b64).LLVM's AMDGPU operand rules spell this out, and LLVM PR #70274 discusses it: a negative literal is "correctly sign extended or incorrectly zero extended" depending on the operand's type.
The lowering always zero-extends:
src64, the vector path:// Literals and the null register: zero-extended.;half, where a literal's high dword is 0.So every signed 64-bit operation with a negative literal sees a large positive number instead:
The affected instructions are
v_cmp_*_i64/v_cmpx_*_i64,v_ashrrev_i64(its 64-bit source) ands_ashr_i64.What the fix does
src64, a literal of an*_i64instruction is sign-extended. Inline constants already were, and other literals stay zero-extended as before.half, a literal's high dword is the sign of the literal for*_i64instructions. It stays 0 otherwise.How it behaves after the fix
Signed 64-bit compares and arithmetic shifts with a negative literal see the same value the hardware does. Unsigned and bit operands (
*_u64,*_b64), inline constants and register operands don't change.Tests
The new test
signed_64_bit_operands_sign_extend_their_literal(valu_tests.rs, run on the host GPU and WARP) checks:v_cmp_eq_i64with the literal -100, forv[1:2]= -100 (1), 0x00000000_FFFFFF9C (0) and 0 (0);v_cmp_lt_i64with -100, for -200 (1), -50 (0) and 0 (0);s_ashr_i64 s[6:7], -100, 4, whose high dword must be 0xFFFFFFFF.I ran
cargo test --workspaceon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml(the runner has a hardware D3D12 device as well as WARP):victory-shaderhas 70 passed, the other crates have 0 failed.main: it fails at its first case:hardware: v_cmp_eq_i64 [ffffff9c, ffffffff, 0, 0]gives0where the hardware rule gives1.--test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (#181, #244).Not tested: a title shader with such a literal.
Related: #163 also adds literal sign-extension to these two functions, but only for
v_mad_i64_i32ands_bfe_i64(plusf64literals in the high dword). It touches the same match arms, so whichever merges second needs a small rebase; the two changes are compatible.@slint6614-dev on GitHub · 2026-11-24 10:16 UTC · original
AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of
run_cases(local adapter-selection patch, not part of this PR);cargo test -p victory-shader --release valu_testsat the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept.signed_64_bit_operands_sign_extend_their_literalpass on both AMD GPUs.