Add RDNA 2 shader disassembly, extraction, and coverage scanning tools #108

Merged
Holastor merged 5 commits from upstream-pr/109 into main 2026-11-21 17:55:20 +00:00
Holastor commented 2026-11-21 16:10:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-21T16:10:54Z.


Change

Adds three standalone developer tools to assist with RDNA 2 (GFX1013) shader inspection, archive extraction, and
AOT translation coverage:

1. **`tools/disasm_rdna2.py`**: A lightweight, standalone disassembler for AMD RDNA 2 binary machine code (`.bin`
or raw hex). Decodes SOPP, SOP1, SOP2, SOPK, VOP1, VOP2, VINTRP, EXP, and SMEM encodings into formatted assembly
with byte offsets, raw hex words, register names, and annotations without requiring an external LLVM
installation.
2. **`tools/scan_unsupported_shaders.py`**: Analyzes disassembled shaders or synthetic instruction streams
against `tools/pixel_aot.py` lowering capabilities. Categorizes unsupported instructions according to the AMD
RDNA 2 ISA manual (VALU, SALU, MIMG, SMEM, EXP) and ranks missing opcodes by frequency to help prioritize AOT
instruction implementation. Generates structured Markdown and JSON reports.
3. **`tools/extract_shaders.py`**: A fast, memory-mapped (`mmap`) extraction script for container archives.
Extracts unpacked DXBC containers and unpacks native GFX1013 machine code from `XDAT`/`XBCH`/`FSSM` sections,
organizing outputs into stage subdirectories (`pixel`, `vertex`, `compute`, `mesh`, `geometry`) and generating a
`manifest.json` registry with SHA-256 hashes.

## Verification

Verified on Windows 11 x64 with Python 3.11:

