Skip to content

Shaders: float inline constants in 64-bit operands are doubles - #206

Open
murillojndem wants to merge 1 commit into
DexNom:mainfrom
murillojndem:fix/inline-float-constants-64bit
Open

murillojndem wants to merge 1 commit into
DexNom:mainfrom
murillojndem:fix/inline-float-constants-64bit

Conversation

@murillojndem

Copy link
Copy Markdown

In a 64-bit operand, the float inline constants (codes 240 to 248: 0.5, -0.5, 1.0, -1.0, 2.0, -2.0, 4.0, -4.0 and 1/(2 pi)) were read as the bits of the 32-bit float. The vector reader, src64 (v_cmp_*_{i64,u64}, v_lshlrev_b64 and the other 64-bit shifts, the addend of v_mad_u64_u32), zero-extended them: v_cmp_eq_u64 s20, 1.0, v[1:2] compared with 0x00000000_3f800000. The scalar reader (s_mov_b64, s_cselect_b64, the 64-bit bit operations, shifts and compares) put those bits in the low dword and their sign in the high one, so the two readers also disagreed with each other on the negative constants.

LLVM's AMDGPU backend reads them as doubles in any 64-bit operand. Its disassembler decodes codes 240 to 248 with getInlineImmVal64 for OPERAND_REG_IMM_INT64 and OPERAND_REG_INLINE_C_INT64 as well as for the FP64 types (decodeImmOperands), and the 64-bit scalar and vector sources have that type (SSrc_b64, VSrc_b64). Its code generator emits a 64-bit integer with those bits as the constant: SIInstrInfo::isInlineConstant checks the INT64 types with isInlinableLiteral64 (SIInstrInfo.cpp).

Both readers now take the double's bits from one table, inline_f64, which f64_pair uses too. For 1/(2 pi) the table has LLVM's value, 0x3fc45f306dc9c882 (getInlineImmVal64). f64_pair computed 1.0 / TAU, which is 0x3fc45f306dc9c883. Its only reader is v_cvt_f32_f64, which gives 0x3e22f983 for both, so that part changes no result today.

Test inline_float_constants_read_as_doubles_in_64_bit_operands reads both dwords of each of the nine constants through v_lshrrev_b64 by 0 and through s_mov_b64, and runs v_cmp_eq_u64 against 1.0. The expected patterns are written out in the test. It lists every mismatch before it fails.

Results on 4ad8692, on WARP:

The test checks the lowering against LLVM's reading of these operands. It does not check the console, and it was not run on AMD or NVIDIA hardware. How often the game's shaders use a float constant in a 64-bit operand was not measured.

Claude (Anthropic) wrote the change and the test and ran the verification.

Generated with Claude Code

Codes 240..248 in a 64-bit operand were read as the f32 bits: src64
zero-extended them (v_cmp_eq_u64 with 1.0 compared with 0x3f800000)
and the scalar 64-bit reader sign-extended them. LLVM's AMDGPU
disassembler decodes them with its f64 table for the INT64 operand
types, and its code generator encodes 64-bit integers that way.

Both readers and f64_pair now take one table, inline_f64, with LLVM's
1/(2 pi), 0x3fc45f306dc9c882 (1.0 / TAU gives 0x3fc45f306dc9c883;
v_cvt_f32_f64, the only reader of f64_pair, rounds both to the same
float).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant