Skip to content

Commit 527aac1

Browse files
Remove atomics from _CHECK_VALIDITY
1 parent b819053 commit 527aac1

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

Python/bytecodes.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,7 +3021,7 @@ dummy_func(
30213021

30223022
_PyExecutorObject *executor = code->co_executors->executors[oparg & 255];
30233023
// We are responsible for cleaning up after ourselves.
3024-
if (!FT_ATOMIC_LOAD_UINT8_RELAXED(executor->vm_data.valid)) {
3024+
if (!executor->vm_data.valid) {
30253025
opcode = executor->vm_data.opcode;
30263026
oparg = (oparg & ~255) | executor->vm_data.oparg;
30273027
next_instr = this_instr;
@@ -5289,7 +5289,19 @@ dummy_func(
52895289
}
52905290

52915291
tier2 op(_CHECK_VALIDITY, (--)) {
5292-
DEOPT_IF(!FT_ATOMIC_LOAD_UINT8(current_executor->vm_data.valid));
5292+
// This doesn't need atomics (for now) as there is only a single time
5293+
// where a write from another thread is possible:
5294+
// when a new thread is spawned and it invalidates all current
5295+
// executors.
5296+
// The new thread can only be created by an executing uop prior to the
5297+
// _CHECK_VALIDITY check. New thread creation is synchronized by
5298+
// locking of the runtime, and the current thread is naturally
5299+
// paused/waiting for the new thread to be created. Thus,
5300+
// there is a strict happens-before relation between that
5301+
// uop's invalidation of validity and this check.
5302+
// So for now, while the JIT does not run on multiple threads,
5303+
// it is safe for this to be nona-atomic.
5304+
DEOPT_IF(!current_executor->vm_data.valid);
52935305
}
52945306

52955307
tier2 pure op(_LOAD_CONST_INLINE, (ptr/4 -- value)) {

Python/executor_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ unlink_executor(_PyExecutorObject *executor)
15041504
return;
15051505
}
15061506
_PyExecutorLinkListNode *links = &executor->vm_data.links;
1507-
assert(FT_ATOMIC_LOAD_UINT8_RELAXED(executor->vm_data.valid) == 0);
1507+
assert(executor->vm_data.valid == 0);
15081508
_PyExecutorObject *next = links->next;
15091509
_PyExecutorObject *prev = links->previous;
15101510
if (next != NULL) {
@@ -1629,7 +1629,7 @@ static int
16291629
executor_clear(PyObject *op)
16301630
{
16311631
_PyExecutorObject *executor = _PyExecutorObject_CAST(op);
1632-
assert(FT_ATOMIC_LOAD_UINT8_RELAXED(executor->vm_data.valid) == 0);
1632+
assert(executor->vm_data.valid == 0);
16331633
unlink_executor(executor);
16341634

16351635
_PyExecutorObject *cold = _PyExecutor_GetColdExecutor();
@@ -1669,7 +1669,7 @@ invalidate_sub_executors(_PyThreadStateImpl *tstate, _PyExecutorObject *executor
16691669
if (!executor->vm_data.valid) {
16701670
return;
16711671
}
1672-
FT_ATOMIC_STORE_UINT8(executor->vm_data.valid, 0);
1672+
executor->vm_data.valid = 0;
16731673
for (uint32_t i = 0; i < executor->exit_count; i++) {
16741674
_PyExecutorObject *next = executor->exits[i].executor;
16751675
if (next != tstate->jit_executor_state.cold_dynamic_executor && next != tstate->jit_executor_state.cold_executor) {
@@ -1729,7 +1729,7 @@ _Py_Executors_InvalidateAllLockHeld(PyInterpreterState *interp, int is_invalidat
17291729
FT_ATOMIC_STORE_UINT8(((_PyThreadStateImpl *)p)->jit_tracer_state.prev_state.dependencies_still_valid, 0);
17301730
for (_PyExecutorObject *exec = ((_PyThreadStateImpl *)p)->jit_executor_state.executor_list_head; exec != NULL;) {
17311731
assert(exec->tstate == p);
1732-
FT_ATOMIC_STORE_UINT8(exec->vm_data.valid, 0);
1732+
exec->vm_data.valid = 0;
17331733
// Let it be cleared up by cold executor invalidation later.
17341734
exec->vm_data.warm = 0;
17351735
if (is_invalidation) {
@@ -1768,7 +1768,7 @@ _Py_Executors_CollectCold(PyThreadState *tstate)
17681768
_PyExecutorObject *next = exec->vm_data.links.next;
17691769

17701770
if (!exec->vm_data.warm || !exec->vm_data.valid) {
1771-
FT_ATOMIC_STORE_UINT8(exec->vm_data.valid, 0);
1771+
exec->vm_data.valid = 0;
17721772
if (PyList_Append(invalidate, (PyObject *)exec) < 0) {
17731773
goto error;
17741774
}

0 commit comments

Comments
 (0)