"""tools/image_fuzz.py --oracle hw without a GPU: the T# words per GPU family (exact dwords), the texture layout, the native kernel text, the model's dwords, the negative control and the exit-77 contract. The last test runs on the hardware when the oracle tools are present and is skipped otherwise. """ import json import os import random import struct import subprocess import sys import pytest HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(HERE) sys.path.insert(0, os.path.join(ROOT, "tools")) import image_fuzz as imf # noqa: E402 from hw_oracle import asm, runner, tsharp # noqa: E402 GFX11, GFX10, GFX101 = "gfx1150", "gfx1030", "gfx1010" def test_t_sharp_gfx11_exact_words(): # 16x8 R8G8B8A8_UNORM, linear: FORMAT 42 at [27:20], WIDTH_LO = 15 & 3 at [31:30], WIDTH_HI = 15 >> 2, # HEIGHT = 7 at [27:14], DST_SEL 4 5 6 7, TYPE 9 (2D) at [31:28], pitch 64 texels (256 bytes) - 1 in DEPTH. assert tsharp.image_words(GFX11, "R8G8B8A8_UNORM", 16, 8, 4) == [ 0, 0xC2A00000, 0x0001C003, 0x90000FAC, 0x3F, 0x00400000, 0, 0] def test_t_sharp_gfx10_exact_words(): # The same texture on GFX10.3: FORMAT 56 at [28:20], RESOURCE_LEVEL (bit 31 of word 2) set, same pitch field. assert tsharp.image_words(GFX10, "R8G8B8A8_UNORM", 16, 8, 4) == [ 0, 0xC3800000, 0x8001C003, 0x90000FAC, 0x3F, 0x00400000, 0, 0] def test_t_sharp_gfx101_has_no_pitch_field(): assert tsharp.image_words(GFX101, "R8G8B8A8_UNORM", 16, 8, 4)[4] == 0 assert not tsharp.has_custom_pitch(GFX101) and tsharp.has_custom_pitch(GFX10) and tsharp.has_custom_pitch(GFX11) def test_t_sharp_wide_texture_and_one_channel_swizzle(): words = tsharp.image_words(GFX11, "R32_SFLOAT", 64, 32, 4, (4, 0, 0, 1)) assert words[1] == (22 << 20) | (3 << 30) # width 64: WIDTH_LO = 63 & 3 assert words[2] == 15 | (31 << 14) # WIDTH_HI = 63 >> 2 assert words[3] == 0x90000204 # DST_SEL x,0,0,1 and the 2D type assert words[4] == 63 # 64 texels * 4 bytes = 256 bytes: pitch 64 words = tsharp.image_words(GFX11, "R32G32B32A32_SFLOAT", 8, 8, 16) assert words[1] == (63 << 20) | (3 << 30) and words[4] == 15 # 8 * 16 = 128 bytes pads to 256: pitch 16 def test_t_sharp_rejects_what_it_cannot_describe(): with pytest.raises(ValueError): tsharp.image_words("gfx1201", "R8G8B8A8_UNORM", 8, 8, 4) # GFX12 is another layout with pytest.raises(ValueError): tsharp.image_words(GFX11, "R8G8B8A8_UNORM", 0, 8, 4) with pytest.raises(KeyError): tsharp.image_words(GFX11, "D32_SFLOAT", 8, 8, 4) def test_format_tables_agree_with_the_translator_table_and_each_other(): with open(os.path.join(imf.TOOLS, "gfx10_formats.json"), encoding="utf-8") as f: by_name = {name: int(code) for code, name in json.load(f).items()} names = {"R8G8B8A8_UNORM": "8_8_8_8_UNORM", "R16G16B16A16_SFLOAT": "16_16_16_16_FLOAT", "R32G32B32A32_SFLOAT": "32_32_32_32_FLOAT", "R32_SFLOAT": "32_FLOAT", "R8G8B8A8_UINT": "8_8_8_8_UINT", "R8G8B8A8_SINT": "8_8_8_8_SINT", "R8_UINT": "8_UINT", "R8_SINT": "8_SINT", "R16_UINT": "16_UINT", "R16_SINT": "16_SINT", "R8G8_UINT": "8_8_UINT", "R8G8_SINT": "8_8_SINT", "R16G16_SINT": "16_16_SINT"} assert set(tsharp.IMG_FORMATS["gfx10"]) == set(tsharp.IMG_FORMATS["gfx11"]) == set(imf.HW_FORMATS) == set(names) assert set(imf.PCK_FORMATS) == set(names) - set(imf.FORMATS) for fmt, short in names.items(): assert tsharp.IMG_FORMATS["gfx10"][fmt] == by_name["GFX10_FORMAT_" + short] == imf.HW_FORMATS[fmt][1] # GFX11 numbers the formats in LLVM's UfmtGFX11 order: no 10_10_10_2-style gaps filled differently from GFX10 assert len(set(tsharp.IMG_FORMATS["gfx11"].values())) == len(imf.HW_FORMATS) def test_pitch_and_layout(): assert [tsharp.pitch_texels(w, 4) for w in (8, 16, 32, 64)] == [64, 64, 64, 64] assert [tsharp.pitch_texels(w, 16) for w in (8, 16, 32, 64)] == [16, 16, 32, 64] assert tsharp.pitch_texels(64, 8) == 64 tight = bytes(range(32)) # 8 x 1 RGBA8 texels laid = tsharp.lay_out(tight, 8, 1, 4) assert laid == tight + b"\xcd" * 224 assert len(tsharp.lay_out(bytes(8 * 4 * 3), 8, 3, 4)) == 3 * 256 def test_lanes(): assert tsharp.lanes(8, 8) == (64, 1) assert tsharp.lanes(32, 32) == (1024, 1) assert tsharp.lanes(64, 64) == (1024, 4) def case(fmt="R8G8B8A8_UNORM", width=8, height=8, dmask=0xf, wave=32, index=0, seed=5): data = imf.make_texels(fmt, width, height, random.Random(seed)) return imf.Case(index, fmt, width, height, dmask, data, wave) def test_kernel_text_per_family(): c = case(width=64, height=64, dmask=0x5) for arch in (GFX11, GFX10): k = imf.hw_kernel(arch, c) text = "\n".join(k.lines) assert k.threads == 1024 and k.words == 64 * 64 * 4 and k.raw and k.wave == 32 assert "image_load v[8:9], v[2:3], s[8:15] dmask:0x5 dim:SQ_RSRC_IMG_2D unorm" in text assert text.count("image_load") == 4 # 4096 texels over 1024 lanes assert "s_getpc_b64 s[4:5]" in text and ".p2align 8" in text and ".Ltex0:" in text assert text.index("s_endpgm") < text.index(".Ltex0:") # the texture sits behind the code full = asm.raw_kernel_text("k0", k.lines, arch) assert ("global_store_b128 v4, v[8:11], s[2:3]" in full) == (arch == GFX11) assert ("global_store_dwordx4 v4, v[8:11], s[2:3]" in full) == (arch == GFX10) one = imf.hw_kernel(GFX11, case(dmask=0x2)) assert "image_load v8, v[2:3]" in "\n".join(one.lines) def test_kernel_data_is_the_laid_out_texture(): c = case(width=8, height=2) lines = imf.hw_kernel(GFX11, c).lines start = lines.index(".Ltex0:") + 1 data = b"".join(int(x, 16).to_bytes(4, "little") for ln in lines[start:] for x in ln.split(".long")[1].split(",")) assert data == tsharp.lay_out(c.data[0], 8, 2, 4) def test_kernel_loads_the_t_sharp_the_builder_made(): c = case(fmt="R16G16B16A16_SFLOAT", width=16, height=8) text = "\n".join(imf.hw_kernel(GFX11, c).lines) words = tsharp.image_words(GFX11, c.fmt, 16, 8, 8) assert f"s_or_b32 s9, s9, 0x{words[1]:08x}" in text for i in range(2, 8): assert f"s_mov_b32 s{8 + i}, 0x{words[i]:08x}" in text def test_expected_words_pack_dmask_and_keep_sentinels(): c = case(fmt="R32G32B32A32_SFLOAT", width=8, height=8, dmask=0x5) want = imf.expected_words(c.fmt, c.data[0], 8, 8, 0x5) assert len(want) == 8 * 8 * 4 texel = struct.unpack_from("<4f", c.data[0], 0) first = [struct.unpack("