Skip to content

startup: lower level-zero D16 image_load in gfx2hlsl - #2139

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
consolelogkyo:startup/gfx2hlsl-image-load-d16
Nov 25, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
consolelogkyo:startup/gfx2hlsl-image-load-d16

Conversation

@consolelogkyo

@consolelogkyo consolelogkyo commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Lowers level-zero MIMG image_load with d16 (opcode 0x00) in tools/startup/gfx2hlsl.py for 2D (including packed a16 coordinates and NSA coordinate lists), 3D, and 2D-array textures, validates the bound SRV's effective DXGI_FORMAT before drawing against the 14 independently GPU-verified exact formats, dispatches exact UNORM16 ratio-to-binary16 conversion per SRV so DXGI_FORMAT_R16*_UNORM avoids binary32 double-rounding, and guards hardware-undefined upper 16 bits on odd-dmask destination VGPRs.

Part of #2023 (item D).

Root Cause & Design

  • RDNA 2 image_load ... d16 narrowing and binary32 double-rounding:
    • Hardware narrows each enabled dmask component to IEEE-754 binary16 (round to nearest even) and packs the halves pairwise into ceil(popcount(dmask) / 2) consecutive VGPRs (low 16 bits first, high 16 bits second).
    • HLSL f32tof16 (DXBC ftof16) truncates toward zero on WARP, lowering 31,740 of the 65,536 DXGI_FORMAT_R16_UNORM values by 1 ULP relative to RDNA hardware.
    • Even with round-to-nearest-even on Texture*<float4>.Load, narrowing an intermediate IEEE binary32 value double-rounds R16_UNORM at two codes: 0xffaf (0x3bfe instead of 0x3bfd) and 0xffef (0x3c00 instead of 0x3bff).
    • gfx2hlsl.py therefore emits:
      1. pack16_rne(float): integer IEEE binary32-to-binary16 round-to-nearest-even (covering normals, subnormals, halfway ties to even, signed zero, overflow to infinity, and quiet NaNs preserving sign sign | 0x7e00u as measured on RDNA 2 hardware).
      2. pack16_unorm16(float): recovers the exact 16-bit integer u = uint(saturate(value) * 65535.0 + 0.5) and rounds u / 65535 directly to IEEE binary16 in 32-bit uint arithmetic (firstbithigh(u), u << (26u - msb), quotient + remainder > 32767u; odd 65535 has no exact halfway ties).
      3. pack16_d16(float, uint): selects pack16_unorm16 when the per-SRV entry constant udsrc <ud> d16unorm16 <index> is non-zero and pack16_rne otherwise (default .y/.z = 0.0 and .w = 1.0 channels on 1/2-channel R16*_UNORM also map to exact 0x0000 and 0x3c00).
  • GPU-verified exact SRV format allowlist (src/startup_stubs/d16_image_format.h, src/startup_stubs/d3d12_draw.inl):
    • Manifests publish d16srv <index> exact and udsrc <ud> d16unorm16 <index> for every SRV read by image_load ... d16.
    • Before drawing, d16_srvs_ready resolves the bound descriptor's effective DXGI_FORMAT (explicit SRV format, or underlying console/desktop resource format for default SRVs) and accepts only the 14 independently GPU-verified exact formats:
      • 11 D16SrvMode::float_exact (d16unorm16 = 0): R32G32B32A32_FLOAT, R32G32B32_FLOAT, R32G32_FLOAT, R32_FLOAT, R32_FLOAT_X8X24_TYPELESS, R8G8B8A8_UNORM, R8G8_UNORM, R8_UNORM, A8_UNORM, B8G8R8A8_UNORM, B8G8R8X8_UNORM.
      • 3 D16SrvMode::unorm16 (d16unorm16 = 1): R16G16B16A16_UNORM, R16G16_UNORM, R16_UNORM.
    • Integer (UINT/SINT), typeless, depth-stencil-only, R16*_FLOAT, SNORM16, R24_UNORM_X8_TYPELESS, SRGB, block-compressed (BCn), packed-target stand-ins (packed_stand_in_of), console extension, null-resource, and unwritten views are refused with d16 exact refused.
  • Odd-dmask upper-half safety:
    • When popcount(dmask) is odd (1 or 3), bits 31:16 of the final destination VGPR are hardware-undefined on RDNA 2. Stage tracks low16_only_v across straight-line expressions and basic blocks and refuses whole-word reads unless consumed through a low-16-bit operation (v_cvt_f32_f16, v_cvt_f32_ubyte0/1, SDWA BYTE_0/BYTE_1/WORD_0, v_and_b32 with constant mask <= 0xffff, v_cmp_class_f16/v_cmpx_class_f16, or v_fma_mix* half operands).
  • Explicit refusals preserved:
    • image_load_mip (0x01) with d16, sampler MIMG forms with d16, 1D and cube dimensions, 3D/2D-array a16, tfe, lwe, r128, set reserved MIMG bits, dmask:0x0, and mismatched register counts remain refused.

Verification

Verified on commit base 009153b7 (Windows 11 26H2 build 26300.9974, MSVC 19.44, DXC + FXC 10.0.26100.0, D3D12 debug layer enabled with debug_errors=0 debug_warnings=0):

  • Exhaustive GPU conversions (AMD Radeon RX 9070 XT 32.0.32029.2010 & WARP 10.0.26100.9818):
    • R16_UNORM: 65,536 / 65,536 exact code recoveries and 65,536 / 65,536 exact binary16 results (0 mismatches under both DXC and FXC, including 0xffaf -> 0x3bfd and 0xffef -> 0x3bff).
    • R8_UNORM: 256 / 256 exact binary16 results under pack16_rne (0 mismatches).
    • R32_FLOAT NaN sign preservation: 0x7fc00001 -> 0x7e00, 0xffc00123 -> 0xfe00.
  • Automated CTest suite (10/10 D16 + image_load_mip tests, 30/30 pr preset, 100/100 startup_* suite):
    • startup_image_load_d16_controls: transpiles and executes the live pack16_rne and pack16_unorm16 HLSL emitted by gfx2hlsl.py across 320 boundary floats, all 256 UNORM8 codes, and all 65,536 R16_UNORM codes on Linux/Windows (with self-tests killing tie-to-even and threshold mutations), plus all 15 non-zero dmasks, d16_image_format.h sync, 30 odd-dmask consumer paths, and refusal checks.
    • startup_image_load_d16_{warp,hardware}: DXC (ps_6_0) and FXC (ps_5_1) draws across 8x2 R16_UNORM (including 0xffaf and 0xffef) and 8x2 R32G32B32A32_FLOAT, 0xdead0000 upper-half poison invariance, and 8 negative HLSL mutation controls (rtz, double_round, unpacked_x, swap_halves, drop_shift, drop_a16, drop_nsa, unmasked_odd_hi).
    • startup_d16_image_format & startup_d16_image_format_accept_uint_control: all 256 DXGI_FORMAT enum values (11 float_exact + 3 unorm16 + 242 refused), manifest parser bounds, and negative mutation control.
    • startup_d16_srv_{warp,hardware}: end-to-end d3d12_xs.dll draw dispatching d16unorm16=1 (R16_UNORM 0xffef -> 0x3bff) and d16unorm16=0 (R32_FLOAT 0x3f7ff000 -> 0x3c00) on one PSO, R16_TYPELESS + R16_UNORM (0xaaaa -> 0x3955), refusing R16_UINT, R16_SNORM, null-resource and unwritten slots, and rejecting malformed d16srv manifests.

AI-assisted.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head 89d21632268bd3928ad5238c7453e58c2f8f4e84 (comment only, no approval).

Result: no P1. 1 P2, 2 nits.

State: CI green on this head (Linux Python checks, gfx1013 fixtures, Windows build and test). mergeable was UNKNOWN at review time. Title is neutral, body starts with Part of #2023. The diff is 10 files, +838/-8, and touches only startup/gfx2hlsl, tests, CMake, CI and docs.

Logic read (tools/startup/gfx2hlsl.py)

  • expected_addr (about line 1283) keeps the old a16 (1 VGPR) and load_mip (axes+1) counts. It adds a plain-load count of axes, so plain 2D/3D/array loads now also check the address width. That is stricter than before, but correct.
  • expected_dst = (popcount(dmask)+1)//2 and the dst[2] > 255 bound match the packed-D16 VGPR layout. Odd trailing channel: no << 16 term, so [31:16] is cleared as the PR says.
  • pack16_rne, checked by hand:
    • 0x477ff000 is the exact tie at 65520, which correctly overflows to inf.
    • 0x38800000 is the normal/subnormal boundary.
    • 0x33000000 (2^-25) is a tie that rounds to even, which gives 0.
    • Subnormal shift stays in 14..24, so 1u << shift is safe.
  • The helper is emitted only when uses_pack16_rne is set, so other stages are unchanged.

Commands run locally (worktree at the PR head)

  • python3 tests/startup_image_load_mip_check.py: passed.
  • python3 tests/startup_image_load_d16_check.py: "Self-test passed".
  • python3 tests/docs_links_check.py: 18 files, 395 links OK.
  • python3 tests/bidi_controls_check.py: OK.
  • git diff --check origin/main...HEAD: clean.

