Skip to content

Commit 4a7106c

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Convert ModuleState from class with getters/setters to struct with public fields
Summary: Replace the ModuleState class's private fields and accessor methods with a plain struct with public fields. This is much simpler, the getter/setters were just a lot of needless boilerplate. The traverse(), clear(), and initBuiltinMembers() methods are retained. Add comments to try to explain what all these fields do as well. Reviewed By: DinoV Differential Revision: D96480101 fbshipit-source-id: 34e8393359cd4d40c93d019bbe7c0ee744888e77
1 parent 4ea018a commit 4a7106c

27 files changed

Lines changed: 217 additions & 383 deletions

cinderx/Jit/codegen/frame_asm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ void FrameAsm::linkLightWeightFunctionFrame(
504504

505505
int frame_header_size = frameHeaderSizeExcludingSpillSpace();
506506
#if PY_VERSION_HEX < 0x030E0000
507-
PyObject* frame_reifier = cinderx::getModuleState()->frameReifier();
507+
PyObject* frame_reifier = cinderx::getModuleState()->frame_reifier;
508508
#else
509509
PyObject* frame_reifier = env_.code_rt->reifier();
510510
#endif
@@ -666,7 +666,7 @@ void FrameAsm::linkLightWeightFunctionFrame(
666666

667667
int frame_header_size = frameHeaderSizeExcludingSpillSpace();
668668
#if PY_VERSION_HEX < 0x030E0000
669-
PyObject* frame_reifier = cinderx::getModuleState()->frameReifier();
669+
PyObject* frame_reifier = cinderx::getModuleState()->frame_reifier;
670670
#else
671671
PyObject* frame_reifier = env_.code_rt->reifier();
672672
#endif

cinderx/Jit/codegen/gen_asm.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ void* finalizeCode(arch::Builder& builder, std::string_view name) {
427427
DebugUtils::errorAsString(err))};
428428
}
429429

