Clang compatibility improvements for RISC-V
cover Clang compatibility improvements for RISC-V
Originally reported by Nathaniel Hourt[1], U-Boot fails to boot on JH7110 platforms (starfive_visionfive2_defconfig) when building with Clang.
This series fixes configuration generation with Clang's preprocessor and add an alternative codepath for RISC-V to access gd to workaround misoptimization of Clang, which is sufficient for building images for JH7110 boards.
Tested with
- starfive_visionfive2_defconfig
- qemu-riscv64_smode_defconfig
- sifive_unleashed_defconfig
built with either GCC 14 or LLVM 19.
Note that images built with LLVM lld are still broken and binutils ld has to be used for now. To test the changes on JH7110 platforms, this patch[2] must be applied for SPL to function.
[1]: https://lore.kernel.org/u-boot/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
Yao Zi (2):
Makefile: Strip leading spaces when preprocessing generated_defconfig
riscv: Access gd with inline assembly when building with LTO or Clang
arch/riscv/cpu/cpu.c | 6 ++++++ arch/riscv/include/asm/global_data.h | 19 +++++++++++++++++++ common/board_r.c | 4 +++- common/init/board_init.c | 7 +++++-- scripts/kconfig/Makefile | 1 + 5 files changed, 34 insertions(+), 3 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/2 Makefile: Strip leading spaces when preprocessing generated_defconfig
Clang's preprocessor may emit extra spaces for lines starting with '#'. Lines with these extra characters cannot be handled by Kconfig and will be ignored with warnings like,
unexpected data: # CONFIG_OF_BOARD_FIXUP is not set
Those options that is expected to be assigned explicitly with N will be set to the default value, messing up board configurations.
Let's sed these spaces away to ensure board configurations could be correctly generated with Clang.
--- scripts/kconfig/Makefile | 1 + 1 file changed, 1 insertion(+)
scripts/kconfig/Makefile +1 -0
@@ -94,6 +94,7 @@ endif %_defconfig: $(obj)/conf $(Q)$(CPP) -nostdinc -P -I $(srctree) -undef -x assembler-with-cpp $(srctree)/arch/$(SRCARCH)/configs/$@ -o generated_defconfig + $(Q)sed -i -e 's/^[[:space:]]//' generated_defconfig $(Q)$< $(silent) --defconfig=generated_defconfig $(Kconfig) # Added for U-Boot (backward compatibility)
Signature · 1 line
2.49.0
Re: [PATCH 1/2] Makefile: Strip leading spaces when preprocessing generated_defconfig
Am 27. April 2025 16:50:10 MESZ schrieb Yao Zi <[email protected]>:
Quoted 4 lines
Clang's preprocessor may emit extra spaces for lines starting with '#'. Lines with these extra characters cannot be handled by
Kconfigand will be ignored with warnings like,
Do you have an example for reprocing the issue? Is there an understanding why Clang behaves in this way?
Best regards
Heinrich
Quoted 27 lines
unexpected data: #
CONFIG_OF_BOARD_FIXUPis not setThose options that is expected to be assigned explicitly with N will be set to the default value, messing up board configurations.
Let's sed these spaces away to ensure board configurations could be correctly generated with Clang.
Fixes 2027e99e61a ("Makefile: Run defconfig files through the C preprocessor")Reported-by Nathaniel Hourt <[email protected]>Signed-off-by Yao Zi <[email protected]>---
scripts/kconfig/Makefile| 1 +1 file changed, 1 insertion(+)scripts/kconfig/Makefile +0
-0@@ -94,6 +94,7 @@ endif%_defconfig: $(obj)/conf
$(Q)$(CPP) -nostdinc -P -I $(srctree) -undef -x assembler-with-cpp $(srctree)/arch/$(SRCARCH)/configs/$@ -o generated_defconfigscripts/kconfig/Makefile +1
-0+ $(Q)sed -i -e 's/^[[:space:]]//' generated_defconfig$(Q)$< $(silent) --defconfig=generated_defconfig $(Kconfig)# Added for U-Boot (backward compatibility)
Re: [PATCH 1/2] Makefile: Strip leading spaces when preprocessing generated_defconfig
Quoted 8 lines · Heinrich Schuchardt
Am 27. April 2025 16:50:10 MESZ schrieb Yao Zi <[email protected]>:
Quoted 4 lines
Clang's preprocessor may emit extra spaces for lines starting with '#'. Lines with these extra characters cannot be handled by
Kconfigand will be ignored with warnings like,Do you have an example for reprocing the issue?
Sure,
clang-19 -E -nostdinc -P -I . -undef -x assembler-with-cpp \ configs/starfive_visionfive2_defconfig
or a smaller example for demonstrating the behaviour,
cat << EOF | clang -E -P -x assembler-with-cpp - # comment line
normal line
EOF
and you could see the strange indentation. For reproducing the exact Kconfig warnings,
make ARCH=riscv \ CC='clang-19 --target=riscv64-unknown-linux-musl' \ starfive_visionfive2_defconfig
(Clang is called clang-19 on my machine)
Quoted 1 line
Is there an understanding why Clang behaves in this way?
Sadly I have no idea. I guess it may serve for improving human-readability of the preprocessed output.
This piece of Makefile (processing defconfigs with C preprocessor) is U-Boot specific and doesn't exist in Linux kernel, thus it's unlikely to have someone noted the bahaviour difference of preprocessors before.
Honestly saying sedding the output isn't a clean solution, but I cannot come up with a better way. It'll be nice to have a flag to control the format of output, but I didn't find one.
Am looking forward to further suggestions and documentation on this.
Quoted 3 lines
Best regards
Heinrich
Thanks,
Yao Zi
Quoted 28 lines
Quoted 27 lines
unexpected data: #
CONFIG_OF_BOARD_FIXUPis not setThose options that is expected to be assigned explicitly with N will be set to the default value, messing up board configurations.
Let's sed these spaces away to ensure board configurations could be correctly generated with Clang.
Fixes 2027e99e61a ("Makefile: Run defconfig files through the C preprocessor")Reported-by Nathaniel Hourt <[email protected]>Signed-off-by Yao Zi <[email protected]>---
scripts/kconfig/Makefile| 1 +1 file changed, 1 insertion(+)scripts/kconfig/Makefile +0
-0@@ -94,6 +94,7 @@ endif%_defconfig: $(obj)/conf
$(Q)$(CPP) -nostdinc -P -I $(srctree) -undef -x assembler-with-cpp $(srctree)/arch/$(SRCARCH)/configs/$@ -o generated_defconfigscripts/kconfig/Makefile +1
-0+ $(Q)sed -i -e 's/^[[:space:]]//' generated_defconfig$(Q)$< $(silent) --defconfig=generated_defconfig $(Kconfig)# Added for U-Boot (backward compatibility)
Re: [PATCH 1/2] Makefile: Strip leading spaces when preprocessing generated_defconfig
Quoted 29 lines · Yao Zi
Quoted 8 lines · Heinrich Schuchardt
Am 27. April 2025 16:50:10 MESZ schrieb Yao Zi <[email protected]>:
Quoted 4 lines
Clang's preprocessor may emit extra spaces for lines starting with '#'. Lines with these extra characters cannot be handled by
Kconfigand will be ignored with warnings like,Do you have an example for reprocing the issue?
Sure,
clang-19 -E -nostdinc -P -I . -undef -x assembler-with-cpp \ configs/starfive_visionfive2_defconfigor a smaller example for demonstrating the behaviour,
cat << EOF | clang -E -P -x assembler-with-cpp - # comment line
normal line
EOFand you could see the strange indentation. For reproducing the exact
Kconfigwarnings,make ARCH=riscv \ CC='clang-19 --target=riscv64-unknown-linux-musl' \ starfive_visionfive2_defconfig
Quoted 6 lines
(Clang is called clang-19 on my machine)
Quoted 1 line
Is there an understanding why Clang behaves in this way?
Sadly I have no idea. I guess it may serve for improving human-readability of the preprocessed output.
This is https://github.com/llvm/llvm-project/issues/78778
Signature · 1 line
Tom
Re: [PATCH 1/2] Makefile: Strip leading spaces when preprocessing generated_defconfig
Quoted 30 lines · Tom Rini
Quoted 29 lines · Yao Zi
Quoted 8 lines · Heinrich Schuchardt
Am 27. April 2025 16:50:10 MESZ schrieb Yao Zi <[email protected]>:
Quoted 4 lines
Clang's preprocessor may emit extra spaces for lines starting with '#'. Lines with these extra characters cannot be handled by
Kconfigand will be ignored with warnings like,Do you have an example for reprocing the issue?
Sure,
clang-19 -E -nostdinc -P -I . -undef -x assembler-with-cpp \ configs/starfive_visionfive2_defconfigor a smaller example for demonstrating the behaviour,
cat << EOF | clang -E -P -x assembler-with-cpp - # comment line
normal line
EOFand you could see the strange indentation. For reproducing the exact
Kconfigwarnings,make ARCH=riscv \ CC='clang-19 --target=riscv64-unknown-linux-musl' \ starfive_visionfive2_defconfig
Quoted 8 lines
Quoted 6 lines
(Clang is called clang-19 on my machine)
Quoted 1 line
Is there an understanding why Clang behaves in this way?
Sadly I have no idea. I guess it may serve for improving human-readability of the preprocessed output.
Thanks for the reference! I did a quick search among LLVM's issues before sending the patch but didn't find anything useful.
Do you think such workaround is acceptable? It seems the link should be included in commit message as well.
Quoted 2 lines
Signature · 1 line
Tom
Thanks,
Yao Zi
Re: [PATCH 1/2] Makefile: Strip leading spaces when preprocessing generated_defconfig
Quoted 38 lines · Yao Zi
Quoted 37 lines · Tom Rini
Quoted 35 lines · Yao Zi
Quoted 8 lines · Heinrich Schuchardt
Am 27. April 2025 16:50:10 MESZ schrieb Yao Zi <[email protected]>:
Quoted 4 lines
Clang's preprocessor may emit extra spaces for lines starting with '#'. Lines with these extra characters cannot be handled by
Kconfigand will be ignored with warnings like,Do you have an example for reprocing the issue?
Sure,
clang-19 -E -nostdinc -P -I . -undef -x assembler-with-cpp \ configs/starfive_visionfive2_defconfigor a smaller example for demonstrating the behaviour,
cat << EOF | clang -E -P -x assembler-with-cpp - # comment line
normal line
EOFand you could see the strange indentation. For reproducing the exact
Kconfigwarnings,make ARCH=riscv \ CC='clang-19 --target=riscv64-unknown-linux-musl' \ starfive_visionfive2_defconfig(Clang is called clang-19 on my machine)
Quoted 1 line
Is there an understanding why Clang behaves in this way?
Sadly I have no idea. I guess it may serve for improving human-readability of the preprocessed output.
Quoted 7 lines
Quoted 1 line
Thanks for the reference! I did a quick search among LLVM's issues before sending the patch but didn't find anything useful.
Do you think such workaround is acceptable? It seems the link should be included in commit message as well.
I'd like to see if we can get llvm to fix this moving forward first. Can you please comment on the issue as you're hitting this now too?
Signature · 1 line
Tom
Re: [PATCH 1/2] Makefile: Strip leading spaces when preprocessing generated_defconfig
Quoted 41 lines · Tom Rini
Quoted 40 lines · Yao Zi
Quoted 38 lines · Tom Rini
Quoted 29 lines · Yao Zi
Quoted 8 lines · Heinrich Schuchardt
Am 27. April 2025 16:50:10 MESZ schrieb Yao Zi <[email protected]>:
Quoted 4 lines
Clang's preprocessor may emit extra spaces for lines starting with '#'. Lines with these extra characters cannot be handled by
Kconfigand will be ignored with warnings like,Do you have an example for reprocing the issue?
Sure,
clang-19 -E -nostdinc -P -I . -undef -x assembler-with-cpp \ configs/starfive_visionfive2_defconfigor a smaller example for demonstrating the behaviour,
cat << EOF | clang -E -P -x assembler-with-cpp - # comment line
normal line
EOFand you could see the strange indentation. For reproducing the exact
Kconfigwarnings,make ARCH=riscv \ CC='clang-19 --target=riscv64-unknown-linux-musl' \ starfive_visionfive2_defconfigQuoted 6 lines
(Clang is called clang-19 on my machine)
Quoted 1 line
Is there an understanding why Clang behaves in this way?
Sadly I have no idea. I guess it may serve for improving human-readability of the preprocessed output.
Quoted 8 lines
Quoted 5 lines
Thanks for the reference! I did a quick search among LLVM's issues before sending the patch but didn't find anything useful.
Do you think such workaround is acceptable? It seems the link should be included in commit message as well.
I'd like to see if we can get llvm to fix this moving forward first. Can you please comment on the issue as you're hitting this now too?
Sure it should be fixed in LLVM and I've left a comment in the issue.
But even though it's fixed in upstream, it will be nice to have such compatibility workarounds for some time to keep compatibile with older Clang toolchains, about which my concern is most.
Quoted 2 lines
Signature · 1 line
Tom
Best regards,
Yao Zi
Re: [PATCH 1/2] Makefile: Strip leading spaces when preprocessing generated_defconfig
Quoted 48 lines · Yao Zi
Quoted 47 lines · Tom Rini
Quoted 45 lines · Yao Zi
Quoted 30 lines · Tom Rini
Quoted 29 lines · Yao Zi
Quoted 8 lines · Heinrich Schuchardt
Am 27. April 2025 16:50:10 MESZ schrieb Yao Zi <[email protected]>:
Quoted 4 lines
Clang's preprocessor may emit extra spaces for lines starting with '#'. Lines with these extra characters cannot be handled by
Kconfigand will be ignored with warnings like,Do you have an example for reprocing the issue?
Sure,
clang-19 -E -nostdinc -P -I . -undef -x assembler-with-cpp \ configs/starfive_visionfive2_defconfigor a smaller example for demonstrating the behaviour,
cat << EOF | clang -E -P -x assembler-with-cpp - # comment line
normal line
EOFand you could see the strange indentation. For reproducing the exact
Kconfigwarnings,make ARCH=riscv \ CC='clang-19 --target=riscv64-unknown-linux-musl' \ starfive_visionfive2_defconfigQuoted 8 lines
Quoted 6 lines
(Clang is called clang-19 on my machine)
Quoted 1 line
Is there an understanding why Clang behaves in this way?
Sadly I have no idea. I guess it may serve for improving human-readability of the preprocessed output.
Thanks for the reference! I did a quick search among LLVM's issues before sending the patch but didn't find anything useful.
Do you think such workaround is acceptable? It seems the link should be included in commit message as well.
Quoted 8 lines
Quoted 2 lines
I'd like to see if we can get llvm to fix this moving forward first. Can you please comment on the issue as you're hitting this now too?
Sure it should be fixed in LLVM and I've left a comment in the issue.
But even though it's fixed in upstream, it will be nice to have such compatibility workarounds for some time to keep compatibile with older Clang toolchains, about which my concern is most.
Depending on what the LLVM people say, we can say what / how long of a workaround to keep in.
Signature · 1 line
Tom
PATCH 2/2 riscv: Access gd with inline assembly when building with LTO or Clang
In reply to Yao ZiSimilar to AArch64's case, Clang may wrongly fold accesses to gd pointer which is defined with register qualifier into constants, breaking various components.
This patch defines gd as a macro when building with Clang or LTO, which expands to get_gd() that accesses gp pointer in assembly, making RISC-V ports function properly and preparing for introduction of LTO in the future. Board initialization code is also adapted for non-assignable gd.
--- arch/riscv/cpu/cpu.c | 6 ++++++ arch/riscv/include/asm/global_data.h | 19 +++++++++++++++++++ common/board_r.c | 4 +++- common/init/board_init.c | 7 +++++-- 4 files changed, 33 insertions(+), 3 deletions(-)
arch/riscv/cpu/cpu.c +6 -0
@@ -18,6 +18,7 @@#include <asm/hwcap.h> #include <asm/cpufeature.h> #include <asm/cache.h> +#include <asm/global_data.h> #include <dm/uclass-internal.h> #include <linux/bitops.h> #include <linux/log2.h>
@@ -746,3 +747,8 @@ __weak int cleanup_before_linux(void)return 0 ; } + +void arch_setup_gd (gd_t *new_gd) +{ +set_gd (new_gd); +}
arch/riscv/include/asm/global_data.h +19 -0
@@ -14,6 +14,7 @@#include <asm/smp.h> #include <asm/u-boot.h> #include <compiler.h> +#include <config.h> /* Architecture-specific global data */ struct arch_global_data {
@@ -47,8 +48,26 @@ struct arch_global_data {#include <asm-generic/global_data.h> +#if defined(__clang__) ||CONFIG_IS_ENABLED (LTO ) + +#define DECLARE_GLOBAL_DATA_PTR +#define gd get_gd() + +static inline gd_t *get_gd (void ) +{ +gd_t *gd_ptr; + + __asm__volatile ("mv %0, gp\n" :"=r" (gd_ptr)); + +return gd_ptr; +} + +#else +#define DECLARE_GLOBAL_DATA_PTR register gd_t *gd asm ("gp") +#endif +static inline void set_gd (volatile gd_t *gd_ptr) {#ifdef CONFIG_64BIT
common/board_r.c +3 -1
@@ -815,7 +815,9 @@ void board_init_r(gd_t *new_gd, ulong dest_addr)if (CONFIG_IS_ENABLED (X86_64 ) && !IS_ENABLED (CONFIG_EFI_APP ))arch_setup_gd (new_gd); -#if !defined(CONFIG_X86 ) && !defined(CONFIG_ARM ) && !defined(CONFIG_ARM64 ) +#if defined(CONFIG_RISCV ) +set_gd (new_gd); +#elif !defined(CONFIG_X86 ) && !defined(CONFIG_ARM ) && !defined(CONFIG_ARM64 ) gd = new_gd;#endif gd->flags &= ~GD_FLG_LOG_READY ;
common/init/board_init.c +5 -2
@@ -13,8 +13,11 @@DECLARE_GLOBAL_DATA_PTR ; -/* Unfortunately x86 or ARM can't compile this code as gd cannot be assigned */ -#if !defined(CONFIG_X86 ) && !defined(CONFIG_ARM ) +/* +* Unfortunately x86, ARM and RISC-V can't compile this code as gd is defined +* as macro and cannot be assigned. +*/ +#if !defined(CONFIG_X86 ) && !defined(CONFIG_ARM ) && !defined(CONFIG_RISCV )__weak void arch_setup_gd (struct global_data *gd_ptr) { gd = gd_ptr;
Signature · 1 line
2.49.0
Re: [PATCH 2/2] riscv: Access gd with inline assembly when building with LTO or Clang
Quoted 9 lines · Yao Zi
Similar to AArch64's case, Clang may wrongly fold accesses to gd pointer which is defined with register qualifier into constants, breaking various components.
This patch defines gd as a macro when building with Clang or LTO, which expands to
get_gd()that accesses gp pointer in assembly, making RISC-V ports function properly and preparing for introduction of LTO in the future. Board initialization code is also adapted for non-assignable gd.
Quoted 8 lines
Reported-by Nathaniel Hourt <[email protected]>Signed-off-by Yao Zi <[email protected]>--- arch/riscv/cpu/cpu.c | 6 ++++++ arch/riscv/include/asm/global_data.h | 19 +++++++++++++++++++ common/board_r.c | 4 +++- common/init/board_init.c | 7 +++++-- 4 files changed, 33 insertions(+), 3 deletions(-)
Re: (subset) [PATCH 0/2] Clang compatibility improvements for RISC-V
In reply to Yao ZiQuoted 10 lines · Yao Zi
Originally reported by Nathaniel Hourt[1], U-Boot fails to boot on JH7110 platforms (
starfive_visionfive2_defconfig) when building with Clang.This series fixes configuration generation with Clang's preprocessor and add an alternative codepath for RISC-V to access gd to workaround misoptimization of Clang, which is sufficient for building images for JH7110 boards.
[...]
Applied to u-boot/master, thanks!
[1/2] Makefile: Strip leading spaces when preprocessing generated_defconfig
commit: 252cd205300c1b9945079c0ae1fe5980ef9dfa56
Signature · 1 line
Tom