IT lexicon › Programming › Function multiversioning

Function multiversioning

Programming På svenska → Updated: 2026-11-05

The compiler builds several variants of one function for different CPU features and picks the best when the program starts.

A distribution cannot assume every user has AVX2, but making those who do run SSE2 code is a waste. With __attribute__((target_clones("avx2","default"))), GCC and Clang compile the function once per target. At load time a resolver runs cpuid, and on glibc the right variant is wired in through an ifunc in the dynamic linker. After that a call costs nothing extra, unlike an if on every invocation.

GCC 4.8 introduced the mechanism for C++ in 2013, and target_clones, which automates the cloning, came in GCC 6. Arm has its own specification for the same idea. It is also how glibc chooses its own memcpy.

← Back to the lexicon