Re: [PATCH] kbuild: rust: pass AutoFDO flags to Clang with inline helpers
From: Nathan Chancellor
Date: Thu Nov 05 2026 - 21:42:47 EST
> When CONFIG_RUST_INLINE_HELPERS=y, cmd_rustc_o_rs and cmd_rustc_library
> compile Rust sources to LLVM bitcode (.bc), link rust/helpers/helpers.bc
> via llvm-link, and then invoke $(CC) to compile the combined bitcode
> into the final ELF object (.o).
>
> When commit a960c2cdb19e ("kbuild: rust: add AutoFDO support") was
> tested with CONFIG_RUST_INLINE_HELPERS=y and CONFIG_LTO_CLANG_THIN=y,
> cmd_rustc_o_rs skipped the post-llvm-link $(CC) step under
> CONFIG_LTO_CLANG and handed the linked bitcode directly to ld.lld, where
> the ThinLTO backend applied --lto-sample-profile,
> -plugin-opt=-split-machine-functions, and LLD's default function
> sections. Non-LTO builds (!CONFIG_LTO_CLANG), however, ran the
> post-llvm-link $(CC) step without $(CFLAGS_AUTOFDO_CLANG) ever since
> commit 3a2486cc1da5 ("kbuild: rust: provide an option to inline C
> helpers into Rust").
>
> Subsequently, commit 5febf432df1c ("kbuild: rust: keep Rust objects out
> of Clang LTO with inline helpers") updated cmd_rustc_o_rs and
> rust/Makefile to always invoke $(CC) on the linked bitcode even when
> CONFIG_LTO_CLANG=y. Because that $(CC) invocation only passes
> $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS)) and does not include
> $(CFLAGS_AUTOFDO_CLANG) (which is normally appended to _c_flags in
> scripts/Makefile.lib), the final code generation step for Rust objects
> is compiled without -ffunction-sections, -fsplit-machine-functions,
> -fprofile-sample-use, or flow-sensitive discriminator flags whenever
> CONFIG_RUST_INLINE_HELPERS=y.
>
> As a result, Rust crates built with both CONFIG_RUST_INLINE_HELPERS=y
> and CONFIG_AUTOFDO_CLANG=y emit all functions into a single monolithic
> .text section (0 .text.<func> sections instead of 417 in
> rust_binder_main.o and 424 in rust/kernel.o on arm64 with
> CLANG_AUTOFDO_PROFILE set), preventing the linker from performing
> AutoFDO hot/cold function clustering and discarding SamplePGO weights on
> the inlined C helpers during final code generation.
>
> Factor out _autofdo_c_flags in scripts/Makefile.lib (preserving per-file
> and per-directory AUTOFDO_PROFILE opt-outs) and pass it to the
> post-llvm-link $(CC) invocation in scripts/Makefile.build and
> rust/Makefile.
>
> Fixes: 3a2486cc1da5 ("kbuild: rust: provide an option to inline C helpers into Rust")
> Fixes: 5febf432df1c ("kbuild: rust: keep Rust objects out of Clang LTO with inline helpers")
> Signed-off-by: Jenhao Chen <jenhaochen@xxxxxxxxxx>
I am assuming this will go via the Rust tree.
Reviewed-by: Nathan Chancellor <nathan@xxxxxxxxxx>
--
Cheers,
Nathan