gh-104635: Eliminate redundant STORE_FAST instructions in the compiler (gh-105320)

This commit is contained in:
Dong-hee Na 2023-06-08 08:39:56 +09:00 committed by GitHub
parent ffeaec7e60
commit aa5b762bd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 10 deletions

View File

@ -808,8 +808,9 @@ dis_extended_arg_quick_code = """\
%3d 2 LOAD_CONST 1 (Ellipsis) %3d 2 LOAD_CONST 1 (Ellipsis)
4 EXTENDED_ARG 1 4 EXTENDED_ARG 1
6 UNPACK_EX 256 6 UNPACK_EX 256
8 STORE_FAST_STORE_FAST 0 (_, _) 8 POP_TOP
10 RETURN_CONST 0 (None) 10 STORE_FAST 0 (_)
12 RETURN_CONST 0 (None)
"""% (extended_arg_quick.__code__.co_firstlineno, """% (extended_arg_quick.__code__.co_firstlineno,
extended_arg_quick.__code__.co_firstlineno + 1,) extended_arg_quick.__code__.co_firstlineno + 1,)

View File

@ -1077,13 +1077,45 @@ class DirectCfgOptimizerTests(CfgOptimizationTestCase):
expected_insts = [ expected_insts = [
('LOAD_CONST', 0, 1), ('LOAD_CONST', 0, 1),
('LOAD_CONST', 1, 2), ('LOAD_CONST', 1, 2),
('LOAD_CONST', 2, 3), ('NOP', 0, 3),
('SWAP', 3, 4), ('STORE_FAST', 1, 4),
('STORE_FAST_STORE_FAST', 17, 4),
('POP_TOP', 0, 4), ('POP_TOP', 0, 4),
('RETURN_VALUE', 5) ('RETURN_VALUE', 5)
] ]
self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1) self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)
def test_dead_store_elimination_in_same_lineno(self):
insts = [
('LOAD_CONST', 0, 1),
('LOAD_CONST', 1, 2),
('LOAD_CONST', 2, 3),
('STORE_FAST', 1, 4),
('STORE_FAST', 1, 4),
('STORE_FAST', 1, 4),
('RETURN_VALUE', 5)
]
expected_insts = [
('LOAD_CONST', 0, 1),
('LOAD_CONST', 1, 2),
('NOP', 0, 3),
('POP_TOP', 0, 4),
('STORE_FAST', 1, 4),
('RETURN_VALUE', 5)
]
self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)
def test_no_dead_store_elimination_in_different_lineno(self):
insts = [
('LOAD_CONST', 0, 1),
('LOAD_CONST', 1, 2),
('LOAD_CONST', 2, 3),
('STORE_FAST', 1, 4),
('STORE_FAST', 1, 5),
('STORE_FAST', 1, 6),
('RETURN_VALUE', 5)
]
self.cfg_optimization_test(insts, insts, consts=list(range(3)), nlocals=1)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -0,0 +1,2 @@
Eliminate redundant :opcode:`STORE_FAST` instructions in the compiler. Patch
by Dong-hee Na and Carl Meyer.

View File

@ -1515,15 +1515,18 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
*/ */
} }
break; break;
case STORE_FAST:
if (opcode == nextop &&
oparg == bb->b_instr[i+1].i_oparg &&
bb->b_instr[i].i_loc.lineno == bb->b_instr[i+1].i_loc.lineno) {
bb->b_instr[i].i_opcode = POP_TOP;
bb->b_instr[i].i_oparg = 0;
}
break;
case SWAP: case SWAP:
if (oparg == 1) { if (oparg == 1) {
INSTR_SET_OP0(inst, NOP); INSTR_SET_OP0(inst, NOP);
break;
} }
if (swaptimize(bb, &i) < 0) {
goto error;
}
apply_static_swaps(bb, i);
break; break;
case KW_NAMES: case KW_NAMES:
break; break;
@ -1538,6 +1541,16 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
assert (!HAS_CONST(inst->i_opcode)); assert (!HAS_CONST(inst->i_opcode));
} }
} }
for (int i = 0; i < bb->b_iused; i++) {
cfg_instr *inst = &bb->b_instr[i];
if (inst->i_opcode == SWAP) {
if (swaptimize(bb, &i) < 0) {
goto error;
}
apply_static_swaps(bb, i);
}
}
return SUCCESS; return SUCCESS;
error: error:
return ERROR; return ERROR;