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 @@ -111,6 +111,7 @@ jobs:
(["tests/startup_image_sample_c_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_image_3d_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_image_load_mip_check.py", "--llvm-mc", "llvm-mc-15"], {}),
(["tests/startup_gfx2hlsl_float_round_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_image_2d_array_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_image_sample_a16_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_image_sample_a16_check.py", "decode", "libLLVM-15.so.1"], {}),
Expand Down Expand Up @@ -242,6 +243,7 @@ jobs:
tests/startup_image_3d_check.py \
tests/startup_image_load_mip_check.py \
tests/startup_gfx2hlsl_operand_count_check.py \
tests/startup_gfx2hlsl_float_round_check.py \
tests/startup_gfx2hlsl_scalar_arity_check.py \
tests/startup_gfx2hlsl_scalar_table_arity_check.py \
tests/startup_image_2d_array_check.py \
Expand Down
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3319,6 +3319,16 @@ if(EXISTS "${INTAKE_PYTHON}")
# gfx2hlsl refuses a binary32 or integer vector operation with a source too few or too many.
add_test(NAME startup_gfx2hlsl_operand_count COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_operand_count_check.py")
# gfx2hlsl binary32 rounding (VEC_F floor, ceil, trunc, fract, rndne): translation controls, then a draw of
# the translated pairs with reference words and one mutated lowering per operation.
add_test(NAME startup_gfx2hlsl_float_round_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_float_round_check.py")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_gfx2hlsl_float_round_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_float_round_check.py" draw "${DXC_EXECUTABLE}"
"$<TARGET_FILE:offscreen_check>" ${adapter} $<$<BOOL:${STARTUP_FXC}>:${STARTUP_FXC}>)
set_tests_properties(startup_gfx2hlsl_float_round_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77)
endforeach()
# gfx2hlsl refuses a scalar ALU operation whose operand list is too short.
add_test(NAME startup_gfx2hlsl_scalar_arity COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_scalar_arity_check.py")
Expand Down
14 changes: 14 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -14891,6 +14891,20 @@ The startup pixel stage lowers constant-lane SGPR spills (#2125 item E): `v_writ

The binary32 table (`VEC_F`) applies the same operand-count rule. Before this change, a form one source short (`v_add_f32 v1, v2`) raised `IndexError` in `Stage.one` instead of `Unsupported`. A form with one source too many (`v_med3_f32 v1, v2, v3, v4, v5`) translated with the extra source dropped. `tests/startup_gfx2hlsl_operand_count_check.py` (`startup_gfx2hlsl_operand_count`) checks every operation of both tables: one source fewer and one source more are refused, and the exact count translates. It also checks that the constant and accumulator forms (`v_fmac_f32`, `v_mac_f32`, `v_fmamk_f32`, `v_fmaak_f32`, `v_madmk_f32`, `v_madak_f32`) keep translating with their usual operands. As a control, it loads the translator with the new `VEC_F` guard removed and requires both old failures to come back.

## Startup translator: binary32 rounding

No check ran the rounding operations of the `VEC_F` table in `tools/startup/gfx2hlsl.py` before: `v_floor_f32`, `v_ceil_f32`, `v_trunc_f32`, `v_fract_f32` and `v_rndne_f32`. They lower to HLSL `floor`, `ceil`, `trunc`, `frac` and `round`. `tests/startup_gfx2hlsl_float_round_check.py` draws them with the shared runner `tests/startup_gfx2hlsl_lane_alu.py`, one operation per lane on flat inputs, alternating the `_e32` and `_e64` spellings, and compares the four result words exactly.

The references follow the AMD RDNA 2 ISA reference:
- A zero result keeps the sign of the source: `ceil(-0.75)`, `trunc(-0.75)` and `rndne(-2^-30)` are -0.
- `v_rndne_f32` rounds halves to even: 2.5 → 2, 3.5 → 4, -2.5 → -2, 1000000.5 → 1000000.
- `v_fract_f32` is `x - floor(x)`, capped at the largest binary32 below 1.0. With -2^-30 the exact difference rounds to 1.0, and the instruction gives `0x3f7fffff`.

The inputs also include the binary32 just below 0.5 and 8388607.5.

Each operation has a wrong lowering whose first draw must read its own mutated words: floor and ceil exchanged, floor for trunc, `x - trunc(x)` for frac, and trunc for round.

On WARP 10.0.26100.9818 and on an AMD Radeon RX 6600 (driver 32.0.21045.29003), all 20 draws matched through FXC `ps_5_1` and DXC `ps_6_0`, and all 10 mutated draws read their mutated words. On both adapters, HLSL `frac` of -2^-30 read `0x3f7fffff`, so the lowering kept the cap on these two adapters without an explicit comparison. Tests: `startup_gfx2hlsl_float_round_controls`, `startup_gfx2hlsl_float_round_warp` and `startup_gfx2hlsl_float_round_hardware`.
## Startup translator: v_swap_b32

`tools/startup/gfx2hlsl.py` now lowers `v_swap_b32` (VOP1, opcode 101), which it used to refuse. Both VGPRs exchange their old values, so the lowering reads the destination into a temporary before either register is written: `{ uint t = v1; v1 = v2; v2 = t; }`. The RDNA 2 ISA allows no input or output modifier on it, and LLVM 15 assembles only the 32-bit VOP1 spelling for gfx1013 (the `_e64`, `_dpp` and `_sdwa` forms and a scalar, constant, negated or 64-bit operand are rejected), so the translator refuses all of those and a modifier. A register-to-itself swap assembles and keeps its value. A proven-uniform register keeps that proof across the swap only while the exec mask is known.
Expand Down
81 changes: 81 additions & 0 deletions tests/startup_gfx2hlsl_float_round_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""Binary32 rounding operations 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_floor_f32, v_ceil_f32, v_trunc_f32, v_fract_f32 and v_rndne_f32, which no check ran before. 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: a zero result keeps the sign
of the source (ceil(-0.75) and trunc(-0.75) are -0), v_rndne_f32 rounds halves to even, and v_fract_f32 is
x - floor(x) capped at the largest binary32 below 1.0 (0x3f7fffff), so a tiny negative source does not give 1.0.
The inputs are exact halves and quarters, 1000000.5, the binary32 just below 0.5, 8388607.5 and -2^-30.
"""
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 signed_zero(function):
"""function on the binary32 value of a word; a zero result keeps the sign of the source."""
def apply(a):
x = v(a)
result = float(function(x))
return f(math.copysign(0.0, x) if result == 0 else result)
return apply


def fract(a):
x = v(a)
return min(f(x - math.floor(x)), 0x3f7fffff)


ONE_BELOW_HALF = 0x3effffff
TINY_NEGATIVE = 0xb0800000 # -2^-30
OPS = {
'v_floor_f32': (1, signed_zero(math.floor), True),
'v_ceil_f32': (1, signed_zero(math.ceil), True),
'v_trunc_f32': (1, signed_zero(math.trunc), True),
'v_fract_f32': (1, fract, True),
'v_rndne_f32': (1, signed_zero(round), True),
}
SETS = [(f(2.5), 0, 0), (f(-0.75), 0, 0), (f(3.5), 0, 0), (f(-2.5), 0, 0),
(f(1000000.5), 0, 0), (ONE_BELOW_HALF, 0, 0), (TINY_NEGATIVE, 0, 0), (f(8388607.5), 0, 0)]
DRAW = {op: SETS for op in OPS}
# A wrong lowering of each operation: (pattern in the translated text, replacement, reference of the mutation).
MUTATIONS = {
'v_floor_f32': (r'U\(floor\(', r'U(ceil(', signed_zero(math.ceil)),
'v_ceil_f32': (r'U\(ceil\(', r'U(floor(', signed_zero(math.floor)),
'v_trunc_f32': (r'U\(trunc\(', r'U(floor(', signed_zero(math.floor)),
'v_fract_f32': (r'U\(fract32\(F\((v\d+)\)\)\)', r'U(F(\1) - trunc(F(\1)))', lambda a: f(v(a) - math.trunc(v(a)))),
'v_rndne_f32': (r'U\(round\(', r'U(trunc(', signed_zero(math.trunc)),
}
BODY = {
'v_floor_f32': words('0049187e0d00a4d50101000002491c7e0f00a4d503010000'),
'v_ceil_f32': words('0045187e0d00a2d50101000002451c7e0f00a2d503010000'),
'v_trunc_f32': words('0043187e0d00a1d50101000002431c7e0f00a1d503010000'),
'v_fract_f32': words('0041187e0d00a0d50101000002411c7e0f00a0d503010000'),
'v_rndne_f32': words('0047187e0d00a3d50101000002471c7e0f00a3d503010000'),
}
FAMILY = {
'ops': OPS, 'draw': DRAW, 'mutations': MUTATIONS, 'body': BODY,
'accepted': ['v_floor_f32 v1, v2', 'v_ceil_f32_e64 v1, -v2', 'v_trunc_f32_e64 v1, |v2| clamp',
'v_fract_f32 v1, s2', 'v_rndne_f32_e64 v1, v2 mul:2'],
'refused': ['v_floor_f32_e64 v1, v2 neg_lo:[1]', 'v_rndne_f32_e64 v1, v2 neg_hi:[1]'],
}

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