Skip to content

Commit 914e9cd

Browse files
DinoVfacebook-github-bot
authored andcommitted
Load eval breaker directly from runtime
Summary: In 3.12 we can load the eval breaker directly from the single runtime. We need to be able to get to the interpreter we're compiling against including in multi-threaded eval so there's a little bit of plumbing. And then we just avoid one extra load. Reviewed By: martindemello Differential Revision: D78311891 fbshipit-source-id: bc1ccfafd7430da06a85701b7eec9204ef72ab19
1 parent 2b10d85 commit 914e9cd

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

Jit/lir/generator.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2603,8 +2603,8 @@ LIRGenerator::TranslatedBlock LIRGenerator::TranslateOneBasicBlock(
26032603
// std::memory_order_relaxed. It's correct on x86-64 but probably isn't
26042604
// on other architectures.
26052605
hir::Register* dest = i.output();
2606-
Instruction* tstate = env_->asm_tstate;
26072606
#if PY_VERSION_HEX >= 0x030D0000
2607+
Instruction* tstate = env_->asm_tstate;
26082608
// tstate->ceval.eval_breaker
26092609
static_assert(
26102610
sizeof(reinterpret_cast<PyThreadState*>(0)->eval_breaker) == 8,
@@ -2613,7 +2613,20 @@ LIRGenerator::TranslatedBlock LIRGenerator::TranslateOneBasicBlock(
26132613
dest,
26142614
Instruction::kMove,
26152615
Ind{tstate, offsetof(PyThreadState, eval_breaker)});
2616+
#elif PY_VERSION_HEX >= 0x030C0000
2617+
// eval_breaker is in the runtime, which the code is generated against,
2618+
// load it directly.
2619+
static_assert(
2620+
sizeof(reinterpret_cast<PyThreadState*>(0)
2621+
->interp->ceval.eval_breaker) == 4,
2622+
"Eval breaker is not a 4 byte value");
2623+
bbb.appendInstr(
2624+
dest,
2625+
Instruction::kMove,
2626+
MemImm{reinterpret_cast<int*>(
2627+
&ThreadedCompileContext::interpreter()->ceval.eval_breaker)});
26162628
#else
2629+
Instruction* tstate = env_->asm_tstate;
26172630
// tstate->interp->ceval.eval_breaker
26182631
static_assert(
26192632
sizeof(reinterpret_cast<PyThreadState*>(0)

Jit/threaded_compile.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ namespace jit {
2121
} \
2222
}
2323

24+
class ThreadedCompileContext;
25+
// Get a reference to the global ThreadedCompileContext.
26+
ThreadedCompileContext& getThreadedCompileContext();
27+
2428
// Threaded-compile state for the whole process.
2529
class ThreadedCompileContext {
2630
public:
@@ -76,8 +80,11 @@ class ThreadedCompileContext {
7680
return !compileRunning() || holder() == std::this_thread::get_id();
7781
}
7882

79-
PyInterpreterState* interpreter() const {
80-
return interpreter_;
83+
static PyInterpreterState* interpreter() {
84+
if (getThreadedCompileContext().compileRunning()) {
85+
return getThreadedCompileContext().interpreter_;
86+
}
87+
return PyInterpreterState_Get();
8188
}
8289

8390
private:
@@ -161,9 +168,6 @@ class ThreadedCompileContext {
161168
PyInterpreterState* interpreter_;
162169
};
163170

164-
// Get a reference to the global ThreadedCompileContext.
165-
ThreadedCompileContext& getThreadedCompileContext();
166-
167171
// RAII device for acquiring the global threaded-compile lock.
168172
class ThreadedCompileSerialize {
169173
public:
@@ -286,7 +290,7 @@ class ThreadedRef : public RefBase<T> {
286290

287291
static void incref(PyObject* obj) {
288292
if (obj != nullptr && !_Py_IsImmortal(obj)) {
289-
incref_total(interpreter());
293+
incref_total(ThreadedCompileContext::interpreter());
290294
obj->ob_refcnt++;
291295
}
292296
}
@@ -300,20 +304,13 @@ class ThreadedRef : public RefBase<T> {
300304

301305
static void decref(PyObject* obj) {
302306
if (obj != nullptr && !_Py_IsImmortal(obj)) {
303-
decref_total(interpreter());
307+
decref_total(ThreadedCompileContext::interpreter());
304308
if (--obj->ob_refcnt == 0) {
305309
_Py_Dealloc((PyObject*)obj);
306310
}
307311
}
308312
}
309313

310-
static PyInterpreterState* interpreter() {
311-
if (jit::getThreadedCompileContext().compileRunning()) {
312-
return jit::getThreadedCompileContext().interpreter();
313-
}
314-
return PyInterpreterState_Get();
315-
}
316-
317314
using RefBase<T>::ptr_;
318315
};
319316

0 commit comments

Comments
 (0)