silk.simd.cpu.

Runtime detection of the CPU features that the running machine has, to choose a kernel at run time.

When to use#

Use CpuFeatures.available when one binary must pick a faster kernel on machines that have a feature the build does not select. Use Target.hasCpuFeature with static if when the build itself selects the feature, which costs nothing at run time.

Details#

A CpuFeatures value is a set of feature names of the target architecture, spelled as in Compilation.cpuFeatures, such as "avx2", "dotprod" or "simd128". CpuFeatures.selected is the set the build selects. CpuFeatures.machine is the set the running machine has, which always includes the selected set:

  • x86-64 reads cpuid, and counts AVX and AVX-512 features only when the operating system saves their registers (xgetbv).

  • AArch64 Linux with GNU libc reads getauxval. AArch64 Darwin reads sysctlbyname.

  • wasm32 cannot ask the engine, and other profiles cannot ask the system, so they report the selected set.

The first question in a process asks the machine and keeps the answer, so later questions cost one memory read. CpuFeatures.available answers for one feature named at compile time: it is true at compile time when the build selects the feature.

A kernel that uses a feature's instructions is an unsafe fn compiled with @features. Calling it is safe once available answered true for each of its features.

Gotchas#

  • A name from another architecture, or an unknown name, is never present.

  • A detected feature does not make Target.hasCpuFeature true, also inside a function with @features. Such a function calls the feature's Intrinsic operations itself.

Examples#

Choose a kernel at run time#

silk
import silk.simd.cpu { CpuFeatures }
import silk.num.u64

static if Intrinsic.targetArchitecture() == "x86_64" {
  @features("popcnt")
  unsafe fn countFast(value: u64) -> u32 {
    return u64.countOnes(value)
  }
}

fn count(value: u64) -> u32 {
  static if Intrinsic.targetArchitecture() == "x86_64" {
    if CpuFeatures.available("popcnt") {
      return unsafe countFast(value)
    }
  }
  return u64.countOnes(value)
}

pub fn main() -> i32 {
  if count(255) != 8 {
    return 1
  }
  return 0
}

Compare the machine with the build#

silk
import silk.simd.cpu { CpuFeatures }

pub fn main() -> i32 {
  let machine = CpuFeatures.machine()
  let build = CpuFeatures.selected()
  // The machine has every feature the build selects.
  if !CpuFeatures.containsAll(&machine, &build) {
    return 1
  }
  if CpuFeatures.has(&machine, "no-such-feature") {
    return 1
  }
  return 0
}

Import as CpuFeatures with import silk.simd.cpu { CpuFeatures }.

Public declarations: 1.

CpuFeatures#

silk
pub struct CpuFeatures

A set of CPU features of the target architecture.

When to use#

Use CpuFeatures.available when one binary must pick a faster kernel on machines that have a feature the build does not select. Use Target.hasCpuFeature with static if when the build itself selects the feature, which costs nothing at run time.

Details#

The names are spelled as in Compilation.cpuFeatures, such as "avx2", "dotprod" or "simd128". CpuFeatures.selected is the set the build selects. CpuFeatures.machine is the set the running machine has, asked once per process, and always includes the selected set.

Gotchas#

  • A name from another architecture, or an unknown name, is never present.

  • A detected feature does not make Target.hasCpuFeature true. Code that uses the feature's instructions must still be compiled for it, in an unsafe fn with @features.

Implementation Copy for CpuFeatures#

silk
impl Copy for CpuFeatures

Implementation CpuFeatures#

silk
impl CpuFeatures

Associated function selected#

silk
pub fn selected() -> CpuFeatures

Returns the features that the build selects, from the compilation settings.

Associated function machine#

silk
pub fn machine() -> CpuFeatures

Returns the features of the running machine, which include CpuFeatures.selected.

Details#

The first call in a process asks the machine and keeps the answer in process-wide storage, so later calls cost one memory read:

  • x86-64 reads cpuid, and counts AVX and AVX-512 features only when the operating system saves their registers (xgetbv).

  • AArch64 Linux with GNU libc reads getauxval. AArch64 Darwin reads sysctlbyname.

  • wasm32 cannot ask the engine, and other profiles cannot ask the system, so they report the selected set.

Threads that call it at the same time may each ask the machine once. They get the same answer.

Gotchas#

A detected feature does not make Target.hasCpuFeature true. Code that uses the feature's instructions must still be compiled for it, for example in an unsafe fn with @features.

Associated function detect#

silk
pub fn detect() -> CpuFeatures

Returns the features of the running machine, asking it again on every call.

When to use#

Use CpuFeatures.machine, which asks once per process. Use detect only where a cached answer must not be used.

Details#

The result is the same set that CpuFeatures.machine returns, and includes CpuFeatures.selected. Asking the machine can take thousands of cycles in a virtual machine.

Associated function available#

silk
pub fn available(static name: string<'static>) -> bool

Returns true if the running machine has the CPU feature name, to choose a kernel at run time.

When to use#

Use it in front of an unsafe fn compiled with @features(name): call the function only when this returns true.

Details#

When the build selects name (Target.hasCpuFeature), the answer is true at compile time and costs nothing. Otherwise it reads the answer that CpuFeatures.machine keeps, which is one memory read and one bit test after the first call in the process. On wasm32 and other profiles that cannot ask the machine, it is false at compile time.

Gotchas#

A name from another architecture, or an unknown name, is never available.

Method has#

silk
pub fn has(self: &Self, name: string) -> bool

Returns true if the set contains the feature name.

Method containsAll#

silk
pub fn containsAll(self: &Self, other: &CpuFeatures) -> bool

Returns true if the set contains every feature of other.

Category: SIMD.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page