From: "Gary Guo" <gary@garyguo.net>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Nathan Chancellor" <nathan@kernel.org>
Cc: "Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
rust-for-linux@vger.kernel.org,
"Nick Desaulniers" <nick.desaulniers+lkml@gmail.com>,
"Bill Wendling" <morbo@google.com>,
"Justin Stitt" <justinstitt@google.com>,
llvm@lists.linux.dev
Subject: Re: [PATCH v2 2/2] rust: std_vendor: add `likely()`, `unlikely()` and `cold_path()`
Date: Mon, 06 Apr 2026 15:46:00 +0100 [thread overview]
Message-ID: <DHM5J1FOI08C.2WB2KB9O30G6L@garyguo.net> (raw)
In-Reply-To: <20260406095820.465994-2-ojeda@kernel.org>
On Mon Apr 6, 2026 at 10:58 AM BST, Miguel Ojeda wrote:
> `feature(cold_path)` is becoming stable [1] in the upcoming Rust 1.95.0
> (expected 2026-04-16).
>
> `cold_path()` can be used directly, but it also allows us to provide
> `likely()` and `unlikely()`, based on `cold_path()`, similar to the C
> side ones.
>
> For instance, given:
>
> fn f1(a: i32) -> i32 {
> if a < 0 {
> return 123;
> }
>
> 42
> }
>
> fn f2(a: i32) -> i32 {
> if likely(a < 0) {
> return 124;
> }
>
> 42
> }
>
> fn f3(a: i32) -> i32 {
> if unlikely(a < 0) {
> return 125;
> }
>
> 42
> }
>
> LLVM emits the same code it would for similar C functions:
>
> f1:
> test %edi,%edi
> mov $0x7b,%ecx
> mov $0x2a,%eax
> cmovs %ecx,%eax
> ret
>
> f2:
> mov $0x7c,%eax
> test %edi,%edi
> /-- jns <f2+0xa>
> | ret
> \-> mov $0x2a,%eax
> ret
>
> f3:
> test %edi,%edi
> /-- js <f3+0xa>
> | mov $0x2a,%eax
> | ret
> \-> mov $0x7d,%eax
> ret
I haven't checked the assembly, but otherwise:
Reviewed-by: Gary Guo <gary@garyguo.net>
>
> The feature itself, `feature(cold_path)`, was added in Rust 1.86.0 [2].
>
> Previously, a PR in Rust 1.84.0 [3] fixed a number of issues with the
> `likely()` and `unlikely()` intrinsics (by implementing them on top of
> the new `cold_path()` intrinsic). So we could use that, in principle,
> for Rust 1.85.0. However, it is just a single version, and it is simpler
> to avoid intrinsics. Instead, approximate it with `#[cold]` [4].
>
> Thus add support for `cold_path()` by applying several approaches:
>
> - For Rust >= 1.86.0, `use` directly `core`'s `cold_path()`.
>
> - For Rust 1.85, provide a `#[cold]` no-op, and vendor `core`s
> documentation.
>
> And, for all versions, simply provide `likely()` and `unlikely()` based
> on `cold_path()`, by vendoring `core`'s unstable ones (the one from
> `intrinsics`, not the `hint` wrapper, to save a layer).
>
> In the future, if `likely()` and `unlikely()` become stable [5], we may
> want to use them directly as well.
>
> Now, in the C side, the `likely()` and `unlikely()` macros come
> from `compiler.h`, which means it is pretty much available everywhere
> directly. Thus just add these to the prelude (instead of e.g. re-exporting
> them in the root or in a new `hint` module).
>
> This will also mean less churn when we can remove the `cold_path()`
> version from `std_vendor` (and potentially the other two too).
>
> I tested that Rust 1.93.0 generate the code above and, in a previous
> version of the patch, that Rust 1.83.0 and 1.78.0 do not, as expected.
>
> Link: https://github.com/rust-lang/rust/pull/151576 [1]
> Link: https://github.com/rust-lang/rust/pull/133695 [2]
> Link: https://github.com/rust-lang/rust/pull/120370 [3]
> Link: https://lore.kernel.org/rust-for-linux/DGA6GUR58QJ7.1XZZ0P4VZNW86@garyguo.net/ [4]
> Link: https://github.com/rust-lang/rust/issues/151619 [5]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---
> v1: https://lore.kernel.org/rust-for-linux/20260208224659.18406-3-ojeda@kernel.org/
> v2:
> - Drop the intrinsics and use `#[cold]` for Rust 1.85.0.
>
> - Sorted after `feature(file_with_nul)` since now we know which was
> the stable version when it happened (Rust 1.92.0).
>
> - Rebased and reworded accordingly.
>
> init/Kconfig | 3 +
> rust/kernel/lib.rs | 4 ++
> rust/kernel/prelude.rs | 5 ++
> rust/kernel/std_vendor.rs | 142 ++++++++++++++++++++++++++++++++++++++
> 4 files changed, 154 insertions(+)
next prev parent reply other threads:[~2026-04-06 14:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-06 9:58 [PATCH v2 1/2] rust: kernel: update `file_with_nul` comment Miguel Ojeda
2026-04-06 9:58 ` [PATCH v2 2/2] rust: std_vendor: add `likely()`, `unlikely()` and `cold_path()` Miguel Ojeda
2026-04-06 14:46 ` Gary Guo [this message]
2026-04-06 14:40 ` [PATCH v2 1/2] rust: kernel: update `file_with_nul` comment Gary Guo
2026-04-06 15:32 ` Boqun Feng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DHM5J1FOI08C.2WB2KB9O30G6L@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=justinstitt@google.com \
--cc=llvm@lists.linux.dev \
--cc=lossin@kernel.org \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox