8000 bpo-43693: Compute deref offsets in compiler by markshannon · Pull Request #25152 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-43693: Compute deref offsets in compiler #25152

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

Merged
Merged
Prev Previous commit
Next Next commit
Remove LOAD_CLOSURE instruction.
  • Loading branch information
markshannon authored and ericsnowcurrently committed Jun 3, 2021
commit 4893684d42fe9abf700d8589e5b832916ed18893
1 change: 0 additions & 1 deletion Include/opcode.h

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

2 changes: 0 additions & 2 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ def jabs_op(name, op):
def_op('MAKE_FUNCTION', 132) # Flags
def_op('BUILD_SLICE', 133) # Number of items

def_op('LOAD_CLOSURE', 135)
hasfree.append(135)
def_op('LOAD_DEREF', 136)
hasfree.append(136)
def_op('STORE_DEREF', 137)
Expand Down
16 changes: 8 additions & 8 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def foo(x):
return foo

dis_nested_0 = """\
%3d 0 LOAD_CLOSURE 2 (y)
%3d 0 LOAD_FAST 2 (y)
2 BUILD_TUPLE 1
4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
6 LOAD_CONST 2 ('_h.<locals>.foo')
Expand All @@ -444,7 +444,7 @@ def foo(x):

dis_nested_1 = """%s
Disassembly of <code object foo at 0x..., file "%s", line %d>:
%3d 0 LOAD_CLOSURE 1 (x)
%3d 0 LOAD_FAST 1 (x)
2 BUILD_TUPLE 1
4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
Expand Down Expand Up @@ -962,8 +962,8 @@ def jumpy():
Instruction = dis.Instruction
expected_opinfo_outer = [
Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_CLOSURE', opcode=135, arg=4, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_FAST', opcode=124, arg=3, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_FAST', opcode=124, arg=4, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=8, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=10, starts_line=None, is_jump_target=False),
Expand All @@ -985,10 +985,10 @@ def jumpy():

expected_opinfo_f = [
Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
Instruction(opname='LOAD_CLOSURE', opcode=135, arg=5, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_CLOSURE', opcode=135, arg=6, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_CLOSURE', opcode=135, arg=4, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_FAST', opcode=124, arg=5, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_FAST', opcode=124, arg=6, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_FAST', opcode=124, arg=3, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_FAST', opcode=124, arg=4, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=12, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=14, starts_line=None, is_jump_target=False),
Expand Down
7 changes: 0 additions & 7 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3070,13 +3070,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
goto error;
}

case TARGET(LOAD_CLOSURE): {
PyObject *cell = GETLOCAL(oparg);
Py_INCREF(cell);
PUSH(cell);
DISPATCH();
}

case TARGET(LOAD_CLASSDEREF): {
PyObject *name, *value, *locals = LOCALS();
assert(locals);
Expand Down
18 changes: 11 additions & 7 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
#define SETUP_WITH 253
#define POP_BLOCK 252

/* Artificial instruction, will be converted to LOAD_FAST */
#define LOAD_CLOSURE 251

#define IS_TOP_LEVEL_AWAIT(c) ( \
(c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
&& (c->u->u_ste->ste_type == ModuleBlock))
Expand Down Expand Up @@ -1185,8 +1188,6 @@ stack_effect(int opcode, int oparg, int jump)
return -1;

/* Closures */
case LOAD_CLOSURE:
return 1;
case LOAD_DEREF:
case LOAD_CLASSDEREF:
return 1;
Expand Down Expand Up @@ -7436,17 +7437,19 @@ guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
}

static void
offset_derefs(struct assembler *a, int nlocals)
offset_derefs(basicblock *entryblock, int nlocals)
{
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
struct instr *inst = &b->b_instr[i];
switch(inst->i_opcode) {
case LOAD_CLOSURE:
inst->i_opcode = LOAD_FAST;
/* fall through */
case LOAD_DEREF:
case STORE_DEREF:
case DELETE_DEREF:
case LOAD_CLASSDEREF:
case LOAD_CLOSURE:
inst->i_oparg += nlocals;
}
}
Expand Down Expand Up @@ -7508,6 +7511,9 @@ assemble(struct compiler *c, int addNone)
a.a_entry = entryblock;
a.a_nblocks = nblocks;

assert(PyDict_GET_SIZE(c->u->u_varnames) < INT_MAX);
offset_derefs(entryblock, (int)PyDict_GET_SIZE(c->u->u_varnames));

consts = consts_dict_keys_inorder(c->u->u_consts);
if (consts == NULL) {
goto error;
Expand All @@ -7517,8 +7523,6 @@ assemble(struct compiler *c, int addNone)
}
guarantee_lineno_for_exits(&a, c->u->u_firstlineno);

offset_derefs(&a, (int)PyDict_GET_SIZE(c->u->u_varnames));

int maxdepth = stackdepth(c);
if (maxdepth < 0) {
goto error;
Expand Down
2 changes: 1 addition & 1 deletion Python/importlib.h

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

16 changes: 8 additions & 8 deletions Python/importlib_external.h

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

2 changes: 1 addition & 1 deletion Python/opcode_targets.h

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

0