Skip to content

Model M (D3D12): hoist 8-byte base alignment masks in post_tf (-41.3 us on Windows AMD DX12, byte-identical) - #296

Open
phantoslayer21 wants to merge 5 commits into
mainfrom
perf/model-m-post-tf-hoist-a8
Open

phantoslayer21 wants to merge 5 commits into
mainfrom
perf/model-m-post-tf-hoist-a8

Conversation

@phantoslayer21

Copy link
Copy Markdown
Owner

Summary

Fixes a +37.6 us Windows AMD DX12 regression introduced in #280 (e52f380) on w64/post_tf_f32_p12_diet_vsit while preserving provable 8-byte alignment for Linux RADV/ACO.

Root Cause

Per-load ld64_a8(a) / ld128_a8(a) (a & ~7u) across 66 load sites in clean/dx12/kernels/post.hlsl caused the Windows AMD DX12 shader compiler to emit 79 v_and_b32 instructions (up from 13), prevented immediate offset folding, and increased USED_VGPRs from 85 -> 96.

Fix

Hoist the & ~7u mask once onto the 256-byte-aligned scalar base pointers (A8 & ~7u, hist_prev & ~7u, and b0 & ~7u) so every derived 8-byte offset remains provably 8-byte aligned for RADV/ACO while cutting v_and_b32 back down (79 -> 17) and restoring USED_VGPRs (96 -> 85) on Windows AMD DX12.

Verification (RX 9070 XT gfx1201, 1440p Quality 1706x960 -> 2560x1440)

  • Paired A/B (w64/post_tf_f32_p12_diet_vsit): 0.5996 ms -> 0.5583 ms (-41.3 us, matching v0.6.5 0.5587 ms).
  • Bit-exactness: 0 byte diffs across 6 frames (HDR + LDR + special).
  • Checks: test_post_tf_align, check_no_dev_diagnostics.py, test_model_select, and tools/ci/check_line_endings.py all pass.

…xposure is not 1 (-0.11 ms at pi = 2, byte-identical)

On the staged native D3D12 route with a pre-exposure other than 1, the post_tf kernels (post_tf_f32_p12_diet_vsit,
post_tf4_dl_*, pmo/post_tf_*_mo) now apply the scale_rgb multiply in their own store (native_scale's values, including the
exact repair of a flushed tiny product), so the separate scale_rgb launch is gone. pi = 1, raw / direct / Model L routes and
the interop output copy are untouched. WG_SCALE_FOLD=0 (runtime option scale_fold=0) brings the launch back.

Windows AMD DX12 (RX 9070 XT, 1706x960 -> 2560x1440, 8 interleaved ABBA rounds, medians): pi = 2.0 frame span 2333 -> 2222 us
(-111 us), kernel profile sum 2318 -> 2202 us (scale_rgb 128 -> 0 us, post_tf +10 us); pi = 1.0 unchanged (span -2 us,
profile sum -2.5 us; post_tf keeps 85 VGPRs, 30336 B LDS). Output, history and every named buffer are byte-identical to
the unfolded sequence and to v0.6.5 for pi in 1, 2, 0.7, 1e-3, 1e4, 1e38 and a mixed-pi stream, with +-0, denormal, huge,
Inf and NaN colour pixels, on the post_tf, post_tf4, native-AA and motion-at-output-resolution routes.
Implement five bit-exact Model M D3D12 optimizations for gfx1201:
- T1.1: hoist scalar alignment mask in post_tf history_cr_span (4 -> 0 v_and_b32 -8) and drop WG_LD_A16 per-VGPR masking on swin_block_c64_nc (17 -> 0 v_and_b32 -16) and dec1_dec0 (11 -> 0) while preserving all buffer_load_b128.
- T1.2: port all 7 POST_OUT==3 bit-exact optimizations to POST_OUT==0 (pd_stage_fetch hoisting, unrolled gather + gather_zero_opq, 2-chain rh_code, 2x QuadReadAcrossX display LDS store + k==0 hsum skip, hoisted h_src/h_dst + pd_vsplit, WG_PT_SMALL/WG_PT_INF, and pi==1 vs pi!=1 body specialization).
- T1.3: add 256 KB FP16 decode LUT (f16lut_build_dec) for staged POST_OUT==0 (output_filter=original), overlapped with input_encoder via unordered_next(), toggleable with WG_M_POST_LUT=0.
- T1.5: drop input_encoder from 103 -> 95 VGPRs (0 scratch, 10 KB lane-private LDS -> 4 WGs/CU) via A-fragment streaming and per-lane LDS parking, plus WaveActiveAnyTrue(ec > 32752.0f) guard on the second wg_log2.
- T1.8: overlap color_divide with exposure_auto_diet via unordered_next() when pi != 1, and add dedicated _pf variants (input_encoder_pf, emo/input_encoder_mo_pf, post_tf*_pf) folding color_divide with zero impact on pi == 1 DXILs, toggleable with WG_M_PF_FOLD=0.
…ut decode table and the encoder LDS (bit-exact)

Follow-up to the Wave 1 levers (7b0f6a5), from the Wave 1 GPU A/B: T1.1 and T1.8 won, T1.2 / _pf regressed
post_tf only because its VGPRs went 85 -> 96 (NW = 8: 2 workgroups per CU -> 1).

