2021-11-23 05:53:24 -04:00
|
|
|
#ifndef Py_INTERNAL_FUNCTION_H
|
|
|
|
#define Py_INTERNAL_FUNCTION_H
|
2022-02-25 11:07:14 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2021-11-23 05:53:24 -04:00
|
|
|
|
2024-05-03 17:21:04 -03:00
|
|
|
#include "pycore_lock.h"
|
|
|
|
|
2022-02-25 11:07:14 -04:00
|
|
|
#ifndef Py_BUILD_CORE
|
|
|
|
# error "this header requires Py_BUILD_CORE define"
|
|
|
|
#endif
|
2021-11-23 05:53:24 -04:00
|
|
|
|
2023-07-22 18:44:33 -03:00
|
|
|
extern PyObject* _PyFunction_Vectorcall(
|
|
|
|
PyObject *func,
|
|
|
|
PyObject *const *stack,
|
|
|
|
size_t nargsf,
|
|
|
|
PyObject *kwnames);
|
|
|
|
|
2022-11-22 08:06:44 -04:00
|
|
|
#define FUNC_MAX_WATCHERS 8
|
|
|
|
|
2024-10-08 11:04:35 -03:00
|
|
|
#define FUNC_VERSION_UNSET 0
|
|
|
|
#define FUNC_VERSION_CLEARED 1
|
|
|
|
#define FUNC_VERSION_FIRST_VALID 2
|
|
|
|
|
2023-08-17 15:29:58 -03:00
|
|
|
#define FUNC_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
|
|
|
|
struct _func_version_cache_item {
|
|
|
|
PyFunctionObject *func;
|
|
|
|
PyObject *code;
|
|
|
|
};
|
|
|
|
|
2023-03-08 18:56:36 -04:00
|
|
|
struct _py_func_state {
|
2024-05-03 17:21:04 -03:00
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
// Protects next_version
|
|
|
|
PyMutex mutex;
|
|
|
|
#endif
|
|
|
|
|
2022-11-16 13:37:29 -04:00
|
|
|
uint32_t next_version;
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
// Borrowed references to function and code objects whose
|
2023-08-17 15:29:58 -03:00
|
|
|
// func_version % FUNC_VERSION_CACHE_SIZE
|
|
|
|
// once was equal to the index in the table.
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
// They are cleared when the function or code object is deallocated.
|
|
|
|
struct _func_version_cache_item func_version_cache[FUNC_VERSION_CACHE_SIZE];
|
2022-11-16 13:37:29 -04:00
|
|
|
};
|
|
|
|
|
2022-02-25 11:07:14 -04:00
|
|
|
extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr);
|
2021-11-23 05:53:24 -04:00
|
|
|
|
2024-10-08 11:04:35 -03:00
|
|
|
static inline int
|
|
|
|
_PyFunction_IsVersionValid(uint32_t version)
|
|
|
|
{
|
|
|
|
return version >= FUNC_VERSION_FIRST_VALID;
|
|
|
|
}
|
|
|
|
|
2022-02-25 11:07:14 -04:00
|
|
|
extern uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
|
2024-02-29 12:11:28 -04:00
|
|
|
PyAPI_FUNC(void) _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version);
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
void _PyFunction_ClearCodeByVersion(uint32_t version);
|
|
|
|
PyFunctionObject *_PyFunction_LookupByVersion(uint32_t version, PyObject **p_code);
|
2023-08-17 15:29:58 -03:00
|
|
|
|
2023-05-16 00:36:23 -03:00
|
|
|
extern PyObject *_Py_set_function_type_params(
|
|
|
|
PyThreadState* unused, PyObject *func, PyObject *type_params);
|
2021-11-23 05:53:24 -04:00
|
|
|
|
2022-02-25 11:07:14 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-23 05:53:24 -04:00
|
|
|
#endif /* !Py_INTERNAL_FUNCTION_H */
|