8000 gh-120323: Constant fold entire attribute loads by Fidget-Spinner · Pull Request #120344 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-120323: Constant fold entire attribute loads #120344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ typedef int(*PyType_WatchCallback)(PyTypeObject *);
PyAPI_FUNC(int) PyType_AddWatcher(PyType_WatchCallback callback);
PyAPI_FUNC(int) PyType_ClearWatcher(int watcher_id);
PyAPI_FUNC(int) PyType_Watch(int watcher_id, PyObject *type);
PyAPI_FUNC(int) PyType_IsWatched(int watcher_id, PyObject *type);
PyAPI_FUNC(int) PyType_Unwatch(int watcher_id, PyObject *type);

/* Attempt to assign a version tag to the given type.
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,16 @@ struct _Py_UOpsContext {
_Py_UopsSymbol **n_consumed;
_Py_UopsSymbol **limit;
_Py_UopsSymbol *locals_and_stack[MAX_ABSTRACT_INTERP_SIZE];

_PyBloomFilter *dependencies;
};

typedef struct _Py_UOpsContext _Py_UOpsContext;

extern bool _Py_uop_sym_is_null(_Py_UopsSymbol *sym);
extern bool _Py_uop_sym_is_not_null(_Py_UopsSymbol *sym);
extern bool _Py_uop_sym_is_const(_Py_UopsSymbol *sym);
extern bool _Py_uop_sym_is_a_class(_Py_UopsSymbol *sym);
extern PyObject *_Py_uop_sym_get_const(_Py_UopsSymbol *sym);
extern _Py_UopsSymbol *_Py_uop_sym_new_unknown(_Py_UOpsContext *ctx);
extern _Py_UopsSymbol *_Py_uop_sym_new_not_null(_Py_UOpsContext *ctx);
Expand All @@ -130,6 +133,7 @@ extern void _Py_uop_sym_set_non_null(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym);
extern void _Py_uop_sym_set_type(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyTypeObject *typ);
extern bool _Py_uop_sym_set_type_version(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, unsigned int version);
extern void _Py_uop_sym_set_const(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyObject *const_val);
extern void _Py_uop_sym_set_is_a_class(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym);
extern bool _Py_uop_sym_is_bottom(_Py_UopsSymbol *sym);
extern int _Py_uop_sym_truthiness(_Py_UopsSymbol *sym);
extern PyTypeObject *_Py_uop_sym_get_type(_Py_UopsSymbol *sym);
Expand All @@ -150,6 +154,12 @@ PyAPI_FUNC(PyObject *) _Py_uop_symbols_test(PyObject *self, PyObject *ignored);

PyAPI_FUNC(int) _PyOptimizer_Optimize(_PyInterpreterFrame *frame, _Py_CODEUNIT *start, PyObject **stack_pointer, _PyExecutorObject **exec_ptr);

/* The first two dict watcher IDs are reserved for CPython,
* so we don't need to check that they haven't been used */
#define BUILTINS_WATCHER_ID 0
#define GLOBALS_WATCHER_ID 1
#define TYPE_WATCHER_ID 0

#ifdef __cplusplus
}
#endif
Expand Down
52 changes: 27 additions & 25 deletions Include/internal/pycore_uop_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,5 +1481,25 @@ def fn(a):
fn(A())


def test_type_attribute_constant_propagated(self):
ns = {}
src = textwrap.dedent("""
class MyEnum:
A = 1

def testfunc(n):
for i in range(n):
x = MyEnum.A
y = MyEnum.A
""")
exec(src, ns, ns)
testfunc = ns['testfunc']
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD * 2)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertLessEqual(uops.count("_LOAD_ATTR_CLASS_0"), 1)
self.assertLessEqual(uops.count("_CHECK_ATTR_CLASS"), 1)


if __name__ == "__main__":
unittest.main()
11 changes: 11 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,17 @@ PyType_Watch(int watcher_id, PyObject* obj)
return 0;
}

int
PyType_IsWatched(int watcher_id, PyObject* obj)
{
if (!PyType_Check(obj)) {
PyErr_SetString(PyExc_ValueError, "Cannot check non-type");
return -1;
}
PyTypeObject *type = (PyTypeObject *)obj;
return type->tp_watched & (1 << watcher_id);
}

int
PyType_Unwatch(int watcher_id, PyObject* obj)
{
Expand Down
16 changes: 13 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2036,10 +2036,9 @@ dummy_func(
unused/5;

op(_CHECK_ATTR_CLASS, (type_version/2, owner -- owner)) {
DEOPT_IF(!PyType_Check(owner));
EXIT_IF(!PyType_Check(owner));
assert(type_version != 0);
DEOPT_IF(((PyTypeObject *)owner)->tp_version_tag != type_version);

EXIT_IF(((PyTypeObject *)owner)->tp_version_tag != type_version);
}

split op(_LOAD_ATTR_CLASS, (descr/4, owner -- attr, null if (oparg & 1))) {
Expand Down Expand Up @@ -4239,6 +4238,11 @@ dummy_func(
value = Py_NewRef(ptr);
}

tier2 pure op (_POP_TOP_LOAD_CONST_INLINE, (ptr/4, pop -- value)) {
Py_DECREF(pop);
value = Py_NewRef(ptr);
}

tier2 pure op(_LOAD_CONST_INLINE_BORROW, (ptr/4 -- value)) {
value = ptr;
}
Expand All @@ -4253,6 +4257,12 @@ dummy_func(
null = NULL;
}

tier2 pure op(_POP_TOP_LOAD_CONST_INLINE_WITH_NULL, (ptr/4, pop -- value, null)) {
Py_DECREF(pop);
value = Py_NewRef(ptr);
null = NULL;
}

tier2 pure op(_LOAD_CONST_INLINE_BORROW_WITH_NULL, (ptr/4 -- value, null)) {
value = ptr;
null = NULL;
Expand Down
26 changes: 26 additions & 0 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading
0