Skip to content

Commit 1fd7604

Browse files
melverwilldeacon
authored andcommitted
compiler_types.h: Optimize __unqual_scalar_typeof compilation time
If the compiler supports C11's _Generic, use it to speed up compilation times of __unqual_scalar_typeof(). GCC version 4.9 or later and all supported versions of Clang support the feature (the oldest supported compiler that doesn't support _Generic is GCC 4.8, for which we use the slower alternative). The non-_Generic variant relies on multiple expansions of __pick_integer_type -> __pick_scalar_type -> __builtin_choose_expr, which increases pre-processed code size, and can cause compile times to increase in files with numerous expansions of READ_ONCE(), or other users of __unqual_scalar_typeof(). Summary of compile-time benchmarking done by Arnd Bergmann: <baseline normalized time> clang-11 gcc-9 this patch 0.78 0.91 ideal 0.76 0.86 See https://lkml.kernel.org/r/CAK8P3a3UYQeXhiufUevz=rwe09WM_vSTCd9W+KvJHJcOeQyWVA@mail.gmail.com Further compile-testing done with: gcc 4.8, 4.9, 5.5, 6.4, 7.5, 8.4; clang 9, 10. Reported-by: Arnd Bergmann <[email protected]> Signed-off-by: Marco Elver <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Tested-by: Arnd Bergmann <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/CAK8P3a0RJtbVi1JMsfik=jkHCNFv+DJn_FeDg-YLW+ueQW3tNg@mail.gmail.com [will: tweak new macros to make them a bit more readable] Signed-off-by: Will Deacon <[email protected]>
1 parent b16d8ec commit 1fd7604

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

include/linux/compiler_types.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ struct ftrace_likely_data {
213213
/*
214214
* __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
215215
* non-scalar types unchanged.
216-
*
216+
*/
217+
#if defined(CONFIG_CC_IS_GCC) && CONFIG_GCC_VERSION < 40900
218+
/*
217219
* We build this out of a couple of helper macros in a vain attempt to
218220
* help you keep your lunch down while reading it.
219221
*/
@@ -235,6 +237,25 @@ struct ftrace_likely_data {
235237
__pick_integer_type(x, int, \
236238
__pick_integer_type(x, long, \
237239
__pick_integer_type(x, long long, x))))))
240+
#else
241+
/*
242+
* If supported, prefer C11 _Generic for better compile-times. As above, 'char'
243+
* is not type-compatible with 'signed char', and we define a separate case.
244+
*/
245+
#define __scalar_type_to_expr_cases(type) \
246+
unsigned type: (unsigned type)0, \
247+
signed type: (signed type)0
248+
249+
#define __unqual_scalar_typeof(x) typeof( \
250+
_Generic((x), \
251+
char: (char)0, \
252+
__scalar_type_to_expr_cases(char), \
253+
__scalar_type_to_expr_cases(short), \
254+
__scalar_type_to_expr_cases(int), \
255+
__scalar_type_to_expr_cases(long), \
256+
__scalar_type_to_expr_cases(long long), \
257+
default: (x)))
258+
#endif
238259

239260
/* Is this type a native word size -- useful for atomic operations */
240261
#define __native_word(t) \

0 commit comments

Comments
 (0)