Skip to content

docs: gfx1036 measurements for v_add/sub_nc_i16, ds_mskor_b32 and the 64-bit LDS bitwise ops - #4059

Open
UlteriorMotive00 wants to merge 1 commit into
ItsNotRexed:mainfrom
UlteriorMotive00:docs/gfx1036-i16-lds-rows
Open

UlteriorMotive00 wants to merge 1 commit into
ItsNotRexed:mainfrom
UlteriorMotive00:docs/gfx1036-i16-lds-rows

Conversation

@UlteriorMotive00

Copy link
Copy Markdown
Contributor

Three rows in docs/design.md said "Not GPU-verified." and had no hardware measurement at all. This PR measures them on native RDNA2: gfx1036, the Radeon iGPU of a Ryzen 7 9800X3D, Windows 11, driver 32.0.21042.62, through tools/hw_oracle (ROCm rocm-sdk-core==10.0.0 clang). Docs only.

Row Result
v_add_nc_i16, v_sub_nc_i16 112/112 directed cases (overflow edges, with and without clamp, four op_sel forms including the destination half, the other half kept). pk16_diff.py --oracle translator,hw --ops v_add_nc_i16,v_sub_nc_i16 --programs 3000 --seed 1: 3000/3000
ds_mskor_b32, ds_mskor_rtn_b32 20/20 one-thread cases match (M & ~DATA) | DATA2 and return M
ds_and/or/xor_b64 (and _rtn_) 39/39 one-thread cases, including vdst aliasing the data pair

Both LDS approximations, measured with 32 lanes on one address:

  • ds_mskor_rtn_b32 (M = 0xABCD12FF, DATA = 0xFF, DATA2 = 1 << (lane & 7)): the hardware runs one lane at a time in lane order. Each lane gets the word the previous lane left, and the dword ends 0xABCD1280. The row's AND-then-OR emission would end at 0xABCD12FF. That is the row's stated approximation, now with numbers. I wrote it as expected and did not run this kernel through the translator.
  • ds_xor_rtn_b64 with (1 << lane) in both dwords: again lane order, and lane i returns 2^i − 1 in both words, so the hardware never returns a torn pair.

Every case uses synthetic immediates and an LDS preset with a read-back. No game data.

Checked: python -m unittest tests.test_docs_design_rows tests.test_docs_table_rows tests.test_docs_repo_paths tests.test_docs_cli_flags tests.test_docs_links passes.

Claim: #3796 (comment)

Probe script (run from the repo root with LEONIDA_HIP_DLL set)
"""Directed gfx1036 measurements for three docs/design.md rows: v_add/sub_nc_i16, ds_mskor(_rtn)_b32,
ds_and/or/xor(_rtn)_b64. Synthetic immediates only. Run from the repo root: python -I <this> (LEONIDA_HIP_DLL set)."""
import os
import sys

sys.path.insert(0, os.path.join(os.getcwd(), "tools", "hw_oracle"))
import asm  # noqa: E402
import runner  # noqa: E402

M = 0xFFFFFFFF
ONE = ["v_mov_b32 v30, 0", "global_store_dwordx4 v30, v[8:11], s[2:3]", "s_endpgm"]


def K(lines, lds=0):
    return asm.Kernel(list(lines) + ONE, 32, 1, 4, raw=True, lds=lds)


def sx16(x):
    x &= 0xFFFF
    return x - 0x10000 if x & 0x8000 else x


def mov(reg, val):
    return f"v_mov_b32 {reg}, 0x{val & M:08x}"


cases = []  # (label, Kernel, expected list of 4 words or None for "report only")

# ---- v_add_nc_i16 / v_sub_nc_i16 -------------------------------------------------------------------------------
def nc16(op, a, b, sel=(0, 0, 0), clamp=False, keep=0xA5A5A5A5):
    ha = (a >> 16) if sel[0] else a
    hb = (b >> 16) if sel[1] else b
    r = sx16(ha) + sx16(hb) if op == "add" else sx16(ha) - sx16(hb)
    r = max(-32768, min(32767, r)) if clamp else r
    r &= 0xFFFF
    d = (keep & 0x0000FFFF) | (r << 16) if sel[2] else (keep & 0xFFFF0000) | r
    return d


grid = [(0x7FFF, 1), (0x8000, 1), (0x8000, 0xFFFF), (0x7FFF, 0x7FFF), (0x1234, 0x0F0F), (0xFFFF, 0xFFFF),
        (0x8000, 0x8000)]
for op in ("add", "sub"):
    for a, b in grid:
        for clamp in (False, True):
            for sel in ((0, 0, 0), (1, 0, 1), (0, 1, 0), (1, 1, 1)):
                av = (a << 16 | 0x0102) if sel[0] else (0x0304 << 16 | a)
                bv = (b << 16 | 0x0506) if sel[1] else (0x0708 << 16 | b)
                mods = f" op_sel:[{','.join(map(str, sel))}]" + (" clamp" if clamp else "")
                lines = [mov("v1", av), mov("v2", bv), mov("v8", 0xA5A5A5A5),
                         f"v_{op}_nc_i16 v8, v1, v2{mods}", mov("v9", 0), mov("v10", 0), mov("v11", 0)]
                exp = [nc16(op, av, bv, sel, clamp), 0, 0, 0]
                cases.append((f"v_{op}_nc_i16 0x{a:04x},0x{b:04x}{mods}", K(lines), exp))

# ---- ds_mskor ---------------------------------------------------------------------------------------------------
def lds_pre(addr_reg, val, off=0):
    return [mov("v20", val), f"ds_write_b32 {addr_reg}, v20 offset:{off}", "s_waitcnt lgkmcnt(0)"]


mskor = [(0xFFFF0000, 0x0000FF00, 0x00001234), (0x12345678, 0xFFFFFFFF, 0), (0x12345678, 0, 0xF0000000),
         (0xAAAAAAAA, 0x0F0F0F0F, 0xFFFF0000), (0, 0, 0)]
for mem, data, data2 in mskor:
    for rtn in (False, True):
        for off in (0, 12):
            lines = [mov("v0", 8)] + lds_pre("v0", mem, off) + [mov("v1", data), mov("v2", data2)]
            if rtn:
                lines += [f"ds_mskor_rtn_b32 v8, v0, v1, v2 offset:{off}"]
            else:
                lines += [f"ds_mskor_b32 v0, v1, v2 offset:{off}", mov("v8", 0)]
            lines += ["s_waitcnt lgkmcnt(0)", f"ds_read_b32 v9, v0 offset:{off}", "s_waitcnt lgkmcnt(0)",
                      mov("v10", 0), mov("v11", 0)]
            new = ((mem & ~data) | data2) & M
            exp = [mem if rtn else 0, new, 0, 0]
            cases.append((f"ds_mskor{'_rtn' if rtn else ''}_b32 M=0x{mem:08x} D=0x{data:08x} D2=0x{data2:08x} "
                          f"off {off}", K(lines, 256), exp))

# ---- ds_{and,or,xor}_b64 ----------------------------------------------------------------------------------------
fn = {"and": lambda x, y: x & y, "or": lambda x, y: x | y, "xor": lambda x, y: x ^ y}
pairs = [(0xFFFF0000_0000FFFF, 0x0F0F0F0F_F0F0F0F0), (0x12345678_9ABCDEF0, 0xFFFFFFFF_00000000),
         (0x80000000_00000001, 0x80000000_00000001)]
for op in ("and", "or", "xor"):
    for mem, data in pairs:
        for rtn in (False, True):
            for off in (0, 24):
                lines = [mov("v0", 16), mov("v20", mem), mov("v21", mem >> 32),
                         f"ds_write_b64 v0, v[20:21] offset:{off}", "s_waitcnt lgkmcnt(0)",
                         mov("v2", data), mov("v3", data >> 32)]
                if rtn:
                    lines += [f"ds_{op}_rtn_b64 v[8:9], v0, v[2:3] offset:{off}"]
                else:
                    lines += [f"ds_{op}_b64 v0, v[2:3] offset:{off}", mov("v8", 0), mov("v9", 0)]
                lines += ["s_waitcnt lgkmcnt(0)", f"ds_read_b64 v[10:11], v0 offset:{off}", "s_waitcnt lgkmcnt(0)"]
                new = fn[op](mem, data)
                exp = [mem & M if rtn else 0, mem >> 32 if rtn else 0, new & M, new >> 32]
                cases.append((f"ds_{op}{'_rtn' if rtn else ''}_b64 M=0x{mem:016x} D=0x{data:016x} off {off}",
                              K(lines, 256), exp))

