Closed
Labels
performance ⚡Performance/optimization related work across the compiler and runtimeruntimeRelating to the IREE runtime library
Description
I was just looking at where iree_slim_mutex_lock spins for 100 iterations before entering the actual wait state:
iree/runtime/src/iree/base/internal/synchronization.c
Lines 504 to 516 in f88fcec
| // 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:
iree/runtime/src/iree/base/internal/synchronization.c
Lines 84 to 91 in f88fcec
| 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:
iree_processor_yieldshould maybe be renamediree_cpu_spin_hint? E.g. looking at the x86 PAUSE instruction, that would be a better name.- 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_yieldsystem call there? - Should we revisit the hardcoded
spin_count = 100constant? 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 correspondinglyspin_countshould be lower?
Metadata
Metadata
Assignees
Labels
performance ⚡Performance/optimization related work across the compiler and runtimeruntimeRelating to the IREE runtime library
Activity
benvanik commentedon May 30, 2025
yield(which is what the naming matches) state:It's a private function in there so it could be changed, but I'm not sure it provides much value.
sched_yieldis 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_yieldis 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.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).
bjacob commentedon May 30, 2025
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 @adeel10xbenvanik commentedon May 30, 2025
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!
adeel10x commentedon May 30, 2025
maxbartel commentedon Jun 2, 2025
@devtbi @ziereis
Thanks for bringing this up! We will try to take a look.
noumanamir-10xe commentedon Jun 2, 2025
Hi Ben, can we use something like this to determine if the extension
Zihintpauseis present?benvanik commentedon Jun 2, 2025
It’s best not to for something like the spinlocks that should be known at compile-time.
noumanamir-10xe commentedon Jun 3, 2025
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.benvanik commentedon Jun 3, 2025
Indeed! https://www.boost.org/doc/libs/1_88_0/boost/atomic/detail/pause.hpp
[runtime] Add riscv `pause` instruction for spinning (#21075)
[runtime] Add riscv `pause` instruction for spinning (#21075)