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

add fenv cache to task struct #51288

Merged
merged 3 commits into from
Oct 16, 2024
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
4 changes: 4 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "libsupport.h"
#include <stdint.h>
#include <string.h>
#include <fenv.h>

#include "htable.h"
#include "arraylist.h"
Expand Down Expand Up @@ -2282,6 +2283,9 @@ typedef struct _jl_task_t {
uint16_t priority;

// hidden state:
// cached floating point environment
// only updated at task switch
fenv_t fenv;

// id of owning thread - does not need to be defined until the task runs
_Atomic(int16_t) tid;
Expand Down
4 changes: 4 additions & 0 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ JL_NO_ASAN static void ctx_switch(jl_task_t *lastt)
jl_set_pgcstack(&t->gcstack);
jl_signal_fence();
lastt->ptls = NULL;
fegetenv(&lastt->fenv);
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should come before the task switch, as some task switch implementations will clobber it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where specifically should it go?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or rather, it looks like the fesetenv is the one that needed to move

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where should it go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to push to this branch

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already done. I don't know about the propagating behavior though. I think we might want to start every task with a call to set fesetenv(FE_DFL_ENV)?

#ifdef MIGRATE_TASKS
ptls->previous_task = lastt;
#endif
Expand Down Expand Up @@ -726,6 +727,7 @@ JL_DLLEXPORT void jl_switch(void) JL_NOTSAFEPOINT_LEAVE JL_NOTSAFEPOINT_ENTER
0 == ptls->finalizers_inhibited);
ptls->finalizers_inhibited = finalizers_inhibited;
jl_timing_block_task_enter(ct, ptls, blk); (void)blk;
fesetenv(&ct->fenv);

sig_atomic_t other_defer_signal = ptls->defer_signal;
ptls->defer_signal = defer_signal;
Expand Down Expand Up @@ -1138,6 +1140,7 @@ JL_DLLEXPORT jl_task_t *jl_new_task(jl_function_t *start, jl_value_t *completion
t->excstack = NULL;
t->ctx.started = 0;
t->priority = 0;
fegetenv(&t->fenv);
jl_atomic_store_relaxed(&t->tid, -1);
t->threadpoolid = ct->threadpoolid;
t->ptls = NULL;
Expand Down Expand Up @@ -1239,6 +1242,7 @@ CFI_NORETURN
if (!pt->sticky && !pt->ctx.copy_stack)
jl_atomic_store_release(&pt->tid, -1);
#endif
fesetenv(&ct->fenv);

ct->ctx.started = 1;
JL_PROBE_RT_START_TASK(ct);
Expand Down