@@ -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.)
10196constexpr 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
0 commit comments