Re: [PATCH v2 2/4] compiler_types: Add __assume macro

Nathan Chancellor
2025-09-17T02:30:22Z

On Tue, Sep 16, 2025 at 03:48:01PM +0200, Heiko Carstens wrote:
> Make the statement attribute "assume" with a new __assume macro available.
>
> The assume attribute is used to indicate that a certain condition is
> assumed to be true. Compilers may or may not use this indication to
> generate optimized code. If this condition is violated at runtime, the
> behavior is undefined.
>
> Note that the clang documentation states that optimizers may react
> differently to this attribute, and this may even have a negative
> performance impact. Therefore this attribute should be used with care.
>
> Signed-off-by: Heiko Carstens <hca@linux.ibm.com>

LGTM.

Reviewed-by: Nathan Chancellor <nathan@kernel.org>

> ---
> include/linux/compiler_types.h | 23 +++++++++++++++++++++++
> init/Kconfig | 10 ++++++++++
> 2 files changed, 33 insertions(+)
>
> diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
> index a910f9fa5341..41c16fb8eb40 100644
> --- a/include/linux/compiler_types.h
> +++ b/include/linux/compiler_types.h
> @@ -329,6 +329,29 @@ struct ftrace_likely_data {
> #define __no_sanitize_or_inline __always_inline
> #endif
>
> +/*
> + * The assume attribute is used to indicate that a certain condition is
> + * assumed to be true. If this condition is violated at runtime, the behavior
> + * is undefined. Compilers may or may not use this indication to generate
> + * optimized code.
> + *
> + * Note that the clang documentation states that optimizers may react
> + * differently to this attribute, and this may even have a negative
> + * performance impact. Therefore this attribute should be used with care.
> + *
> + * Optional: only supported since gcc >= 13
> + * Optional: only supported since clang >= 19
> + *
> + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#index-assume-statement-attribute
> + * clang: https://clang.llvm.org/docs/AttributeReference.html#id13
> + *
> + */
> +#ifdef CONFIG_CC_HAS_ASSUME
> +# define __assume(expr) __attribute__((__assume__(expr)))
> +#else
> +# define __assume(expr)
> +#endif
> +
> /*
> * Optional: only supported since gcc >= 15
> * Optional: only supported since clang >= 18
> diff --git a/init/Kconfig b/init/Kconfig
> index 59ae2b967195..935eff59af97 100644
> --- a/init/Kconfig
> +++ b/init/Kconfig
> @@ -112,6 +112,16 @@ config TOOLS_SUPPORT_RELR
> config CC_HAS_ASM_INLINE
> def_bool $(success,echo 'void foo(void) { asm inline (""); }' | $(CC) -x c - -c -o /dev/null)
>
> +config CC_HAS_ASSUME
> + bool
> + # clang needs to be at least 19.1.0 since the meaning of the assume
> + # attribute changed:
> + # https://github.com/llvm/llvm-project/commit/c44fa3e8a9a44c2e9a575768a3c185354b9f6c17
> + default y if CC_IS_CLANG && CLANG_VERSION >= 190100
> + # supported since gcc 13.1.0
> + # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106654
> + default y if CC_IS_GCC && GCC_VERSION >= 130100
> +
> config CC_HAS_NO_PROFILE_FN_ATTR
> def_bool $(success,echo '__attribute__((no_profile_instrument_function)) int x();' | $(CC) -x c - -c -o /dev/null -Werror)
>
> --
> 2.48.1
>