Shaders: VICTORY_F16_NEAREST rounds packed and mixed-precision 16-bit results too, and follows the program's round mode #239
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/239"
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?
Imported from DexNom/VIctory. Originally posted by murillojndem on 2026-11-30T01:43:53Z.
VICTORY_F16_NEAREST=1makes 16-bit float results round as the console rounds them, instead of as the host'sf32tof16does: that one rounds toward zero on AMD and NVIDIA. The switch was read only forvalu16_op's results (v_add_f16,v_cvt_f16_f32and the like). Packed results (v_pk_add_f16,v_pk_mul_f16,v_pk_fma_f16...) and the mixed-precision ones (v_fma_mixlo_f16,v_fma_mixhi_f16) still went through the host's conversion, so with the switch on, an RX 7900 XT still rounded them toward zero. The console also rounds 16-bit results as its MODE register says (bits 3:2, set bys_round_modeor by ans_setregof MODE), and the translation ignored both: with the switch on, a program that asked for rounding toward zero got rounding to nearest. (A refuseds_setregis an instruction without a side effect or a result, so the program is translated without it.)Change (
emit/lower.rs):round_modesgives the 16-bit round mode in force at each instruction: nearest even where the program starts, then ass_round_mode, or ans_setregof MODE with a constant, sets it; unknown after ans_setregof a register's value, or where paths that set it differently join.Lower::insttakes it intomode16.narrowconverts a 16-bit result. Without the switch it is the host's conversion, as before. With it,f16_toward_zerowhere the mode is toward zero, elsef16_nearest, both whatever the host's conversion does.valu16_op,packed16, thev_fma_mix*_f16results and the f32 wave sum of halves (half_sum_here) usenarrow. The exact wave sum of halves (add_halves), which rounds to nearest with or without the switch, rounds toward zero where the mode says so.Without the switch the translation is the same as before, except for that exact wave sum under a toward-zero mode.
The round-mode pass follows upstream Marvisak/VIctory
77053a0(Marvis), which570a941removed with the rest of that commit. The conversions are this repository'sf16_nearestandf16_toward_zero, and the switch stays opt-in as here.Tests, in
emit::wave64_tests, on the hardware device:a_packed_16_bit_sum_rounds_to_nearest_when_asked_to:v_pk_add_f16of 0x3c03 + 0x4000 in both halves, the tie of the existinga_16_bit_sum_rounds_to_nearest_when_asked_to, which the console rounds up to 0x4202. With the switch on, main gives 0x42014201 on the RX 7900 XT; this change gives 0x42024202.a_mixed_precision_16_bit_result_rounds_to_nearest_when_asked_to:v_fma_mixlo_f16of 0x3c03 * 1.0 + 0x4000 into the low half gives 0x12344202, the high half kept. A mutant with the host's conversion at that site gives 0x12344201.a_16_bit_sum_rounds_toward_zero_where_the_program_sets_it:s_round_mode 0xc, ors_setreg_imm32_b32 hwreg(HW_REG_MODE, 2, 2), 3, then the samev_add_f16: 0x4201 with the switch on. Main gives 0x4202 for both, and so does a mutant that leavess_setregout. Without the switch the test takes the host's value, which is 0x4201 on the RX 7900 XT.victory-shader --libon the RX 7900 XT and WARP with one test thread: 240 passed, 2 failed, 4 ignored (main: 237, 2, 4). The 2 failures (emit::rect::tests::rectangles_are_drawn_wholeandthe_rectangle_stage_is_a_geometry_shader) fail on main too.-D warnings, with and without--all-features(and forvictory), andcargo xtask policypass. rustfmt reports the same differences as on main. It merges with my open PRs.Limits:
Generated with Claude Code
Imported from DexNom/VIctory. Originally posted by Zippp69 on 2026-11-30T16:08:42Z.
Review of
733e248Fail on PSO creation errors after a device opens.
stored_on_hardware()treats a failedcompute_pipeline()as an unavailable device. That lets all three new rounding tests pass without readback if PSO creation regresses. Keep the skip for an unavailable device and fail the test if PSO creation fails after the device opens. Four focused rounding tests ran successfully with device readback on this head. wave64_tests.rsImported from DexNom/VIctory. Originally posted by Zippp69 on 2026-11-30T17:37:39Z.
Tested patch for the PSO test failure. Follow-up to the rounding test review at
733e248. Keep the skip when device creation fails. Once the device opens, report PSO creation failure as a test failure.Four focused rounding tests passed with device readback after the change. One pre-existing test selected by the filter was ignored. wave64_tests.rs
Imported from DexNom/VIctory. Originally posted by murillojndem on 2026-11-30T18:42:06Z.
Thanks, applied in
54f2c5f.stored_on_hardwarenow returnsNoneonly whenHost::newfails. Once the device opens, acompute_pipelineerror panics, and the three callers print "no SM 6.6 hardware device; not run".Checked with a mutation that truncates the DXIL just before
compute_pipeline: on733e248the three tests printed "not run" and passed, and with54f2c5fthey fail with "compute_pipeline failed". Without the mutation the four rounding tests pass with readback on an RX 7900 XT and on WARP (VICTORY_WARP=1).The older skips in
wave64_tests.rs,a_16_bit_sum_rounds_to_nearest_when_asked_toamong them, come from main and keep the behavior they have there.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.