On 2026-09-29 10:47, Linus Torvalds wrote:
On Sun, 27 Sept 2026 at 08:51, Mathieu Desnoyers
<[email protected]> wrote:

+static __always_inline
+int ptr_eq(const volatile void *a, const volatile void *b)
+{
+       OPTIMIZER_HIDE_VAR(a);
+       OPTIMIZER_HIDE_VAR(b);
+       return a == b;
+}

I had to think about why I hate this so much.

It's probably entirely senseless, but this just rubs me the wrong way.
Those OPTIMIZER_HIDE_VAR() statements are hiding the wrong thing. They
are telling the compiler to treat valid information (the pointers) as
if they had changed, so then the compiler will have to just reload
those pointers when they are used later (and "used later" is part of
the whole *point* of this, after all).

But it was never the pointer values that needed hiding from the
compiler. It's always just the comparison. And while the "optimal"
thing would be to just do something like

   static __always_inline bool ptr_eq(const volatile void *a, const
volatile void *b)
   {
         bool ret;
         asm("cmpq %0,%1":"=@ccz" (ret):"r" (a),"r"(b))
         return ret;
   }

that is an architecture-specific thing - and then have that
OPTIMIZER_HIDE_VAR() available as a fallback.

I don't know if it's really worth it, but I really do despise that
"hide the wrong values" thing that generates two new values just to
compare them and throw them away, and adds register pressure for no
good reason.

I agree that doing it in asm would generate better code. Also, I've
been told that clang now preserves the address dependency in this
scenario, so perhaps we'd want to use preprocessor conditionals to
select how to implement ptr_eq based on:

- architecture (allowing asm implementation overrides),
- compiler (e.g. if compiler is clang >= version X, just do a plain
  comparison). Likewise for gcc if it ever decides to fix this
  behavior.

I think this "hide var" hack is a fallback which can be used as a
starting point, and then we can specialize based on architecture and
compiler.

Or do you prefer this in a different order ?

Thanks,

Mathieu

--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com

Reply via email to