Skip to content

Commit 9d1bca0

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Move preload unit-deleted callback to ModuleState
Summary: Move handle_unit_deleted_during_preload from a pyjit.cpp global variable into ModuleState, continuing the effort to encapsulate JIT mutable state in the ModuleState singleton. Reviewed By: yoney Differential Revision: D96138655 fbshipit-source-id: ccdb8e6cc39f0f75d6ddbab2370d9de84a855ae2
1 parent 590efb4 commit 9d1bca0

2 files changed

Lines changed: 39 additions & 24 deletions

File tree

cinderx/Jit/pyjit.cpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ CompilerContext<Compiler>* jitCtx() {
9191
return nullptr;
9292
}
9393

94-
// Only set during preloading. Used to keep track of functions that were
95-
// deleted as a side effect of preloading.
96-
using UnitDeletedCallback = std::function<void(PyObject*)>;
97-
UnitDeletedCallback handle_unit_deleted_during_preload = nullptr;
98-
9994
// Don't care flags: CO_NOFREE, CO_FUTURE_* (the only still-relevant future is
10095
// "annotations" which doesn't impact bytecode execution.)
10196
constexpr int required_code_flags = CO_OPTIMIZED | CO_NEWLOCALS;
@@ -1035,11 +1030,11 @@ bool compile_all(size_t workers = 0) {
10351030

10361031
std::vector<BorrowedRef<>> compilation_units;
10371032
// units that were deleted during preloading
1038-
std::unordered_set<PyObject*> deleted_units;
1033+
std::unordered_set<BorrowedRef<>> deleted_units;
10391034

10401035
auto error_cleanup = [&]() {
10411036
hir::preloaderManager().clear();
1042-
handle_unit_deleted_during_preload = nullptr;
1037+
cinderx::getModuleState()->clearUnitDeletedDuringPreloadCallback();
10431038
};
10441039

10451040
auto& jit_reg_units = cinderx::getModuleState()->registeredCompilationUnits();
@@ -1059,9 +1054,10 @@ bool compile_all(size_t workers = 0) {
10591054
if (deleted_units.contains(unit)) {
10601055
continue;
10611056
}
1062-
handle_unit_deleted_during_preload = [&](PyObject* deleted_unit) {
1063-
deleted_units.emplace(deleted_unit);
1064-
};
1057+
cinderx::getModuleState()->setUnitDeletedDuringPreloadCallback(
1058+
[&](BorrowedRef<> deleted_unit) {
1059+
deleted_units.emplace(deleted_unit);
1060+
});
10651061
hir::Preloader* preloader = preload(unit);
10661062
if (!preloader) {
10671063
error_cleanup();
@@ -1070,7 +1066,7 @@ bool compile_all(size_t workers = 0) {
10701066
compilation_units.push_back(unit);
10711067
}
10721068
}
1073-
handle_unit_deleted_during_preload = nullptr;
1069+
cinderx::getModuleState()->clearUnitDeletedDuringPreloadCallback();
10741070

10751071
// Filter out any units that were deleted as a side effect of preloading.
10761072
std::erase_if(compilation_units, [&](BorrowedRef<> unit) {
@@ -3349,17 +3345,13 @@ void unregisterFunctionCodes(BorrowedRef<PyFunctionObject> func) {
33493345
if (existing != jit_code_outer_funcs.end() && existing->second == func) {
33503346
jit_code_outer_funcs.erase(code);
33513347
}
3352-
if (handle_unit_deleted_during_preload != nullptr) {
3353-
handle_unit_deleted_during_preload(code.getObj());
3354-
}
3348+
mod_state->notifyUnitDeletedDuringPreload(code.getObj());
33553349
}
33563350
}
33573351

33583352
jit_reg_units.erase(func);
33593353
jit_reg_units.erase(top_code);
3360-
if (handle_unit_deleted_during_preload != nullptr) {
3361-
handle_unit_deleted_during_preload(func.getObj());
3362-
}
3354+
mod_state->notifyUnitDeletedDuringPreload(func.getObj());
33633355
}
33643356

33653357
} // namespace
@@ -3767,11 +3759,12 @@ std::vector<BorrowedRef<PyFunctionObject>> preloadFuncAndDeps(
37673759
// This needs to be set every time before preload() is kicked off.
37683760
// Preloading can run arbitrary Python code, which means it can re-enter
37693761
// the JIT.
3770-
handle_unit_deleted_during_preload = [&](PyObject* deleted_unit) {
3771-
deleted_units.emplace(deleted_unit);
3772-
};
3762+
cinderx::getModuleState()->setUnitDeletedDuringPreloadCallback(
3763+
[&](BorrowedRef<> deleted_unit) {
3764+
deleted_units.emplace(deleted_unit);
3765+
});
37733766
hir::Preloader* preloader = preload(f);
3774-
handle_unit_deleted_during_preload = nullptr;
3767+
cinderx::getModuleState()->clearUnitDeletedDuringPreloadCallback();
37753768

37763769
if (preloader == nullptr) {
37773770
return {};
@@ -3825,9 +3818,7 @@ void codeDestroyed(BorrowedRef<PyCodeObject> code) {
38253818
auto& jit_code_outer_funcs = jitCtx()->codeOuterFunctions();
38263819
jit_reg_units.erase(code.getObj());
38273820
jit_code_outer_funcs.erase(code);
3828-
if (handle_unit_deleted_during_preload != nullptr) {
3829-
handle_unit_deleted_during_preload(code.getObj());
3830-
}
3821+
mod_state->notifyUnitDeletedDuringPreload(code.getObj());
38313822
}
38323823
}
38333824

cinderx/module_state.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
#include "cinderx/Jit/symbolizer_iface.h"
1515
#include "cinderx/async_lazy_value_iface.h"
1616

17+
#include <functional>
1718
#include <memory>
1819
#include <unordered_map>
1920

2021
namespace cinderx {
2122

23+
using UnitDeletedCallback = std::function<void(BorrowedRef<>)>;
24+
2225
class ModuleState {
2326
public:
2427
// Implements CPython's traverse functionality for tracing through to GC
@@ -197,6 +200,25 @@ class ModuleState {
197200

198201
jit::UnorderedSet<BorrowedRef<>>& registeredCompilationUnits();
199202

203+
const UnitDeletedCallback& unitDeletedDuringPreloadCallback() const {
204+
return unit_deleted_during_preload_;
205+
}
206+
207+
template <class Fn>
208+
void setUnitDeletedDuringPreloadCallback(Fn&& cb) {
209+
unit_deleted_during_preload_ = std::forward<Fn>(cb);
210+
}
211+
212+
void clearUnitDeletedDuringPreloadCallback() {
213+
unit_deleted_during_preload_ = nullptr;
214+
}
215+
216+
void notifyUnitDeletedDuringPreload(BorrowedRef<> unit) {
217+
if (unit_deleted_during_preload_) {
218+
unit_deleted_during_preload_(unit);
219+
}
220+
}
221+
200222
std::atomic<int>& compileWorkersAttempted() {
201223
return compile_workers_attempted_;
202224
}
@@ -236,6 +258,8 @@ class ModuleState {
236258

237259
std::atomic<int> compile_workers_attempted_{0};
238260
std::atomic<int> compile_workers_retries_{0};
261+
262+
UnitDeletedCallback unit_deleted_during_preload_;
239263
};
240264

241265
// Get the global ModuleState singleton.

0 commit comments

Comments
 (0)