Scripts: harden gen_rdna2 CLI argument handling and output option #1494

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

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


Harden scripts/gen_rdna2.py CLI argument parsing, error diagnostics, 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/rdna2_spec.rs] to write output to a custom destination, defaulting to crates/xcl/src/gpu/rdna2_spec.rs.
  • Validate input XML file existence and report diagnostic errors to sys.stderr when the XML file is missing, contains invalid XML, or lacks required AMD ISA elements (exiting with code 1 instead of unhandled tracebacks).
  • Structure generator logic into generate(xml_input) returning (content, stats) to enable modular programmatic use and unit testing.
  • Add scripts/test_gen_rdna2.py covering CLI help flags, missing argument handling, invalid XML diagnostics, custom output path generation, synthetic ISA parsing, and instruction flag decoding.

Tested

  • python -B -m unittest discover -s scripts -p test_*.py -v: 26 passed, 0 failed.
  • Tested scripts/gen_rdna2.py --help (exits 0 with usage docstring).
  • Tested scripts/gen_rdna2.py with no args (exits 2 with usage docstring).
  • Tested scripts/gen_rdna2.py nonexistent_isa.xml (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_rdna2.py, ran the test suite, and verified PR conventions.

<!-- greyfinch-sync:ivall/xcl:issue:5476712724 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1504). Originally posted by [heditrigui](https://github.com/heditrigui) on 2026-11-24T17:02:46Z. --- Harden `scripts/gen_rdna2.py` CLI argument parsing, error diagnostics, 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/rdna2_spec.rs] to write output to a custom destination, defaulting to crates/xcl/src/gpu/rdna2_spec.rs. - Validate input XML file existence and report diagnostic errors to sys.stderr when the XML file is missing, contains invalid XML, or lacks required AMD ISA elements (exiting with code 1 instead of unhandled tracebacks). - Structure generator logic into generate(xml_input) returning (content, stats) to enable modular programmatic use and unit testing. - Add scripts/test_gen_rdna2.py covering CLI help flags, missing argument handling, invalid XML diagnostics, custom output path generation, synthetic ISA parsing, and instruction flag decoding. **Tested** - python -B -m unittest discover -s scripts -p test_*.py -v: 26 passed, 0 failed. - Tested scripts/gen_rdna2.py --help (exits 0 with usage docstring). - Tested scripts/gen_rdna2.py with no args (exits 2 with usage docstring). - Tested scripts/gen_rdna2.py nonexistent_isa.xml (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_rdna2.py, ran the test suite, and verified PR conventions.
nnonno91 commented 2026-11-24 18:08:52 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by nnonno91 on 2026-11-24T17:10:02Z.


Independent Windows check of 3608a7aa744bf5d9b8ce9413d58b639ee1e6fc39 using the full AMD RDNA 2 XML.

Tested

  • Windows 11 25H2 (26200.9974), Ryzen 7 9800X3D, AMD Radeon(TM) Graphics (32.0.21042.62), Python 3.14.5, Rust 1.100.0 MSVC, DXC 1.8.2502.11, debug.
  • With amdgpu_isa_rdna2.xml (release 2026-02-20, schema 1.1.1, SHA-256 d671ecbc36543674ab59e9b2feffd56718fd2137e7ad913c314f2e2508b9f4f7), I ran python -I scripts/gen_rdna2.py <xml> <output> on base b7aa9f3bd212088e0d927439284dc5ed325ce3c7 and the PR. The PR’s custom output used a new directory and a filename with spaces. I also ran python -I <copied-script> <xml> from outside its temporary project tree to exercise the default output.
  • All three invocations exited 0 and generated byte-for-byte identical Rust files (751,032 bytes, 42 encodings, 21 primary, 1,153 instructions). git hash-object --no-filters matched the tracked rdna2_spec.rs blob e91c5c623da8ad154be57e17cc1e884d8d77b090. AMD GPUOpen XML source
  • python -B -m unittest discover -s scripts -p 'test_*.py' -v passed all 26 tests. cargo build --workspace --locked and cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --show-output passed with 770 passed, 0 failed, 41 ignored, and no SKIPPED GPU lines. The bundled title test passed.

Not tested

Retail titles, release builds, and other operating systems.

AI use

OpenAI Codex (GPT-6 family, exact variant unavailable) ran the checks and drafted this report.

<!-- greyfinch-sync:ivall/xcl:comment:7138785806 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1504#issuecomment-7138785806). Originally posted by [nnonno91](https://github.com/nnonno91) on 2026-11-24T17:10:02Z. --- Independent Windows check of `3608a7aa744bf5d9b8ce9413d58b639ee1e6fc39` using the full AMD RDNA 2 XML. **Tested** - Windows 11 25H2 (26200.9974), Ryzen 7 9800X3D, AMD Radeon(TM) Graphics (32.0.21042.62), Python 3.14.5, Rust 1.100.0 MSVC, DXC 1.8.2502.11, debug. - With `amdgpu_isa_rdna2.xml` (release 2026-02-20, schema 1.1.1, SHA-256 `d671ecbc36543674ab59e9b2feffd56718fd2137e7ad913c314f2e2508b9f4f7`), I ran `python -I scripts/gen_rdna2.py <xml> <output>` on base `b7aa9f3bd212088e0d927439284dc5ed325ce3c7` and the PR. The PR’s custom output used a new directory and a filename with spaces. I also ran `python -I <copied-script> <xml>` from outside its temporary project tree to exercise the default output. - All three invocations exited 0 and generated byte-for-byte identical Rust files (751,032 bytes, 42 encodings, 21 primary, 1,153 instructions). `git hash-object --no-filters` matched the tracked `rdna2_spec.rs` blob `e91c5c623da8ad154be57e17cc1e884d8d77b090`. [AMD GPUOpen XML source](https://gpuopen.com/machine-readable-isa/) - `python -B -m unittest discover -s scripts -p 'test_*.py' -v` passed all 26 tests. `cargo build --workspace --locked` and `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --show-output` passed with 770 passed, 0 failed, 41 ignored, and no `SKIPPED` GPU lines. The bundled title test passed. **Not tested** Retail titles, release builds, and other operating systems. **AI use** OpenAI Codex (GPT-6 family, exact variant unavailable) ran the checks and drafted this report.
gut0to commented 2026-11-24 18:52:42 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-24T18:52:42Z.

Review: COMMENTED


The focused script tests are useful. CONTRIBUTING.md also requires the full workspace GPU suite for each PR, with pass/fail/ignored counts and skipped-test names; this description reports only the Python tests. Please add the full-suite result before merge. AI-assisted review; I checked the Tested section against the guide.

<!-- greyfinch-sync:ivall/xcl:review:6045379381 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1504#pullrequestreview-6045379381). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-24T18:52:42Z. **Review:** COMMENTED --- The focused script tests are useful. CONTRIBUTING.md also requires the full workspace GPU suite for each PR, with pass/fail/ignored counts and skipped-test names; this description reports only the Python tests. Please add the full-suite result before merge. AI-assisted review; I checked the Tested section against the guide.
DavidM1911 commented 2026-11-24 19:07:47 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T18:53:43Z.


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

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

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7141302009 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1504#issuecomment-7141302009). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T18:53:43Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified `main`.) Thanks!
RavenCurseBurg commented 2026-11-25 17:07:36 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by RavenCurseBurg on 2026-11-25T17:06:08Z.


On 3608a7aa744bf5d9b8ce9413d58b639ee1e6fc39, python -I scripts/gen_rdna2.py input.xml out.rs rejected a UTF-16 ISA file that the parent accepted. I encoded the same synthetic ISA (including a non-ASCII copyright character) as UTF-8, UTF-8 with BOM, UTF-16LE with BOM and UTF-16BE with BOM. The parent generated identical Rust for all four. This head succeeded for the UTF-8 files and exited 1 for both UTF-16 files before writing output.

Fix and regression test. The CLI passes the path to ElementTree so the XML reader can honor the encoding. The new CLI test failed on the original head and passed with the fix. All four encodings then produced the same 1,808 bytes as the parent (SHA-256 40a6cef572f2521a7ba78ef84a382f942699f853b98fe18c4c1abcae743f18e2). All 27 Python tests passed with python -B -m unittest discover -s scripts -p "test_*.py".

Windows 11 26100.9711, i7-13700, RTX 4060, Python 3.14.3. Synthetic ISA only in this check; no full AMD XML, Rust build, GPU or retail run. OpenAI Codex (GPT-6) wrote the patch and ran the checks.

Follow-up on the review at 3608a7aa744b. Tested candidate cbdc8a4a2. Removing only S_BRANCH’s InstructionEncodings from the PR’s XML fixture reproduced the successful incomplete output. The candidate returned 1, named S_BRANCH and InstructionEncodings, and preserved the previous output bytes. The complete fixture still passed. All 27 script tests passed.

Command on the candidate: python -B -m unittest discover -s scripts -p "test_*.py". Windows 11 build 26100, Core i7-13700, Python 3.14.3. OpenAI Codex (GPT-6) prepared the patch and ran the tests with synthetic inputs. I did not run the full Rust/GPU suite.

<!-- greyfinch-sync:ivall/xcl:comment:7169005814 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1504#issuecomment-7169005814). Originally posted by [RavenCurseBurg](https://github.com/RavenCurseBurg) on 2026-11-25T17:06:08Z. --- On `3608a7aa744bf5d9b8ce9413d58b639ee1e6fc39`, `python -I scripts/gen_rdna2.py input.xml out.rs` rejected a UTF-16 ISA file that the parent accepted. I encoded the same synthetic ISA (including a non-ASCII copyright character) as UTF-8, UTF-8 with BOM, UTF-16LE with BOM and UTF-16BE with BOM. The parent generated identical Rust for all four. This head succeeded for the UTF-8 files and exited 1 for both UTF-16 files before writing output. [Fix and regression test](https://github.com/RavenCurseBurg/xcl/commit/84500ef0aeccdd1dc91d371c0dd9031b0bdef4d9). The CLI passes the path to ElementTree so the XML reader can honor the encoding. The new CLI test failed on the original head and passed with the fix. All four encodings then produced the same 1,808 bytes as the parent (SHA-256 `40a6cef572f2521a7ba78ef84a382f942699f853b98fe18c4c1abcae743f18e2`). All 27 Python tests passed with `python -B -m unittest discover -s scripts -p "test_*.py"`. Windows 11 26100.9711, i7-13700, RTX 4060, Python 3.14.3. Synthetic ISA only in this check; no full AMD XML, Rust build, GPU or retail run. OpenAI Codex (GPT-6) wrote the patch and ran the checks. Follow-up on [the review](https://github.com/xcl-dev/xcl/pull/1504#pullrequestreview-6071955680) at `3608a7aa744b`. [Tested candidate cbdc8a4a2](https://github.com/RavenCurseBurg/xcl/commit/cbdc8a4a238f564de73b80413cec54bc4cb99df2). Removing only `S_BRANCH`’s `InstructionEncodings` from the PR’s XML fixture reproduced the successful incomplete output. The candidate returned 1, named `S_BRANCH` and `InstructionEncodings`, and preserved the previous output bytes. The complete fixture still passed. All 27 script tests passed. Command on the candidate: `python -B -m unittest discover -s scripts -p "test_*.py"`. Windows 11 build 26100, Core i7-13700, Python 3.14.3. OpenAI Codex (GPT-6) prepared the patch and ran the tests with synthetic inputs. I did not run the full Rust/GPU suite.
luckyteagirl commented 2026-11-26 09:56:59 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T09:56:59Z.

Review: COMMENTED


On 3608a7a, the new optional InstructionEncodings handling can silently drop an instruction from generated output. I copied this PR's own synthetic XML fixture, removed only S_BRANCH's InstructionEncodings element, and ran the generator against a preseeded output sentinel. The unmodified fixture emits three instructions. With the one-element mutation, this head exited 0 and replaced the output with a two-instruction table missing S_BRANCH; the PR base and current main exited 1 and preserved the sentinel. A structurally absent encoding table is malformed input, so success here can publish an incomplete generated table.

Could the parser require InstructionEncodings for each instruction before writing output, with an error naming that instruction? A regression using the mutated synthetic fixture and an existing output sentinel would cover both the exit status and preservation of the prior file. This was an exact-head Python-only differential, not a full SDK regeneration or GPU run.

<!-- greyfinch-sync:ivall/xcl:review:6071955680 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1504#pullrequestreview-6071955680). Originally posted by [luckyteagirl](https://github.com/luckyteagirl) on 2026-11-26T09:56:59Z. **Review:** COMMENTED --- On `3608a7a`, the new optional `InstructionEncodings` handling can silently drop an instruction from generated output. I copied this PR's own synthetic XML fixture, removed only `S_BRANCH`'s `InstructionEncodings` element, and ran the generator against a preseeded output sentinel. The unmodified fixture emits three instructions. With the one-element mutation, this head exited 0 and replaced the output with a two-instruction table missing `S_BRANCH`; the PR base and current main exited 1 and preserved the sentinel. A structurally absent encoding table is malformed input, so success here can publish an incomplete generated table. Could the parser require `InstructionEncodings` for each instruction before writing output, with an error naming that instruction? A regression using the mutated synthetic fixture and an existing output sentinel would cover both the exit status and preservation of the prior file. This was an exact-head Python-only differential, not a full SDK regeneration or GPU run.
github-actions[bot] commented 2026-12-01 02:33:56 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-12-01T02:33:56Z.


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:7298752587 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1504#issuecomment-7298752587). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-12-01T02:33:56Z. --- 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 05:57:18 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:57:18Z.


Triage (#1607) – head 3608a7a

No open or merged PR makes the same change. #2612 (reject non-RDNA2 input) and #2674 (strip whitespace in instruction flags) touch the same script for other reasons and will conflict with this refactor.
Points in the diff: scripts/gen_rdna2.py:generate now treats a missing MicrocodeFormat/BitLayout/Operands/InstructionFlags as empty and still writes a table, so a malformed spec yields a silently incomplete rdna2_spec.rs instead of an error, which is the opposite of the hardening the title describes. Reading the XML as text and calling ET.fromstring (instead of ET.parse on the file) also stops honouring the file's declared encoding and fails on a UTF-8 BOM.
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:7302316838 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1504#issuecomment-7302316838). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:57:18Z. --- **Triage (#1607)** – head 3608a7a No open or merged PR makes the same change. #2612 (reject non-RDNA2 input) and #2674 (strip whitespace in instruction flags) touch the same script for other reasons and will conflict with this refactor. Points in the diff: scripts/gen_rdna2.py:generate now treats a missing MicrocodeFormat/BitLayout/Operands/InstructionFlags as empty and still writes a table, so a malformed spec yields a silently incomplete rdna2_spec.rs instead of an error, which is the opposite of the hardening the title describes. Reading the XML as text and calling ET.fromstring (instead of ET.parse on the file) also stops honouring the file's declared encoding and fails on a UTF-8 BOM. 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/1504:upstream-pr/1504'
git switch 'upstream-pr/1504'

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/1504'
git switch 'upstream-pr/1504'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1504'
git switch 'upstream-pr/1504'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1504'
git switch 'main'
git merge --squash 'upstream-pr/1504'
git switch 'main'
git merge --ff-only 'upstream-pr/1504'
git switch 'main'
git merge 'upstream-pr/1504'
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!1494
No description provided.