Debian generally builds binaries for the baseline instruction set of each architecture.

Often upstream projects want to require to newer instruction targets, which means that binaries won't work on older systems.

There are several options to resolve these sorts of conflicts. These changes are often suitable for upstream but can also be done just in Debian with a small amount of maintenance needed as upstream code changes.

Porting between targets

SIMDEverywhere, sse2neon and similar projects allow building code written using one instruction target (such as x86 SSE) to work on another instruction set (such as ARM NEON) or even just the architecture baseline. When combined with runtime instruction selection this can greatly increase portability of code that was written with one target in mind, often in a way that patches can be sent and accepted upstream.

Runtime selection

Manual

Your code can check the CPU it is currently running on and run the appropriate code based on the available instructions.

On x86 the CPUID instruction is used for this, but the __builtin_cpu_supports builtin function simplifies this.

The getauxval function can also be used for this, using FIXME: the AT_HWCAP/AT_HWCAP2/AT_PLATFORM/AT_BASE_PLATFORM option.

Function multi-versioning

Function multi-versioning (LWN article) involves using a compiler-supplied ifunc (more) at program start to automatically resolve functions to the right instruction target.

target_clones attribute

The target_clones attribute can allow you to compile one implementation of a function for multiple instruction targets and then select the best one at runtime:

__attribute__((target_clones("avx2", "default")))
int foo(){
  return 1;
}

This is supported for C and C++ source files in GCC 6+ and clang 14+ compilers.

In theory it should be possible to use #ifdef in target_clones functions in C source files in GCC to get different implementations for different targets, but the necessary changes have not been implemented.

target attribute

The target attribute can allow you to write independent implementations of a function for multiple instruction targets and then select the best one at runtime:

__attribute__ ((target ("default")))
int foo ()
{
  return 0;
}

__attribute__ ((target ("sse4.2")))
int foo ()
{
  return 1;
}

This is supported for C++ source files in GCC 4.8+ and clang 8+ compilers.

In theory this could be supported for C source files in GCC but the necessary changes have not been added yet.

hwcaps

The hwcaps feature allows you to build an entire library for multiple instruction targets, install them into a different directory for each target and then select the best one at runtime.

This is supported for ELF libraries in glibc and version 2.33 improved this mechanism significantly.

scripts

Writing scripts allows you to build an entire program for multiple instruction targets, install them into a different directory for each target and then select the best one at runtime.

The Debian Med team has a simd-dispatch script that is a good example of this approach.

Partial architectures

In theory it could be possible to build packages for increased baselines with different architecture names and allow users to install select packages where the performance difference is noticeable. SIMDebian took this approach.

Blocking unsupported systems

isa-support allows preventing installation of packages that require instructions that are not available on the current CPU.

In future it may also provide a way to allow installation but block running programs that require instructions that are not available on the current CPU.