Skip to main content

hermes_simd_core/
execution.rs

1//! Execution-mode markers for SIMD operations.
2//!
3//! Two ZST markers define whether a [`crate::view::SimdView`] operates on all lanes (dense)
4//! or a hardware-predicated subset (masked). The sealed `ExecutionMode` trait prevents
5//! external implementations and enables the compiler to eliminate dead mode branches
6//! via DCE during monomorphization.
7//!
8//! # Design
9//! - `Unmasked` — default; maps to unconditional arithmetic. Zero overhead vs. not
10//!   parameterizing by mode at all.
11//! - `Masked` — activates predicated methods on `SimdView`. Maps to AVX-512 mask registers
12//!   (`__mmask16`/`__mmask8`), AVX2 blend masks (`__m256`), SVE predicates, or scalar
13//!   `[bool; N]` arrays depending on the bound `SimdKernel` implementation.
14
15/// Private module that seals `ExecutionMode`.
16mod sealed {
17    pub trait Sealed {}
18}
19
20/// Marker trait for SIMD execution modes.
21///
22/// Sealed to prevent external implementations. Only `Unmasked` and `Masked` satisfy this.
23pub trait ExecutionMode: sealed::Sealed + Send + Sync + 'static + Copy + Clone {
24    /// Whether this mode requires mask operands on hot-path operations.
25    const IS_MASKED: bool;
26}
27
28/// Dense execution — all lanes are active. Default mode for [`crate::view::SimdView`].
29///
30/// Monomorphization eliminates all masking overhead entirely; the compiler sees no
31/// conditional paths through the `ExecutionMode` bound.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33pub struct Unmasked;
34
35/// Predicated execution — a hardware mask selects active lanes.
36///
37/// Enables the `dot_masked`, `sum_masked`, and `elementwise_add_masked` methods on
38/// `SimdView`. Each method accepts an architecture-native `Arch::Mask` operand.
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
40pub struct Masked;
41
42impl sealed::Sealed for Unmasked {}
43impl sealed::Sealed for Masked {}
44
45impl ExecutionMode for Unmasked {
46    const IS_MASKED: bool = false;
47}
48
49impl ExecutionMode for Masked {
50    const IS_MASKED: bool = true;
51}