Skip to content

startup: lower image_load and image_load_mip with packed 16-bit results (d16) - #2119

Closed
Leyukaka wants to merge 2 commits into
R0Y00:mainfrom
Leyukaka:startup/gfx2hlsl-image-load-d16
Closed

Leyukaka wants to merge 2 commits into
R0Y00:mainfrom
Leyukaka:startup/gfx2hlsl-image-load-d16

Conversation

@Leyukaka

Copy link
Copy Markdown
Contributor

Part of #2023 (item D).

tools/startup/gfx2hlsl.py refused image_load and image_load_mip with the d16 modifier ("image_load address or modifiers: ... d16"). This lowers them.

  • Address. Unchanged, including the VGPR-supplied mip of image_load_mip; d16 does not affect it.
  • Result. The enabled channels are converted with f32tof16 in ascending dmask bit order and packed two per destination VGPR, low half first. For an odd last channel the lowering writes a zero high half (a choice; the hardware value of that half is not relied on). disasm_rdna2.py already decodes d16 and prints the halved vdata range.
  • Validation. The printed vdata range must have ceil(popcount(dmask) / 2) VGPRs. An empty dmask, or one wider than 0xf, is refused.
  • Assumption. The lowering assumes a float-returning view, like the plain load path, which declares Texture*<float4> and writes U(t.x). An integer view would need the bound view's format to choose a 16-bit integer result instead. The startup request does not carry that format today.
  • Still refused. d16 together with a16, and d16 on samples and gathers. image_store is not lowered at all.

Test changes

startup_image_load_mip_check.py and startup_image_2d_array_check.py listed d16 among the flags that must be refused ("Packed data ... need separate lowering"). This change is that lowering, so d16 is removed from those refusal loops; a16, tfe and the other entries stay. The positive behaviour is checked in the new tests/startup_image_load_d16_check.py:

  • decoded instruction text and emitted packing for dmask 0x1, 0x3, 0x7, 0xf and 0xa, on both opcodes, and a 2D-array image_load_mip (int4 address);
  • vdata-count mismatches, an invalid dmask, d16 with a16, and d16 on image_sample and image_gather4_lz, all refused;
  • a control that strips d16 before lowering and a mutation that swaps the two halves, both of which must fail the packing checks;
  • a plain (non-d16) image_load with dmask 0xf still emits the four U(t.c) writes. Every new branch is gated on d16.
python -B tests/startup_image_load_d16_check.py   # passes
python -B tests/startup_image_load_mip_check.py   # passes
python -B tests/startup_image_2d_array_check.py   # passes

The other startup_image_* checks, startup_translator_check.py and startup_gfx2hlsl_capture_check.py also pass. Registered as startup_image_load_d16_controls and added to the pure-Python lists.

Not measured: the checks compare emitted HLSL text only (no FXC compile, no WARP or hardware draw), and whether f32tof16 matches the console's D16 conversion (rounding, denormals, NaN payloads) is not tested. The fixtures are hand-encoded and decoded with disasm_rdna2.py, without an llvm-mc cross-check.

Negative control: against the previous tools/startup/gfx2hlsl.py, the new check fails with Unsupported: image_load address or modifiers: dmask:0x1 dim:SQ_RSRC_IMG_2D d16.

AI-assisted.

@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 packed dmask tests cover the register shape. The emitted f32tof16 still applies the same conversion to every bound view, while the startup request cannot prove the view is a float or normalized format. Please carry and validate the bound format, limit the first slice to a supported format family, and verify actual packed bits with a source-built case that distinguishes the required rounding. Include an odd dmask consumer control and refuse unsupported integer or ambiguous formats. The item D claimant at #2023 can contribute the normalization boundary case. https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf

@Leyukaka

Copy link
Copy Markdown
Contributor Author

Agreed: the same f32tof16 is emitted for every bound view, and the startup request cannot prove the view is a float or normalized format. This PR does not carry or validate the bound format. The emitted conversion assumes a float-returning view, as the plain-load path does.

