Skip to content

shader: lower v_sin_f16 and v_cos_f16 in direct Compute DXIL - #3300

Open
legacy-code-archaeologist wants to merge 1 commit into
R0Y00:mainfrom
legacy-code-archaeologist:shader/direct-sin-cos-f16
Open

legacy-code-archaeologist wants to merge 1 commit into
R0Y00:mainfrom
legacy-code-archaeologist:shader/direct-sin-cos-f16

Conversation

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Lowers VOP1 (0x60/0x61), VOP3 (0x1e0/0x1e1), and SDWA v_sin_f16 and v_cos_f16 in the direct Compute DXIL path via binary16_unary() and a shared DirectCompute::trig_turns() helper.

Fixes #3298. Refs #2935.

Changes

  • src/gfx1013_compute_native.cpp (binary16_unary()): Add v_sin_f16 and v_cos_f16, enabling direct binary16 unary decode/SDWA subword handling, MODE checking, and DPP form refusal.
  • src/gfx1013_direct_compute.inl (trig_turns(), float_alu()):
    • Extract trig_turns(bool cosine, const string &a) shared by v_sin_f32/v_cos_f32 and v_sin_f16/v_cos_f16: reduces in turns (part = a - trunc(a) with signed zero preserved) before converting to radians (* 2*pi), and forces exact +0.0 at exact half/quarter turns (|part| == 0.5 for sine; 0.25 and 0.75 for cosine).
    • Lower v_sin_f16 / v_cos_f16 through trig_turns() + packed_half_rne(), mapping ±Inf and NaN (|source| >= 0x7c00) to 0x7e00, preserving -0.0 (0x8000) on v_sin_f16, and keeping destination D[31:16] unchanged on VOP1/VOP3 (or applying SDWA PRESERVE/PAD).
  • tests/native_sin_cos_f16_check.py, tests/cmake/native_sin_cos_f16.cmake, docs/experiments/native-sin-cos-f16.md: Add ir, controls, and hardware checks covering 34 cases (VOP1, VOP3 abs/neg, SDWA WORD_0/WORD_1 PRESERVE/PAD, inline float/int, literal, SGPR, VGPR overlap, ±0, ±0.25, ±0.5, ±0.75, -1.75, 2048.0, 65504.0, ±Inf, qNaN, sNaN, subnormals, general angles) and 8 negative refusal cases.

Verification

All tests ran on both dev and release presets:

  • ctest --preset dev --no-tests=error --output-on-failure -j 1 -V -R '^native_sin_cos_f16_':
    • native_sin_cos_f16_ir: Passed (0.38s, 34 cases matched, 8 refusals verified)
    • native_sin_cos_f16_controls: Passed (1.03s, 34 cases compiled to DXIL, 8 refusals verified)
    • native_sin_cos_f16_hardware: Skipped (77) on local Intel(R) HD Graphics 530 (driver=31.0.101.2111, requires shader model 6.6 with wave32; hardware execution unmeasured locally)
  • ctest --preset dev --no-tests=error --output-on-failure -j 1 -R '^(native_f16_alu_|native_sdwa_f16_|native_direct_container$)': native_direct_container (23.64s), native_f16_alu_ir, native_sdwa_f16_ir, and native_sdwa_f16_scalar_ir Passed; 3 hardware tests Skipped (77).
  • ctest --preset release --no-tests=error --output-on-failure -j 1 -R '^(native_sin_cos_f16_|native_f16_alu_|native_sdwa_f16_|native_direct_container$)': 6 non-hardware tests Passed (45.85s), 4 hardware tests Skipped (77).
  • python -B tests/docs_links_check.py: Passed (146 Markdown files, 665 links).
  • Sensitivity mutants on dev (native_sin_cos_f16_ir):
    1. Swap cos/sin DXIL opcodes (12/13): killed at sin_pos_zero / cos_pos_zero / sin_qtr_pos (0xabcd3c00 != 0x0)
    2. Drop exact quarter/half-turn zero select in trig_turns(): killed at cos_qtr_pos, cos_qtr_neg, sin_half_pos, sin_half_neg (0xabcd8001 != 0x0)
    3. Drop -0.0 preservation on v_sin_f16: killed at sin_neg_zero (0xabcd0000 != 0x8000)
      (src/gfx1013_direct_compute.inl restored cleanly and native_sin_cos_f16_ir re-passed.)