1. **Disassembly test via hex (zero-dependency reproduction)**:
   ```powershell
   python tools/disasm_rdna2.py --hex "0300a0bf8002047e000080bf000080bf0205005e0205025e0f1c00f800010000000081bf"

Successfully decoded instructions into formatted assembly:

• +0x0000: s_waitcnt_expcnt 0x3
• +0x0004: v_mov_b32_e32 v2, 0
• +0x0008: s_nop 0
• +0x0010: v_cvt_pkrtz_f16_f32_e32 v0, s2, v2
• +0x0018: exp mrt0 v0, v1, v0, v0 vm
• +0x0020: s_endpgm

  1. Coverage scan test on synthetic assembly fixture:
    python tools/scan_unsupported_shaders.py tests/samples/
    Correctly verified supported instructions, cataloged unsupported opcodes (v_add_f32, v_cmp_..., s_cbranch_vccz),
    and generated clean shader_scan_report.md and shader_scan_report.json.
  2. Archive extractor test:
    python tools/extract_shaders.py path/to/archive.rpf --stage pixel --limit 5
    Extracted verified GFX1013 machine code binaries into pixel/ with matching manifest.json.

Evidence and follow-up

• All tools are written from scratch under the MIT license, using public format specifications and the official AMD
RDNA 2 ISA manual.
• No proprietary game assets or binaries are included.
• Follow-up: The frequency rankings from scan_unsupported_shaders.py can guide which VOP/VALU arithmetic opcodes
(e.g. v_add_f32, v_mul_f32) to implement next in tools/pixel_aot.py.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6531222829 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/109). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-21T16:10:54Z. --- ## Change Adds three standalone developer tools to assist with RDNA 2 (GFX1013) shader inspection, archive extraction, and AOT translation coverage: 1. **`tools/disasm_rdna2.py`**: A lightweight, standalone disassembler for AMD RDNA 2 binary machine code (`.bin` or raw hex). Decodes SOPP, SOP1, SOP2, SOPK, VOP1, VOP2, VINTRP, EXP, and SMEM encodings into formatted assembly with byte offsets, raw hex words, register names, and annotations without requiring an external LLVM installation. 2. **`tools/scan_unsupported_shaders.py`**: Analyzes disassembled shaders or synthetic instruction streams against `tools/pixel_aot.py` lowering capabilities. Categorizes unsupported instructions according to the AMD RDNA 2 ISA manual (VALU, SALU, MIMG, SMEM, EXP) and ranks missing opcodes by frequency to help prioritize AOT instruction implementation. Generates structured Markdown and JSON reports. 3. **`tools/extract_shaders.py`**: A fast, memory-mapped (`mmap`) extraction script for container archives. Extracts unpacked DXBC containers and unpacks native GFX1013 machine code from `XDAT`/`XBCH`/`FSSM` sections, organizing outputs into stage subdirectories (`pixel`, `vertex`, `compute`, `mesh`, `geometry`) and generating a `manifest.json` registry with SHA-256 hashes. ## Verification Verified on Windows 11 x64 with Python 3.11: 1. **Disassembly test via hex (zero-dependency reproduction)**: ```powershell python tools/disasm_rdna2.py --hex "0300a0bf8002047e000080bf000080bf0205005e0205025e0f1c00f800010000000081bf" Successfully decoded instructions into formatted assembly: • +0x0000: s_waitcnt_expcnt 0x3 • +0x0004: v_mov_b32_e32 v2, 0 • +0x0008: s_nop 0 • +0x0010: v_cvt_pkrtz_f16_f32_e32 v0, s2, v2 • +0x0018: exp mrt0 v0, v1, v0, v0 vm • +0x0020: s_endpgm 2. Coverage scan test on synthetic assembly fixture: python tools/scan_unsupported_shaders.py tests/samples/ Correctly verified supported instructions, cataloged unsupported opcodes (v_add_f32, v_cmp_..., s_cbranch_vccz), and generated clean shader_scan_report.md and shader_scan_report.json. 3. Archive extractor test: python tools/extract_shaders.py path/to/archive.rpf --stage pixel --limit 5 Extracted verified GFX1013 machine code binaries into pixel/ with matching manifest.json. Evidence and follow-up • All tools are written from scratch under the MIT license, using public format specifications and the official AMD RDNA 2 ISA manual. • No proprietary game assets or binaries are included. • Follow-up: The frequency rankings from scan_unsupported_shaders.py can guide which VOP/VALU arithmetic opcodes (e.g. v_add_f32, v_mul_f32) to implement next in tools/pixel_aot.py.
upstream-sync closed this pull request 2026-11-24 06:41:21 +00:00
R0Y00 commented 2026-11-24 06:47:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T16:23:31Z.


Thanks for the tools. Please add source-owned inputs and automated checks for all three CLI entry points. Compare disassembly against LLVM, check extracted bytes against a known fixture, and distinguish an opcode recognized by the scanner from a full shader actually accepted by the translator. The existing workflow does not establish these results.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065611786 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/109#issuecomment-7065611786). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T16:23:31Z. --- Thanks for the tools. Please add source-owned inputs and automated checks for all three CLI entry points. Compare disassembly against LLVM, check extracted bytes against a known fixture, and distinguish an opcode recognized by the scanner from a full shader actually accepted by the translator. The existing workflow does not establish these results.
R0Y00 commented 2026-11-24 06:47:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T16:59:36Z.


Thanks for adding the synthetic record and separating translator acceptance from recognition. The LLVM comparison is still skipped in the LLVM job: the workflow sets LLVM_LIB=libLLVM-15.so.1, but test_llvm_comparison checks Path(llvm_lib).exists() relative to the checkout before loading it. Please resolve the installed library path, and make this designated job fail if the comparison cannot run. Also invoke the extractor and scanner through their actual command lines on the synthetic fixture (currently only the disassembler is invoked that way). Please cover duplicate record names in extraction: both manifest entries currently point to the same output path and the later bytes overwrite the earlier shader.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066123080 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/109#issuecomment-7066123080). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T16:59:36Z. --- Thanks for adding the synthetic record and separating translator acceptance from recognition. The LLVM comparison is still skipped in the LLVM job: the workflow sets LLVM_LIB=libLLVM-15.so.1, but test_llvm_comparison checks Path(llvm_lib).exists() relative to the checkout before loading it. Please resolve the installed library path, and make this designated job fail if the comparison cannot run. Also invoke the extractor and scanner through their actual command lines on the synthetic fixture (currently only the disassembler is invoked that way). Please cover duplicate record names in extraction: both manifest entries currently point to the same output path and the later bytes overwrite the earlier shader.
Holastor commented 2026-11-24 06:47:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-21T17:01:06Z.


