Skip to content

check_variants: a caller's dispatcher can stand for a variant's name - #4536

Closed
alex wants to merge 1 commit into
mainfrom
claude/check-variants-dispatch
Closed

alex wants to merge 1 commit into
mainfrom
claude/check-variants-dispatch

Conversation

@alex

@alex alex commented Nov 7, 2026

Copy link
Copy Markdown
Member

Why

check_variants.py's "variants flow to callers" rule needs, for each variant f_<s> of a function g calls, a function of g named g_<s> (or a name composed with _<s>).

Removing the duplicate instances means the implementation a CPU actually runs often has a different name. For example, after AES-GCM-SIV's aliases go, vg_aes_gcm_siv_seal_vaes_pclmul_avx2 is the variant of vg_aes_gcm_siv_seal_pclmul for vg_aes_ctr32_vaes. The name rule then asks for the duplicate back. Session C's branch removing GCM-SIV's 14 aliases (claude/aes-gcm-siv-no-aliases) got 12 such reports.

What changes

Where g's function has a generated dispatcher, the check now replays the dispatcher's arms (file_arms) on a CPU with g's features plus f_<s>'s. Since CPU detection reports BMI2 and ADX only with AVX2, AVX2 is added when either is present. It passes if the implementation the dispatcher runs:

  • calls f_<s>, or
  • calls an implementation that f's dispatcher tries before f_<s>, or
  • calls none of f's implementations.

The name rule still applies first, and alone where there is no dispatcher, such as for internal functions. An arm whose condition depends on the compilation target (cfg!, Apple's _neon) is not counted.

This is untrusted CI tooling, not a TCB or Spec change. It doesn't touch the generated code, and main passes as before.

Effect

On session C's branch, the 12 reports drop to 2:

  • vg_aes_gcm_siv_{seal,open}_avx2 with vg_aes_encrypt_blocks_vaes

These two are real. With VG_CPU_FEATURES=aes,avx,avx2,vaes (no SSSE3), the dispatcher runs the baseline AES blocks, because every VAES instance of seal and open also needs SSSE3. Main runs the same baseline code there today; the old rule passed only by name, through an alias whose code needs SSSE3 too.

No real CPU has VAES without SSSE3. The faithful fix is for detection to report VAES only with SSSE3, as it already does for BMI2 and ADX with AVX2, and for VG_CPU_FEATURES to refuse VAES without SSSE3. That would change the CPU crate and the aes,avx,avx2,avx512f,vaes | aes_cbc matrix line, so it belongs in a separate change.

Tests

New tests in ci/test_check_variants.py (Dispatched) cover:

  • a dispatcher that runs a caller of the variant (passes);
  • one that runs a caller of the baseline (fails);
  • parsing arms with extra features and a cfg! condition.

All 48 tests of the module pass, as does run_unit_tests.py. check_variants.py passes on main.


Generated by Claude Code

"Variants flow to callers" asked for a function named `g_<s>` for each
variant `f_<s>` of a function `g` calls. Once duplicate instances are no
longer emitted, the implementation a CPU runs is often named otherwise
(AES-GCM-SIV's `vg_aes_gcm_siv_seal_vaes_pclmul_avx2` for
`vg_aes_gcm_siv_seal_pclmul` and `vg_aes_ctr32_vaes`), and the name rule
then wanted a duplicate back. Where `g`'s function has a dispatcher, the
check now replays its arms (`file_arms`) on a CPU with `g`'s features and
`f_<s>`'s: it passes if the implementation run calls `f_<s>`, one `f`'s
dispatcher tries before it, or none of `f`'s implementations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHE2dqL6Q2PDogxfD5f
@alex
alex enabled auto-merge November 7, 2026 19:59
@alex

alex commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Now that we have dispatch in verified code, do we need check_variants at all?

alex commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Only part of it. One property still matters; the rest was about the Rust choosing, and that's gone.

Still needed: a dispatcher can only pick among implementations that were emitted. If a caller isn't instantiated over a callee's variant (say seal doesn't get a _vaes instance when vg_aes_ctr32_vaes exists), every dispatcher stays correct, but no CPU ever runs the faster callee under that caller. The emitter can't catch this: it's a property of which generic registrations exist. "Every emitted function is reached" (no dead code) is a related property that also still matters.

No longer needed:

  • reading the Rust source for uses of variants;
  • NEVER_WITH;
  • the name-composition rules (variants_for, qualified parts, avoided, unneeded).

All of these approximate the question "on this CPU, does the code that runs call the best callee?" without being able to see what runs. The dispatchers now answer it directly.

So rather than layering this PR on top of the old rules, I'd replace check_variants.py with one rule over the generated dispatchers. For each dispatcher g_auto, and each implementation g_i it can choose, take a CPU with exactly g_i's features. For every function f that g_i calls, g_i must call what f_auto would choose on that CPU, or something f_auto ranks above it, or none of f's implementations. Add "every emitted function is called by a dispatcher or by another emitted function", and that's the whole check. It's a fraction of the current file, and NEVER_WITH disappears.

One gap to handle: internal functions have no dispatcher, so their callers' choices are what count. The rule follows calls through them.

Want me to close this PR and do that rewrite instead?


Generated by Claude Code

@alex

alex commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Sure

@alex
alex added this pull request to the merge queue Nov 7, 2026

alex commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Closing in favour of rewriting check_variants.py around the dispatchers, as described above. I'll link the new PR here.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants