Re: [PATCH v2 2/2] rust: std_vendor: add `likely()`, `unlikely()` and `cold_path()`

Gary Guo
2026-04-06T14:46:03Z

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(+)