Skip to content

shader_stats: reject ambiguous duplicate CSV counters (BUILD-1490) - #4976

Open
Misha0009 wants to merge 2 commits into
ItsNotRexed:mainfrom
Misha0009:fix/shader-stats-duplicate-counters
Open

Misha0009 wants to merge 2 commits into
ItsNotRexed:mainfrom
Misha0009:fix/shader-stats-duplicate-counters

Conversation

@Misha0009

Copy link
Copy Markdown

shader_stats: reject ambiguous duplicate CSV counters (BUILD-1490)

Item: BUILD-1490. Claim: coordination issue #4933.

Problem

scripts/shader_stats.py silently accepts multiple values for the same (name, stage, statistic) key. The resulting code-growth, register-pressure, and other statistics can depend on CSV row order rather than on the measurements.

For example, with a synthetic eight-instruction shader, a CSV containing two Instructions rows with values 16 and 32 produces a growth ratio of 4.00x. Reversing the rows produces 2.00x. Both runs exit successfully.

Root cause

load_stats() stores the values in a nested dictionary keyed by (name, stage) and then by statistic name. An assignment to an existing key replaces the previous value. The write_stats loop in runtime/test/vk_pipeline_check.cpp emits rows for every pipeline executable whose stages include the translated stage. The four-column CSV does not contain a pipeline-executable identifier, so repeated measurements cannot be distinguished or safely aggregated. Vulkan does not guarantee a one-to-one mapping between shader stages and pipeline executables (VkPipelineExecutablePropertiesKHR).

Solution

  • Detect a duplicate statistic within the same shader and stage before inserting it into the dictionary.
  • Return the existing CSV error path with a line-specific diagnostic, rather than printing a misleading summary or details table.
  • Document this requirement in the tool's help text. Keep repeated statistic names across different shaders or stages valid.
  • Add CLI regression tests for reversed duplicates, identical duplicate values, interleaved rows from different shaders/stages, an initially blank counter, both output modes, a quoted unfamiliar statistic, and legitimate repeated names across distinct shader/stage keys.

Reproduction

Use a synthetic eight-instruction .s listing named ps_flat.s and this CSV:

name,stage,statistic,value
ps_flat,ps,Instructions,16
ps_flat,ps,VGPRs,8
ps_flat,ps,Instructions,32

Run python -I scripts/shader_stats.py stats.csv disasm (also with --details). On the baseline, the last Instructions row wins; swapping the first and last data rows changes the reported growth from 4.00x to 2.00x. On this branch, both orders return exit code 1, print a duplicate-statistic diagnostic with the line number, and produce no results on stdout. The regression fixture creates its own CSV and listing with no external dependencies.

Tests

Apple M5 (arm64), macOS 27.2.1, CPython 3.15.2; upstream 3faac557. All inputs synthetic. The statistics-reader tests are CPU-only; the neighboring pipeline-check smoke test used MoltenVK 1.4.2 (device Vulkan 1.4.357). No game data.

Command Result
python -I tests/test_shader_stats.py -v before adding regression cases 19/19 passed
Final regression suite against a separate copy of upstream 3faac557 8 expected failing subcases; distinct-shader/stage control passed
python -I tests/test_shader_stats.py -v with fix 23/23 passed
python -I tests/test_shader_ir.py 37/37 passed
python -I tests/test_ci_registration.py 11/11 passed
python -I tests/test_text_write_line_endings.py 3/3 passed
python -I tests/test_tool_stdout_encoding.py -v 4/4 passed (with loopback available)
Build vk_pipeline_check and run tests/test_vk_pipeline_check_index.py -v against the built binary 7/7 passed
vk_pipeline_check on one synthetic compute shader 1 module and 1 pipeline passed; --stats reports missing extension on MoltenVK
Apply the current #3890 and #4917 patches together to a separate copy of the branch and run test_shader_stats.py Both patches apply cleanly; 25/25 passed
python -m compileall -q scripts/shader_stats.py tests/test_shader_stats.py Passed
python -I scripts/check_no_game_data.py scripts/shader_stats.py tests/test_shader_stats.py Passed
git diff --check Clean