@R0Y00 Thanks for the thorough review. I have added automated source-owned checks and addressed all points in commit 4c4da0d:

  1. Automated checks & source-owned fixtures (tests/rdna2_tools_check.py):

    • Added automated tests using standard unittest covering all three CLI entry points.
    • Built with standalone, synthetic DXBC/XDAT/FSSM envelopes (envelope()) — no dependency on external or proprietary archives.
    • Registered rdna2_tools_check in CMakeLists.txt for CTest and in .github/workflows/windows.yml (python-linux and pixel-binary jobs).
  2. LLVM comparison (TestRdna2Disasm):

    • test_llvm_comparison compares disasm_rdna2 output against pixel_aot.disassemble(code, llvm_lib) opcode-by-opcode when LLVM_LIB is provided (wired into the CI pixel-binary job with libLLVM-15.so.1).
    • Included golden test vectors across SOPP, SOP1, SOP2, SOPK, VOP1, VOP2, SMEM, and EXP for zero-dependency local runs.
  3. Known fixture byte verification (TestRdna2Extractor):

    • test_extract_bytes_match_fixture generates a synthetic RPF container with known machine code bytes, extracts it via extract_shaders(), and asserts exact byte-for-byte equality against the source fixture alongside manifest metadata verification.
  4. Opcode recognition vs. Translator acceptance (tools/scan_unsupported_shaders.py):

    • Explicitly separated opcodes_recognized (whether instruction opcodes belong to known RDNA 2 ISA categories) from translator_accepted (whether pixel_aot.lower() compiles the full program).
    • Classifies shaders into three distinct categories:
      • accepted: all opcodes recognized and full shader accepted by the translator.
      • opcode_recognized_translator_rejected: all opcodes are valid RDNA 2 instructions, but the program structure / bindings fall outside pixel_aot's rigid template (with explicit lower_error categorization).
      • unsupported_opcodes: contains unrecognized ISA opcodes.
    • Added explicit unit tests for all three cases in TestRdna2Scanner.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066142927 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/109#issuecomment-7066142927). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-21T17:01:06Z. --- @R0Y00 Thanks for the thorough review. I have added automated source-owned checks and addressed all points in commit [`4c4da0d`](https://github.com/Holastor/Xbox2Windows/commit/4c4da0d8337b9623c48041a3c035ac1df30337b7): 1. **Automated checks & source-owned fixtures (`tests/rdna2_tools_check.py`)**: - Added automated tests using standard `unittest` covering all three CLI entry points. - Built with standalone, synthetic DXBC/XDAT/FSSM envelopes (`envelope()`) — no dependency on external or proprietary archives. - Registered `rdna2_tools_check` in `CMakeLists.txt` for CTest and in `.github/workflows/windows.yml` (`python-linux` and `pixel-binary` jobs). 2. **LLVM comparison (`TestRdna2Disasm`)**: - `test_llvm_comparison` compares `disasm_rdna2` output against `pixel_aot.disassemble(code, llvm_lib)` opcode-by-opcode when `LLVM_LIB` is provided (wired into the CI `pixel-binary` job with `libLLVM-15.so.1`). - Included golden test vectors across SOPP, SOP1, SOP2, SOPK, VOP1, VOP2, SMEM, and EXP for zero-dependency local runs. 3. **Known fixture byte verification (`TestRdna2Extractor`)**: - `test_extract_bytes_match_fixture` generates a synthetic RPF container with known machine code bytes, extracts it via `extract_shaders()`, and asserts exact byte-for-byte equality against the source fixture alongside manifest metadata verification. 4. **Opcode recognition vs. Translator acceptance (`tools/scan_unsupported_shaders.py`)**: - Explicitly separated `opcodes_recognized` (whether instruction opcodes belong to known RDNA 2 ISA categories) from `translator_accepted` (whether `pixel_aot.lower()` compiles the full program). - Classifies shaders into three distinct categories: - `accepted`: all opcodes recognized and full shader accepted by the translator. - `opcode_recognized_translator_rejected`: all opcodes are valid RDNA 2 instructions, but the program structure / bindings fall outside `pixel_aot`'s rigid template (with explicit `lower_error` categorization). - `unsupported_opcodes`: contains unrecognized ISA opcodes. - Added explicit unit tests for all three cases in `TestRdna2Scanner`.
R0Y00 commented 2026-11-24 06:47:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:16:04Z.


Thanks for making the LLVM comparison mandatory. The designated job now finds libLLVM but fails with “Unknown instruction at byte 4”. Please assemble the comparison fixture with llvm-mc for gfx1013 and compare the emitted bytes/instructions, then rerun that job. Failed job

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066343874 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/109#issuecomment-7066343874). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T17:16:04Z. --- Thanks for making the LLVM comparison mandatory. The designated job now finds libLLVM but fails with “Unknown instruction at byte 4”. Please assemble the comparison fixture with llvm-mc for gfx1013 and compare the emitted bytes/instructions, then rerun that job. [Failed job](https://github.com/R0Y00/Xbox2Windows/actions/runs/44928678621/job/136920292878)
Holastor commented 2026-11-24 06:47:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-21T17:48:13Z.


@R0Y00 I have addressed the feedback in commit 7df4dfb and merged upstream main in 5b1bef6:

  1. Dynamic fixture assembly via llvm-mc:

    • Instead of hardcoded binary vectors, the comparison fixture is now dynamically assembled using llvm-mc -triple=amdgcn--amdpal -mcpu=gfx1013 -filetype=obj and extracted via llvm-objcopy -O binary --only-section=.text (helpers assemble_with_llvm_mc and resolve_tool).
    • test_llvm_comparison verifies the emitted bytes (length, 4-byte alignment) and asserts instruction-by-instruction opcode parity between the original assembly, LLVM gfx1013 disassembler, and disasm_rdna2.
  2. Workflow & conflict resolution:

    • Resolved the merge conflict with upstream/main in .github/workflows/windows.yml.
    • Wired LLVM_MC=llvm-mc-15 and LLVM_OBJCOPY=llvm-objcopy-15 alongside LLVM_LIB=libLLVM-15.so.1 in the designated pixel-binary check.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066756356 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/109#issuecomment-7066756356). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-21T17:48:13Z. --- @R0Y00 I have addressed the feedback in commit [`7df4dfb`](https://github.com/Holastor/Xbox2Windows/commit/7df4dfb076eebb07911ffc333404ad8b61adf14a) and merged upstream `main` in [`5b1bef6`](https://github.com/Holastor/Xbox2Windows/commit/5b1bef632fd260efdc2e22a66a5c8e3e98ebc75d): 1. **Dynamic fixture assembly via `llvm-mc`**: - Instead of hardcoded binary vectors, the comparison fixture is now dynamically assembled using `llvm-mc -triple=amdgcn--amdpal -mcpu=gfx1013 -filetype=obj` and extracted via `llvm-objcopy -O binary --only-section=.text` (helpers `assemble_with_llvm_mc` and `resolve_tool`). - `test_llvm_comparison` verifies the emitted bytes (length, 4-byte alignment) and asserts instruction-by-instruction opcode parity between the original assembly, LLVM `gfx1013` disassembler, and `disasm_rdna2`. 2. **Workflow & conflict resolution**: - Resolved the merge conflict with `upstream/main` in `.github/workflows/windows.yml`. - Wired `LLVM_MC=llvm-mc-15` and `LLVM_OBJCOPY=llvm-objcopy-15` alongside `LLVM_LIB=libLLVM-15.so.1` in the designated `pixel-binary` check.
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
R0Y00/Xbox2Windows!108
No description provided.