Scripts: harden gen_tiling CLI argument handling and output option #1577

Open
heditrigui wants to merge 1 commit from upstream-pr/1587 into main
heditrigui commented 2026-11-24 17:59:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by heditrigui on 2026-11-24T17:59:40Z.


Harden scripts/gen_tiling.py CLI argument parsing, modularity, and output path configuration:

  • Support -h and --help flags, printing the script's docstring usage and exiting with code 0.
  • Print usage and exit with code 2 when invoked without arguments, avoiding an unhandled IndexError.
  • Accept an optional second argument [crates/xcl/src/gpu/tiling_tables.rs] to write output to a custom destination, defaulting to crates/xcl/src/gpu/tiling_tables.rs.
  • Validate input C header file existence and report diagnostic errors to sys.stderr when <gfx10SwizzlePattern.h> is missing, lacks required pattern tables, or has malformed rows (exiting with code 1 instead of unhandled tracebacks).
  • Structure generator logic into generate(src_header, modes=None, modes_3d=None) returning (content, stats) to enable modular programmatic use and unit testing.
  • Add scripts/test_gen_tiling.py covering CLI help flags, missing argument handling, missing table diagnostics, custom output path generation, swizzle token evaluation, and synthetic pattern generation.

Tested

  • python -B -m unittest discover -s scripts -p test_*.py -v: 27 passed, 0 failed.
  • Tested scripts/gen_tiling.py --help (exits 0 with usage docstring).
  • Tested scripts/gen_tiling.py with no args (exits 2 with usage docstring).
  • Tested scripts/gen_tiling.py nonexistent_swizzle.h (exits 1 with clear diagnostic).
  • Validated PR description and changed files with scripts/pr_check.py --local.

Not tested

  • Retail game smoke tests: this PR modifies only offline code generator tooling in scripts/ and does not touch runtime code or dependencies.

AI use
Antigravity (Gemini 3.8 Flash (High)) designed the CLI argument handling and optional output destination, implemented unit tests in scripts/test_gen_tiling.py, ran the test suite, and verified PR conventions.

<!-- greyfinch-sync:ivall/xcl:issue:5477570351 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1587). Originally posted by [heditrigui](https://github.com/heditrigui) on 2026-11-24T17:59:40Z. --- Harden `scripts/gen_tiling.py` CLI argument parsing, modularity, and output path configuration: - Support -h and --help flags, printing the script's docstring usage and exiting with code 0. - Print usage and exit with code 2 when invoked without arguments, avoiding an unhandled IndexError. - Accept an optional second argument [crates/xcl/src/gpu/tiling_tables.rs] to write output to a custom destination, defaulting to crates/xcl/src/gpu/tiling_tables.rs. - Validate input C header file existence and report diagnostic errors to sys.stderr when <gfx10SwizzlePattern.h> is missing, lacks required pattern tables, or has malformed rows (exiting with code 1 instead of unhandled tracebacks). - Structure generator logic into generate(src_header, modes=None, modes_3d=None) returning (content, stats) to enable modular programmatic use and unit testing. - Add scripts/test_gen_tiling.py covering CLI help flags, missing argument handling, missing table diagnostics, custom output path generation, swizzle token evaluation, and synthetic pattern generation. **Tested** - python -B -m unittest discover -s scripts -p test_*.py -v: 27 passed, 0 failed. - Tested scripts/gen_tiling.py --help (exits 0 with usage docstring). - Tested scripts/gen_tiling.py with no args (exits 2 with usage docstring). - Tested scripts/gen_tiling.py nonexistent_swizzle.h (exits 1 with clear diagnostic). - Validated PR description and changed files with scripts/pr_check.py --local. **Not tested** - Retail game smoke tests: this PR modifies only offline code generator tooling in scripts/ and does not touch runtime code or dependencies. **AI use** Antigravity (Gemini 3.8 Flash (High)) designed the CLI argument handling and optional output destination, implemented unit tests in scripts/test_gen_tiling.py, ran the test suite, and verified PR conventions.
pickles976 commented 2026-11-24 19:07:24 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by pickles976 on 2026-11-24T18:19:44Z.


Approve — with one thing called out because hardening passes usually break it silently: the AMD/MIT attribution header survives the rewrite (out.write("// Generated by … Copyright (C) 2007-2026 Advanced Micro Devices, Inc., MIT.")) and is now pinned by a test (assertIn("// Generated by scripts/gen_tiling.py from AMD addrlib", content)). MIT is permissive but the notice-retention clause is the one condition, and generators dropping it are a classic drive-by; a regression test around a comment line in generated output is the correct immune response. Other checks clean: docstring-usage on -h, default output path still pinned to the in-tree tiling_tables.rs (test confirms), no shell=True/os.system introduced, parse errors on malformed header lines still fail loudly rather than emitting silently-wrong tiling tables — which matters, because a wrong swizzle table is silent VRAM corruption on RADV paths, not a crash. LGTM from the license/build-script lane (same lens as the Cargo-profile audit posted on the sibling).

<!-- greyfinch-sync:ivall/xcl:comment:7140488160 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1587#issuecomment-7140488160). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-24T18:19:44Z. --- Approve — with one thing called out because hardening passes usually break it silently: the **AMD/MIT attribution header survives the rewrite** (`out.write("// Generated by … Copyright (C) 2007-2026 Advanced Micro Devices, Inc., MIT.")`) **and is now pinned by a test** (`assertIn("// Generated by scripts/gen_tiling.py from AMD addrlib", content)`). MIT is permissive but the notice-retention clause is the one condition, and generators dropping it are a classic drive-by; a regression test around a comment line in generated output is the correct immune response. Other checks clean: docstring-usage on `-h`, default output path still pinned to the in-tree `tiling_tables.rs` (test confirms), no `shell=True`/`os.system` introduced, parse errors on malformed header lines still fail loudly rather than emitting silently-wrong tiling tables — which matters, because a wrong swizzle table is silent VRAM corruption on RADV paths, not a crash. LGTM from the license/build-script lane (same lens as the Cargo-profile audit posted on the sibling).
futurepro62 commented 2026-11-24 19:07:31 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T18:34:33Z.


Tested exact head dd9639f, script tests only, since this PR has no test report yet.

Tested

  • Windows 11 Home 26300, Python 3.14.3 with pytest 9.0.3, Windows PowerShell 5.1.26100.9818 (no PowerShell 7 here).
  • python -m pytest -q scripts/test_gen_tiling.py at the head: 11 passed, 2 subtests passed.
  • The PR's test file against current main's gen_tiling.py (9e3831e): collection error. Main's script does its work at import (io.open(sys.argv[1]) on line 9, no __main__ guard), so under pytest it tries to open -q (FileNotFoundError). The test does not get to run on main; it needs the PR's restructured script.

Not tested

  • Regenerating the checked-in tiling tables with the new CLI and diffing them, a game (no runtime code changed).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:ivall/xcl:comment:7140836753 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1587#issuecomment-7140836753). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-24T18:34:33Z. --- Tested exact head `dd9639f`, script tests only, since this PR has no test report yet. **Tested** - Windows 11 Home 26300, Python 3.14.3 with pytest 9.0.3, Windows PowerShell 5.1.26100.9818 (no PowerShell 7 here). - `python -m pytest -q scripts/test_gen_tiling.py` at the head: 11 passed, 2 subtests passed. - The PR's test file against current main's `gen_tiling.py` (`9e3831e`): collection error. Main's script does its work at import (`io.open(sys.argv[1])` on line 9, no `__main__` guard), so under pytest it tries to open `-q` (`FileNotFoundError`). The test does not get to run on main; it needs the PR's restructured script. **Not tested** - Regenerating the checked-in tiling tables with the new CLI and diffing them, a game (no runtime code changed). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
RobertAndreiGit commented 2026-11-24 21:08:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by RobertAndreiGit on 2026-11-24T19:12:29Z.


Tested: On head dd9639f6ea18f606b0c819794042bc74731b70b2, the generator still silently accepts one malformed table. Starting with make_synthetic_header() from the PR’s tests, replace the first { 0, 0, 0, 0, 0 }, with not_a_pattern_row,. Running python3 -B scripts/gen_tiling.py bad.h bad.rs exited 0 and wrote (1, &[]) for that mode. By contrast, shortening the braced row to three entries exited 1 and named the table. The unbraced case would make a useful regression fixture.

For the unchanged synthetic header (all 14 2D and 6 3D modes), python3 -B scripts/gen_tiling.py valid.h produced byte-identical default output on base 9e3831e1f570d3646e53ab753b19ec07eb8f1485 and head: cmp exited 0, 2,398 bytes each. On head, python3 -B -m unittest discover -s scripts -p 'test_*.py' -v: 27 passed, none failed or skipped. Apple M3 Pro, macOS 15.7.3 arm64, Python 3.14.7.

Not tested: Full Mesa header regeneration, Windows runtime, or GPU.

AI use: OpenCode (GPT-6.2 Astra) ran the tests and wrote this report.

<!-- greyfinch-sync:ivall/xcl:comment:7141755825 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1587#issuecomment-7141755825). Originally posted by [RobertAndreiGit](https://github.com/RobertAndreiGit) on 2026-11-24T19:12:29Z. --- **Tested:** On head `dd9639f6ea18f606b0c819794042bc74731b70b2`, the generator still silently accepts one malformed table. Starting with `make_synthetic_header()` from the PR’s tests, replace the first `{ 0, 0, 0, 0, 0 },` with `not_a_pattern_row,`. Running `python3 -B scripts/gen_tiling.py bad.h bad.rs` exited **0** and wrote `(1, &[])` for that mode. By contrast, shortening the braced row to three entries exited **1** and named the table. The unbraced case would make a useful regression fixture. For the unchanged synthetic header (all 14 2D and 6 3D modes), `python3 -B scripts/gen_tiling.py valid.h` produced byte-identical default output on base `9e3831e1f570d3646e53ab753b19ec07eb8f1485` and head: `cmp` exited 0, 2,398 bytes each. On head, `python3 -B -m unittest discover -s scripts -p 'test_*.py' -v`: 27 passed, none failed or skipped. Apple M3 Pro, macOS 15.7.3 arm64, Python 3.14.7. **Not tested:** Full Mesa header regeneration, Windows runtime, or GPU. **AI use:** OpenCode (GPT-6.2 Astra) ran the tests and wrote this report.
DavidM1911 commented 2026-11-24 21:09:14 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T20:02:56Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~22 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7142968065 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1587#issuecomment-7142968065). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T20:02:56Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~22 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified `main`.) Thanks!
RavenCurseBurg commented 2026-11-25 16:12:14 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by RavenCurseBurg on 2026-11-25T15:35:55Z.


I’ll retest the unbraced table-row case reported here and check that malformed input leaves an existing output file intact. If it still generates an empty mode, I’ll prepare a focused fix and regression test. Testing dd9639f6ea with OpenAI Codex (GPT-6).

<!-- greyfinch-sync:ivall/xcl:comment:7167089332 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1587#issuecomment-7167089332). Originally posted by [RavenCurseBurg](https://github.com/RavenCurseBurg) on 2026-11-25T15:35:55Z. --- I’ll retest the unbraced table-row case reported here and check that malformed input leaves an existing output file intact. If it still generates an empty mode, I’ll prepare a focused fix and regression test. Testing dd9639f6ea18f606b0c819794042bc74731b70b2 with OpenAI Codex (GPT-6).
RavenCurseBurg commented 2026-11-25 16:12:20 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by RavenCurseBurg on 2026-11-25T15:51:51Z.


Reproduced @RobertAndreiGit’s missing-row report on dd9639f6ea18f606b0c819794042bc74731b70b2 with python -B scripts/gen_tiling.py <header> <existing-output>. An unbraced initializer, both as the only row and between two valid rows, returned 0 and overwrote the destination. A valid row followed by a C++ comment containing braces failed. The 11 existing generator tests passed on this head.

Fix and regression tests. Both malformed cases now exit 1, name the affected table and leave the previous output bytes intact. A valid row with braces in a comment now succeeds. The two malformed subcases and the comment case failed on the original implementation. With the fix, python -B -m unittest discover -s scripts -p "test_*.py" passed all 29 tests.

I also ran the original and fixed generators against the same public AMD PAL addrlib header. Both generated 14 2D and 6 3D modes, with identical Rust output (SHA-256 f8d82935aa137953768a6e058100a78d7dcc51c43fcb0a58534161d329d4dc60).

Windows 11 26100.9711, i7-13700, RTX 4060, Python 3.14.3. No Rust/GPU suite, retail assets or Mesa build. OpenAI Codex (GPT-6) ran the checks, prepared the patch and wrote this report.

<!-- greyfinch-sync:ivall/xcl:comment:7167379713 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1587#issuecomment-7167379713). Originally posted by [RavenCurseBurg](https://github.com/RavenCurseBurg) on 2026-11-25T15:51:51Z. --- Reproduced @RobertAndreiGit’s missing-row report on `dd9639f6ea18f606b0c819794042bc74731b70b2` with `python -B scripts/gen_tiling.py <header> <existing-output>`. An unbraced initializer, both as the only row and between two valid rows, returned 0 and overwrote the destination. A valid row followed by a C++ comment containing braces failed. The 11 existing generator tests passed on this head. [Fix and regression tests](https://github.com/RavenCurseBurg/xcl/commit/8e29540ddcd168bcb185a84ecfd285a2ff7cd07f). Both malformed cases now exit 1, name the affected table and leave the previous output bytes intact. A valid row with braces in a comment now succeeds. The two malformed subcases and the comment case failed on the original implementation. With the fix, `python -B -m unittest discover -s scripts -p "test_*.py"` passed all 29 tests. I also ran the original and fixed generators against the same [public AMD PAL addrlib header](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/imported/addrlib/src/gfx10/gfx10SwizzlePattern.h). Both generated 14 2D and 6 3D modes, with identical Rust output (SHA-256 `f8d82935aa137953768a6e058100a78d7dcc51c43fcb0a58534161d329d4dc60`). Windows 11 26100.9711, i7-13700, RTX 4060, Python 3.14.3. No Rust/GPU suite, retail assets or Mesa build. OpenAI Codex (GPT-6) ran the checks, prepared the patch and wrote this report.
github-actions[bot] commented 2026-11-29 20:43:05 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-29T20:43:05Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7262302080 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1587#issuecomment-7262302080). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-29T20:43:05Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 06:11:58 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T06:11:58Z.


Triage (#1607) – head dd9639f

No other PR makes this change. #2661 (C integer spellings in value), #2685 (comments and spaced terminators in table) and #43 (MSAA modes) change other parts of scripts/gen_tiling.py, so they will conflict with this one.
Most of the diff is a whole-file switch from single to double quotes and one-per-line imports; CONTRIBUTING.md asks for no drive-by reformatting, and it makes those conflicts larger. The generated output looks unchanged (same header, tables and order).
main(argv=None, dst=None) accepting a bare string as argv is unused by the CLI and the tests; it can go.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7302600294 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1587#issuecomment-7302600294). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T06:11:58Z. --- **Triage (#1607)** – head dd9639f No other PR makes this change. #2661 (C integer spellings in `value`), #2685 (comments and spaced terminators in `table`) and #43 (MSAA modes) change other parts of scripts/gen_tiling.py, so they will conflict with this one. Most of the diff is a whole-file switch from single to double quotes and one-per-line imports; CONTRIBUTING.md asks for no drive-by reformatting, and it makes those conflicts larger. The generated output looks unchanged (same header, tables and order). `main(argv=None, dst=None)` accepting a bare string as `argv` is unused by the CLI and the tests; it can go. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1587:upstream-pr/1587'
git switch 'upstream-pr/1587'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1587'
git switch 'upstream-pr/1587'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1587'
git switch 'upstream-pr/1587'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1587'
git switch 'main'
git merge --squash 'upstream-pr/1587'
git switch 'main'
git merge --ff-only 'upstream-pr/1587'
git switch 'main'
git merge 'upstream-pr/1587'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!1577
No description provided.