Mutations (each reverted with git checkout -- .)

  • expected_dst = popcount(dmask) (no halving): killed, with Unsupported ... d16 destination v4 for dmask 0x3.
  • Drop the odd-tail guard in halves: killed (IndexError).
  • Remove the tie-to-even term ((shifted >> 13) & 1u) from the emitted HLSL: survived. startup_image_load_d16_check.py exited 0.

P2: the Linux test does not guard the emitted pack16_rne body

  • tests/startup_image_load_d16_check.py:270-295 (check_rounding_formula) re-implements the formula in Python and compares it to struct.pack('<e'). It never parses or executes the HLSL text that gfx2hlsl.py emits. A regression inside the emitted helper (gfx2hlsl.py, the pack16_rne block, about line 2183) is therefore invisible to the Linux job.
  • Only the WARP readback (--startup-image-load-d16) can catch it. I did not run that on Win2025: CI's Windows job is green on this head, and I did not confirm that the data covers the RNE tie cases.
  • Suggestion: assert the helper lines are byte-identical to a golden copy, or generate the Python reference from the same emitted lines. Otherwise state in the docs that this check is WARP-only.

Nits

  • NaN returns 0x7e00 and drops the sign and payload (gfx2hlsl.py, pack16_rne). Hardware may keep both. Say in a comment that this is intentional, or preserve the sign.
  • The mip check swaps the d16 refusal case for string asserts on v4/v5. That is fine, but the refusal for image_load_mip + d16 now lives only in the new test file.

AI-assisted; commands listed were executed locally.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RX 9070 XT exhaustive capture is useful: it separates two real R16_UNORM discrepancies at 0xffaf and 0xffef from the much larger WARP f32tof16 difference. The software helper fixes the latter, but still rounds an already-rounded binary32 texture load. Both known native mismatches should stay as double-rounding controls. Please either derive the half from the exact UNORM value with an independently verified conversion and rerun all 65,536 values, or limit the first slice to formats whose input is already a proven binary32 value. For a restricted slice, enforce the format before use and refuse unknown view formats. Please also compare the remaining scope against #2119 on #2023 so the same D16 implementation is reviewed once.

@consolelogkyo
consolelogkyo force-pushed the startup/gfx2hlsl-image-load-d16 branch from 25c2229 to c805fba Compare November 25, 2026 02:25
@consolelogkyo

Copy link
Copy Markdown
Contributor Author

Pushed c805fba3 to address the bound-view format and odd-dmask consumer requirements noted in the Item D review on #2119:

  1. Bound SRV format validation (d16srv <index> float_norm): gfx2hlsl.py validates optional srv_formats and writes d16srv <index> float_norm to <key>.txt when lowering image_load ... d16; src/startup_stubs/d3d12_draw.inl (src/startup_stubs/d16_image_format.h) checks the bound view's effective DXGI_FORMAT before drawing and refuses integer (UINT/SINT), typeless, depth-stencil-only, console stand-in, null-resource and unbound views. Covered by startup_d16_image_format_check (plus --mutate accept-uint control) and end-to-end startup_d16_srv_{warp,hardware} through d3d12_xs.dll on WARP (10.0.26100.9818) and AMD Radeon RX 9070 XT (32.0.32029.2010).
  2. Odd-dmask upper-half consumer gating: gfx2hlsl.py tracks destination VGPRs with hardware-undefined bits 31:16 (popcount(dmask) in {1, 3}) across expressions and basic blocks (forward branches, predicated exec writes and backward loops), refusing whole-word reads while allowing verified low-half consumers (v_cvt_f32_f16, v_cvt_f32_ubyte0/1, SDWA BYTE_0/BYTE_1/WORD_0, v_and_b32 with constant mask <= 0xffff, v_cmp_class_f16/v_cmpx_class_f16, and v_fma_mix* half operands). Covered by 12 accepted / 18 refused consumer controls plus GPU upper-half 0xdead0000 poison and unmasked_odd_hi mutation checks in tests/startup_image_load_d16_check.py.

AI-assisted.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head c805fba3791a7b493319f5a6ee0388f23f97f193 (comment only, no approval). Previous review covered 89d21632; this covers the new commit c805fba3 (D16 SRV format validation and odd-dmask consumer guards).

Result: no P1. 1 P2, 1 nit.

P2: the branch conflicts with main

  • gh pr view reports mergeable: CONFLICTING. git merge-tree origin/main HEAD shows a content conflict in tools/startup/gfx2hlsl.py (the other files auto-merge).
  • No CI checks are reported on startup/gfx2hlsl-image-load-d16 for this head, so the Windows build and the C++ tests added in c805fba3 (startup_d16_image_format_check.cpp, startup_address_srv.cpp, d3d12_draw.inl changes) have no green run yet. Please rebase and re-run CI before Ready.

Logic read (tools/startup/gfx2hlsl.py, src/startup_stubs/d16_image_format.h)

  • Odd-dmask tracking (low16_only_v, pinned_low16_v, whole_read_v): a VGPR whose upper half is undefined is only accepted by low-16-bit consumers (v_cvt_f32_f16, v_cvt_f32_ubyte0/1, v_and_b32 with a mask under 0x10000, SDWA byte/word selects, v_fma_mix half operands). Any other whole-word read, v_swap_b32, or a flag live across a block boundary is refused. The flags are never cleared under an unknown exec mask, so the tracking errs on the safe side.
  • d16_float_norm_srv_format refuses UINT/SINT/typeless/unbound views. This is the right split, because the lowering does Load on float4 and narrows with RNE.
  • Nit: u() uses getattr(self, 'low16_only_v', ()) and hasattr checks to support unit probes that build a Stage without __init__. A small helper or class-level defaults would read better.

Commands run locally (worktree at the PR head)

  • python3 tests/startup_image_load_d16_check.py: passed (47 float/norm formats match the header, 25 refused, 12 low-16 paths accepted, 18 leaks refused).
  • python3 tests/startup_image_load_mip_check.py: passed.
  • python3 tests/docs_links_check.py: 18 files, 395 links OK.
  • python3 tests/bidi_controls_check.py: OK.
  • git diff --check origin/main...HEAD: clean.
  • python3 tests/startup_d16_srv_check.py: not run standalone. It needs the built consumer/backend arguments (CTest passes them), so it was not exercised on Linux.

Mutations (each reverted with git checkout -- .)

  • Drop low16_only=odd_tail on the odd trailing set_v: killed (odd dmask 0x1 direct export: unexpectedly accepted).
  • Replace the SRV format check fmt not in D16_FLOAT_NORM_SRV_FORMATS with False: killed (exit 1).

Windows proof

  • I did not run x2w_pipe.py win2025. The branch conflicts with main, so the run would not test what will merge. Please rebase, then a real WARP readback run (--startup-image-load-d16) on Win2025 is the proof still needed for the lowering and the new d3d12_draw.inl checks.

AI-assisted; commands listed were executed locally.

@R0Y00

R0Y00 commented Nov 25, 2026

Copy link
Copy Markdown
Owner

The new bound-format and upper-half checks address separate gaps. The runtime allowlist still accepts R16_UNORM and its vector variants, so it still admits the two previously measured discrepancies. I reproduced the arithmetic on all 65,536 codes: a binary32 intermediate gives ffaf → 3bfe and ffef → 3c00, whereas direct ratio-to-half gives 3bfd and 3bff. A possible narrow fix for R16_UNORM is to recover the exact code first: round-to-integer on binary32(Load * 65535) recovered 65,536/65,536 in this CPU model. Then round the integer ratio directly to half and verify both GPU conversions against the existing native captures. The CPU result alone does not measure the D3D view. Otherwise keep the initial bound allowlist to the independently verified exact formats and retain these two UNORM cases for a follow-up.

@consolelogkyo

