Skip to content

Commit 737095e

Browse files
committed
Address Irit's review agian
1 parent 79d68e0 commit 737095e

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

Lib/test/test_peepholer.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,8 +2509,43 @@ def test_fold_constant_big_set_for_iter(self):
25092509
("LOAD_CONST", 0, 14),
25102510
("RETURN_VALUE", None, 15),
25112511
]
2512-
self.cfg_optimization_test(before, after, consts=[None],
2513-
expected_consts=[None, frozenset({1, 2, 3})])
2512+
self.cfg_optimization_test(before, after, consts=["test"],
2513+
expected_consts=["test", frozenset({1, 2, 3})])
2514+
2515+
def test_fold_constant_list_to_tuple_for_iter(self):
2516+
INTRINSIC_LIST_TO_TUPLE = 6
2517+
before = [
2518+
("BUILD_LIST", 0, 1),
2519+
("LOAD_SMALL_INT", 1, 2), ("LIST_APPEND", 1, 3),
2520+
("LOAD_SMALL_INT", 2, 4), ("LIST_APPEND", 1, 5),
2521+
("LOAD_SMALL_INT", 3, 6), ("LIST_APPEND", 1, 7),
2522+
("CALL_INTRINSIC_1", INTRINSIC_LIST_TO_TUPLE, 8),
2523+
("GET_ITER", 0, 9),
2524+
top := self.Label(),
2525+
("FOR_ITER", end := self.Label(), 10),
2526+
("STORE_FAST", 0, 11),
2527+
("JUMP", top, 12),
2528+
end,
2529+
("END_FOR", None, 13),
2530+
("POP_ITER", None, 14),
2531+
("LOAD_CONST", 0, 15),
2532+
("RETURN_VALUE", None, 16),
2533+
]
2534+
after = [
2535+
("LOAD_CONST", 1, 8),
2536+
("GET_ITER", 0, 9),
2537+
top := self.Label(),
2538+
("FOR_ITER", end := self.Label(), 10),
2539+
("STORE_FAST", 0, 11),
2540+
("JUMP", top, 12),
2541+
end,
2542+
("END_FOR", None, 13),
2543+
("POP_ITER", None, 14),
2544+
("LOAD_CONST", 0, 15),
2545+
("RETURN_VALUE", None, 16),
2546+
]
2547+
self.cfg_optimization_test(before, after, consts=["test"],
2548+
expected_consts=["test", (1, 2, 3)])
25142549

25152550
def test_fold_constant_big_list_contains_op(self):
25162551
# x in [c1, c2, ..., cN] (N > 30) should fold to LOAD_CONST tuple
@@ -2569,7 +2604,7 @@ def test_no_fold_big_list_for_iter_with_non_const(self):
25692604
("LOAD_CONST", 0, 14),
25702605
("RETURN_VALUE", None, 15),
25712606
]
2572-
self.cfg_optimization_test(same, same, consts=[None])
2607+
self.cfg_optimization_test(same, same, consts=["test"])
25732608

25742609

25752610
class OptimizeLoadFastTestCase(DirectCfgOptimizerTests):

Python/flowgraph.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,12 +2530,10 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts,
25302530
break;
25312531
case CALL_INTRINSIC_1:
25322532
if (oparg == INTRINSIC_LIST_TO_TUPLE) {
2533-
if (nextop == GET_ITER) {
2533+
RETURN_IF_ERROR(fold_constant_seq_into_load_const(bb, i, consts, const_cache, consts_index));
2534+
if (inst->i_opcode == CALL_INTRINSIC_1 && nextop == GET_ITER) {
25342535
INSTR_SET_OP0(inst, NOP);
25352536
}
2536-
else {
2537-
RETURN_IF_ERROR(fold_constant_seq_into_load_const(bb, i, consts, const_cache, consts_index));
2538-
}
25392537
}
25402538
else if (oparg == INTRINSIC_UNARY_POSITIVE) {
25412539
RETURN_IF_ERROR(fold_const_unaryop(bb, i, consts, const_cache, consts_index));

0 commit comments

Comments
 (0)