Skip to content
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

gh-130480: Move LOAD_SMALL_INT optimization from codegen to CFG #130481

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

WolframAlph
Copy link
Contributor

@WolframAlph WolframAlph commented Feb 23, 2025

if (maybe_instr_make_load_smallint(inst, constant, consts, const_cache)) {
assert(inst->i_opcode == LOAD_SMALL_INT);
}
Py_DECREF(constant);
Copy link
Contributor Author

@WolframAlph WolframAlph Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For small ints this is effectively no-op as they are immortal, but it helps readability because get_const_value returns new reference.

if (inst->i_opcode == LOAD_CONST) {
PyObject *constant = get_const_value(inst->i_opcode, inst->i_oparg, consts);
if (maybe_instr_make_load_smallint(inst, constant, consts, const_cache)) {
assert(inst->i_opcode == LOAD_SMALL_INT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the assert needed? If so, maybe you can assert inside maybe_instr_make_load_smallint (then we could also get rid of the if)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It helps readability IMO, and performance-wise (if this is your concern), asserts are stripped out on non-debug builds so compiler should eliminate this branch.

@@ -1992,6 +2006,13 @@ basicblock_optimize_load_const(PyObject *const_cache, basicblock *bb, PyObject *
int oparg = 0;
for (int i = 0; i < bb->b_iused; i++) {
cfg_instr *inst = &bb->b_instr[i];
if (inst->i_opcode == LOAD_CONST) {
PyObject *constant = get_const_value(inst->i_opcode, inst->i_oparg, consts);
if (maybe_instr_make_load_smallint(inst, constant, consts, const_cache)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that having an if with just one assert feels a bit weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants