diff --git a/CMakeLists.txt b/CMakeLists.txt index b50d1ba1e0..fd491c10d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3302,6 +3302,9 @@ if(EXISTS "${INTAKE_PYTHON}") # lowering of the same selections. add_test(NAME startup_gfx2hlsl_sdwa COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sdwa_check.py") + # Handlers that do not model a modifier they can receive refuse it (#1771). + add_test(NAME startup_gfx2hlsl_modifier_free COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_modifier_free_check.py") # image_sample_c_lz with sampler word 2 bits 28-30 set (#1723). add_test(NAME startup_gfx2hlsl_sample_c_lz_mip COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sample_c_lz_mip_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index 691a34a15c..61263e8153 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -16103,6 +16103,20 @@ Building on #2443 (Slice 1), `X2W_D3D12_RELEASE_RETIRED=1` extends desktop objec ctest --preset dev --no-tests=error -R '^startup_resource_(retire|budget)' -V ``` +## Refusing unmodelled modifiers in the startup translator + +Several `Stage` instruction handlers accepted a `mods` list without reading it, so an encoding modifier such as +`clamp`, `omod` (`mul:2`), or `op_sel` on `v_mov_b32`, `v_cvt_pkrtz_f16_f32`, or `v_interp_mov_f32` was silently +dropped from the emitted HLSL (#1771). `MODIFIER_FREE_HANDLERS` declares these five handlers (`op_v_mov_b32`, +`op_v_cvt_pkrtz_f16_f32`, `op_v_interp_p1_f32`, `op_v_interp_p2_f32`, and `op_v_interp_mov_f32`), and `Stage.one` +refuses any non-empty `mods` list before dispatching to them. Scalar-ALU handlers are omitted because their encodings +have no modifier field. Separately, `op_v_interp_p1_f32` and `op_v_interp_p2_f32` refuse `abs`/`neg` modifiers (`|...|` +or `-...`) printed on the ignored data-source operand (`ops[1]`). + +```powershell +ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_modifier_free$' -V +``` + ## Source `abs` modifiers (`|src|` and `-|src|`) on `v_cndmask_b32_e64` in the startup translator `Stage.u_src` in [`tools/startup/gfx2hlsl.py`](../tools/startup/gfx2hlsl.py#L1506-L1521) now strips `|...|` after `split_neg(tok)` for `v_cndmask_b32_e64` data sources (`src0` and `src1`, both VGPRs and SGPRs), clearing the `off_diag.py` refusal `operand '|v0|' at v_cndmask_b32_e64 v0, v0, |v0|, s30`. Per the RDNA2 VOP3A source-modifier order, `abs` clears bit 31 (`& 0x7fffffffu`) first and `neg` flips bit 31 (`^ 0x80000000u`) second: diff --git a/tests/startup_gfx2hlsl_modifier_free_check.py b/tests/startup_gfx2hlsl_modifier_free_check.py new file mode 100644 index 0000000000..e18d4c1d16 --- /dev/null +++ b/tests/startup_gfx2hlsl_modifier_free_check.py @@ -0,0 +1,179 @@ +"""Handlers whose lowering does not model a modifier they can receive (issue #1771). + +usage: startup_gfx2hlsl_modifier_free_check.py + +Several handlers take the modifier list and never read it, so a modifier their encoding can carry was dropped +from the emitted HLSL - the same class as the `op_sel`/`clamp` cases fixed one at a time earlier +(`set_vf`, `op_v_cndmask_b32`, `compare`). The dispatch now refuses a modifier for the handlers declared in +`MODIFIER_FREE_HANDLERS`, before the handler can ignore it. + +The instruction bytes are **constructed here** from the field layout the checked-in decoder reads +(`tools/disasm_rdna2.py`): VOP1/VOP2-as-VOP3 use prefix `0b110101` with opcode `0x180 + n` / `0x100 + n`, +modifiers at `w0[15]` (clamp), `w0[14:11]` (op_sel) and `w1[28:27]` (omod); VOP3A uses the opcode in `w0[25:16]`. +""" +import os +import struct +import sys + +here = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.join(here, '..', 'tools')) +sys.path.insert(0, os.path.join(here, '..', 'tools', 'startup')) +import gfx2hlsl as G # noqa: E402 + +failures = [] + + +def check(label, condition, detail=''): + print(f"{label}: {'ok' if condition else 'MISMATCH'}") + if not condition: + failures.append(label) + if detail: + print(' ' + detail) + + +VOP1_E64_BASE = 0x180 +VOP2_E64_BASE = 0x100 +VOP1_MOV_B32 = 0x01 +VOP2_CVT_PKRTZ_F16_F32 = 0x2F +VOP3A_INTERP_P1 = 0x200 +VOP3A_INTERP_P2 = 0x201 +VOP3A_INTERP_MOV = 0x202 +V1, V2, V3 = 0x101, 0x102, 0x103 + + +def _word0(opcode, vdst, *, clamp=0, op_sel=(0, 0, 0, 0)): + w0 = (0b110101 << 26) | ((opcode & 0x3FF) << 16) | ((clamp & 1) << 15) | (vdst & 0xFF) + for i, bit in enumerate(op_sel): + w0 |= (bit & 1) << (11 + i) + return w0 + + +def encode_vop1(op, vdst, src0, *, clamp=0): + return struct.pack('