summaryrefslogtreecommitdiffstats
path: root/Python/optimizer_bytecodes.c
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2024-02-27 13:25:02 (GMT)
committerGitHub <noreply@github.com>2024-02-27 13:25:02 (GMT)
commit6ecfcfe8946cf701c6c7018e30ae54d8b7a7ac2a (patch)
tree866ef77e88de4f69c85fda6d61a43bc17f1ccd2f /Python/optimizer_bytecodes.c
parent10fbcd6c5dc25bfe14e02fd93ef93498a393860c (diff)
downloadcpython-6ecfcfe8946cf701c6c7018e30ae54d8b7a7ac2a.zip
cpython-6ecfcfe8946cf701c6c7018e30ae54d8b7a7ac2a.tar.gz
cpython-6ecfcfe8946cf701c6c7018e30ae54d8b7a7ac2a.tar.bz2
GH-115816: Assorted naming and formatting changes to improve maintainability. (GH-115987)
* Rename _Py_UOpsAbstractInterpContext to _Py_UOpsContext and _Py_UOpsSymType to _Py_UopsSymbol. * #define shortened form of _Py_uop_... names for improved readability.
Diffstat (limited to 'Python/optimizer_bytecodes.c')
-rw-r--r--Python/optimizer_bytecodes.c213
1 files changed, 115 insertions, 98 deletions
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index e6e41f8..6873738 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -1,29 +1,46 @@
#include "Python.h"
+#include "pycore_optimizer.h"
#include "pycore_uops.h"
#include "pycore_uop_ids.h"
#include "internal/pycore_moduleobject.h"
#define op(name, ...) /* NAME is ignored */
-typedef struct _Py_UOpsSymType _Py_UOpsSymType;
-typedef struct _Py_UOpsAbstractInterpContext _Py_UOpsAbstractInterpContext;
+typedef struct _Py_UopsSymbol _Py_UopsSymbol;
+typedef struct _Py_UOpsContext _Py_UOpsContext;
typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
+/* Shortened forms for convenience */
+#define sym_is_not_null _Py_uop_sym_is_not_null
+#define sym_is_const _Py_uop_sym_is_const
+#define sym_get_const _Py_uop_sym_get_const
+#define sym_new_unknown _Py_uop_sym_new_unknown
+#define sym_new_not_null _Py_uop_sym_new_not_null
+#define sym_new_type _Py_uop_sym_new_type
+#define sym_is_null _Py_uop_sym_is_null
+#define sym_new_const _Py_uop_sym_new_const
+#define sym_new_null _Py_uop_sym_new_null
+#define sym_matches_type _Py_uop_sym_matches_type
+#define sym_set_null _Py_uop_sym_set_null
+#define sym_set_type _Py_uop_sym_set_type
+#define frame_new _Py_uop_frame_new
+#define frame_pop _Py_uop_frame_pop
+
static int
dummy_func(void) {
PyCodeObject *code;
int oparg;
- _Py_UOpsSymType *flag;
- _Py_UOpsSymType *left;
- _Py_UOpsSymType *right;
- _Py_UOpsSymType *value;
- _Py_UOpsSymType *res;
- _Py_UOpsSymType *iter;
- _Py_UOpsSymType *top;
- _Py_UOpsSymType *bottom;
+ _Py_UopsSymbol *flag;
+ _Py_UopsSymbol *left;
+ _Py_UopsSymbol *right;
+ _Py_UopsSymbol *value;
+ _Py_UopsSymbol *res;
+ _Py_UopsSymbol *iter;
+ _Py_UopsSymbol *top;
+ _Py_UopsSymbol *bottom;
_Py_UOpsAbstractFrame *frame;
- _Py_UOpsAbstractInterpContext *ctx;
+ _Py_UOpsContext *ctx;
_PyUOpInstruction *this_instr;
_PyBloomFilter *dependencies;
int modified;
@@ -33,7 +50,7 @@ dummy_func(void) {
op(_LOAD_FAST_CHECK, (-- value)) {
value = GETLOCAL(oparg);
// We guarantee this will error - just bail and don't optimize it.
- if (_Py_uop_sym_is_null(value)) {
+ if (sym_is_null(value)) {
goto out_of_space;
}
}
@@ -44,8 +61,8 @@ dummy_func(void) {
op(_LOAD_FAST_AND_CLEAR, (-- value)) {
value = GETLOCAL(oparg);
- _Py_UOpsSymType *temp;
- OUT_OF_SPACE_IF_NULL(temp = _Py_uop_sym_new_null(ctx));
+ _Py_UopsSymbol *temp;
+ OUT_OF_SPACE_IF_NULL(temp = sym_new_null(ctx));
GETLOCAL(oparg) = temp;
}
@@ -54,147 +71,147 @@ dummy_func(void) {
}
op(_PUSH_NULL, (-- res)) {
- res = _Py_uop_sym_new_null(ctx);
+ res = sym_new_null(ctx);
if (res == NULL) {
goto out_of_space;
};
}
op(_GUARD_BOTH_INT, (left, right -- left, right)) {
- if (_Py_uop_sym_matches_type(left, &PyLong_Type) &&
- _Py_uop_sym_matches_type(right, &PyLong_Type)) {
+ if (sym_matches_type(left, &PyLong_Type) &&
+ sym_matches_type(right, &PyLong_Type)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
- _Py_uop_sym_set_type(left, &PyLong_Type);
- _Py_uop_sym_set_type(right, &PyLong_Type);
+ sym_set_type(left, &PyLong_Type);
+ sym_set_type(right, &PyLong_Type);
}
op(_GUARD_BOTH_FLOAT, (left, right -- left, right)) {
- if (_Py_uop_sym_matches_type(left, &PyFloat_Type) &&
- _Py_uop_sym_matches_type(right, &PyFloat_Type)) {
+ if (sym_matches_type(left, &PyFloat_Type) &&
+ sym_matches_type(right, &PyFloat_Type)) {
REPLACE_OP(this_instr, _NOP, 0 ,0);
}
- _Py_uop_sym_set_type(left, &PyFloat_Type);
- _Py_uop_sym_set_type(right, &PyFloat_Type);
+ sym_set_type(left, &PyFloat_Type);
+ sym_set_type(right, &PyFloat_Type);
}
op(_GUARD_BOTH_UNICODE, (left, right -- left, right)) {
- if (_Py_uop_sym_matches_type(left, &PyUnicode_Type) &&
- _Py_uop_sym_matches_type(right, &PyUnicode_Type)) {
+ if (sym_matches_type(left, &PyUnicode_Type) &&
+ sym_matches_type(right, &PyUnicode_Type)) {
REPLACE_OP(this_instr, _NOP, 0 ,0);
}
- _Py_uop_sym_set_type(left, &PyUnicode_Type);
- _Py_uop_sym_set_type(right, &PyUnicode_Type);
+ sym_set_type(left, &PyUnicode_Type);
+ sym_set_type(right, &PyUnicode_Type);
}
op(_BINARY_OP_ADD_INT, (left, right -- res)) {
- if (_Py_uop_sym_is_const(left) && _Py_uop_sym_is_const(right)) {
- assert(PyLong_CheckExact(_Py_uop_sym_get_const(left)));
- assert(PyLong_CheckExact(_Py_uop_sym_get_const(right)));
- PyObject *temp = _PyLong_Add((PyLongObject *)_Py_uop_sym_get_const(left),
- (PyLongObject *)_Py_uop_sym_get_const(right));
+ if (sym_is_const(left) && sym_is_const(right)) {
+ assert(PyLong_CheckExact(sym_get_const(left)));
+ assert(PyLong_CheckExact(sym_get_const(right)));
+ PyObject *temp = _PyLong_Add((PyLongObject *)sym_get_const(left),
+ (PyLongObject *)sym_get_const(right));
if (temp == NULL) {
goto error;
}
- OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_const(ctx, temp));
+ OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
}
else {
- OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_type(ctx, &PyLong_Type));
+ OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type));
}
}
op(_BINARY_OP_SUBTRACT_INT, (left, right -- res)) {
- if (_Py_uop_sym_is_const(left) && _Py_uop_sym_is_const(right)) {
- assert(PyLong_CheckExact(_Py_uop_sym_get_const(left)));
- assert(PyLong_CheckExact(_Py_uop_sym_get_const(right)));
- PyObject *temp = _PyLong_Subtract((PyLongObject *)_Py_uop_sym_get_const(left),
- (PyLongObject *)_Py_uop_sym_get_const(right));
+ if (sym_is_const(left) && sym_is_const(right)) {
+ assert(PyLong_CheckExact(sym_get_const(left)));
+ assert(PyLong_CheckExact(sym_get_const(right)));
+ PyObject *temp = _PyLong_Subtract((PyLongObject *)sym_get_const(left),
+ (PyLongObject *)sym_get_const(right));
if (temp == NULL) {
goto error;
}
- OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_const(ctx, temp));
+ OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
}
else {
- OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_type(ctx, &PyLong_Type));
+ OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type));
}
}
op(_BINARY_OP_MULTIPLY_INT, (left, right -- res)) {
- if (_Py_uop_sym_is_const(left) && _Py_uop_sym_is_const(right)) {
- assert(PyLong_CheckExact(_Py_uop_sym_get_const(left)));
- assert(PyLong_CheckExact(_Py_uop_sym_get_const(right)));
- PyObject *temp = _PyLong_Multiply((PyLongObject *)_Py_uop_sym_get_const(left),
- (PyLongObject *)_Py_uop_sym_get_const(right));
+ if (sym_is_const(left) && sym_is_const(right)) {
+ assert(PyLong_CheckExact(sym_get_const(left)));
+ assert(PyLong_CheckExact(sym_get_const(right)));
+ PyObject *temp = _PyLong_Multiply((PyLongObject *)sym_get_const(left),
+ (PyLongObject *)sym_get_const(right));
if (temp == NULL) {
goto error;
}
- OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_const(ctx, temp));
+ OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
}
else {
- OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_type(ctx, &PyLong_Type));
+ OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type));
}
}
op(_BINARY_OP_ADD_FLOAT, (left, right -- res)) {
- if (_Py_uop_sym_is_const(left) && _Py_uop_sym_is_const(right)) {
- assert(PyFloat_CheckExact(_Py_uop_sym_get_const(left)));
- assert(PyFloat_CheckExact(_Py_uop_sym_get_const(right)));
+ if (sym_is_const(left) && sym_is_const(right)) {
+ assert(PyFloat_CheckExact(sym_get_const(left)));
+ assert(PyFloat_CheckExact(sym_get_const(right)));
PyObject *temp = PyFloat_FromDouble(
- PyFloat_AS_DOUBLE(_Py_uop_sym_get_const(left)) +
- PyFloat_AS_DOUBLE(_Py_uop_sym_get_const(right)));
+ PyFloat_AS_DOUBLE(sym_get_const(left)) +
+ PyFloat_AS_DOUBLE(sym_get_const(right)));
if (temp == NULL) {
goto error;
}
- res = _Py_uop_sym_new_const(ctx, temp);
+ res = sym_new_const(ctx, temp);
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
}
else {
- OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_type(ctx, &PyFloat_Type));
+ OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type));
}
}
op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res)) {
- if (_Py_uop_sym_is_const(left) && _Py_uop_sym_is_const(right)) {
- assert(PyFloat_CheckExact(_Py_uop_sym_get_const(left)));
- assert(PyFloat_CheckExact(_Py_uop_sym_get_const(right)));
+ if (sym_is_const(left) && sym_is_const(right)) {
+ assert(PyFloat_CheckExact(sym_get_const(left)));
+ assert(PyFloat_CheckExact(sym_get_const(right)));
PyObject *temp = PyFloat_FromDouble(
- PyFloat_AS_DOUBLE(_Py_uop_sym_get_const(left)) -
- PyFloat_AS_DOUBLE(_Py_uop_sym_get_const(right)));
+ PyFloat_AS_DOUBLE(sym_get_const(left)) -
+ PyFloat_AS_DOUBLE(sym_get_const(right)));
if (temp == NULL) {
goto error;
}
- res = _Py_uop_sym_new_const(ctx, temp);
+ res = sym_new_const(ctx, temp);
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
}
else {
- OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_type(ctx, &PyFloat_Type));
+ OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type));
}
}
op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
- if (_Py_uop_sym_is_const(left) && _Py_uop_sym_is_const(right)) {
- assert(PyFloat_CheckExact(_Py_uop_sym_get_const(left)));
- assert(PyFloat_CheckExact(_Py_uop_sym_get_const(right)));
+ if (sym_is_const(left) && sym_is_const(right)) {
+ assert(PyFloat_CheckExact(sym_get_const(left)));
+ assert(PyFloat_CheckExact(sym_get_const(right)));
PyObject *temp = PyFloat_FromDouble(
- PyFloat_AS_DOUBLE(_Py_uop_sym_get_const(left)) *
- PyFloat_AS_DOUBLE(_Py_uop_sym_get_const(right)));
+ PyFloat_AS_DOUBLE(sym_get_const(left)) *
+ PyFloat_AS_DOUBLE(sym_get_const(right)));
if (temp == NULL) {
goto error;
}
- res = _Py_uop_sym_new_const(ctx, temp);
+ res = sym_new_const(ctx, temp);
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
}
else {
- OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_type(ctx, &PyFloat_Type));
+ OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type));
}
}
@@ -205,21 +222,21 @@ dummy_func(void) {
}
op(_LOAD_CONST_INLINE, (ptr/4 -- value)) {
- OUT_OF_SPACE_IF_NULL(value = _Py_uop_sym_new_const(ctx, ptr));
+ OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr));
}
op(_LOAD_CONST_INLINE_BORROW, (ptr/4 -- value)) {
- OUT_OF_SPACE_IF_NULL(value = _Py_uop_sym_new_const(ctx, ptr));
+ OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr));
}
op(_LOAD_CONST_INLINE_WITH_NULL, (ptr/4 -- value, null)) {
- OUT_OF_SPACE_IF_NULL(value = _Py_uop_sym_new_const(ctx, ptr));
- OUT_OF_SPACE_IF_NULL(null = _Py_uop_sym_new_null(ctx));
+ OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr));
+ OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx));
}
op(_LOAD_CONST_INLINE_BORROW_WITH_NULL, (ptr/4 -- value, null)) {
- OUT_OF_SPACE_IF_NULL(value = _Py_uop_sym_new_const(ctx, ptr));
- OUT_OF_SPACE_IF_NULL(null = _Py_uop_sym_new_null(ctx));
+ OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr));
+ OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx));
}
@@ -240,8 +257,8 @@ dummy_func(void) {
op(_CHECK_ATTR_MODULE, (dict_version/2, owner -- owner)) {
(void)dict_version;
- if (_Py_uop_sym_is_const(owner)) {
- PyObject *cnst = _Py_uop_sym_get_const(owner);
+ if (sym_is_const(owner)) {
+ PyObject *cnst = sym_get_const(owner);
if (PyModule_CheckExact(cnst)) {
PyModuleObject *mod = (PyModuleObject *)cnst;
PyObject *dict = mod->md_dict;
@@ -257,23 +274,23 @@ dummy_func(void) {
op(_LOAD_ATTR_MODULE, (index/1, owner -- attr, null if (oparg & 1))) {
(void)index;
- OUT_OF_SPACE_IF_NULL(null = _Py_uop_sym_new_null(ctx));
+ OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx));
attr = NULL;
if (this_instr[-1].opcode == _NOP) {
// Preceding _CHECK_ATTR_MODULE was removed: mod is const and dict is watched.
- assert(_Py_uop_sym_is_const(owner));
- PyModuleObject *mod = (PyModuleObject *)_Py_uop_sym_get_const(owner);
+ assert(sym_is_const(owner));
+ PyModuleObject *mod = (PyModuleObject *)sym_get_const(owner);
assert(PyModule_CheckExact(mod));
PyObject *dict = mod->md_dict;
PyObject *res = convert_global_to_const(this_instr, dict);
if (res != NULL) {
this_instr[-1].opcode = _POP_TOP;
- OUT_OF_SPACE_IF_NULL(attr = _Py_uop_sym_new_const(ctx, res));
+ OUT_OF_SPACE_IF_NULL(attr = sym_new_const(ctx, res));
}
}
if (attr == NULL) {
/* No conversion made. We don't know what `attr` is. */
- OUT_OF_SPACE_IF_NULL(attr = _Py_uop_sym_new_not_null(ctx));
+ OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx));
}
}
@@ -297,38 +314,38 @@ dummy_func(void) {
op(_LOAD_ATTR_METHOD_WITH_VALUES, (descr/4, owner -- attr, self if (1))) {
(void)descr;
- OUT_OF_SPACE_IF_NULL(attr = _Py_uop_sym_new_not_null(ctx));
+ OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx));
self = owner;
}
op(_LOAD_ATTR_METHOD_NO_DICT, (descr/4, owner -- attr, self if (1))) {
(void)descr;
- OUT_OF_SPACE_IF_NULL(attr = _Py_uop_sym_new_not_null(ctx));
+ OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx));
self = owner;
}
op(_LOAD_ATTR_METHOD_LAZY_DICT, (descr/4, owner -- attr, self if (1))) {
(void)descr;
- OUT_OF_SPACE_IF_NULL(attr = _Py_uop_sym_new_not_null(ctx));
+ OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx));
self = owner;
}
op(_INIT_CALL_BOUND_METHOD_EXACT_ARGS, (callable, unused, unused[oparg] -- func, self, unused[oparg])) {
(void)callable;
- OUT_OF_SPACE_IF_NULL(func = _Py_uop_sym_new_not_null(ctx));
- OUT_OF_SPACE_IF_NULL(self = _Py_uop_sym_new_not_null(ctx));
+ OUT_OF_SPACE_IF_NULL(func = sym_new_not_null(ctx));
+ OUT_OF_SPACE_IF_NULL(self = sym_new_not_null(ctx));
}
op(_CHECK_FUNCTION_EXACT_ARGS, (func_version/2, callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
- _Py_uop_sym_set_type(callable, &PyFunction_Type);
+ sym_set_type(callable, &PyFunction_Type);
(void)self_or_null;
(void)func_version;
}
op(_CHECK_CALL_BOUND_METHOD_EXACT_ARGS, (callable, null, unused[oparg] -- callable, null, unused[oparg])) {
- _Py_uop_sym_set_null(null);
- _Py_uop_sym_set_type(callable, &PyMethod_Type);
+ sym_set_null(null);
+ sym_set_type(callable, &PyMethod_Type);
}
op(_INIT_CALL_PY_EXACT_ARGS, (callable, self_or_null, args[oparg] -- new_frame: _Py_UOpsAbstractFrame *)) {
@@ -344,29 +361,29 @@ dummy_func(void) {
assert(self_or_null != NULL);
assert(args != NULL);
- if (_Py_uop_sym_is_not_null(self_or_null)) {
+ if (sym_is_not_null(self_or_null)) {
// Bound method fiddling, same as _INIT_CALL_PY_EXACT_ARGS in VM
args--;
argcount++;
}
- _Py_UOpsSymType **localsplus_start = ctx->n_consumed;
+ _Py_UopsSymbol **localsplus_start = ctx->n_consumed;
int n_locals_already_filled = 0;
// Can determine statically, so we interleave the new locals
// and make the current stack the new locals.
// This also sets up for true call inlining.
- if (_Py_uop_sym_is_null(self_or_null) || _Py_uop_sym_is_not_null(self_or_null)) {
+ if (sym_is_null(self_or_null) || sym_is_not_null(self_or_null)) {
localsplus_start = args;
n_locals_already_filled = argcount;
}
OUT_OF_SPACE_IF_NULL(new_frame =
- _Py_uop_ctx_frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0));
+ frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0));
}
op(_POP_FRAME, (retval -- res)) {
SYNC_SP();
ctx->frame->stack_pointer = stack_pointer;
- _Py_uop_ctx_frame_pop(ctx);
+ frame_pop(ctx);
stack_pointer = ctx->frame->stack_pointer;
res = retval;
}
@@ -383,7 +400,7 @@ dummy_func(void) {
/* This has to be done manually */
(void)seq;
for (int i = 0; i < oparg; i++) {
- OUT_OF_SPACE_IF_NULL(values[i] = _Py_uop_sym_new_unknown(ctx));
+ OUT_OF_SPACE_IF_NULL(values[i] = sym_new_unknown(ctx));
}
}
@@ -392,12 +409,12 @@ dummy_func(void) {
(void)seq;
int totalargs = (oparg & 0xFF) + (oparg >> 8) + 1;
for (int i = 0; i < totalargs; i++) {
- OUT_OF_SPACE_IF_NULL(values[i] = _Py_uop_sym_new_unknown(ctx));
+ OUT_OF_SPACE_IF_NULL(values[i] = sym_new_unknown(ctx));
}
}
op(_ITER_NEXT_RANGE, (iter -- iter, next)) {
- OUT_OF_SPACE_IF_NULL(next = _Py_uop_sym_new_type(ctx, &PyLong_Type));
+ OUT_OF_SPACE_IF_NULL(next = sym_new_type(ctx, &PyLong_Type));
(void)iter;
}