shader_profile: report instruction categories by stage (#6856)

Squash-merged after diff review of PR #6856 - shader_profile: report instruction categories by stage
This commit is contained in:
Solthodox
2026-11-23 16:54:03 +01:00
committed by GitHub
parent 2fe59273b9
commit 108c36ef75
2 changed files with 28 additions and 3 deletions

View File

@ -388,9 +388,28 @@ class TestCommandLineEndToEnd(unittest.TestCase):
self.assertEqual(data["categories"]["memory"], 0)
self.assertIsNone(data["ratios"]["alu_to_memory"])
def test_cli_counts_category_occurrences_by_stage(self):
self.write("ps_first", "\tv_mov_b32 v0, 1\n" * 2)
self.write("ps_second", "\tv_mov_b32 v1, 2\n\ts_endpgm\n")
self.write("vs_first", "\ts_load_dword s0, s[2:3], 0\n")
self.write("unclassified", "")
self.write("zz_foo", "\tv_mov_b32 v2, 1\n")
self.write("LIB_helper", "\ts_endpgm\n")
_, data = self.run_profile()
stages = data["categories_by_stage"]
self.assertEqual(list(stages), ["?", "lib", "ps", "vs"])
self.assertEqual(stages["ps"], {"alu": 3, "memory": 0, "branch": 0,
"export": 0, "other": 1, "total": 4})
self.assertEqual(stages["vs"]["memory"], 1)
self.assertEqual(stages["?"]["alu"], 1)
self.assertEqual(stages["lib"]["total"], 1)
for key, count in data["categories"].items():
self.assertEqual(sum(stage[key] for stage in stages.values()), count)
def test_cli_empty_corpus(self):
stdout, data = self.run_profile()
self.assertEqual(data["shaders"], 0)
self.assertEqual(data["categories_by_stage"], {})
self.assertEqual(data["ratios"]["alu_to_memory"], 0.0)
self.assertEqual(data["undecoded"], {"warnings": 0, "unresolved": 0, "by_word": []})
self.assertIn(" undecoded: 0 LLVM warnings, 0 distinct words, 0 unresolved", stdout)

View File

@ -7,6 +7,7 @@ Counts each mnemonic (number of shaders using it and total occurrences), groups
classifies instructions (ALU vs memory vs branch), computes operation ratios, and summarises register pressure
per stage (VGPR/SGPR counts from `profile_shader`; `--per-shader` also lists every shader).
In the JSON report, ratios.alu_to_memory is null when the corpus has ALU instructions and no memory instructions.
`categories_by_stage` uses the carve index first, then a recognized stage prefix in the filename, otherwise `?`.
`undecoded` counts the llvm-mc 'invalid instruction encoding' warnings shader_disasm.py copies into each .s file.
by_word lists the four bytes at each warning position, their total occurrences and how many shaders contain them.
@ -27,6 +28,7 @@ FAMILIES = [("s_buffer_", "smem"), ("s_load", "smem"), ("s_", "scalar"), ("v_",
("buffer_", "buffer"), ("tbuffer_", "buffer"), ("image_", "image"), ("global_", "global"),
("flat_", "flat"), ("scratch_", "scratch"), ("exp", "export")]
STAGE_PREFIX = re.compile(r"(ps|vs|gs|hs|ds|cs|ms|as|lib)_", re.IGNORECASE | re.ASCII)
VGPR_RANGE = re.compile(r"\bv\[(\d+):(\d+)\]")
VGPR_SINGLE = re.compile(r"\bv(\d+)\b")
SGPR_RANGE = re.compile(r"\bs\[(\d+):(\d+)\]")
@ -327,7 +329,7 @@ def main():
occ, users = Counter(), Counter()
word_occ, word_users = Counter(), Counter() # warned-at word -> warnings, shaders
unresolved = 0
per_stage = defaultdict(Counter)
categories_by_stage = defaultdict(Counter)
reg_stage = defaultdict(list) # stage -> [(vgpr_count, sgpr_count)]
shaders = []
stages = stage_map(a.src)
@ -335,7 +337,10 @@ def main():
for entry in shader_files:
name = entry.name
n += 1
stage = stages.get(name[:-2]) or (name[:2].lower() if name[2:3] == "_" else "?")
stage = stages.get(name[:-2])
if not stage:
prefix = STAGE_PREFIX.match(name)
stage = prefix[1].lower() if prefix else "?"
try:
with open(entry.path, encoding="utf-8", errors="replace") as f:
text = f.read()
@ -350,7 +355,7 @@ def main():
"vgpr_count": regs["vgpr_count"], "sgpr_count": regs["sgpr_count"]})
occ.update(prof["mnemonics"])
users.update(prof["mnemonics"].keys())
per_stage[stage].update(prof["mnemonics"].keys())
categories_by_stage[stage].update(prof["categories"])
words, bad = warning_words(text)
word_occ.update(words)
word_users.update(set(words))
@ -367,6 +372,7 @@ def main():
out = {"shaders": n, "mnemonics": len(occ),
"families": {k: {"mnemonics": v[0], "occurrences": v[1]} for k, v in sorted(fam.items())},
"categories": cats,
"categories_by_stage": dict(sorted(categories_by_stage.items())),
"ratios": ratios,
"by_mnemonic": [{"m": m, "shaders": shader_count, "count": occ[m]}
for m, shader_count in ranked_users],