@@ -870,7 +870,7 @@ std::string unitFullname(BorrowedRef<> unit) {
870870 if (func != nullptr ) {
871871 return funcFullname (func);
872872 }
873- auto & jit_code_outer_funcs = cinderx::getModuleState ()->codeOuterFunctions ();
873+ auto & jit_code_outer_funcs = jitCtx ()->codeOuterFunctions ();
874874 auto iter = jit_code_outer_funcs.find (code);
875875 if (iter == jit_code_outer_funcs.end ()) {
876876 return fmt::format (
@@ -897,8 +897,7 @@ hir::Preloader* preload(BorrowedRef<> unit) {
897897 preloader =
898898 hir::Preloader::makePreloader (func, makeFrameReifier (func->func_code ));
899899 } else {
900- auto & jit_code_outer_funcs =
901- cinderx::getModuleState ()->codeOuterFunctions ();
900+ auto & jit_code_outer_funcs = jitCtx ()->codeOuterFunctions ();
902901 auto it = jit_code_outer_funcs.find (code);
903902 if (it == jit_code_outer_funcs.end ()) {
904903 PyErr_Format (
@@ -1113,9 +1112,6 @@ bool compile_all(size_t workers = 0) {
11131112
11141113 hir::preloaderManager ().clear ();
11151114
1116- auto & jit_code_outer_funcs = cinderx::getModuleState ()->codeOuterFunctions ();
1117- jit_code_outer_funcs.clear ();
1118-
11191115 return true ;
11201116}
11211117
@@ -1183,6 +1179,11 @@ bool registerFunction(BorrowedRef<PyFunctionObject> func) {
11831179 auto & jit_reg_units = cinderx::getModuleState ()->registeredCompilationUnits ();
11841180 jit_reg_units.emplace (func.getObj ());
11851181
1182+ // Map this function's code object to itself.
1183+ auto & jit_code_outer_funcs = jitCtx ()->codeOuterFunctions ();
1184+ BorrowedRef<PyCodeObject> func_code{func->func_code };
1185+ jit_code_outer_funcs.emplace (func_code, func);
1186+
11861187 // If we have an active jit-list, scan this function's code object for any
11871188 // nested functions that might be on the jit-list, and register them as well.
11881189 if (cinderx::getModuleState ()->jitList () != nullptr ) {
@@ -1191,8 +1192,6 @@ bool registerFunction(BorrowedRef<PyFunctionObject> func) {
11911192 BorrowedRef<> top_consts{top_code->co_consts };
11921193 for (BorrowedRef<PyCodeObject> code : findNestedCodes (module , top_consts)) {
11931194 jit_reg_units.emplace (code.getObj ());
1194- auto & jit_code_outer_funcs =
1195- cinderx::getModuleState ()->codeOuterFunctions ();
11961195 jit_code_outer_funcs.emplace (code, func);
11971196 }
11981197 }
@@ -2989,6 +2988,11 @@ _PyJIT_Result compile_func(BorrowedRef<PyFunctionObject> func) {
29892988 // jitable function, resulting in a single-function compile
29902989 hir::IsolatedPreloaders ip;
29912990
2991+ // Ensure the function's code object is mapped to itself.
2992+ auto & jit_code_outer_funcs = jitCtx ()->codeOuterFunctions ();
2993+ BorrowedRef<PyCodeObject> func_code{func->func_code };
2994+ jit_code_outer_funcs.emplace (func_code, func);
2995+
29922996 // Collect a list of functions to compile. If it's empty then there must have
29932997 // been a Python error during preloading.
29942998 std::vector<BorrowedRef<PyFunctionObject>> targets = preloadFuncAndDeps (func);
@@ -3433,7 +3437,7 @@ void finalize() {
34333437
34343438 // Clear some global maps that reference Python data.
34353439 auto mod_state = cinderx::getModuleState ();
3436- auto & jit_code_outer_funcs = mod_state ->codeOuterFunctions ();
3440+ auto & jit_code_outer_funcs = jitCtx () ->codeOuterFunctions ();
34373441 auto & jit_reg_units = mod_state->registeredCompilationUnits ();
34383442 jit_code_outer_funcs.clear ();
34393443 jit_reg_units.clear ();
@@ -3622,9 +3626,9 @@ void codeDestroyed(BorrowedRef<PyCodeObject> code) {
36223626 if (isJitUsable ()) {
36233627 auto mod_state = cinderx::getModuleState ();
36243628 auto & jit_reg_units = mod_state->registeredCompilationUnits ();
3625- auto & jit_code_outer_funcs = mod_state ->codeOuterFunctions ();
3629+ auto & jit_code_outer_funcs = jitCtx () ->codeOuterFunctions ();
36263630 jit_reg_units.erase (code.getObj ());
3627- jit_code_outer_funcs.erase (code. getObj () );
3631+ jit_code_outer_funcs.erase (code);
36283632 if (handle_unit_deleted_during_preload != nullptr ) {
36293633 handle_unit_deleted_during_preload (code.getObj ());
36303634 }
@@ -3643,7 +3647,7 @@ void funcDestroyed(BorrowedRef<PyFunctionObject> func) {
36433647
36443648 // erase any child code objects we registered too
36453649 if (mod_state->jitList () != nullptr ) {
3646- auto & jit_code_outer_funcs = mod_state ->codeOuterFunctions ();
3650+ auto & jit_code_outer_funcs = jitCtx () ->codeOuterFunctions ();
36473651 PyObject* module = func->func_module ;
36483652 BorrowedRef<PyCodeObject> top_code{func->func_code };
36493653 BorrowedRef<> top_consts{top_code->co_consts };
0 commit comments