- post_tf / post_tf_pf: one body per kernel (no inlined pi == 1 / pi != 1 copies). post_tf keeps b28679f's run-time
  pi == 1 branch at the store; _pf takes PiDiv + m1_scale unconditionally. The T1.2 cuts are now one switch each
  (WG_PO_*); the three per-lane hoists (staging row/column, horizontal roles, vertical first item) are left out of
  the NW = 8 kernels and the horizontal one of the NW = 4 kernels, everything else stays (exposure loads after the
  first staging loads, unrolled gather, QuadReadAcrossX display store, 2-chain reduced code, WG_PT_SMALL / WG_PT_INF,
  history base mask). RGA gfx1201, 0 spills: post_tf 96 -> 76, post_tf_pf 96 -> 77, post_tf4_dl 108 -> 83,
  post_tf4_dl_pf 90 -> 85, no v_and_b32 -8. Direct (form 3) kernels are unchanged.
- T1.3 removed (f16lut_build_dec, the decode-table path of post.hlsl, wg_f16lut.hlsli / exposure.hlsl back to
  b28679f, runtime m_post_lut): it cost 5.5 us of build and saved nothing at pi = 1.
- T1.5 reverted: input_encoder is back to b28679f's register-only A load (0 B LDS, 0 scratch); the
  WaveActiveAnyTrue(ec > 32752) guard and the _pf kernels stay.
- kernel_sets.json regenerated (107 Model M / L kernels).
@j0nsh1n

j0nsh1n commented Nov 25, 2026

Copy link
Copy Markdown
Contributor
Model M, 1440p Quality, RX 9070 XT, Linux Whole-frame GPU time
Before: main 0bf6aa4 2.085 ms
After: PR 5ceffd9 2.172 ms (+0.087 ms)

The PR is slower here in 9/9 interleaved rounds. Every round was +0.082 to +0.090 ms. These are unprofiled whole-frame GPU times at 1706x960 -> 2560x1440.

Output exactness

Output is byte-identical to main. I compared whole frames: every stage buffer plus the image, 3 frames, with each build run twice.
At 64x40, 77x45 and 1706x960 -> 2560x1440, 84/84 files matched main in each case. Nothing was unstable run to run.

Per-kernel timing

These are profiled per-frame GPU times, in milliseconds:

Kernel Main PR Change
d1p12/swin_block_c64_add_dec0_nc 0.348 0.409 +0.061 (9/9)
swin_block_c64_nc 0.244 0.268 +0.024 (9/9)
w64/post_tf_f32_p12_diet_vsit 0.567 0.572 +0.006 (9/9)
swin_block_c96_add_xe_xo_nc — — -0.002 (8/9)

input_encoder, swin_bneck6_mbo_nc, swin_block_c96_pm_rev_nc and exposure had no stable change.

What ACO shows

I compiled for gfx1201 without the card. There were no spills in either build.
The two c64 Swin kernels each grow by about 11% in instructions, with no occupancy change: d1p12/swin_block_c64_add_dec0_nc goes from 4122 to 4578 instructions at 192 VGPRs and 8 waves, and swin_block_c64_nc from 4921 to 5441 at 240 VGPRs and 6 waves. LDS stays at 16 KB for the first kernel. This fits their slowdown.
w64/post_tf_f32_p12_diet_vsit goes from 3694 to 4148 instructions (+12%), and from 84 to 96 VGPRs, with 16 waves in both. Its +0.006 ms and VGPR increase come from the post.hlsl changes; I did not separate them further. On this compiler its VGPR count goes up, not down.
input_encoder stays at 96 VGPRs and goes from 5322 to 5380 instructions.

Likely source of the c64 regression

The diff removes WG_LD_A16=1 from the @wg-kernel lines of exactly swin_block_c64_nc and d1p12/swin_block_c64_add_dec0_nc. That flag selects the 16-byte aligned load form added in PR 294, which was worth about -0.1 ms per frame on RADV at the time.
My inference is that the PR undoes that gain on RADV: the two kernels add +0.085 ms together, which fits. I have not rebuilt the PR with only that flag restored, so this is not confirmed. If removing it helps Windows, a Linux-only path could keep it.

Setup and scope

I used synthetic Model M with raw synthetic weights and frames, on its default route, with Model L also built in for both builds. Both were built the same way. The PR head contains the post_tf alignment-mask hoist, scale_rgb store fold, Wave 1 levers, and Wave 1.5 post_tf/VGPR, f16lut and encoder LDS changes.
The stack is D3D12 kernels through Wine + vkd3d-proton 5b1089c8 with dxil-spirv patches, RADV/ACO (Mesa git 1c3a936), on the DIRECT queue.
I did not test Windows, real model weights, RDNA 3, or other output sizes for timing.

Was removing WG_LD_A16=1 from those two kernels intended, or a side effect? If it helps Windows, would you keep it enabled for the Linux path?
Should I rebuild the PR with only that flag restored on RADV to confirm the number?

…history pitch visibly 8-aligned (byte-identical)

RADV review of #296: whole-frame GPU time was +0.087 ms against main because 7b0f6a5 dropped ,WG_LD_A16=1 from the
@wg-kernel lines of swin_block_c64_nc (+0.024 ms, 4921 -> 5441 instructions on ACO) and d1p12/swin_block_c64_add_dec0_nc
(+0.061 ms, 4122 -> 4578), which #294 had added for -101.6 us on RADV. Both lines have the flag again; no other
alignment define differs from main.

post.hlsl history_cr_span: the 24-bit multiplicand pitch & 0xffffff becomes pitch & 0xfffff8 (pitch is a multiple of 8,
so the value is the same bits), a mask with the low three bits clear, so the product with it is visibly a multiple of 8
to a compiler that reads masks and not known bits. RGA gfx1201: post_tf 76 VGPRs / 4212 instructions and post_tf_pf
77 / 5337, both unchanged (same code, one scalar and moved), still no v_and_b32 -8.
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.

2 participants