public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] RISC-V: Execute a PAUSE hint in spin loops
@ 2024-04-18 21:58 Christoph Müllner
  2024-04-19  1:03 ` Palmer Dabbelt
  0 siblings, 1 reply; 2+ messages in thread
From: Christoph Müllner @ 2024-04-18 21:58 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, Kito Cheng,
	Jeff Law, Vineet Gupta
  Cc: Christoph Müllner

The atomic_spin_nop() macro can be used to run arch-specific
code in the body of a spin loop to potentially improve efficiency.
RISC-V's Zihintpause extension includes a PAUSE instruction for
this use-case, which is encoded as a HINT, which means that it
behaves like a NOP on systems that don't implement Zihintpause.

Binutils supports Zihintpause since 2.36, so this patch uses
the ".insn" directive to keep the code compatible with older
toolchains.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/unix/sysv/linux/riscv/atomic-machine.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/sysdeps/unix/sysv/linux/riscv/atomic-machine.h b/sysdeps/unix/sysv/linux/riscv/atomic-machine.h
index c1c9d949a0..90283d9746 100644
--- a/sysdeps/unix/sysv/linux/riscv/atomic-machine.h
+++ b/sysdeps/unix/sysv/linux/riscv/atomic-machine.h
@@ -178,4 +178,7 @@
 # error "ISAs that do not subsume the A extension are not supported"
 #endif /* !__riscv_atomic */
 
+/* Execute a PAUSE hint when spinning.  */
+#define atomic_spin_nop() __asm(".insn i 0x0f, 0, x0, x0, 0x010")
+
 #endif /* bits/atomic.h */
-- 
2.44.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] RISC-V: Execute a PAUSE hint in spin loops
  2024-04-18 21:58 [PATCH] RISC-V: Execute a PAUSE hint in spin loops Christoph Müllner
@ 2024-04-19  1:03 ` Palmer Dabbelt
  0 siblings, 0 replies; 2+ messages in thread
From: Palmer Dabbelt @ 2024-04-19  1:03 UTC (permalink / raw)
  To: christoph.muellner
  Cc: libc-alpha, adhemerval.zanella, Darius Rad, Andrew Waterman,
	philipp.tomsich, Evan Green, kito.cheng, jeffreyalaw,
	Vineet Gupta, christoph.muellner

On Thu, 18 Apr 2024 14:58:58 PDT (-0700), christoph.muellner@vrull.eu wrote:
> The atomic_spin_nop() macro can be used to run arch-specific
> code in the body of a spin loop to potentially improve efficiency.
> RISC-V's Zihintpause extension includes a PAUSE instruction for
> this use-case, which is encoded as a HINT, which means that it
> behaves like a NOP on systems that don't implement Zihintpause.
>
> Binutils supports Zihintpause since 2.36, so this patch uses
> the ".insn" directive to keep the code compatible with older
> toolchains.
>
> Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
> ---
>  sysdeps/unix/sysv/linux/riscv/atomic-machine.h | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/sysdeps/unix/sysv/linux/riscv/atomic-machine.h b/sysdeps/unix/sysv/linux/riscv/atomic-machine.h
> index c1c9d949a0..90283d9746 100644
> --- a/sysdeps/unix/sysv/linux/riscv/atomic-machine.h
> +++ b/sysdeps/unix/sysv/linux/riscv/atomic-machine.h
> @@ -178,4 +178,7 @@
>  # error "ISAs that do not subsume the A extension are not supported"
>  #endif /* !__riscv_atomic */
>
> +/* Execute a PAUSE hint when spinning.  */
> +#define atomic_spin_nop() __asm(".insn i 0x0f, 0, x0, x0, 0x010")
> +
>  #endif /* bits/atomic.h */

I think this one should be pretty non-controversial: the whole point of 
PAUSE is for this sort of spin loop, so I don't think we need any 
benchmarks to justify using it.  So

Reviewed-by: Palmer Dabbelt <palmer@rivosinc.com>
Acked-by: Palmer Dabbelt <palmer@rivosinc.com>

IIRC there's some tunables for spin counts already, so hopefully that's 
enough to cover us for now.  I guess we'll found out when people have 
hardware...

Thanks!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-04-19  1:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-18 21:58 [PATCH] RISC-V: Execute a PAUSE hint in spin loops Christoph Müllner
2024-04-19  1:03 ` Palmer Dabbelt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).