From 8a891a17569337db812bf7867828bf51748d89a6 Mon Sep 17 00:00:00 2001 From: Roan System Labs Date: Sun, 22 Nov 2026 21:46:02 +0100 Subject: [PATCH] disasm_rdna2: decode s_waitcnt with the GFX10 counter fields On GFX10 the s_waitcnt simm16 holds vmcnt in bits [3:0] and [15:14], expcnt in [6:4] and lgkmcnt in [13:8]. The decoder read vmcnt from [7:4] and dropped expcnt, so the compiled "s_waitcnt vmcnt(0)" (0x3f70) decoded as "vmcnt(7) lgkmcnt(63)" and "s_waitcnt expcnt(0)" (0xff0f) as "vmcnt(0) lgkmcnt(63)". Print the three counters as LLVM 15 does: counters at their maximum are omitted unless all three are. AI-assisted: yes --- tests/rdna2_tools_check.py | 27 ++++++++++++++++++++++++++- tools/disasm_rdna2.py | 10 +++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/tests/rdna2_tools_check.py b/tests/rdna2_tools_check.py index c27f5ef2b..48537ea9e 100644 --- a/tests/rdna2_tools_check.py +++ b/tests/rdna2_tools_check.py @@ -119,7 +119,7 @@ def test_golden_vectors(self): ("000080bf", "s_nop", "s_nop 0"), ("000081bf", "s_endpgm", "s_endpgm"), ("000082bf", "s_branch", "s_branch label_0x4"), - ("7f008cbf", "s_waitcnt", "s_waitcnt vmcnt(7) lgkmcnt(0)"), + ("7f008cbf", "s_waitcnt", "s_waitcnt vmcnt(15) lgkmcnt(0)"), ("000382be", "s_mov_b32", "s_mov_b32 s2, s0"), ("000482be", "s_mov_b64", "s_mov_b64 s2, s0"), ("00008080", "s_sub_u32", "s_sub_u32 s0, s0, s0"), @@ -300,6 +300,31 @@ def test_compressed_export_keeps_unpaired_mask(self): self.assertEqual(records[0]["assembly"], f"exp mrt0 {registers} done compr vm en:{mask:#x}") + def test_waitcnt_uses_gfx10_counter_fields(self): + # GFX10 s_waitcnt simm16: vmcnt [3:0] and [15:14], expcnt [6:4], lgkmcnt [13:8]. + vectors = [ + # Printed by LLVM 15.0.7 (MC/Disassembler/AMDGPU/gfx10_dasm_all.txt). + ("00008cbf", "s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)"), + ("34128cbf", "s_waitcnt vmcnt(4) expcnt(3) lgkmcnt(18)"), + ("51c18cbf", "s_waitcnt vmcnt(49) expcnt(5) lgkmcnt(1)"), + # Encoded by LLVM 15.0.7 (MC/AMDGPU/gfx10_asm_sop.s); bit 7 is unused. + ("d1c18cbf", "s_waitcnt vmcnt(49) expcnt(5) lgkmcnt(1)"), + # One counter each; the others at their maximum are omitted. + ("703f8cbf", "s_waitcnt vmcnt(0)"), + ("707f8cbf", "s_waitcnt vmcnt(16)"), + ("7f3f8cbf", "s_waitcnt vmcnt(15)"), + ("0fff8cbf", "s_waitcnt expcnt(0)"), + ("7fc08cbf", "s_waitcnt lgkmcnt(0)"), + ("7fff8cbf", "s_waitcnt vmcnt(63) expcnt(7) lgkmcnt(63)"), + ] + for hex_str, expected in vectors: + with self.subTest(encoding=hex_str): + records = disasm_rdna2(bytes.fromhex(hex_str + "000081bf")) + self.assertEqual([(r["opcode"], r["length"]) for r in records], + [("s_waitcnt", 4), ("s_endpgm", 4)]) + self.assertEqual(records[0]["assembly"], expected) + self.assertTrue(is_instruction_supported(expected)) + def test_literal_operands_keep_stream_aligned(self): # SOP1, SOP2, VOP1, VOP2 (src0 == 255) and v_fmaak_f32 each consume a trailing 32-bit literal. stream = bytes.fromhex( diff --git a/tools/disasm_rdna2.py b/tools/disasm_rdna2.py index 16669705c..a39bb5aaf 100644 --- a/tools/disasm_rdna2.py +++ b/tools/disasm_rdna2.py @@ -956,9 +956,13 @@ def decode_instruction(data: bytes, offset: int) -> tuple[int, str, str, str]: elif op_name == "s_nop": asm = f"s_nop {simm}" elif op_name == "s_waitcnt": - vm = (simm >> 4) & 0xf - lgkm = (simm >> 8) & 0x3f - asm = f"s_waitcnt vmcnt({vm}) lgkmcnt({lgkm})" + # GFX10 split: vmcnt is simm16[3:0] plus [15:14], expcnt [6:4], lgkmcnt [13:8]. + # Like LLVM, omit counters at their "no wait" maximum unless all three are. + counters = (("vmcnt", (simm & 0xf) | ((simm >> 10) & 0x30), 0x3f), + ("expcnt", (simm >> 4) & 0x7, 0x7), + ("lgkmcnt", (simm >> 8) & 0x3f, 0x3f)) + waits = [f"{name}({value})" for name, value, limit in counters if value != limit] + asm = "s_waitcnt " + " ".join(waits or (f"{name}({value})" for name, value, _ in counters)) elif op_name.startswith("s_cbranch") or op_name == "s_branch": target = offset + 4 + signed_simm * 4 asm = f"{op_name} label_0x{target:x}"