upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts/vs_layout_census.py
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-23 14:10:52 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 08:00:13 -0500
commitad136ffb9c8ab1b36cf4ccbe0ca72aecb50217b6 (patch)
treea4ba91de7aa947a23de58a42f24de86de4827d31 /scripts/vs_layout_census.py
parent51f31609236a952badeb9c9aba9efc2a6e4dc2ff (diff)
vs_layout_census: refuse a --json that is an input or a directory before survey() (BUILD-589)
--json was checked with a raw os.path.samefile against index.json only, so naming an indexed .dxbc container or a disasm/*.s listing overwrote that input, and a directory ran survey() (llvm-mc over the corpus) before the write failed. main() now refuses, before survey() runs, a --json that is an existing directory, and one that tools/report_paths.same_report_path matches against index.json or any container or listing the index names (input_paths(), which shares load_index() with survey()). Each refusal is one error line and exit 2. Synthetic tests in tests/test_vs_layout_census.py: a listing, a container and index.json refuse and stay unchanged; a directory refuses with survey() and default_disasm mocked and never called; a ../ spelling, a symlink, a hard link and (on a case-folding volume) another case refuse; a separate --json still runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'scripts/vs_layout_census.py')
-rw-r--r--scripts/vs_layout_census.py40
1 files changed, 33 insertions, 7 deletions
diff --git a/scripts/vs_layout_census.py b/scripts/vs_layout_census.py
index 249a9c74e..1d7174e77 100644
--- a/scripts/vs_layout_census.py
+++ b/scripts/vs_layout_census.py
@@ -31,6 +31,7 @@ sys.path.insert(0, os.path.join(ROOT, "scripts"))
31import fssm_unpack # noqa: E402 31import fssm_unpack # noqa: E402
32import pal_stages # noqa: E402 32import pal_stages # noqa: E402
33import shader_disasm # noqa: E402 33import shader_disasm # noqa: E402
34from report_paths import same_report_path # noqa: E402
34import shader_ir # noqa: E402 35import shader_ir # noqa: E402
35from vs_input_survey import (FETCH_CALL, MBCNT, PARAM_EXPORT, POS0_EXPORT, PRIM_EXPORT, SENDMSG, # noqa: E402 36from vs_input_survey import (FETCH_CALL, MBCNT, PARAM_EXPORT, POS0_EXPORT, PRIM_EXPORT, SENDMSG, # noqa: E402
36 read_before_write) 37 read_before_write)
@@ -87,10 +88,9 @@ def payloads_of(dxbc_path):
87 for i, blob in enumerate(blobs)] 88 for i, blob in enumerate(blobs)]
88 89
89 90
90def survey(dxbc_dir, disasm_fn=None, limit=None): 91def load_index(dxbc_dir):
91 """The census over <dxbc>: counts by stage, hardware id, markers and VS entry VGPRs.""" 92 """(stage by stem, stems in index order) from <dxbc>/index.json."""
92 index_path = os.path.join(dxbc_dir, "index.json") 93 with open(os.path.join(dxbc_dir, "index.json"), encoding="utf-8") as f:
93 with open(index_path, encoding="utf-8") as f:
94 index = json.load(f) 94 index = json.load(f)
95 if not isinstance(index, list) or not all(isinstance(e, dict) for e in index): 95 if not isinstance(index, list) or not all(isinstance(e, dict) for e in index):
96 raise ValueError("index.json is not a list of shader entries") 96 raise ValueError("index.json is not a list of shader entries")
@@ -102,6 +102,26 @@ def survey(dxbc_dir, disasm_fn=None, limit=None):
102 if stem not in stages: 102 if stem not in stages:
103 names.append(stem) 103 names.append(stem)
104 stages[stem] = e.get("stage") 104 stages[stem] = e.get("stage")
105 return stages, names
106
107
108def input_paths(dxbc_dir):
109 """Every file the census may read: index.json, then each indexed container and its disasm/<stem>.s listing.
110 An index that cannot be read names no more; survey() reports it."""
111 paths = [os.path.join(dxbc_dir, "index.json")]
112 try:
113 _stages, names = load_index(dxbc_dir)
114 except (OSError, ValueError):
115 return paths
116 for name in names:
117 paths.append(os.path.join(dxbc_dir, name + ".dxbc"))
118 paths.append(os.path.join(dxbc_dir, "disasm", name + ".s"))
119 return paths
120
121
122def survey(dxbc_dir, disasm_fn=None, limit=None):
123 """The census over <dxbc>: counts by stage, hardware id, markers and VS entry VGPRs."""
124 stages, names = load_index(dxbc_dir)
105 if limit is not None: 125 if limit is not None:
106 names = names[:limit] 126 names = names[:limit]
107 127
@@ -186,9 +206,15 @@ def main(argv=None):
186 ap.add_argument("--limit", type=int, help="stop after this many containers (index order)") 206 ap.add_argument("--limit", type=int, help="stop after this many containers (index order)")
187 a = ap.parse_args(argv) 207 a = ap.parse_args(argv)
188 index_path = os.path.join(a.dxbc, "index.json") 208 index_path = os.path.join(a.dxbc, "index.json")
189 if a.json and os.path.exists(a.json) and os.path.exists(index_path) and os.path.samefile(a.json, index_path): 209 if a.json:
190 print(f"vs_layout_census: error: --json would overwrite the input {index_path}", file=sys.stderr) 210 # Refuse before survey(): it reads the whole corpus and runs llvm-mc for every unlisted payload.
191 return 2 211 if os.path.isdir(a.json):
212 print(f"vs_layout_census: error: --json {a.json} is a directory, not a JSON file", file=sys.stderr)
213 return 2
214 for path in input_paths(a.dxbc):
215 if same_report_path(a.json, path):
216 print(f"vs_layout_census: error: --json would overwrite the input {path}", file=sys.stderr)
217 return 2
192 try: 218 try:
193 r = survey(a.dxbc, disasm_fn=default_disasm, limit=a.limit) 219 r = survey(a.dxbc, disasm_fn=default_disasm, limit=a.limit)
194 except (OSError, ValueError) as e: 220 except (OSError, ValueError) as e: