Re: More annoying code generation by clang

From: Peter Zijlstra
Date: Tue Apr 09 2024 - 06:45:58 EST


On Mon, Apr 08, 2024 at 12:42:31PM -0700, Linus Torvalds wrote:

> Actually, one of the github issues pages has more of an explanation
> (and yes, it's tied to impedance issues between the inline asm syntax
> and how clang works):
>
> https://github.com/llvm/llvm-project/issues/20571#issuecomment-980933442
>

So that same issue seems to suggest Nick is actually working on this and
got stuff merged. Nick, what is the status of your efforts and should we
indeed do the below as Linus suggests or should he upgrade his compiler?

> diff --git a/include/linux/compiler-clang.h b/include/linux/compiler-clang.h
> index 49feac0162a5..0dee061fd7a6 100644
> --- a/include/linux/compiler-clang.h
> +++ b/include/linux/compiler-clang.h
> @@ -118,3 +118,15 @@
>
> #define __diag_ignore_all(option, comment) \
> __diag_clang(13, ignore, option)
> +
> +/*
> + * clang has horrible behavior with "g" or "rm" constraints for asm
> + * inputs, turning them into something worse than "m". Avoid using
> + * constraints with multiple possible uses (but "ir" seems to be ok):
> + *
> + * https://github.com/llvm/llvm-project/issues/20571
> + * https://github.com/llvm/llvm-project/issues/30873
> + * https://github.com/llvm/llvm-project/issues/34837
> + */
> +#define ASM_INPUT_G "ir"
> +#define ASM_INPUT_RM "r"
> diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
> index 2abaa3a825a9..e53acd310545 100644
> --- a/include/linux/compiler_types.h
> +++ b/include/linux/compiler_types.h
> @@ -380,6 +380,15 @@ struct ftrace_likely_data {
> #define asm_goto_output(x...) asm volatile goto(x)
> #endif
>
> +/*
> + * Clang has trouble with constraints with multiple
> + * alternative behaviors (mainly "g" and "rm").
> + */
> +#ifndef ASM_INPUT_G
> + #define ASM_INPUT_G "g"
> + #define ASM_INPUT_RM "rm"
> +#endif
> +
> #ifdef CONFIG_CC_HAS_ASM_INLINE
> #define asm_inline asm __inline
> #else
> --
> 2.44.0.330.g4d18c88175
>