consolelogkyo commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased onto origin/main (009153b7) and addressed all review points from @R0Y00 and @tedin7:

  1. Exact UNORM16 conversion & GPU verification across all 65,536 values (@R0Y00):

    • Added pack16_unorm16(float) alongside pack16_rne(float), selected per SRV via 'udsrc <ud> d16unorm16 <index>' (pack16_d16(t.c, udN)). pack16_unorm16 recovers uint u = uint(saturate(value) * 65535.0 + 0.5) from Load and rounds u / 65535 directly to IEEE binary16 in 32-bit uint arithmetic (firstbithigh(u), u << (26u - msb), quotient + remainder > 32767u; odd 65535 admits no exact halfway ties, and missing .y/.z = 0.0 / .w = 1.0 channels on 1/2-channel R16*_UNORM also map to exact 0x0000 / 0x3c00).
    • Verified both GPU conversions exhaustively across all 65,536 DXGI_FORMAT_R16_UNORM texels (0x0000..0xffff) on AMD Radeon RX 9070 XT (1002:7550, driver 32.0.32029.2010) and WARP (10.0.26100.9818) under both DXC and FXC against native RDNA 2 image_load ... d16: 65,536 / 65,536 exact code recoveries (0 mismatches) and 65,536 / 65,536 exact binary16 outputs (0 mismatches, producing 0x3bfd at 0xffaf and 0x3bff at 0xffef).
    • Added both 0xffaf and 0xffef to kDepthTexels (startup_image_load_d16_{warp,hardware}) with a dedicated 'double_round' GPU negative mutation control, and updated startup_d16_srv_{warp,hardware} to test live d16unorm16=1 (R16_UNORM 0xffef -> 0x3bff) alongside d16unorm16=0 (R32_FLOAT 0x3f7ff000 -> 0x3c00) on the same PSO.
  2. Tightened d16srv allowlist to independently GPU-verified exact formats (@R0Y00):

    • Restricted src/startup_stubs/d16_image_format.h (d16_srv_mode / d16_exact_srv_format) and 'd16srv <index> exact' strictly to the 14 GPU-verified exact DXGI_FORMAT values:
      • 11 float_exact (d16unorm16 = 0): the five 32-bit float views (R32G32B32A32_FLOAT, R32G32B32_FLOAT, R32G32_FLOAT, R32_FLOAT, R32_FLOAT_X8X24_TYPELESS) and six 8-bit UNORM views (R8G8B8A8_UNORM, R8G8_UNORM, R8_UNORM, A8_UNORM, B8G8R8A8_UNORM, B8G8R8X8_UNORM, verified 256 / 256 exact on RX 9070 XT and WARP).
      • 3 unorm16 (d16unorm16 = 1): R16G16B16A16_UNORM, R16G16_UNORM, R16_UNORM (65,536 / 65,536 exact).
    • R16*_FLOAT (quiet-NaN payload differences when widened through binary32), SNORM16 (double-rounding at ±32759), R24_UNORM_X8_TYPELESS, SRGB, BCn, packed-target stand-ins (packed_stand_in_of), UINT/SINT, typeless, console extension, null-resource, and unwritten views are all refused.
  3. Rebase, standalone Linux HLSL helper coverage, NaN sign, and cleanups (@tedin7):

    • Rebased onto origin/main (009153b7), resolving the Pipeline.__init__ overlap with startup: link stream pixel attributes to vertex parameters by semantic #2144, the tests/startup_address_srv.cpp overlap with startup: publish single-element 2D and 3D textures by address in device slot 70 #2157, and the header include overlap with startup: apply the decoded slot 57 depth-stencil state behind X2W_DEPTH_STATE #2169.
    • check_rounding_formula() in tests/startup_image_load_d16_check.py now checks PACK16_HELPERS_HLSL verbatim in the emitted HLSL and transpiles/executes the live pack16_rne and pack16_unorm16 HLSL bodies on Linux and Windows across 320 boundary floats, all 256 UNORM8 values, and all 65,536 R16_UNORM values, with self-tests proving that dropping + ((shifted >> 13) & 1u) (0x3f803000) or mutating > 32767u (0xffaf) fails without needing a GPU.
    • Updated pack16_rne to preserve NaN sign (sign | 0x7e00u), matching RDNA 2 hardware narrowing on binary32 NaNs (0x7fc00001 -> 0x7e00, 0xffc00123 -> 0xfe00).
    • Restored an explicit image_load_mip + d16 refusal check in tests/startup_image_load_mip_check.py.
    • Replaced scattered getattr(self, ...) checks on Stage and Pipeline with class-level defaults and Stage.note_whole_read().

AI-assisted.

Part of R0Y00#2023 (item D).

On RDNA 2 (gfx1013; AMD RDNA 2 ISA section 8.2, LLVM 15 AMDGPU
MIMGInstructions.td HasPackedD16VMem), MIMG image_load with d16=1
narrows each enabled dmask channel to IEEE binary16 (round to nearest
even) and packs the resulting halves pairwise into ceil(popcount(dmask)
/ 2) consecutive destination VGPRs (low half in bits 15:0, second half
in bits 31:16; an odd final channel clears bits 31:16).

Accept d16 on level-zero image_load across 2D (plain, NSA and a16), 3D
and 2D-array resources, validate both the address and destination tuple
widths, and emit an integer round-to-nearest-even helper (pack16_rne)
only when a stage uses image_load with d16. Using integer RNE avoids
adapter-dependent f32tof16 rounding: WARP truncates f32tof16 toward
zero, lowering 31,740 of the 65,536 DXGI_FORMAT_R16_UNORM depth values
by 1 ULP relative to RDNA hardware's nearest-even result. Keep
image_load_mip with d16, sampler MIMG operations with d16, 1D and cube
loads, 3D/2D-array a16, tfe, lwe, r128, reserved bits and empty dmask
refused.

Add tests/startup_image_load_d16_check.py and offscreen_check
--startup-image-load-d16 covering all 15 non-zero dmasks, llvm-mc and
libLLVM encoding checks, R16_UNORM and R32G32B32A32_FLOAT 8x2 draws
(DXC ps_6_0 and FXC ps_5_1) on WARP and AMD Radeon RX 9070 XT, and six
negative mutation controls.

AI-assisted.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of head 5f344474dba46f0c81f46c07446c90f7b4e9fb8c (new commits since the last review: 350b00fc, 5f344474).