430-
ICodeAllocator* code_allocator = cinderx::getModuleState()->codeAllocator();
430+
ICodeAllocator* code_allocator =
431+
cinderx::getModuleState()->code_allocator.get();
431432
AllocateResult result = code_allocator->addCode(builder.code());
432433
if (result.error != kErrorOk) {
433434
throw std::runtime_error{fmt::format(
@@ -452,7 +453,7 @@ void* generateDeoptTrampoline(bool generator_mode) {
452453
generator_mode ? "deopt_trampoline_generators" : "deopt_trampoline";
453454

454455
CodeHolder code;
455-
ICodeAllocator* code_allocator = mod_state->codeAllocator();
456+
ICodeAllocator* code_allocator = mod_state->code_allocator.get();
456457
ASM_CHECK(code.init(code_allocator->asmJitEnvironment()), name);
457458
arch::Builder a(&code);
458459
Annotations annot;
@@ -900,7 +901,7 @@ void* generateFailedDeferredCompileTrampoline() {
900901
"CinderX not initialized, cannot generate deopt trampolines"};
901902
}
902903
CodeHolder code;
903-
ICodeAllocator* code_allocator = mod_state->codeAllocator();
904+
ICodeAllocator* code_allocator = mod_state->code_allocator.get();
904905
code.init(code_allocator->asmJitEnvironment());
905906
arch::Builder a(&code);
906907
Annotations annot;
@@ -1146,7 +1147,8 @@ void* NativeGenerator::getVectorcallEntry() {
11461147
JIT_CHECK(as_ == nullptr, "Builder should not have been initialized.");
11471148

11481149
CodeHolder code;
1149-
ICodeAllocator* code_allocator = cinderx::getModuleState()->codeAllocator();
1150+
ICodeAllocator* code_allocator =
1151+
cinderx::getModuleState()->code_allocator.get();
11501152
code.init(code_allocator->asmJitEnvironment());
11511153
ThrowableErrorHandler eh;
11521154
code.setErrorHandler(&eh);
@@ -2373,7 +2375,7 @@ Py_ssize_t NativeGenerator::giJITDataOffset() {
23732375
Py_ssize_t python_frame_slots =
23742376
_PyFrame_NumSlotsForCodeObject(GetFunction()->code);
23752377
return _PyObject_VAR_SIZE(
2376-
cinderx::getModuleState()->genType(), python_frame_slots);
2378+
cinderx::getModuleState()->gen_type, python_frame_slots);
23772379
#endif
23782380
}
23792381

cinderx/Jit/compiled_function.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ bool isJitCompiled(const PyFunctionObject* func) {
1919
if (mod_state == nullptr) {
2020
return false;
2121
}
22-
jit::ICodeAllocator* code_allocator = mod_state->codeAllocator();
22+
jit::ICodeAllocator* code_allocator = mod_state->code_allocator.get();
2323
return code_allocator != nullptr &&
2424
code_allocator->contains(reinterpret_cast<const void*>(func->vectorcall));
2525
}
@@ -33,7 +33,7 @@ CompiledFunction::~CompiledFunction() {
3333
data_.runtime->releaseReferences();
3434
}
3535

36-
auto code_allocator = cinderx::getModuleState()->codeAllocator();
36+
auto code_allocator = cinderx::getModuleState()->code_allocator.get();
3737
code_allocator->releaseCode(const_cast<std::byte*>(data_.code.data()));
3838
}
3939

cinderx/Jit/context.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ void Context::watchType(
349349
}
350350

351351
JIT_CHECK(
352-
cinderx::getModuleState()->watcherState().watchType(type) == 0,
352+
cinderx::getModuleState()->watcher_state.watchType(type) == 0,
353353
"Failed to watch type {}",
354354
type->tp_name);
355355
}
@@ -365,7 +365,7 @@ BorrowedRef<> Context::strBuildClass() {
365365
void Context::watchPendingTypes() {
366366
for (auto& type : pending_watches_) {
367367
JIT_CHECK(
368-
cinderx::getModuleState()->watcherState().watchType(type) == 0,
368+
cinderx::getModuleState()->watcher_state.watchType(type) == 0,
369369
"Failed to watch pending type {}",
370370
type->tp_name);
371371
}
@@ -684,7 +684,7 @@ Context* getContext() {
684684
if (state == nullptr) {
685685
return nullptr;
686686
}
687-
return static_cast<Context*>(state->jitContext());
687+
return static_cast<Context*>(state->jit_context.get());
688688
}
689689

690690
} // namespace jit

cinderx/Jit/frame.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ CodeRuntime* getCodeRuntime(_PyInterpreterFrame* frame) {
3838
func = jitFrameGetFunction(frame);
3939
}
4040

41-
return cinderx::getModuleState()->jitContext()->lookupCodeRuntime(func);
41+
return cinderx::getModuleState()->jit_context->lookupCodeRuntime(func);
4242
}
4343

4444
#if PY_VERSION_HEX >= 0x030E0000
@@ -61,7 +61,7 @@ bool isJitFrame(_PyInterpreterFrame* frame) {
6161
return PyUnstable_JITExecutable_Check(code) &&
6262
((PyUnstable_PyJitExecutable*)code)->je_reifier == &reifyRunningFrame;
6363
#else
64-
return frameFunction(frame) == cinderx::getModuleState()->frameReifier();
64+
return frameFunction(frame) == cinderx::getModuleState()->frame_reifier;
6565
#endif
6666

6767
#else
@@ -532,9 +532,9 @@ void jitFrameInitLightweight(
532532
frame->prev_instr = _PyCode_CODE(code) - 1;
533533
setFrameCode(frame, (PyObject*)code);
534534
JIT_DCHECK(
535-
_Py_IsImmortal(cinderx::getModuleState()->frameReifier()),
535+
_Py_IsImmortal(cinderx::getModuleState()->frame_reifier),
536536
"frame helper must be immortal");
537-
setFrameFunction(frame, cinderx::getModuleState()->frameReifier());
537+
setFrameFunction(frame, cinderx::getModuleState()->frame_reifier);
538538
jitFrameSetFunction(frame, (PyFunctionObject*)Py_NewRef(func));
539539
#endif
540540
frame->previous = previous;

cinderx/Jit/gen_data_footer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ GenDataFooter** jitGenDataFooterPtr(PyGenObject* gen, PyCodeObject* gen_code) {
1212
// use PyObject_VAR_HEAD like it probably should this would get simpler. If
1313
// we expanded the allocation to include the GenDataFooter it'd get simpler
1414
// still.
15-
BorrowedRef<PyTypeObject> gen_type = cinderx::getModuleState()->genType();
15+
BorrowedRef<PyTypeObject> gen_type = cinderx::getModuleState()->gen_type;
1616

1717
size_t python_frame_data_bytes =
1818
_PyFrame_NumSlotsForCodeObject(gen_code) * gen_type->tp_itemsize;

cinderx/Jit/generators_core.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace jit {
1414

1515
bool jitgen_is_coroutine(PyObject* o) {
16-
if (Py_TYPE(o) != cinderx::getModuleState()->genType() &&
16+
if (Py_TYPE(o) != cinderx::getModuleState()->gen_type &&
1717
!PyGen_CheckExact(o)) {
1818
return false;
1919
}
@@ -29,11 +29,11 @@ bool jitgen_is_coroutine(PyObject* o) {
2929

3030
extern "C" {
3131
int JitGen_CheckExact(PyObject* o) {
32-
return Py_TYPE(o) == cinderx::getModuleState()->genType();
32+
return Py_TYPE(o) == cinderx::getModuleState()->gen_type;
3333
}
3434

3535
int JitCoro_CheckExact(PyObject* o) {
36-
return Py_TYPE(o) == cinderx::getModuleState()->coroType();
36+
return Py_TYPE(o) == cinderx::getModuleState()->coro_type;
3737
}
3838

3939
// This is a slightly modified version of _PyCoro_GetAwaitableIter. It

cinderx/Jit/generators_mm.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ size_t computeSlots(BorrowedRef<PyCodeObject> code, uint64_t jit_data_size) {
2929
std::pair<JitGenObject*, size_t> allocateNonFreeList(
3030
size_t slots,
3131
bool is_coro) {
32-
BorrowedRef<PyTypeObject> gen_tp = cinderx::getModuleState()->genType();
32+
BorrowedRef<PyTypeObject> gen_tp = cinderx::getModuleState()->gen_type;
3333
// All the generator types should be the same size.
3434
size_t size = _PyObject_VAR_SIZE(gen_tp, slots);
3535

3636
JitGenObject* gen = is_coro
3737
? reinterpret_cast<JitGenObject*>(PyObject_GC_NewVar(
38-
PyCoroObject, cinderx::getModuleState()->coroType(), slots))
38+
PyCoroObject, cinderx::getModuleState()->coro_type, slots))
3939
: reinterpret_cast<JitGenObject*>(
4040
PyObject_GC_NewVar(PyGenObject, gen_tp, slots));
4141
// See comment in allocate_and_link_interpreter_frame about failure.
@@ -62,7 +62,7 @@ void* JitGenFreeList::rawAllocate() {
6262
// The memory for the free-list is backed by the module state, so bump the
6363
// reference count to prevent it being free'd before all free-listed
6464
// generators are.
65-
Py_INCREF(cinderx::getModuleState()->module());
65+
Py_INCREF(cinderx::getModuleState()->cinderx_module);
6666
return entry->data;
6767
}
6868

@@ -88,13 +88,13 @@ void JitGenFreeList::free(PyObject* ptr) {
8888
entry->next = head_;
8989
head_ = entry;
9090
// See comment in rawAllocate()
91-
Py_DECREF(cinderx::getModuleState()->module());
91+
Py_DECREF(cinderx::getModuleState()->cinderx_module);
9292
}
9393

9494
std::pair<JitGenObject*, size_t> JitGenFreeList::allocate(
9595
BorrowedRef<PyCodeObject> code,
9696
uint64_t jit_data_size) {
97-
BorrowedRef<PyTypeObject> gen_tp = cinderx::getModuleState()->genType();
97+
BorrowedRef<PyTypeObject> gen_tp = cinderx::getModuleState()->gen_type;
9898
// We *assume* these assertions hold in free().
9999
JIT_DCHECK_ONCE(
100100
_PyType_PreHeaderSize(gen_tp) == sizeof(PyGC_Head) &&
@@ -126,7 +126,7 @@ std::pair<JitGenObject*, size_t> JitGenFreeList::allocate(
126126
reinterpret_cast<PyVarObject*>( // NOLINT(performance-no-int-to-ptr)
127127
reinterpret_cast<uintptr_t>(raw) + sizeof(PyGC_Head));
128128

129-
PyTypeObject* tp = is_coro ? cinderx::getModuleState()->coroType() : gen_tp;
129+
PyTypeObject* tp = is_coro ? cinderx::getModuleState()->coro_type : gen_tp;
130130
_PyObject_InitVar(op, tp, slots);
131131

132132
return {reinterpret_cast<JitGenObject*>(op), size};

cinderx/Jit/generators_rt.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void raise_already_running_exception(JitGenObject* jit_gen) {
9797
// If the executor is running we cannot deopt so have to replicate the
9898
// errors from CPython here.
9999
const char* msg = "generator already executing";
100-
if (Py_TYPE(jit_gen) == cinderx::getModuleState()->coroType()) {
100+
if (Py_TYPE(jit_gen) == cinderx::getModuleState()->coro_type) {
101101
msg = "coroutine already executing";
102102
}
103103
PyErr_SetString(PyExc_ValueError, msg);
@@ -707,7 +707,7 @@ PyType_Spec JitCoro_Spec = {
707707
void deopt_jit_gen_object_only(JitGenObject* gen) {
708708
PyTypeObject* old_type = Py_TYPE(gen);
709709

710-
PyTypeObject* type = Py_TYPE(gen) == cinderx::getModuleState()->genType()
710+
PyTypeObject* type = Py_TYPE(gen) == cinderx::getModuleState()->gen_type
711711
? &PyGen_Type
712712
: &PyCoro_Type;
713713
Py_DECREF(old_type);
@@ -771,8 +771,8 @@ bool deopt_jit_gen(PyObject* obj) {
771771
void init_jit_genobject_type() {
772772
using namespace std::literals;
773773
// Copy base type functions
774-
BorrowedRef<PyTypeObject> gen_type = cinderx::getModuleState()->genType();
775-
BorrowedRef<PyTypeObject> coro_type = cinderx::getModuleState()->coroType();
774+
BorrowedRef<PyTypeObject> gen_type = cinderx::getModuleState()->gen_type;
775+
BorrowedRef<PyTypeObject> coro_type = cinderx::getModuleState()->coro_type;
776776

777777
gen_type->tp_repr = PyGen_Type.tp_repr;
778778

@@ -865,10 +865,10 @@ void init_jit_genobject_type() {
865865
copy_methods(PyCoro_Type.tp_methods, coro_type->tp_methods);
866866

867867
#ifdef Py_GIL_DISABLED
868-
cinderx::getModuleState()->setJitGenFreeList(
868+
cinderx::getModuleState()->jit_gen_free_list.reset(
869869
new JITGenFreeThreadedFreeList());
870870
#else
871-
cinderx::getModuleState()->setJitGenFreeList(new JitGenFreeList());
871+
cinderx::getModuleState()->jit_gen_free_list.reset(new JitGenFreeList());
872872
#endif
873873

874874
// Override dealloc so we can use a "free-list" for our objects.
@@ -927,7 +927,7 @@ PyObject* JitGen_AnextAwaitable_New(
927927
PyObject* awaitable,
928928
PyObject* defaultValue) {
929929
anextawaitableobject* anext =
930-
PyObject_GC_New(anextawaitableobject, moduleState->anextAwaitableType());
930+
PyObject_GC_New(anextawaitableobject, moduleState->anext_awaitable_type);
931931
if (anext == nullptr) {
932932
return nullptr;
933933
}

cinderx/Jit/generators_rt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ template <typename PyObjectT>
3838
int JitGen_CheckAny(PyObjectT* op) {
3939
return Py_IS_TYPE(
4040
reinterpret_cast<PyObject*>(op),
41-
cinderx::getModuleState()->genType()) ||
41+
cinderx::getModuleState()->gen_type) ||
4242
Py_IS_TYPE(
4343
reinterpret_cast<PyObject*>(op),
44-
cinderx::getModuleState()->coroType());
44+
cinderx::getModuleState()->coro_type);
4545
}
4646

4747
struct JitGenObject : PyGenObject {

0 commit comments

Comments
 (0)