Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use isb for normal cpu pause on aarch64 #49481

Merged
merged 3 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/locks-mt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function lock(l::SpinLock)
if @inline trylock(l)
return
end
ccall(:jl_cpu_pause, Cvoid, ())
ccall(:jl_cpu_suspend, Cvoid, ())
# Temporary solution before we have gc transition support in codegen.
ccall(:jl_gc_safepoint, Cvoid, ())
end
Expand Down
11 changes: 8 additions & 3 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
JL_GC_POP();
return mark_or_box_ccall_result(ctx, retval, retboxed, rt, unionall, static_rt);
}
else if (is_libjulia_func(jl_cpu_pause)) {
else if (is_libjulia_func(jl_cpu_pause)||is_libjulia_func(jl_cpu_suspend)) {
++CCALL_STAT(jl_cpu_pause);
// Keep in sync with the julia_threads.h version
assert(lrt == getVoidTy(ctx.builder.getContext()));
Expand All @@ -1539,9 +1539,14 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
&& ctx.emission_context.TargetTriple.getSubArch() != Triple::SubArchType::NoSubArch
// ARMv7 and above is < armv6
&& ctx.emission_context.TargetTriple.getSubArch() < Triple::SubArchType::ARMSubArch_v6)) {
auto wfeinst = InlineAsm::get(FunctionType::get(getVoidTy(ctx.builder.getContext()), false), "wfe",
InlineAsm* wait_inst;
if (is_libjulia_func(jl_cpu_pause))
wait_inst = InlineAsm::get(FunctionType::get(getVoidTy(ctx.builder.getContext()), false), "isb",
"~{memory}", true);
ctx.builder.CreateCall(wfeinst);
else
wait_inst = InlineAsm::get(FunctionType::get(getVoidTy(ctx.builder.getContext()), false), "wfe",
"~{memory}", true);
ctx.builder.CreateCall(wait_inst);
JL_GC_POP();
return ghostValue(ctx, jl_nothing_type);
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/jlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@ JL_DLLEXPORT void (jl_cpu_pause)(void)
jl_cpu_pause();
}

JL_DLLEXPORT void (jl_cpu_suspend)(void)
{
jl_cpu_suspend();
}

JL_DLLEXPORT void (jl_cpu_wake)(void)
{
jl_cpu_wake();
Expand Down
7 changes: 6 additions & 1 deletion src/julia_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,23 +292,28 @@ JL_DLLEXPORT void *jl_get_ptls_states(void);
// Update codegen version in `ccall.cpp` after changing either `pause` or `wake`
#ifdef __MIC__
# define jl_cpu_pause() _mm_delay_64(100)
# define jl_cpu_suspend() _mm_delay_64(100)
# define jl_cpu_wake() ((void)0)
# define JL_CPU_WAKE_NOOP 1
#elif defined(_CPU_X86_64_) || defined(_CPU_X86_) /* !__MIC__ */
# define jl_cpu_pause() _mm_pause()
# define jl_cpu_suspend() _mm_pause()
# define jl_cpu_wake() ((void)0)
# define JL_CPU_WAKE_NOOP 1
#elif defined(_CPU_AARCH64_) || (defined(_CPU_ARM_) && __ARM_ARCH >= 7)
# define jl_cpu_pause() __asm__ volatile ("wfe" ::: "memory")
# define jl_cpu_pause() __asm__ volatile ("isb" ::: "memory")
# define jl_cpu_suspend() __asm__ volatile ("wfe" ::: "memory")
# define jl_cpu_wake() __asm__ volatile ("sev" ::: "memory")
# define JL_CPU_WAKE_NOOP 0
#else
# define jl_cpu_pause() ((void)0)
# define jl_cpu_suspend() ((void)0)
# define jl_cpu_wake() ((void)0)
# define JL_CPU_WAKE_NOOP 1
#endif

JL_DLLEXPORT void (jl_cpu_pause)(void);
JL_DLLEXPORT void (jl_cpu_suspend)(void);
JL_DLLEXPORT void (jl_cpu_wake)(void);

#ifdef __clang_gcanalyzer__
Expand Down
2 changes: 1 addition & 1 deletion src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ void _jl_mutex_wait(jl_task_t *self, jl_mutex_t *lock, int safepoint)
uv_cond_wait(&cond, &tls_lock);
uv_mutex_unlock(&tls_lock);
}
jl_cpu_pause();
jl_cpu_suspend();
owner = jl_atomic_load_relaxed(&lock->owner);
}
}
Expand Down