AI-assisted.

Adds v_sin_f16 and v_cos_f16 to binary16_unary() and shares a DirectCompute::trig_turns() helper with v_sin_f32/v_cos_f32, rounding via packed_half_rne() while preserving D[31:16], -0.0 on v_sin_f16, exact quarter/half-turn zeros, and Inf/NaN -> 0x7e00.

Fixes R0Y00#3298. Refs R0Y00#2935.

AI-assisted.
@Leyukaka

Copy link
Copy Markdown
Collaborator

I reviewed the diff (not run). Three notes:

  • The rounding op. trig_turns computes part = a - op26(a). DXIL op 26 is Round_ne: the repository maps v_rndne_f32 to 26 and v_floor_f32 to 27. That call is inherited from the v_sin_f32/v_cos_f32 path on main. What is new here describes it as truncation: the docs, the math.trunc reference model, and interpret_trig in the IR interpreter.
    • With round-to-nearest, |part| <= 0.5, so the |part| == 0.75 test for cos can never be true. The 0.25 test still catches -0.75 turns, which reduce to +0.25.
    • The "kills drop 0.75" comment on cos_3qtr_pos holds only under the trunc model. On hardware, +0.75 reduces to -0.25.
    • sin and cos are periodic, so both reductions are mathematically equivalent. But the radians argument differs (0.75 against -0.25 turns), so bit-identical results are not guaranteed, and the ir test checks the IR against trunc semantics.
  • The test tolerance. The hardware check accepts abs(got - want) <= 0.001. That is about 2 binary16 ULP near sin = 0.95, 16 ULP near 0.063, and 262 ULP near 0.0061 (2^-10 turns). The PR's own sin_subnormal case expects about 3.7e-7, so the check accepts any result up to 0.001 in magnitude, including 0.
  • The accuracy bound. The docs do not state an error bound for these ops. In compute: lower binary16 VOP1 rcp, sqrt, exp and rsq #3220 the maintainer asked for a documented host error bound before relying on host transcendental functions for binary16 results; for exp and rsq that bound came from the shader model 4 assembly references (docs/experiments/f16-alu-direct.md). Saying which bound applies to DXIL Sin/Cos, and comparing it with the RDNA2 accuracy for V_SIN_F16/V_COS_F16, would show whether this needs the same treatment.

AI-assisted.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Thanks for the PR. I built it at 475a232 (clang-cl cross build, run under Wine, no GPU). The ir and controls modes both pass: 34 cases matched; 8 refusals verified and compiled 34 cases to DXIL; verified 8 refusals. A few requests, building on the comment above:

  1. Op 26 reduction. I confirm the point above. The translator maps v_rndne_f32 to unary(26, a) and v_trunc_f32 to 29, so part = a - unary(26, a) gives |part| <= 0.5.
    The new |part| == 0.75 clause in trig_turns() can therefore never be true, and it changes nothing for v_cos_f32 or v_cos_f16.
    Could you make expected_trig_f16, interpret_trig and the doc use round-to-nearest-even, and drop the clause (or use op 29 if truncation was intended)?
    I recomputed all 34 expected values with a round-to-nearest-even reduction and none change, so the fix is cheap.
  2. cos_3qtr_pos ("kills drop 0.75"). Deleting the 0.75 clause leaves native_sin_cos_f16_ir green even under the current trunc model.
    cos(0.75 turn) is 1.19e-08 in binary32, which rounds to 0x0000 with or without the clause. Please drop or reword that comment.
  3. Smaller.
    • The |source| >= 0x7c00 -> 0x7e00 select is unpinned. Removing it, or changing uge to ugt, still passes, because same_binary16 accepts any NaN and Inf - round(Inf) is already NaN. Either compare the NaN bits or drop "canonical" from the doc.
    • Each -0.0 path is unpinned on its own. Removing either the v_sin_f16 override or the a == 0 select in trig_turns() still passes; only removing both fails at sin_neg_zero.

This comment was AI-assisted.

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.

shader: lower v_sin_f16 and v_cos_f16 in direct Compute DXIL

3 participants