-
Notifications
You must be signed in to change notification settings - Fork 186
feat: arch intrinsic feature discovery pattern #8750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joseph-isaacs
wants to merge
1
commit into
develop
Choose a base branch
from
claude/vortex-aarch-pattern-abstraction-s3javb
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| //! Overhead comparison of one-time CPU-feature dispatch mechanisms. | ||
| //! | ||
| //! Every variant ends in a call to the same `#[inline(never)]` kernel, so the | ||
| //! difference between rows is pure dispatch overhead: | ||
| //! | ||
| //! * `direct`: plain call — the floor. | ||
| //! * `cpu_kernel`: [`CpuKernel`] — relaxed load + null check + indirect call. | ||
| //! * `cpu_kernel_unconditional`: a selector with no probes (the aarch64 shape) — | ||
| //! same steady state as `cpu_kernel`. | ||
| //! * `lazy_lock`: `std::sync::LazyLock<fn>` — Once-state check + value load + | ||
| //! indirect call. | ||
| //! * `feature_detect_each_call_1/_3`: the pre-`CpuKernel` pattern — 1 or 3 | ||
| //! `is_x86_feature_detected!` cached lookups per call, then a direct call. | ||
|
|
||
| use std::sync::LazyLock; | ||
|
|
||
| use divan::Bencher; | ||
| use divan::black_box; | ||
| use divan::counter::ItemsCount; | ||
| use vortex_buffer::CpuKernel; | ||
|
|
||
| fn main() { | ||
| // Resolve every dispatcher and warm the std feature-detection cache so no | ||
| // benchmark iteration pays a one-time probe. | ||
| let seed = black_box(7); | ||
| let _ = via_direct(1, seed) | ||
| + via_cpu_kernel(1, seed) | ||
| + via_cpu_kernel_unconditional(1, seed) | ||
| + via_lazy_lock(1, seed); | ||
| #[cfg(target_arch = "x86_64")] | ||
| let _ = via_feature_detect_1(1, seed) + via_feature_detect_3(1, seed); | ||
|
|
||
| divan::main(); | ||
| } | ||
|
|
||
| type Kernel = fn(u64, u64) -> u64; | ||
|
|
||
| #[inline(never)] | ||
| fn kernel_a(x: u64, seed: u64) -> u64 { | ||
| x.wrapping_mul(31).wrapping_add(seed) | ||
| } | ||
|
|
||
| #[inline(never)] | ||
| fn kernel_b(x: u64, seed: u64) -> u64 { | ||
| x.wrapping_mul(33).wrapping_add(seed) | ||
| } | ||
|
|
||
| fn has_bmi2() -> bool { | ||
| #[cfg(target_arch = "x86_64")] | ||
| { | ||
| std::arch::is_x86_feature_detected!("bmi2") | ||
| } | ||
| #[cfg(not(target_arch = "x86_64"))] | ||
| { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| // ── dispatch variants ─────────────────────────────────────────────────── | ||
|
|
||
| #[inline(never)] | ||
| fn via_direct(x: u64, seed: u64) -> u64 { | ||
| kernel_a(x, seed) | ||
| } | ||
|
|
||
| #[cfg(target_arch = "x86_64")] | ||
| #[inline(never)] | ||
| fn via_feature_detect_1(x: u64, seed: u64) -> u64 { | ||
| if std::arch::is_x86_feature_detected!("bmi2") { | ||
| kernel_a(x, seed) | ||
| } else { | ||
| kernel_b(x, seed) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(target_arch = "x86_64")] | ||
| #[inline(never)] | ||
| fn via_feature_detect_3(x: u64, seed: u64) -> u64 { | ||
| // Non-baseline features that are present on any recent x86_64 part, so all | ||
| // three cached lookups actually execute (mirrors the old select_in_chunk). | ||
| if std::arch::is_x86_feature_detected!("avx") | ||
| && std::arch::is_x86_feature_detected!("avx2") | ||
| && std::arch::is_x86_feature_detected!("bmi2") | ||
| { | ||
| kernel_a(x, seed) | ||
| } else { | ||
| kernel_b(x, seed) | ||
| } | ||
| } | ||
|
|
||
| static LAZY: LazyLock<Kernel> = LazyLock::new(|| if has_bmi2() { kernel_a } else { kernel_b }); | ||
|
|
||
| #[inline(never)] | ||
| fn via_lazy_lock(x: u64, seed: u64) -> u64 { | ||
| (*LAZY)(x, seed) | ||
| } | ||
|
|
||
| static CPU_KERNEL: CpuKernel<Kernel> = | ||
| CpuKernel::new(|| if has_bmi2() { kernel_a } else { kernel_b }); | ||
|
|
||
| #[inline(never)] | ||
| fn via_cpu_kernel(x: u64, seed: u64) -> u64 { | ||
| CPU_KERNEL.get()(x, seed) | ||
| } | ||
|
|
||
| static CPU_KERNEL_UNCONDITIONAL: CpuKernel<Kernel> = CpuKernel::new(|| kernel_a); | ||
|
|
||
| #[inline(never)] | ||
| fn via_cpu_kernel_unconditional(x: u64, seed: u64) -> u64 { | ||
| CPU_KERNEL_UNCONDITIONAL.get()(x, seed) | ||
| } | ||
|
|
||
| // ── benches ───────────────────────────────────────────────────────────── | ||
|
|
||
| const CALLS: u64 = 1024; | ||
|
|
||
| fn bench_via(bencher: Bencher, via: fn(u64, u64) -> u64) { | ||
| bencher.counter(ItemsCount::new(CALLS)).bench(|| { | ||
| let mut acc = 0u64; | ||
| for i in 0..CALLS { | ||
| acc = acc.wrapping_add(via(black_box(i), black_box(7))); | ||
| } | ||
| acc | ||
| }) | ||
| } | ||
|
|
||
| #[divan::bench] | ||
| fn direct(bencher: Bencher) { | ||
| bench_via(bencher, via_direct); | ||
| } | ||
|
|
||
| #[divan::bench] | ||
| fn cpu_kernel_unconditional(bencher: Bencher) { | ||
| bench_via(bencher, via_cpu_kernel_unconditional); | ||
| } | ||
|
|
||
| #[divan::bench] | ||
| fn cpu_kernel(bencher: Bencher) { | ||
| bench_via(bencher, via_cpu_kernel); | ||
| } | ||
|
|
||
| #[divan::bench] | ||
| fn lazy_lock(bencher: Bencher) { | ||
| bench_via(bencher, via_lazy_lock); | ||
| } | ||
|
|
||
| #[cfg(target_arch = "x86_64")] | ||
| #[divan::bench] | ||
| fn feature_detect_each_call_1(bencher: Bencher) { | ||
| bench_via(bencher, via_feature_detect_1); | ||
| } | ||
|
|
||
| #[cfg(target_arch = "x86_64")] | ||
| #[divan::bench] | ||
| fn feature_detect_each_call_3(bencher: Bencher) { | ||
| bench_via(bencher, via_feature_detect_3); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to rebase, this is no longer here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebased onto the latest
develop(6b27c45) and force-pushed. The rebase was clean — no upstream commits touched the files in this PR, andbit_transpose/mod.rsis unchanged on develop, so the diff here is unaffected.Generated by Claude Code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I forgot that this is in my pr, sorry claude