Skip to content

Spin locks #20955

Closed
Closed

Description

@bjacob

I was just looking at where iree_slim_mutex_lock spins for 100 iterations before entering the actual wait state:

// Spin a small amount to give us a tiny chance of falling through to the
// wait. We can tune this value based on likely contention, however 10-60
// is the recommended value and we should keep it in that order of
// magnitude. A way to think of this is "how many spins would we have to
// do to equal one call to iree_futex_wait" - if it's faster just to do
// a futex wait then we shouldn't be spinning!
// TODO(benvanik): measure on real workload on ARM; maybe remove entirely.
int spin_count = 100;
for (int i = 0; i < spin_count && iree_slim_mutex_is_locked(value); ++i) {
iree_processor_yield();
value = iree_atomic_load(&mutex->value, iree_memory_order_relaxed);
}
}

Where iree_processor_yield is defined here when Clang or GCC is the compiler:

static inline void iree_processor_yield(void) {
#if defined(IREE_ARCH_X86_32) || defined(IREE_ARCH_X86_64)
asm volatile("pause");
#elif defined(IREE_ARCH_ARM_32) || defined(IREE_ARCH_ARM_64)
asm volatile("yield");
#else
// None available; we'll spin hard.
#endif // IREE_ARCH_*

Topics to discuss:

  1. iree_processor_yield should maybe be renamed iree_cpu_spin_hint? E.g. looking at the x86 PAUSE instruction, that would be a better name.
  2. What should this be implemented as on RISC-V? Apparently there is no spin-lock-hint instruction there. The intent seems to be that spin locks use the sched_yield system call there?
  3. Should we revisit the hardcoded spin_count = 100 constant? Should we use different values on different targets, e.g. if on RISC-V we are going to use a syscall that actually relinquishes our time slice, which is much coarser granularity than the x86 PAUSE instruction, maybe correspondingly spin_count should be lower?

Activity

  1. added
    runtime
    performance ⚡
    on May 30, 2025
  2. benvanik commented on May 30, 2025

    @benvanik
    Collaborator
    1. that can be used for spin loops but isn't only for them - the arm docs for yield (which is what the naming matches) state:

    This instruction indicates that the thread is performing a task that may be temporarily suspended from execution—for example, a spinlock—without adversely affecting the program. It enables the CPU to execute other tasks during execution cycles that would otherwise be wasted.

    It's a private function in there so it could be changed, but I'm not sure it provides much value.

    1. sched_yield is not really what you want to use to spin as the first tier - it's something you escalate to after trying real hard not to. The goal of the spin lock is to not have a context switch. sched_yield is also an OS-level thing and not available on bare-metal (or may be called other random things in other OS). We'd need an intrinsic/instruction. https://five-embeddev.com/riscv-user-isa-manual/Priv-v1.12/zihintpause.html looks like what to use (when available). Parking a mutex after trying to avoid the context switch is fine, but it's not really a spin lock if you're making a syscall/relinquishing the quantum.

    2. 100 can be revisited (with data, otherwise we're guessing again :) - but we really don't want different behavior on different platforms (it's bad if on some archs/OS' the spin lock doesn't force a context switch and on some it does).

  3. bjacob commented on May 30, 2025

    @bjacob
    CollaboratorAuthor

    Thanks for the explanation and finding that RISC-V PAUSE instructions... it would probably be a good idea to add it in iree_processor_yield. Hopefully someone with a stake on RISC-V can take this? @maxbartel @adeel10x

  4. benvanik commented on May 30, 2025

    @benvanik
    Collaborator

    Yeah, not sure how to detect when it's available. Probably something like https://marc.info/?l=git-commits-head&m=166700216817098&w=2. Also not sure how widely deployed it is but since support has landed in the linux kernel it's at least useful to someone!

  5. adeel10x commented on May 30, 2025

    @adeel10x
    Contributor

    Thanks for the explanation and finding that RISC-V PAUSE instructions... it would probably be a good idea to add it in iree_processor_yield. Hopefully someone with a stake on RISC-V can take this? @maxbartel @adeel10x
    @NoumanAmir657

  6. maxbartel commented on Jun 2, 2025

    @maxbartel
    Contributor

    @devtbi @ziereis
    Thanks for bringing this up! We will try to take a look.

  7. noumanamir-10xe commented on Jun 2, 2025

    @noumanamir-10xe
    Contributor

    Yeah, not sure how to detect when it's available. Probably something like https://marc.info/?l=git-commits-head&m=166700216817098&w=2. Also not sure how widely deployed it is but since support has landed in the linux kernel it's at least useful to someone!

    Hi Ben, can we use something like this to determine if the extension Zihintpause is present?

  8. benvanik commented on Jun 2, 2025

    @benvanik
    Collaborator

    It’s best not to for something like the spinlocks that should be known at compile-time.

  9. noumanamir-10xe commented on Jun 3, 2025

    @noumanamir-10xe
    Contributor

    In both clang and gcc we have this macro __riscv_zihintpause, which we can use to check if we can use the pause instruction or not depending on if the runtime was compiled with that extension.

  10. added a commit that references this issue on Jun 16, 2025
    7e273a1
  11. added a commit that references this issue on Dec 1, 2025
    d7e7a53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performance ⚡runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions