Skip to content

Commit b8a054f

Browse files
committed
Add a placeholder CORRADE_BUILD_CPU_RUNTIME_DISPATCH option.
To be used by code that actually provides multiple variants. Enabled by default only on platforms that have IFUNC support, elsewhere it's currently disabled because I'm not yet sure about perf consequences of going through a function pointer.
1 parent 10ccf25 commit b8a054f

7 files changed

+50
-0
lines changed

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ else()
252252
endif()
253253
cmake_dependent_option(CORRADE_CPU_USE_IFUNC "Allow using GNU IFUNC for runtime CPU dispatch" ${_CORRADE_CPU_USE_IFUNC_DEFAULT} "_CORRADE_CPU_CAN_USE_IFUNC" OFF)
254254

255+
# Runtime CPU dispatch. Because going through a function pointer may have
256+
# negative perf consequences, enable it by default only on platforms that have
257+
# IFUNC, and thus can avoid the function pointer indirection.
258+
option(CORRADE_BUILD_CPU_RUNTIME_DISPATCH "Build with runtime dispatch for CPU-dependent functionality" ${_CORRADE_CPU_CAN_USE_IFUNC})
259+
255260
# Backwards compatibility for unprefixed CMake options. If the user isn't
256261
# explicitly using prefixed options in the first run already, accept the
257262
# unprefixed options, and remember this decision for subsequent runs

doc/building-corrade.dox

+11
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,17 @@ Options controlling the build:
550550
Corrade features simultaneously in multiple threads. Enabled by default,
551551
disable if you don't need this and don't want to pay potential performance
552552
penalties coming from thread-local variables.
553+
- `CORRADE_BUILD_CPU_RUNTIME_DISPATCH` --- Build with performance-critical
554+
code paths optimized for multiple architectures (such as SSE or AVX on
555+
x86), with the best matching variant selected at runtime based on detected
556+
CPU features. If not enabled, the library is built with just a single
557+
variant that's picked at compile time depending on target architecture
558+
flags being passed to the compiler. Enabled by default on platforms that
559+
support [GNU IFUNC](https://sourceware.org/glibc/wiki/GNU_IFUNC), which is
560+
currently only Linux with glibc and Android with API 30+. See also the
561+
`CORRADE_CPU_USE_IFUNC` option below and
562+
@ref Cpu-usage-automatic-cached-dispatch for details and information about
563+
performance tradeoffs.
553564

554565
Platform-specific options:
555566

doc/corrade-changelog.dox

+7
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,13 @@ namespace Corrade {
364364
Windows as well instead of declaring aligned memory allocation functions on
365365
its own, as it's not worth the seriously-looking compiler warnings (see
366366
[mosra/corrade#145](https://github.com/mosra/corrade/issues/145))
367+
- Platforms that support @ref CORRADE_CPU_USE_IFUNC will now build certain
368+
code paths optimized for multiple architectures, with the best variant
369+
selected at runtime using the @ref Cpu library based on available CPU
370+
features. This behavior can be disabled with the
371+
@ref CORRADE_BUILD_CPU_RUNTIME_DISPATCH CMake option. Platforms without
372+
IFUNC support implement runtime dispatch using function pointers instead of
373+
indirect functions and have this currently disabled by default.
367374

368375
@subsection corrade-changelog-latest-bugfixes Bug fixes
369376

doc/corrade-cmake.dox

+3
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ also available as preprocessor variables if you include
211211
- `CORRADE_BUILD_MULTITHREADED` --- Defined if compiled in a way that makes
212212
it possible to safely use certain Corrade features simultaneously in
213213
multiple threads.
214+
- `CORRADE_BUILD_CPU_RUNTIME_DISPATCH` --- Defined if built with code paths
215+
optimized for multiple architectres with the best matching variant selected
216+
at runtime based on detected CPU features
214217
- `CORRADE_TARGET_UNIX` --- Defined if compiled for some Unix flavor (Linux,
215218
BSD, macOS, iOS, Android, ...)
216219
- `CORRADE_TARGET_APPLE` --- Defined if compiled for Apple platforms

modules/FindCorrade.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
# CORRADE_BUILD_MULTITHREADED - Defined if compiled in a way that makes it
8181
# possible to safely use certain Corrade features simultaneously in multiple
8282
# threads
83+
# CORRADE_BUILD_CPU_RUNTIME_DISPATCH - Defined if built with code paths
84+
# optimized for multiple architectres with the best matching variant selected
85+
# at runtime based on detected CPU features
8386
# CORRADE_TARGET_UNIX - Defined if compiled for some Unix flavor
8487
# (Linux, BSD, macOS)
8588
# CORRADE_TARGET_APPLE - Defined if compiled for Apple platforms
@@ -321,6 +324,7 @@ set(_corradeFlags
321324
BUILD_STATIC
322325
BUILD_STATIC_UNIQUE_GLOBALS
323326
BUILD_MULTITHREADED
327+
BUILD_CPU_RUNTIME_DISPATCH
324328
TARGET_UNIX
325329
TARGET_APPLE
326330
TARGET_IOS

src/Corrade/Corrade.h

+19
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ read/write access to global data.
115115
#define CORRADE_BUILD_MULTITHREADED
116116
#undef CORRADE_BUILD_MULTITHREADED
117117

118+
/**
119+
@brief Build with runtime CPU dispatch
120+
@m_since_latest
121+
122+
Defined if the library is built with performance-critical code paths optimized
123+
for multiple architectures (such as SSE or AVX on x86), with the best matching
124+
variant selected at runtime based on detected CPU features. If not defined, the
125+
library is built with just a single variant that's picked at compile time
126+
depending on target architecture flags being passed to the compiler.
127+
128+
The actual feature detection and dispatch both in the runtime and compile-time
129+
scenario is performed by the @relativeref{Corrade,Cpu} library. See
130+
@ref Cpu-usage-automatic-cached-dispatch for details and information about
131+
performance tradeoffs.
132+
@see @see @ref CORRADE_CPU_USE_IFUNC, @ref building-corrade, @ref corrade-cmake
133+
*/
134+
#define CORRADE_BUILD_CPU_RUNTIME_DISPATCH
135+
#undef CORRADE_BUILD_CPU_RUNTIME_DISPATCH
136+
118137
/**
119138
@brief Debug build
120139

src/Corrade/configure.h.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#cmakedefine CORRADE_BUILD_STATIC
3535
#cmakedefine CORRADE_BUILD_STATIC_UNIQUE_GLOBALS
3636
#cmakedefine CORRADE_BUILD_MULTITHREADED
37+
#cmakedefine CORRADE_BUILD_CPU_RUNTIME_DISPATCH
3738

3839
#cmakedefine CORRADE_TARGET_APPLE
3940
#cmakedefine CORRADE_TARGET_IOS

0 commit comments

Comments
 (0)