Linux Trace Kernel
 help / color / mirror / Atom feed
From: Miguel Ojeda <ojeda@kernel.org>
To: Nathan Chancellor <nathan@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>,
	llvm@lists.linux.dev,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	Arnd Bergmann <arnd@arndb.de>
Subject: `likely` not applied with Clang and `CONFIG_TRACE_BRANCH_PROFILING`
Date: Fri, 16 Oct 2026 19:12:42 +0200	[thread overview]
Message-ID: <20261016171242.125850-1-ojeda@kernel.org> (raw)

Hi Nathan, Steve, Masami, Arnd, all,

I was updating my old patch to add `likely()` and friends to the Rust
side of the kernel [1] and I noticed something odd when comparing with
the C side with Clang:

  https://godbolt.org/z/6Kendxzzd

Under `CONFIG_TRACE_BRANCH_PROFILING`, the way we use `__builtin_expect`
with a temporary variable makes Clang ignore the hint (GCC is fine).

So essentially:

    #define likely(x)   __builtin_expect(!!(x), 1)
    #define unlikely(x) __builtin_expect(!!(x), 0)

is fine for both compilers, but:

    #define __branch_check__(x, expect, is_constant) ({ \
        long ______r; \
        ...
        ______r = __builtin_expect(!!(x), expect); \
        ...
        ______r; \
    })

    #define likely(x)   (__branch_check__(x, 1, __builtin_constant_p(x)))
    #define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))

is not. Now, if we do move the `__builtin_expect` outside like:

    #define __branch_check__(x, expect, is_constant) ({ \
        long ______r; \
        ...
        ______r = !!(x); \
        ...
        ______r; \
    })

    #define likely(x)   __builtin_expect(__branch_check__(x, 1, __builtin_constant_p(x)), 1)
    #define unlikely(x) __builtin_expect(__branch_check__(x, 0, __builtin_constant_p(x)), 0)

then it works with Clang again.

Asking an LLM to sample a few object files to see the effect, it does
seem like it works as expected, e.g. `kernel/signal.o` goes from 1 to
125 branches carrying weights and 39 of its 355 functions change codegen
(675 of ~3800 across the dozen objects sampled).

It doesn't mean it is better of course -- I didn't benchmark it, but it
means it is passing the information to the compiler that we expected.

Cheers,
Miguel

Link: https://lore.kernel.org/rust-for-linux/20261016162446.121509-1-ojeda@kernel.org/ [1]

             reply	other threads:[~2026-10-16 17:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-16 17:12 Miguel Ojeda [this message]
2026-10-16 17:46 ` `likely` not applied with Clang and `CONFIG_TRACE_BRANCH_PROFILING` Steven Rostedt

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=20261016171242.125850-1-ojeda@kernel.org \
    --to=ojeda@kernel.org \
    --cc=arnd@arndb.de \
    --cc=justinstitt@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=rostedt@goodmis.org \
    /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