Re: [PATCH] locking/local_lock: Don't evaluate the per-CPU lock pointer if unused
From: Nick Desaulniers
Date: Tue Nov 24 2026 - 17:45:08 EST
On Tue, Nov 24, 2026 at 2:25 PM Ard Biesheuvel <ardb@xxxxxxxxxx> wrote:
>
>
>
> On Tue, 24 Nov 2026, at 20:32, Nick Desaulniers wrote:
> > On Tue, Nov 24, 2026 at 8:39 AM Sebastian Andrzej Siewior
> > <bigeasy@xxxxxxxxxxxxx> wrote:
> >>
> >> On 2026-11-23 13:52:09 [-0800], Nick Desaulniers wrote:
> >> > On Mon, Nov 23, 2026 at 1:23 PM Ard Biesheuvel <ardb@xxxxxxxxxx> wrote:
> >> > >
> >> > >
> >> +/*
> >> + * Only evaluate @lock if it is actually used, i.e., for local_trylock_t, or
> >> + * when lock debugging is enabled (see local_lock_acquire() above).
> >> + */
> >> #define __local_lock_acquire(lock) \
> >> do { \
> >> - local_trylock_t *__tl; \
> >> - local_lock_t *__l; \
> >> - \
> >> - __l = (local_lock_t *)(lock); \
> >> - __tl = (local_trylock_t *)__l; \
> >> _Generic((lock), \
> >> local_trylock_t *: ({ \
> >> + local_trylock_t *__tl = (local_trylock_t *)(lock); \
> >> + \
> >
> > Why do we need the temporary?
> >
>
> Because 'lock' is a macro argument that we should evaluate only once.
Ah, right! Don't we have __auto_type or something for that though?
Maybe cleaner than the cast? Ah, that also doesn't work for the same
reason as below.
>
> >> lockdep_assert(__tl->acquired == 0); \
> >> WRITE_ONCE(__tl->acquired, 1); \
> >> + local_lock_acquire((local_lock_t *)__tl); \
> >> }), \
> >> - local_lock_t *: (void)0); \
> >> - local_lock_acquire(__l); \
> >> + local_lock_t *: ({ \
> >> + local_lock_acquire((local_lock_t *)(lock)); \
> >
> > Is this cast necessary?
> >
>
> Surprisingly, yes.
>
> https://godbolt.org/z/E8rGchfev
What the f...
Anyways, with the additional context (git trailers) linking to the
clang bugs (and missed optimizations):
Reviewed-by: Nick Desaulniers <ndesaulniers@xxxxxxxxxx>
--
Thanks,
~Nick Desaulniers