From: Steven Rostedt <rostedt@goodmis.org>
To: Miguel Ojeda <ojeda@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
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: Re: `likely` not applied with Clang and `CONFIG_TRACE_BRANCH_PROFILING`
Date: Fri, 16 Oct 2026 13:46:32 -0400 [thread overview]
Message-ID: <20261016134632.19a83a57@gandalf.local.home> (raw)
In-Reply-To: <20261016171242.125850-1-ojeda@kernel.org>
On Fri, 16 Oct 2026 19:12:42 +0200
Miguel Ojeda <ojeda@kernel.org> wrote:
> Hi Nathan, Steve, Masami, Arnd, all,
Hi Miguel,
>
> 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.
>
If it works, I'm fine with changing it. The branch profile is *not* optimized ;-)
Hmm, which reminds me, I'll be enabling it again on my server to see where
things are wrong soon.
-- Steve
prev parent reply other threads:[~2026-10-16 17:46 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-16 17:12 `likely` not applied with Clang and `CONFIG_TRACE_BRANCH_PROFILING` Miguel Ojeda
2026-10-16 17:46 ` Steven Rostedt [this message]
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=20261016134632.19a83a57@gandalf.local.home \
--to=rostedt@goodmis.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=ojeda@kernel.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