Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ jobs:
(["tests/startup_image_load_d16_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_image_load_d16_check.py", "decode", "libLLVM-15.so.1"], {}),
(["tests/startup_flat_param_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_gfx2hlsl_float_transcendental_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_gfx2hlsl_bit_alu_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_gfx2hlsl_interp_f16_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_gfx2hlsl_interp_p2_hi_check.py", "binary", "llvm-mc-15"], {}),
Expand Down Expand Up @@ -288,6 +289,7 @@ jobs:
tests/startup_run_ps1_check.py \
tests/startup_run_sh_check.py \
tests/startup_flat_param_check.py \
tests/startup_gfx2hlsl_float_transcendental_check.py \
tests/startup_ps_input_scan_check.py \
tests/startup_packed_position_provenance_check.py \
tests/startup_build_rev_check.py \
Expand Down
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4131,6 +4131,17 @@ if(EXISTS "${INTAKE_PYTHON}")
"$<TARGET_FILE:offscreen_check>" ${adapter} $<$<BOOL:${STARTUP_FXC}>:${STARTUP_FXC}>)
set_tests_properties(startup_inline_sampler_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
# gfx2hlsl binary32 reciprocal, roots, exp2 and log2 (VEC_F) on exact inputs: translation controls, then a
# draw of the translated pairs with reference words and one mutated lowering per operation.
add_test(NAME startup_gfx2hlsl_float_transcendental_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_float_transcendental_check.py")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_gfx2hlsl_float_transcendental_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_float_transcendental_check.py" draw "${DXC_EXECUTABLE}"
"$<TARGET_FILE:offscreen_check>" ${adapter} $<$<BOOL:${STARTUP_FXC}>:${STARTUP_FXC}>)
set_tests_properties(startup_gfx2hlsl_float_transcendental_${adapter} PROPERTIES TIMEOUT 300
SKIP_RETURN_CODE 77)
endforeach()
add_test(NAME startup_gfx2hlsl_scalar_shift64_bfe_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py")
add_test(NAME startup_gfx2hlsl_scalar_m0_controls COMMAND "${INTAKE_PYTHON}" -B
Expand Down
13 changes: 13 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -15757,6 +15757,19 @@ The [MASM fixture](../tests/cpu/vex_parity.asm) and [differential check](../test
ctest --preset release --no-tests=error -R '^cpu_aot_vex_parity$' -V
```

## Startup translator: reciprocal, roots, exp2 and log2

The `VEC_F` table of `tools/startup/gfx2hlsl.py` lowers six operations to HLSL with no check that ran them before: `v_rcp_f32` and `v_rcp_iflag_f32` to `1.0 / x`, `v_sqrt_f32` to `sqrt`, `v_rsq_f32` to `rsqrt`, `v_exp_f32` to `exp2` and `v_log_f32` to `log2`. `tests/startup_gfx2hlsl_float_transcendental_check.py` draws them with the shared runner `tests/startup_gfx2hlsl_lane_alu.py`. Each draw applies one operation per lane on flat inputs, alternates the `_e32` and `_e64` spellings, and compares the four result words exactly.

Every input is a power of two, or 0 and 9 for the square root, so every result is exact. The check therefore tests which function each operation is lowered to and the base 2 of `v_exp_f32` and `v_log_f32`. It does not test how many ulps an adapter's approximation may be off.

Each operation has a wrong lowering whose first draw must read its own mutated words:
- `2.0 / x` for the reciprocals;
- sqrt and rsqrt exchanged;
- the exponent off by one;
- the logarithm plus one.

On WARP 10.0.26100.9818 and on an AMD Radeon RX 6600 (driver 32.0.21045.29003), all 24 draws matched through FXC `ps_5_1` and DXC `ps_6_0`, and all 12 mutated draws read their mutated words. `v_sin_f32` and `v_cos_f32` are not covered: their results at the few exact points depend on the approximation. Tests: `startup_gfx2hlsl_float_transcendental_controls`, `startup_gfx2hlsl_float_transcendental_warp` and `startup_gfx2hlsl_float_transcendental_hardware`.
### gfx1013 pixel DPP16 quad broadcast

The pixel translator lowers `quad_perm:[k,k,k,k]` (k = 0 to 3) with full `row_mask` and `bank_mask` on `v_mov_b32` and the VOP1/VOP2 float, integer and integer-to-float forms to `QuadReadLaneAt(value, k)`. The four lanes of a pixel quad are one 2 x 2 block (lane = x & 1 | (y & 1) << 1). It is accepted in the entry straight-line prefix of a pixel stage that ran `s_wqm_b64 exec, exec` (or `s_wqm_b32 exec_lo, exec_lo`) with EXEC unchanged since: the entry EXEC can leave out lanes of a partly covered quad, and the guest would then read an inactive source lane while `QuadReadLaneAt` returns a helper lane's value. A later uniform loop or forward branch elsewhere in the same stage does not drop that proof (the M200-01/003 shape). Without the proof the instruction is refused, as are a broadcast after a branch target, after EXEC changed, or with divergent EXEC before the broadcast, swaps, pair broadcasts, row shifts, mirrors, DPP8, partial masks, `bound_ctrl:0`, non-VGPR sources and vertex stages. `fi:1` is accepted with the proof and changes nothing, since no source lane is disabled. A stage that holds a quad read compiles as `ps_6_0` with DXC (`X2W_DXC`, the Windows Kits `bin` folder or `PATH`), and its vertex stage as `vs_6_0`.
Expand Down
84 changes: 84 additions & 0 deletions tests/startup_gfx2hlsl_float_transcendental_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""Binary32 reciprocal, root, exponential and logarithm of tools/startup/gfx2hlsl.py (its VEC_F table).

(no arguments) translation controls, pure Python
binary <llvm-mc> reassemble the pixel bodies and compare them with the recorded words
draw <dxc> <offscreen_check> <warp|hardware> [fxc]
draw one pixel per four input sets and compare the four result words

Covers v_rcp_f32, v_rcp_iflag_f32, v_sqrt_f32, v_rsq_f32, v_exp_f32 and v_log_f32, which no check ran before. They
lower to 1.0 / x, sqrt, rsqrt, exp2 and log2. The fixture and runner are startup_gfx2hlsl_lane_alu.py: one operation
per lane, alternating the e32 and e64 spellings, on flat parameters the compilers cannot fold. The references follow
the AMD RDNA 2 ISA reference (v_exp_f32 and v_log_f32 are base 2). Every input is a power of two, or 0 and 9 for the
square root, so every result is exact and the comparison does not depend on how many ulps an adapter's
approximation may be off: it checks which function each operation is lowered to, not its precision.
"""
import math
import struct
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
from startup_gfx2hlsl_lane_alu import main, words # noqa: E402


def f(value):
return struct.unpack('<I', struct.pack('<f', value))[0]


def v(word):
return struct.unpack('<f', struct.pack('<I', word))[0]


def op(function):
return lambda a: f(function(v(a)))


OPS = {
'v_rcp_f32': (1, op(lambda x: 1.0 / x), True),
'v_rcp_iflag_f32': (1, op(lambda x: 1.0 / x), True),
'v_sqrt_f32': (1, op(math.sqrt), True),
'v_rsq_f32': (1, op(lambda x: 1.0 / math.sqrt(x)), True),
'v_exp_f32': (1, op(lambda x: 2.0 ** x), True),
'v_log_f32': (1, op(math.log2), True),
}


def sets(*values):
return [(f(x), 0, 0) for x in values]


RCP = sets(4.0, -0.5, 1.0, 0.125, 2.0 ** 20, -8.0, 256.0, 2.0 ** -10)
DRAW = {
'v_rcp_f32': RCP, 'v_rcp_iflag_f32': RCP,
'v_sqrt_f32': sets(16.0, 0.25, 1.0, 4096.0, 2.0 ** -20, 64.0, 0.0, 9.0),
'v_rsq_f32': sets(4.0, 0.25, 1.0, 16.0, 2.0 ** 20, 64.0, 2.0 ** -20, 0.0625),
'v_exp_f32': sets(3.0, -2.0, 0.0, 10.0, -10.0, 1.0, 20.0, -1.0),
'v_log_f32': sets(8.0, 0.125, 1.0, 1024.0, 2.0 ** -20, 2.0, 2.0 ** 30, 0.5),
}
F = r'F\((v\d+)\)'
# A wrong lowering of each operation: (pattern in the translated text, replacement, reference of the mutation).
MUTATIONS = {
'v_rcp_f32': (r'U\(1\.0 / ', r'U(2.0 / ', op(lambda x: 2.0 / x)),
'v_rcp_iflag_f32': (r'U\(1\.0 / ', r'U(2.0 / ', op(lambda x: 2.0 / x)),
'v_sqrt_f32': (r'U\(sqrt\(', r'U(rsqrt(', op(lambda x: 1.0 / math.sqrt(x))),
'v_rsq_f32': (r'U\(rsqrt\(', r'U(sqrt(', op(math.sqrt)),
'v_exp_f32': (r'U\(exp2\(%s\)\)' % F, r'U(exp2(F(\1)) * 2.0)', op(lambda x: 2.0 ** (x + 1))),
'v_log_f32': (r'U\(log2\(%s\)\)' % F, r'U(log2(F(\1)) + 1.0)', op(lambda x: math.log2(x) + 1)),
}
BODY = {
'v_rcp_f32': words('0055187e0d00aad50101000002551c7e0f00aad503010000'),
'v_rcp_iflag_f32': words('0057187e0d00abd50101000002571c7e0f00abd503010000'),
'v_sqrt_f32': words('0067187e0d00b3d50101000002671c7e0f00b3d503010000'),
'v_rsq_f32': words('005d187e0d00aed501010000025d1c7e0f00aed503010000'),
'v_exp_f32': words('004b187e0d00a5d501010000024b1c7e0f00a5d503010000'),
'v_log_f32': words('004f187e0d00a7d501010000024f1c7e0f00a7d503010000'),
}
FAMILY = {
'ops': OPS, 'draw': DRAW, 'mutations': MUTATIONS, 'body': BODY,
'accepted': ['v_rcp_f32 v1, v2', 'v_sqrt_f32_e64 v1, |v2|', 'v_rsq_f32_e64 v1, v2 clamp', 'v_exp_f32 v1, 1.0',
'v_log_f32_e64 v1, -v2 mul:2'],
'refused': ['v_rcp_f32_e64 v1, v2 neg_lo:[1]', 'v_log_f32_e64 v1, v2 neg_hi:[1]'],
}

if __name__ == '__main__':
main(FAMILY, __doc__)
Loading