Scope and limitations

This is a statistics-reader fix. It does not change the Vulkan CSV writer or introduce a new executable-level CSV format. A report with duplicate keys is rejected rather than guessing how to combine its values; the format cannot identify multiple executables whose counter names are disjoint. Supporting distinct pipeline-executable measurements would require a separate format change. The Apple M5's MoltenVK driver does not expose VK_KHR_pipeline_executable_properties, so real duplicate emission was not measured; the failure and fix were verified via the existing CLI's synthetic test harness.

Separate from #3890 (strict quoting), #4917 (directory argument validation), #4576 (writer I/O errors), #1099 (writer CSV escaping), and #3674 (stdout encoding). The first two touch the same files but their current patches compose with this branch; there is no functional dependency.

AI-assisted (Codex). No game data, keys, decryption or protected files were used.

@Atomic258

Copy link
Copy Markdown
Contributor

Independent check of 2b56bd9c — pass.

Environment: Windows 11, CPython 3.15.2, CPU only. Synthetic CSV fixtures, no GPU, no game files.

  • python -I tests/test_shader_stats.py → 23/23 pass
  • Negative control: PR test file against main's scripts/shader_stats.py → 8 failures (the duplicate-statistic cases exit 0 instead of 1), so the new tests exercise the change
  • compileall clean, git diff --check main...HEAD clean

Read the diff: load_stats now keys counters per (name, stage) and refuses a repeated statistic with the line number and shader name — matches the docstring's new paragraph. No issues found.

AI-assisted (Devin).

@kotekompotek

Copy link
Copy Markdown

Independent Windows CPU verification on 2b56bd9c:

  • python -m pytest tests/test_shader_stats.py --basetemp D:\temp\pytest_tmp -q: 23 passed in 4.82s

@kapitche

Copy link
Copy Markdown
Contributor

Tested 2b56bd9 merged with main 3faac55 (fast-forward, no conflicts) on Ubuntu (WSL2) / CPython 3.12.16 and Windows 11 / CPython 3.13.1: pass.

tests/test_shader_stats.py: 23 run, 23 pass, 0 fail on both. CPU only, synthetic CSVs.

Negative control: with scripts/shader_stats.py taken from main and the PR's tests kept, 8 fail (the three new duplicate-counter tests, including each row-order and --details subcase); restored, 23/23 pass.

CLI check: a CSV with the same (shader, stage, statistic) twice exits 1 with line 3: duplicate statistic 'VGPRs' for shader 'A' (stage 'CS'); ... and no traceback. The same statistic on two different shaders is not rejected.

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good regression coverage: verifies (shader, stage, stat) duplicate detection across interleaved shader/stage rows (pinning exact line 5, exit code 1, empty stdout, and stage diagnostic). Tested locally: tests/test_shader_stats.py 23/23 passed (3.48s), git diff --check clean. Clean-room OK. AI-assisted review + local test run.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 2b56bd9 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_shader_stats.py -> Ran 23 tests, OK
  • Negative control (scripts/shader_stats.py swapped to upstream/main): FAILED (failures=8)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Reviewed change in PR #4976: shader_stats: reject ambiguous duplicate CSV counters (BUILD-1490).

Scope: scripts/shader_stats.py, tests/test_shader_stats.py
Diff: +49 / -1 across 2 file(s).

The change stays inside the stated scope and the touched files line up with the description. No unrelated production paths are pulled in, and the stated behavior is directly exercised by the diff. I'm approving this one.

Reviewed by shayderrr via automated batch review.

Raxstreams added a commit to Raxstreams/Leonida that referenced this pull request Nov 23, 2026
shader_stats: reject ambiguous duplicate CSV counters (BUILD-1490)

Independent review: ItsNotRexed#4976 (comment)

Fork integration. AI-assisted (Codex).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants