Skip to content

Commit 62423c9

Browse files
authored
GH-141794: Limit size of generated machine code. (GH-142228)
* Factor out bodies of the largest uops, to reduce jit code size. * Factor out common assert, also reducing jit code size. * Limit size of jitted code for a single executor to 1MB.
1 parent aea5531 commit 62423c9

14 files changed

Lines changed: 1410 additions & 1735 deletions

File tree

‎Include/internal/pycore_ceval.h‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,64 @@ _PyForIter_VirtualIteratorNext(PyThreadState* tstate, struct _PyInterpreterFrame
408408

409409
PyAPI_DATA(const _Py_CODEUNIT *) _Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS_PTR;
410410

411+
/* Helper functions for large uops */
412+
413+
PyAPI_FUNC(PyObject *)
414+
_Py_VectorCall_StackRefSteal(
415+
_PyStackRef callable,
416+
_PyStackRef *arguments,
417+
int total_args,
418+
_PyStackRef kwnames);
419+
420+
PyAPI_FUNC(PyObject *)
421+
_Py_BuiltinCallFast_StackRefSteal(
422+
_PyStackRef callable,
423+
_PyStackRef *arguments,
424+
int total_args);
425+
426+
PyAPI_FUNC(PyObject *)
427+
_Py_BuiltinCallFastWithKeywords_StackRefSteal(
428+
_PyStackRef callable,
429+
_PyStackRef *arguments,
430+
int total_args);
431+
432+
PyAPI_FUNC(PyObject *)
433+
_PyCallMethodDescriptorFast_StackRefSteal(
434+
_PyStackRef callable,
435+
PyMethodDef *meth,
436+
PyObject *self,
437+
_PyStackRef *arguments,
438+
int total_args);
439+
440+
PyAPI_FUNC(PyObject *)
441+
_PyCallMethodDescriptorFastWithKeywords_StackRefSteal(
442+
_PyStackRef callable,
443+
PyMethodDef *meth,
444+
PyObject *self,
445+
_PyStackRef *arguments,
446+
int total_args);
447+
448+
PyAPI_FUNC(PyObject *)
449+
_Py_CallBuiltinClass_StackRefSteal(
450+
_PyStackRef callable,
451+
_PyStackRef *arguments,
452+
int total_args);
453+
454+
PyAPI_FUNC(PyObject *)
455+
_Py_BuildString_StackRefSteal(
456+
_PyStackRef *arguments,
457+
int total_args);
458+
459+
PyAPI_FUNC(PyObject *)
460+
_Py_BuildMap_StackRefSteal(
461+
_PyStackRef *arguments,
462+
int half_args);
463+
464+
PyAPI_FUNC(void)
465+
_Py_assert_within_stack_bounds(
466+
_PyInterpreterFrame *frame, _PyStackRef *stack_pointer,
467+
const char *filename, int lineno);
468+
411469
#ifdef __cplusplus
412470
}
413471
#endif

‎Include/internal/pycore_jit.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ extern "C" {
1313
# error "this header requires Py_BUILD_CORE define"
1414
#endif
1515

16+
/* To be able to reason about code layout and branches, keep code size below 1 MB */
17+
#define PY_MAX_JIT_CODE_SIZE ((1 << 20)-1)
18+
1619
#ifdef _Py_JIT
1720

1821
typedef _Py_CODEUNIT *(*jit_func)(_PyInterpreterFrame *frame, _PyStackRef *stack_pointer, PyThreadState *tstate);

‎Include/internal/pycore_uop.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ typedef struct _PyUOpInstruction{
3636
} _PyUOpInstruction;
3737

3838
// This is the length of the trace we translate initially.
39+
#ifdef Py_DEBUG
40+
// With asserts, the stencils are a lot larger
41+
#define UOP_MAX_TRACE_LENGTH 1000
42+
#else
3943
#define UOP_MAX_TRACE_LENGTH 3000
44+
#endif
4045
#define UOP_BUFFER_SIZE (UOP_MAX_TRACE_LENGTH * sizeof(_PyUOpInstruction))
4146

4247
/* Bloom filter with m = 256

‎Lib/test/test_generated_cases.py‎

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test_inst_one_pop(self):
165165
value = stack_pointer[-1];
166166
SPAM(value);
167167
stack_pointer += -1;
168-