Skip to content

Commit 2735fb4

Browse files
DinoVmeta-codesync[bot]
authored andcommitted
Move outer coder funcs to jitContext
Summary: This is just a simple refactoring - these are better suited in the Jit's Context and we need to have access to them there in the future. Reviewed By: alexmalyshev Differential Revision: D92781350 fbshipit-source-id: c50314160d16ed752581d42029f45ac5aacf9c19
1 parent 5928818 commit 2735fb4

4 files changed

Lines changed: 27 additions & 27 deletions

File tree

cinderx/Jit/context.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ class Context : public IJitContext {
398398

399399
const hir::Type& typeForCommonConstant(int i) const;
400400

401+
// Map of all code objects to the functions that they were found in.
402+
// Needed for printing the name of the code object and for preloading.
403+
UnorderedMap<BorrowedRef<PyCodeObject>, BorrowedRef<PyFunctionObject>>&
404+
codeOuterFunctions() {
405+
return code_outer_funcs_;
406+
}
407+
401408
// Allocate all CodeRuntimes together so they can be mlocked() without
402409
// including any other data that happened to be on the same page.
403410
SlabArena<CodeRuntime> code_runtimes_;
@@ -475,6 +482,10 @@ class Context : public IJitContext {
475482
Ref<> cinderjit_module_;
476483

477484
std::atomic_size_t total_compile_time_ms_;
485+
486+
// Map of all code objects to the functions that they were found in.
487+
UnorderedMap<BorrowedRef<PyCodeObject>, BorrowedRef<PyFunctionObject>>
488+
code_outer_funcs_;
478489
};
479490

480491
// A CompilerContext is like a Context but it also holds a compiler object

cinderx/Jit/pyjit.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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};

cinderx/module_state.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,4 @@ jit::UnorderedSet<BorrowedRef<>>& ModuleState::registeredCompilationUnits() {
101101
return registered_compilation_units;
102102
}
103103

104-
jit::UnorderedMap<BorrowedRef<PyCodeObject>, BorrowedRef<PyFunctionObject>>&
105-
ModuleState::codeOuterFunctions() {
106-
return code_outer_funcs;
107-
}
108-
109104
} // namespace cinderx

cinderx/module_state.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ class ModuleState {
189189

190190
jit::UnorderedSet<BorrowedRef<>>& registeredCompilationUnits();
191191

192-
jit::UnorderedMap<BorrowedRef<PyCodeObject>, BorrowedRef<PyFunctionObject>>&
193-
codeOuterFunctions();
194-
195192
private:
196193
WatcherState watcher_state_;
197194

@@ -215,13 +212,6 @@ class ModuleState {
215212
// Function and code objects ("units") registered for compilation.
216213
jit::UnorderedSet<BorrowedRef<>> registered_compilation_units;
217214

218-
// Map of all compiled or to-be-compiled code objects to the functions that
219-
// they were found in.
220-
//
221-
// Needed for printing the name of the code object and for preloading.
222-
jit::UnorderedMap<BorrowedRef<PyCodeObject>, BorrowedRef<PyFunctionObject>>
223-
code_outer_funcs;
224-
225215
// Function objects registered for pre-fork perf-trampoline compilation.
226216
jit::UnorderedSet<BorrowedRef<PyFunctionObject>> perf_trampoline_worklist_;
227217

0 commit comments

Comments
 (0)