# vdst aliasing the data pair (row: "vdst aliasing the data")
for op in ("and", "or", "xor"):
    mem, data = 0x0000FFFF_FFFF0000, 0x00FF00FF_0F0F0F0F
    lines = [mov("v0", 16), mov("v20", mem), mov("v21", mem >> 32), "ds_write_b64 v0, v[20:21]",
             "s_waitcnt lgkmcnt(0)", mov("v8", data), mov("v9", data >> 32), f"ds_{op}_rtn_b64 v[8:9], v0, v[8:9]",
             "s_waitcnt lgkmcnt(0)", "ds_read_b64 v[10:11], v0", "s_waitcnt lgkmcnt(0)"]
    new = fn[op](mem, data)
    cases.append((f"ds_{op}_rtn_b64 vdst = data pair", K(lines, 256),
                  [mem & M, mem >> 32, new & M, new >> 32]))

# ---- 32 lanes on one LDS dword / element (the rows' approximation notes): report the words ----------------------
STORE = ["v_lshlrev_b32 v30, 4, v31", "global_store_dwordx4 v30, v[8:11], s[2:3]", "s_endpgm"]
LANE = ["v_mbcnt_lo_u32_b32 v31, -1, 0"]
# mskor: every lane DATA=0xFF, DATA2=lane id, memory starts 0xABCD12FF. One op at a time: the final word is
# 0xABCD1200 | (the last lane's id), and each rtn is the word before that lane. AND-then-OR: 0xABCD1200 | 0x1F.
coll = LANE + [mov("v0", 0), "s_mov_b32 s20, 0xabcd12ff", "v_mov_b32 v20, s20", "v_cmp_eq_u32 vcc_lo, 0, v31",
               "s_and_saveexec_b32 s21, vcc_lo", "ds_write_b32 v0, v20", "s_waitcnt lgkmcnt(0)",
               "s_mov_b32 exec_lo, s21", "s_barrier", mov("v1", 0xFF),
               "v_and_b32 v29, 7, v31", "v_lshlrev_b32 v29, v29, 1", "ds_mskor_rtn_b32 v8, v0, v1, v29", "s_waitcnt lgkmcnt(0)", "s_barrier", "ds_read_b32 v9, v0",
               "s_waitcnt lgkmcnt(0)", "v_mov_b32 v10, v29", mov("v11", 0)] + STORE
cases.append(("collide32 ds_mskor_rtn_b32", asm.Kernel(coll, 32, 32, 128, raw=True, lds=256), None))
# xor_b64: every lane xors (1 << lane) into both dwords' bits: rtn pairs should be whole (lo == hi) on hardware.
coll = LANE + [mov("v0", 0), "v_cmp_eq_u32 vcc_lo, 0, v31", "s_and_saveexec_b32 s21, vcc_lo", mov("v20", 0),
               mov("v21", 0), "ds_write_b64 v0, v[20:21]", "s_waitcnt lgkmcnt(0)", "s_mov_b32 exec_lo, s21",
               "s_barrier", "v_lshlrev_b32 v2, v31, 1", "v_mov_b32 v3, v2",
               "ds_xor_rtn_b64 v[8:9], v0, v[2:3]", "s_waitcnt lgkmcnt(0)", "s_barrier", "ds_read_b64 v[10:11], v0",
               "s_waitcnt lgkmcnt(0)"] + STORE
cases.append(("collide32 ds_xor_rtn_b64", asm.Kernel(coll, 32, 32, 128, raw=True, lds=256), None))

o = runner.open_oracle()
print("#", o.device)
res = o.run([k for _, k, _ in cases])
fails = 0
for (label, _, exp), got in zip(cases, res):
    if exp is None:
        print("REPORT", label)
        for lane in range(32):
            print("   lane %2d: %s" % (lane, " ".join("%08x" % w for w in got[lane * 4:lane * 4 + 4])))
        continue
    ok = got == exp
    fails += not ok
    print(("PASS" if ok else "FAIL"), label, " ".join("%08x" % w for w in got) if isinstance(got, list) else got,
          "" if ok else "expected " + " ".join("%08x" % w for w in exp))
print(f"{len([c for c in cases if c[2] is not None]) - fails} of {len([c for c in cases if c[2] is not None])} match")
o.close()

AI-assisted (Claude Code).

🤖 Generated with Claude Code

… 64-bit LDS bitwise ops

The three rows said "Not GPU-verified" with no hardware data. Measured on native RDNA2 (gfx1036) through
tools/hw_oracle: 112 + 20 + 39 directed one-thread cases match each row's rule, pk16_diff agrees on 3000
programs, and two 32-lane collision kernels show the LDS ops run lane by lane: ds_mskor ends where the
two-atomic emission would not, and ds_xor_rtn_b64 never returns a half-done pair.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@YaBulochka

Copy link
Copy Markdown
Contributor

Heads-up on one row: v_add_nc_i16/v_sub_nc_i16 is also in #4045, which I opened a couple of minutes before this PR and which rewrites the same sentence to point at runtime/test/shaders/cs_w32_i16_clamp.asm. The two will conflict on that line.

Small correction to the claim as well: that row already carried a native measurement ("Measured on native gfx1100: clamp saturates ..."), so it may not fit the "no hardware measurement of any kind yet" criterion you picked it under. What it lacked was a Vulkan-level check, which #4045 adds.

Either of these settles it, whichever you prefer:

Your ds_mskor and ds_and/or/xor_b64 measurements do not overlap with anything of mine (my #3208 only adds fixtures for ds_wrxchg2_* and the LDS bitwise b64 ops, without touching those doc rows).

@YaBulochka

Copy link
Copy Markdown
Contributor

Correction to my last sentence: #3208 does edit the ds_and_b64/ds_or_b64/ds_xor_b64 row as well (it adds the cs_w32_lds_b64_bitwise.asm reference next to the gfx1100 text), so that row conflicts too, not just the v_add_nc_i16 one. Only ds_mskor_b32 is free of overlap.

For both rows the same two options work: either you keep the native gfx1036 words and include the fixture reference in the same sentence, or the fixtures keep the doc text and your words go in a follow-up. I do not mind rebasing mine; just tell me which way you want it and I will adjust #4045/#3208 to match.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs checks at 42f97ea7: test_docs_links.py 6 OK, test_docs_design_rows.py 2 OK, test_docs_list_items.py 2 OK, git diff --check clean.

The three rows it edits have matching CPU semantic coverage on this tree: test_valu_add_sub_nc_i16.py 4 OK, test_lds_mskor.py 4 OK, test_lds_bitwise_b64.py 6 OK. The new hardware sentences are scoped to gfx1036/driver 32.0.21042.62 and tools/hw_oracle, and the tables list exact counts/directions; I cannot reproduce the RDNA2 numbers on this NVIDIA host, but the claims are internally consistent with the existing translator tests. No blocking issue.

@tupacfan04

Copy link
Copy Markdown
Contributor

Content verified on Windows 11 / Python 3.13.0: the 20 docs cases (7 modules) pass, git diff --check clean, files LF-only, and tools/pk16_diff.py --ops v_add_nc_i16,v_sub_nc_i16 --programs 3000 --seed 1 gives 3000 pass / 0 mismatch on the translator side (no gfx1036 hardware here). No duplicate rows, each new row cites gfx1036 + tools/hw_oracle, the pk16 row cites seed 1, and the counts reconcile (112 = 7 pairs x 2 ops x 2 clamp x 4 op_sel; 20 = 5 triples x 2 forms x 2 offsets; 39 = 3 ops x 3 pairs x 2 forms x 2 offsets + 3 aliasing). The mskor "not run through the translator" tail is scoped correctly.

Needs a rebase: open #4045 rewrites the same v_add_nc_i16 row (docs/design.md:283), and merge-tree conflicts at :283 and adjacent :343. The two should be combined - #4045's gfx1102/9600X evidence plus this 9800X3D run.

Two content notes: the body says the rows "had no hardware measurement at all", but main already ends each with "Measured on native gfx1100" (:283,343,347), so this adds a second GPU rather than the first; and :282 says clamp on "any other 16-bit integer op" is Unsupported while :283 documents it for v_add/sub_nc_i16 - a pre-existing tension worth smoothing while you are in the row.

Approve after the rebase. AI-assisted review, no game files, no GPU.

@UlteriorMotive00

Copy link
Copy Markdown
Contributor Author

Thanks @YaBulochka, and you're right about the claim wording. On f847748c, where I picked the rows, v_add_nc_i16 said only "Not GPU-verified". The gfx1100 sentence landed on main after that. The gfx1036 numbers are still a second architecture, and RDNA2 is the console's.

Please don't change #4045 or #3208. They were first, and their fixture references should stay as they are. Whichever of them lands before this one, I'll rebase #4059 onto it and keep your GPU-checked by … clause, with the gfx1036 sentence added next to the gfx1100 one. So the conflict is mine to resolve, not yours. (test_docs_repo_paths would fail if I cited your fixtures before they exist on main, so I won't add them here first.)

Thanks also @tupacfan04 and @Kam471 for the checks. AI-assisted (Claude Code).

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.

4 participants