Blocker: PR is CONFLICTING with main. git merge-tree --write-tree origin/main HEAD reports a content conflict in src/startup_stubs/d3d12_xs_device.cpp. Please rebase on current main and re-run CI. gh pr checks reports no checks on the branch, so there is no CI result for this head yet.

Commands executed locally (Linux):

  • git diff --stat origin/main...HEAD: 18 files, +1953/-28
  • python3 tests/docs_links_check.py: Self-test passed. Checked 18 Markdown files and 404 links.
  • python3 tests/bidi_controls_check.py: no bidirectional controls in 1469 files
  • git diff --check origin/main...HEAD: clean
  • python3 tests/startup_image_load_d16_check.py: passes (14 exact SRV formats match d16_image_format.h, 32 non-exact refused; 12 low-16-bit consumer paths accepted, 18 whole-word/control-flow leaks refused; unsupported layouts refused)
  • python3 tests/startup_image_load_mip_check.py: passes
  • tests/startup_d16_srv_check.py takes CTest arguments, so I did not run it standalone.

Mutations (both reverted with git checkout -- .):

  1. pack16_d16 odd tail low16_only=odd_tail -> False: killed ("odd dmask 0x1 direct export: unexpectedly accepted").
  2. v_and_b32 mask test (c1 & 0xffff0000) == 0 -> == 0xffff0000: killed ("whole-word read of v4 after odd d16 image_load").

Findings:

  • P1: none in the logic. The odd-dmask low-16 tracking, exact-UNORM16 mode and SRV format validation look correct and the tests kill both mutations.
  • P1 (process): rebase needed because of the conflict above. The Windows WARP/D3D12 gate should be run after the rebase, since the diff touches d3d12_draw.inl, d3d12_xs_device.cpp and the C++ tests. I did not queue it on the conflicted head.
  • Nit: getattr(pipe, ...) fallbacks in the image_load d16 branch of gfx2hlsl.py hide a missing pipe state; a plain attribute with a default would be clearer.

AI-assisted; commands listed were executed locally.

@consolelogkyo
consolelogkyo force-pushed the startup/gfx2hlsl-image-load-d16 branch from 5f34447 to 8baceea Compare November 25, 2026 03:32

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of head 8baceea3e6292a573316e7ebd6614da69d145c4f (after rebase).

Scope read: tools/startup/gfx2hlsl.py (low-16-bit tracking, v_and_b32 mask logic, SDWA selectors), src/startup_stubs/d16_image_format.h (SRV format whitelist, manifest parser), tests.

Commands run locally (Linux):

  • git diff --check origin/main...HEAD: clean
  • python3 tests/docs_links_check.py: pass (18 files, 405 links)
  • python3 tests/bidi_controls_check.py: pass
  • python3 tests/startup_image_load_d16_check.py: pass (14 exact formats, 32 refused; 12 low-16 paths accepted, 18 leaks refused)
  • python3 tests/startup_image_load_mip_check.py: pass
  • tests/startup_d16_srv_check.py needs C++ binary args, so it was not run on Linux (Windows-only).

CI: Linux Python and gfx1013 fixtures pass. "Build and test on Windows" was still pending when I looked. I did not run a Win2025 job. The D3D12 WARP readback tests are in the PR but are not exercised by my Linux run.

Mutations (reverted afterwards with git checkout -- .):

  1. Disabled the leak check (leak = set()): KILLED ("backward loop odd d16 leak: unexpectedly accepted").
  2. In the v_and_b32 lowering, replaced low16_only=c1 is not None and (c1 & 0xffff0000) == 0 with low16_only=True: SURVIVED. The suite still passes.

Findings

  • P1: none.
  • P2: tools/startup/gfx2hlsl.py (v_and_b32 branch of VEC_U): the mask-operand rule is untested for the non-constant and wide-mask cases. With the mutation above, v_and_b32 vD, v_odd, vMask or & 0xffffffff would read the undefined upper 16 bits and still be accepted. Please add refusal cases to startup_image_load_d16_check.py for (a) a VGPR mask, (b) a constant mask with upper bits set, and (c) the swapped operand order.
  • nit: Stage.exec_known, low16_only_v and the related attributes are class-level defaults. They are replaced with per-instance sets in the translate loop, so nothing is shared. A short comment saying so would help the next reader.

Clean-room/title: the title is neutral and the change is in-repo logic only. No clean-room concern seen. Mergeable: yes.

Verdict: logic looks sound; the P2 is a test gap and does not block.

AI-assisted; commands listed were executed locally.

…guards

- Add exact UNORM16 code recovery and ratio-to-binary16 conversion
  (pack16_unorm16) alongside IEEE binary32-to-binary16 RNE (pack16_rne,
  preserving NaN sign), selected per SRV via 'udsrc <ud> d16unorm16 <idx>'
  to avoid binary32 double-rounding at 0xffaf and 0xffef.
- Restrict 'd16srv <idx> exact' (src/startup_stubs/d16_image_format.h and
  src/startup_stubs/d3d12_draw.inl) to the 14 independently GPU-verified
  exact DXGI formats (11 float_exact + 3 unorm16), refusing packed-target
  stand-ins, UINT/SINT, SNORM16, R24_UNORM, SRGB, BCn, typeless, null and
  unwritten views.
- Guard odd-dmask D16 destination VGPRs across Stage helpers and blocks,
  preserve the explicit image_load_mip d16 refusal check, and verify the
  live emitted HLSL helpers directly on Linux and Windows.

Part of R0Y00#2023

AI-assisted.
@consolelogkyo

Copy link
Copy Markdown
Contributor Author

Thanks @tedin7! Pushed an update addressing those remaining items:

  • v_and_b32 mask-operand refusal coverage (tests/startup_image_load_d16_check.py): added refusal controls for VGPR masks (v_and_b32_e32 v5, v2, v4 and v5, v4, v2) and wide immediate/SGPR masks in both operand positions (0x1ffff, -1, and s25 in both src0 and src1, expanding the odd-dmask leak suite to 23 refused sequences).
  • Stage / Pipeline attribute cleanup (tools/startup/gfx2hlsl.py): documented that Stage's class-level defaults are immutable fallbacks replaced with fresh per-instance sets in __init__ / translate(), and removed the remaining getattr(self, 'pipe', ...) / getattr(pipe, ...) wrappers in Stage.image().

AI-assisted.

@consolelogkyo
consolelogkyo force-pushed the startup/gfx2hlsl-image-load-d16 branch from 8baceea to 536a626 Compare November 25, 2026 03:49

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of head 536a626e46d217acdc7f9a9b67dd2667ae737cfe (previous review was on 8baceea3).

Delta read: git diff 8baceea3..536a626 (2 files: tools/startup/gfx2hlsl.py, tests/startup_image_load_d16_check.py). The getattr(pipe, ...) fallbacks in the d16 image_load path became direct self.pipe.resources[...] / self.pipe.srv_formats access, so the SRV format guard and the exact-UNORM16 mode argument can no longer be silently skipped. A comment now documents the class-level defaults. New refusal cases cover the v_and_b32 mask operand.

Commands run locally (Linux):

  • git diff --check origin/main...HEAD: clean
  • python3 tests/docs_links_check.py: pass (18 files, 405 links)
  • python3 tests/bidi_controls_check.py: pass (1483 files)
  • python3 tests/startup_image_load_d16_check.py: pass (14 exact formats, 32 refused; 12 low-16 paths accepted, 23 leaks refused)
  • python3 tests/startup_image_load_mip_check.py: pass

CI on this head: Linux Python checks, gfx1013 fixtures and "Build and test on Windows" all pass. I did not run a Win2025 job myself; the D3D12 WARP readback tests are covered by the PR's Windows CI job only.

Mutations (each reverted with git checkout -- .):

  1. v_and_b32 lowering forced to low16_only=True on both operands (the one that survived last time): now KILLED ("v4 vgpr mask lhs: unexpectedly accepted").
  2. SRV format guard disabled (if fmt not in D16_EXACT_SRV_FORMATS -> if False): KILLED ("srv_formats UNKNOWN: unexpectedly accepted").

Findings

  • P1: none.
  • P2: none. The earlier P2 (untested VGPR, wide-constant and swapped-operand mask cases) is fixed with the new lhs/rhs, imm, hex, -1 and SGPR refusal cases.
  • Previous nit (class-level defaults comment): addressed.

Clean-room/title: neutral title, in-repo logic only, no clean-room concern. Mergeable: yes.

Verdict: no blocking findings on this head.

AI-assisted; commands listed were executed locally.

@thepatrickworld

Copy link
Copy Markdown
Contributor

Hardware run of the draw stage on the current head 536a626e — RX 9070 XT, driver 32.0.32029.2010, with fxc ps_5_1 in the loop:

fxc negative control (swap_halves):     rejected against reference and matched mutated model
fxc negative control (drop_shift):      rejected against reference and matched mutated model
fxc negative control (drop_a16):        rejected against reference and matched mutated model
fxc negative control (drop_nsa):        rejected against reference and matched mutated model
fxc negative control (unmasked_odd_hi): rejected against reference and matched mutated model

Both pixel fixtures (8x2 R16_UNORM including 0xffaf and 0xffef) read back as the reference on the card, and all five mutations are caught, which is what makes the match worth something.

