Re: [PATCH v3 2/2] kbuild: rust: provide an option to inline C helpers into Rust

From: Alice Ryhl
Date: Wed Apr 09 2025 - 16:18:49 EST


On Wed, Mar 19, 2025 at 9:53 PM Gary Guo <gary@xxxxxxxxxxx> wrote:
>
> A new Kconfig option, `RUST_INLINE_HELPERS` is added to allow C helpers
> (which were created to allow Rust to call into inline/macro C functions
> without having to re-implement the logic in Rust) to be inlined into
> Rust crates without performing global LTO.
>
> If the option is enabled, the following is performed:
> * For helpers, instead of compiling them to an object file to be linked
> into vmlinux, they're compiled to LLVM IR.
> * The LLVM IR is patched to add `linkonce_odr` linkage. This linkage
> means that functions are inlineable (effect of `_odr`), and the
> symbols generated will have weak linkage if emitted into object file
> (important since as later described, we might have multiple copies of
> the same symbol) and it will may be discarded if it is not invoked or
> all invocations are inlined.
> * The LLVM IR is compiled to bitcode (This is step is not necessary, but
> is a performance optimisation to prevent LLVM from always have to
> reparse the same IR).
> * When a Rust crate is compiled, instead of generating an object file, we
> ask LLVM bitcode to be generated.
> * llvm-link is invoked to combine the helper bitcode with the crate
> bitcode. This step is similar to LTO, but this is much faster since it
> only needs to inline the helpers.
> * clang is invoked to turn the combined bitcode into a final object file.
>
> Some caveats with the option:
> * clang and Rust doesn't have the exact target string. Clang generates
> `+cmov,+cx8,+fxsr` but Rust doesn't enable them (in fact, Rust will
> complain if `-Ctarget-feature=+cmov,+cx8,+fxsr` is used). x86-64 always
> enable these features, so they are in fact the same target string, but
> LLVM doesn't understand this and so inlining is inhibited. This is bypassed
> with `--ignore-tti-inline-compatible`.
> * LLVM doesn't want to inline functions compiled with
> `-fno-delete-null-pointer-checks` with code compiled without. So we
> remove this flag when compiling helpers. This is okay since this is one of
> the hardening features that does not change the ABI, and we shouldn't have
> null pointer dereferences in these helpers.
>
> The checks can also be bypassed with force inlining (`__always_inline`),
> but doing so would also bypass inlining cost analysis.
>
> Reviewed-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
> Tested-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
> Co-developed-by: Boqun Feng <boqun.feng@xxxxxxxxx>
> Signed-off-by: Boqun Feng <boqun.feng@xxxxxxxxx>
> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>

Is this compatible with DEBUG_INFO_BTF? I'm concerned that we have the
same issue as in commit 5daa0c35a1f0 ("rust: Disallow BTF generation
with Rust + LTO").

The commit message should either explain why we don't have the same
issue, or this patch should prevent configurations from enabling both.

Alice