Differences between revisions 31 and 34 (spanning 3 versions)
Revision 31 as of 2023-05-22 05:51:37
Size: 9115
Editor: PaulWise
Comment: add link to bug about target-specific C++ template specialisation
Revision 34 as of 2023-06-11 04:52:22
Size: 11334
Editor: PaulWise
Comment: more fixing
Deletions are marked like this. Additions are marked like this.
Line 44: Line 44:
int foo (void);
Line 50: Line 52:
This is supported for C and C++ source files in GCC 6+ and clang 14+ compilers. However, GCC support for PowerPC, MIPS and SPARC is spotty. This is supported for C and C++ source files in [[https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-target_005fclones-function-attribute|GCC]] 6+ (with GLIBC 2.23+) and [[https://clang.llvm.org/docs/AttributeReference.html#target-clones|clang]] 14+ compilers.
Line 52: Line 54:
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. In theory it should be possible to use #ifdef in target_clones functions in C source files in GCC and clang to get different implementations for different targets, but the necessary changes have not been implemented.
Line 54: Line 56:
Similarly, we could have target-specific C++ template specialisation but the necessary changes have not been added yet (see [[https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81276|bug]]). In theory target-specific C++ template specialisation could be supported for C++ source files but the necessary changes have not been added yet (see [[https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81276|GCC bug]]).

The support for C source files in clang requires a function prototype for each multi-target function, and empty argument lists are not supported in the prototypes, a void argument is required. Neither are required for C++ source files in clang.

There is support for this in GCC on [[https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html#index-target-function-attribute-5|x86]], [[https://gcc.gnu.org/onlinedocs/gcc/PowerPC-Function-Attributes.html#index-target-function-attribute-3|PowerPC]], [[https://gcc.gnu.org/onlinedocs/gcc/ARM-Function-Attributes.html#index-target-function-attribute-1|armel/armhf]], [[https://gcc.gnu.org/onlinedocs/gcc/AArch64-Function-Attributes.html|arm64]], [[https://gcc.gnu.org/onlinedocs/gcc/S_002f390-Function-Attributes.html#index-target-function-attribute-4|s390x]] and some other architectures. However, GCC support for PowerPC, MIPS and SPARC may be spotty.
Line 61: Line 67:
int foo (void);
Line 74: Line 82:
This is supported for C++ source files in GCC 4.8+ and Clang 8+ compilers for some architectures. However, GCC support for PowerPC, MIPS and SPARC is spotty. This is supported for C++ source files in [[https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-target-function-attribute|GCC]] 4.8+ compilers.
Line 78: Line 86:
Similarly, we could have target-specific C++ template specialisation but the necessary changes have not been added yet (see [[https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81276|bug]]). In theory target-specific C++ template specialisation could be supported for C++ source files in GCC but the necessary changes have not been added yet (see [[https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81276|GCC bug]]).

This supported for C and C++ source files in [[https://clang.llvm.org/docs/AttributeReference.html#target|Clang]] 8+ compilers.

The support for C source files in clang requires a function prototype for each multi-target function, and empty argument lists are not supported in the prototypes, a void argument is required. Neither are required for C++ source files in clang.

There is support for this in GCC on [[https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html#index-target-function-attribute-5|x86]], [[https://gcc.gnu.org/onlinedocs/gcc/PowerPC-Function-Attributes.html#index-target-function-attribute-3|PowerPC]], [[https://gcc.gnu.org/onlinedocs/gcc/ARM-Function-Attributes.html#index-target-function-attribute-1|armel/armhf]], [[https://gcc.gnu.org/onlinedocs/gcc/AArch64-Function-Attributes.html|arm64]], [[https://gcc.gnu.org/onlinedocs/gcc/S_002f390-Function-Attributes.html#index-target-function-attribute-4|s390x]] and some other architectures. However, GCC support for PowerPC, MIPS and SPARC may be spotty.

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

The lack of suitable instruction target is broadly separated in two cases:

  • Lack of so called SIMD instruction (vectorized maths). In this case SIMDEverywhere 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. There are several example patches from packages that have switched to SIMDe.

  • Lack of atomic instruction or SMP instruction. In this case libatomic will help and improve upstream portability.

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 (LWN article) can also be used for this, using the AT_HWCAP/AT_HWCAP2 options. The LD_SHOW_AUXV=1 sleep 0 command can print all the getauxval results at the command-line.

The Google cpu_features library can detect CPU features on Linux using the above mechanisms and other ones on other platforms.

/!\ Please note that using /proc/cpuinfo is not reliable, particularly in multiarch context and when using qemu-user.

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.

This is supported in at least C, C++, Rust and is planned for Zig.

Manual

You can write your own ifunc that will be run at program start to automatically resolve functions to the right instruction target. This could be a useful replacement for the target attribute (see below) where it isn't supported.

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:

int foo (void);

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

This is supported for C and C++ source files in GCC 6+ (with GLIBC 2.23+) and clang 14+ compilers.

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

In theory target-specific C++ template specialisation could be supported for C++ source files but the necessary changes have not been added yet (see GCC bug).

The support for C source files in clang requires a function prototype for each multi-target function, and empty argument lists are not supported in the prototypes, a void argument is required. Neither are required for C++ source files in clang.

There is support for this in GCC on x86, PowerPC, armel/armhf, arm64, s390x and some other architectures. However, GCC support for PowerPC, MIPS and SPARC may be spotty.

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:

int foo (void);

__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+ compilers.

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

In theory target-specific C++ template specialisation could be supported for C++ source files in GCC but the necessary changes have not been added yet (see GCC bug).

This supported for C and C++ source files in Clang 8+ compilers.

The support for C source files in clang requires a function prototype for each multi-target function, and empty argument lists are not supported in the prototypes, a void argument is required. Neither are required for C++ source files in clang.

There is support for this in GCC on x86, PowerPC, armel/armhf, arm64, s390x and some other architectures. However, GCC support for PowerPC, MIPS and SPARC may be spotty.

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 an example of this approach. In future the isa-support source package may provide a way to do this in a simpler way.

/!\ Please note that using /proc/cpuinfo is not reliable, particularly in multiarch context and when using qemu-user.

Blocking unsupported systems

/!\ Since blocking use of software on unsupported systems isn't very user friendly, these options are to be used only as a last resort.

/!\ Packages can allow installation but instead have scripts or programs that check the CPU they are currently running on and print an error message to stderr or show an error message using a graphical tool. The chromium wrapper script is an example of this approach. In future the src:isa-support source package may provide a way to do this in a simpler way.

/!\ The binary packages of src:isa-support allow preventing installation of packages that require instructions that are not available on the current CPU. /!\ This approach prevents lots of valid use-cases where the install system has different CPU features than the running system, like creating installs on older systems and running them on newer systems.

/!\ Please note that using /proc/cpuinfo is not reliable, particularly in multiarch context and when using qemu-user.

Architectures

Partial

In theory it could be possible to add additional architectures with increased baselines with different architecture names that only build select packages where the performance difference is noticeable. SIMDebian took this approach.

Change baselines

Increasing the CPU requirements of an architecture can increase its performance on newer or more capable hardware while preventing the port from being used on older or less capable hardware. Decreasing the CPU requirements does the opposite of course. Benchmarks are needed to determine exactly how useful a CPU requirements change could be.

Build flags

Users can use Debian build flags to rebuild individual packages for their own use. See the dpkg-buildflags manual page for the override mechanism and the compiler documentation for which build flags to select. Some packages will not properly inject the new flags into the build system, so inspect the build logs to find out if they do that correctly and file bugs for packages that don't inject build flags correctly. Benchmarks are needed to determine exactly how useful a build flags change could be.

Architecture Specific memo

Baseline are specified in this memo.

amd64

Instead of using instruction set compilation flags, software are encouraged to compile using psABI (Table 3.1: Micro-Architecture Levels) level. Use for instance if you need AVX and AVX2:

__attribute__((target("arch=x86-64-v3")))

or -march=x86-64-v3 gcc flag, and library should be installed in /usr/lib/x86_64-linux-gnu/glibc-hwcaps/x86-64-v3

i386

Same as amd64 software are encouraged to use psABI (Table 3.1: Micro-Architecture Levels) level.

x86_64 baseline may be used (arch=x86-64) instead of specifying SSE2/SSE instruction support.