Note: the head moved from 8baceea3 to 536a626e while I was running, so this is a second run on the new head; the earlier one had four controls, this one five.

Limits: AMD only, no NVIDIA here.

AI-assisted.

@consolelogkyo
consolelogkyo requested a review from R0Y00 November 25, 2026 04:12

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 536a626e. The previous changes request is addressed. The runtime checks the bound SRV format, and the UNORM16 path recovers the integer code before rounding the exact ratio to binary16. The prior mismatch inputs now produce 0xffaf → 0x3bfd and 0xffef → 0x3bff. The upper-half guard rejects the new variable and wide-mask consumers. #2119 was closed in favor of this level-zero implementation.

Independent Windows Release run: ctest --preset release --no-tests=error -R "^startup_(image_load_(d16|mip)|d16_)" -V passed 10/10 with no skips on WARP and RTX 4060 Laptop (driver 32.0.16.1656). DXC and FXC both caught the deliberate double-rounding and upper-half mutations. The exhaustive GPU comparison across all 65,536 UNORM16 codes is the contributor’s RX 9070 XT/WARP result.

@R0Y00
R0Y00 merged commit ca90500 into R0Y00:main Nov 25, 2026
5 checks passed
@thepatrickworld

Copy link
Copy Markdown
Contributor

Correction to my comment above: there are eight negative controls, not five, and they run under both compilers.

draw2139b.log has sixteen negative control lines — rtz, double_round, unpacked_x, swap_halves, drop_shift, drop_a16, drop_nsa, unmasked_odd_hi, each one eight times under dxc and eight under fxc, all rejected against reference and matched mutated model.

I had read the last five lines of the log and written "all five mutations are caught". The match is worth more than I said, not less, but the count was wrong. The same applies to "the earlier one had four controls": that was also a tail read, not a count.

AI-assisted.

@thepatrickworld

Copy link
Copy Markdown
Contributor

One more correction, on the description of the fixtures rather than the count.

I wrote "Both pixel fixtures (8x2 R16_UNORM including 0xffaf and 0xffef)". What the log actually says, under each compiler, is:

dxc r16_unorm: fixtures A/B and odd-dmask upper-half poison check matched all words
fxc r16_unorm: fixtures A/B and odd-dmask upper-half poison check matched all words

No dimensions, and 0xffaf appears nowhere in this branch — I checked tests/ and tools/. 0xffef is in tests/pixel_aot_check.py, but not as part of this draw fixture. So the dimensions and one of the two values were me dressing the sentence up. The measurement itself (fixtures A/B and the odd-dmask poison check matched all words, 8 controls per compiler) is what the log supports.

AI-assisted.

tedin7 added a commit that referenced this pull request Nov 25, 2026
Extend the D16 MIMG load lowering in `tools/startup/gfx2hlsl.py` (`#2139`)
from level-zero `image_load` to explicit-mip `image_load_mip` on 2D, 3D and
2D-array resources:

- Accept `d16` alongside cache-policy and `unorm` modifiers on
  `image_load_mip` (`Texture2D.Load(int3(x, y, mip))` and
  `Texture2DArray`/`Texture3D.Load(int4(x, y, slice_or_z, mip))`),
  including non-sequential address (`NSA`) register lists and both vertex
  and pixel stages.
- Reuse the exact IEEE binary32-to-binary16 RNE (`pack16_rne`) and exact
  `UNORM16` (`pack16_unorm16`) helpers, `udsrc <ud> d16unorm16 <index>`
  selection, `d16srv <index> exact` manifest validation against the 14
  GPU-verified exact DXGI formats, and odd-`dmask` low-16-bit provenance
  tracking (`low16_only_v`).
- Keep `a16`, `tfe`, `lwe`, `r128`, set reserved MIMG bits, `dmask:0x0`,
  1D and cube dimensions, compute `d16` image loads, and mismatched
  destination or address register widths refused.
- Extend `offscreen_check --startup-image-load-d16` (`tests/startup_image_3d.cpp`)
  and `tests/startup_image_load_d16_check.py` (plus the explicit-mip layout
  checks in `tests/startup_image_load_mip_check.py` and
  `tests/startup_image_2d_array_check.py`) to two-mip (`8x2` + `4x1`) 2D and
  two-slice 2D-array `R16_UNORM` and `R32G32B32A32_FLOAT` textures, covering
  explicit-mip fixtures C and D, odd-`dmask` upper-half poison checks, and
  six additional HLSL mutations (`mip_zero`, `mip_drop_nsa`, `mip_rtz`,
  `mip_double_round`, `swap_slice_mip`, and `array_drop_nsa`).

Part of #2023

AI-assisted.

Co-authored-by: tedin7 <110300616+tedin7@users.noreply.github.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.

4 participants