Not addressed in this PR yet:

  • carrying and validating the bound format, and limiting the slice to a supported family;
  • the source-built case that checks the packed bits and the rounding;
  • the odd-dmask consumer control (0x1 and 0x7 are only checked as emitted text);
  • refusing integer or ambiguous formats.

@consolelogkyo's item D claim on #2023 describes narrowing with an integer round-to-nearest-even helper instead of f32tof16, which overlaps the conversion here. @R0Y00, @consolelogkyo, which do you prefer, to avoid two implementations of the conversion?

  • (a) The conversion and format validation land in consolelogkyo's change, and this PR is closed or reduced to the register-shape checks.
  • (b) They land here, and consolelogkyo adds the normalization boundary case on top.

I will hold further conversion changes until that is settled.

Parts of this PR that either change can reuse:

  • the vdata-range check (ceil(popcount(dmask)/2) VGPRs, and a refused empty or wider dmask);
  • the 2D-array mip case and the a16 + d16 refusal, both driven through printed operands, not encoded words;
  • the fixtures in tests/startup_image_load_d16_check.py for dmask 0x1/0x3/0x7/0xf/0xa on image_load and image_load_mip;
  • the unpacked negative control and the swapped-halves mutation.

AI-assisted.

@consolelogkyo

Copy link
Copy Markdown
Contributor

Thanks for checking in, @Leyukaka! Let's go with (a) and consolidate on #2139:

  • startup: lower level-zero D16 image_load in gfx2hlsl #2139 is already scoped to level-zero image_load (keeping image_load_mip pending per @R0Y00's note on help wanted: GPU translation, part 5 (vertex and pixel pipelines) #2023), uses an integer IEEE round-to-nearest-even helper (pack16_rne), and has llvm-mc / libLLVM checks plus DXC ps_6_0 and FXC ps_5_1 desktop GPU readback against DXGI_FORMAT_R16_UNORM and DXGI_FORMAT_R32G32B32A32_FLOAT textures (including ties separating nearest-even from truncation) on WARP and RX 9070 XT.
  • I am pushing a follow-up commit to startup: lower level-zero D16 image_load in gfx2hlsl #2139 now to add the remaining two requirements from @R0Y00's review here:
    1. Bound view format validation: carry the d16 float/normalized expectation in the manifest and validate the bound SRV format at draw time in d3d12_draw.inl (accepting proven floating-point / normalized formats and refusing integer, typeless, or unknown/ambiguous views).
    2. Odd-dmask consumer control: guard the undefined upper 16 bits ([31:16]) of odd-channel d16 loads (0x1, 0x2, 0x4, 0x8, 0x7, 0xb, 0xd, 0xe) so whole-dword consumers are refused while 16-bit low-half consumers (v_cvt_f32_f16, WORD_0 / BYTE_0 / BYTE_1 SDWA, low-half mix / op_sel, etc.) are accepted, with negative controls.

Also, as a friendly coordination note for future items on #2023 ("pick one, say so in a comment"): posting a quick claim comment on the tracking issue before starting/opening a PR really helps everyone avoid overlapping work and wasted effort when checking the issue thread for open items.

AI-assisted.

@Leyukaka

Copy link
Copy Markdown
Contributor Author

Thanks @consolelogkyo, (a) it is. Closing this in favour of #2139. Point taken on claiming in the tracking issue first; I'll do that before opening a PR.

AI-assisted.

@consolelogkyo

Copy link
Copy Markdown
Contributor

Follow-up on option (a): commit c805fba3 is now pushed on #2139 with both d16srv <index> float_norm bound SRV format validation (src/startup_stubs/d16_image_format.h, src/startup_stubs/d3d12_draw.inl, startup_d16_srv_{warp,hardware}) and the odd-dmask upper-half consumer guard + GPU poison controls (tools/startup/gfx2hlsl.py, tests/startup_image_load_d16_check.py), verified on WARP and AMD Radeon RX 9070 XT.

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.

3 participants