Re: [RFC PATCH] sched: Add scx_cpuperf_target in sched_cpu_util()
From: Uros Bizjak
Date: Thu Mar 19 2026 - 07:20:41 EST
On Thu, Mar 19, 2026 at 12:12 PM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> On Thu, Mar 19, 2026 at 12:02:29PM +0100, Uros Bizjak wrote:
> > On Thu, Mar 19, 2026 at 11:27 AM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
> > >
> > > On Thu, Mar 19, 2026 at 11:01:03AM +0100, Uros Bizjak wrote:
> > > > On Thu, Mar 19, 2026 at 10:02 AM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
> > > >
> > > > > That fastpath is definitely better; the slowpath is worse, but that is
> > > > > in part because the compilers are stupid and cannot eliminate
> > > > > static_branch().
> > > >
> > > > asm gotos are implicitly volatile because they are control flow
> > > > primitives. The compiler will *not* remove them.
> > >
> > > Yes, but I want ponies ;-)
> > >
> > > if (static_branch_unlikely(&foo)) {
> > > if (static_branch_unlikely(&foo)) {
> > > /* A */
> > > } else {
> > > /* B */
> > > }
> > > /* C */
> > > }
> > >
> > > Is a very common occurence. And we all know this really should be:
> > >
> > > if (static_branch_unlikely(&foo)) {
> > > /* A */
> > > /* C */
> > > }
> > >
> > > So how can we make this happen? IMO marking those functions __const
> > > should tell the compiler that yes, it can elimintate them.
> >
> > Huh, __const promises that the function does not access global memory
> > and that the function does not have side effects other than returning
> > a value. asm volatile inside the __const function creates a side
> > effect, so removing function calls would be considered a
> > misoptimization. Probably this could lead to undefined behavior in
> > terms of what the compiler expects from a __const function.
>
> So since the whole function reduces to a single branch or nop, it does
> not in fact access memory or have side effects, right?
But there is "asm goto" inside, this tells the compiler that there is
a side effect, no matter what is written in the asm template. Even
'asm volatile ("nop");' inside function declares a side effect.
> (and there is still __pure, for if we somehow consider the key governing
> the text patching to be an 'access' in this case)
__pure function also can't have side effects.
> > > You should not try and protect the user. If they use __const
> > > incorrectly, they get to keep the pieces and all that.
> >
> > I'm afraid that here the user wants the "__const with only partial
> > properties of __const" function, where the compiler does not know what
> > the user really wants.
>
> Well, clearly I'm not a compiler person :-) I just want to be able to
> tell the compiler that it can collapse these branches. Do you have
> another option that would be less offensive to compiler folks such as
> yourself?
I don't see any in this case (mixing volatiles with __const or __pure
attribute), but please ask on gcc@ mailing list, if there is any other
option I have missed.
Uros.