Skip to content

Commit 89cb71e

Browse files
committed
Add POP_DEAD_INPUTS
1 parent 30962c4 commit 89cb71e

File tree

6 files changed

+93
-6
lines changed

6 files changed

+93
-6
lines changed

Lib/test/test_generated_cases.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,80 @@ def test_escaping_call_next_to_cmacro(self):
16391639
"""
16401640
self.run_cases_test(input, output)
16411641

1642+
def test_pop_dead_inputs_all_live(self):
1643+
input = """
1644+
inst(OP, (a, b --)) {
1645+
POP_DEAD_INPUTS();
1646+
HAM(a, b);
1647+
INPUTS_DEAD();
1648+
}
1649+
"""
1650+
output = """
1651+
TARGET(OP) {
1652+
frame->instr_ptr = next_instr;
1653+
next_instr += 1;
1654+
INSTRUCTION_STATS(OP);
1655+
_PyStackRef a;
1656+
_PyStackRef b;
1657+
b = stack_pointer[-1];
1658+
a = stack_pointer[-2];
1659+
HAM(a, b);
1660+
stack_pointer += -2;
1661+
assert(WITHIN_STACK_BOUNDS());
1662+
DISPATCH();
1663+
}
1664+
"""
1665+
self.run_cases_test(input, output)
1666+
1667+
def test_pop_dead_inputs_some_live(self):
1668+
input = """
1669+
inst(OP, (a, b, c --)) {
1670+
POP_DEAD_INPUTS();
1671+
HAM(a);
1672+
INPUTS_DEAD();
1673+
}
1674+
"""
1675+
output = """
1676+
TARGET(OP) {
1677+
frame->instr_ptr = next_instr;
1678+
next_instr += 1;
1679+
INSTRUCTION_STATS(OP);
1680+
_PyStackRef a;
1681+
a = stack_pointer[-3];
1682+
stack_pointer += -2;
1683+
assert(WITHIN_STACK_BOUNDS());
1684+
HAM(a);
1685+
stack_pointer += -1;
1686+
assert(WITHIN_STACK_BOUNDS());
1687+
DISPATCH();
1688+
}
1689+
"""
1690+
self.run_cases_test(input, output)
1691+
1692+
def test_pop_dead_inputs_with_output(self):
1693+
input = """
1694+
inst(OP, (a, b -- c)) {
1695+
POP_DEAD_INPUTS();
1696+
c = SPAM();
1697+
}
1698+
"""
1699+
output = """
1700+
TARGET(OP) {
1701+
frame->instr_ptr = next_instr;
1702+
next_instr += 1;
1703+
INSTRUCTION_STATS(OP);
1704+
_PyStackRef c;
1705+
stack_pointer += -2;
1706+
assert(WITHIN_STACK_BOUNDS());
1707+
c = SPAM();
1708+
stack_pointer[0] = c;
1709+
stack_pointer += 1;
1710+
assert(WITHIN_STACK_BOUNDS());
1711+
DISPATCH();
1712+
}
1713+
"""
1714+
self.run_cases_test(input, output)
1715+
16421716

16431717
class TestGeneratedAbstractCases(unittest.TestCase):
16441718
def setUp(self) -> None:

Python/bytecodes.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,8 +2174,7 @@ dummy_func(
21742174
PyObject *attr_o = FT_ATOMIC_LOAD_PTR_RELAXED(ep->me_value);
21752175
DEAD(mod_keys);
21762176
// Clear mod_keys from stack in case we need to deopt
2177-
SAVE_STACK();
2178-
RELOAD_STACK();
2177+
POP_DEAD_INPUTS();
21792178
DEOPT_IF(attr_o == NULL);
21802179
#ifdef Py_GIL_DISABLED
21812180
int increfed = _Py_TryIncrefCompareStackRef(&ep->me_value, attr_o, &attr);

Python/executor_cases.c.h

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/cases_generator/generators_common.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def __init__(self, out: CWriter):
120120
"PyStackRef_AsPyObjectSteal": self.stackref_steal,
121121
"DISPATCH": self.dispatch,
122122
"INSTRUCTION_SIZE": self.instruction_size,
123+
"POP_DEAD_INPUTS": self.pop_dead_inputs,
123124
}
124125
self.out = out
125126

@@ -348,6 +349,19 @@ def save_stack(
348349
self.emit_save(storage)
349350
return True
350351

352+
def pop_dead_inputs(
353+
self,
354+
tkn: Token,
355+
tkn_iter: TokenIterator,
356+
uop: Uop,
357+
storage: Storage,
358+
inst: Instruction | None,
359+
) -> None:
360+
next(tkn_iter)
361+
next(tkn_iter)
362+
next(tkn_iter)
363+
storage.pop_dead_inputs(self.out)
364+
351365
def emit_reload(self, storage: Storage) -> None:
352366
storage.reload(self.out)
353367
self._print_storage(storage)

Tools/cases_generator/stack.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,10 @@ def flush(self, out: CWriter, cast_type: str = "uintptr_t", extract_bits: bool =
512512
self._push_defined_outputs()
513513
self.stack.flush(out, cast_type, extract_bits)
514514

515+
def pop_dead_inputs(self, out: CWriter, cast_type: str = "uintptr_t", extract_bits: bool = True) -> None:
516+
self.clear_dead_inputs()
517+
self.stack.flush(out, cast_type, extract_bits)
518+
515519
def save(self, out: CWriter) -> None:
516520
assert self.spilled >= 0
517521
if self.spilled == 0:

0 commit comments

Comments
 (0)