Skip to content

Commit 511733c

Browse files
committed
Add no_save_ip annotation to indicate that an instruction shouldn't save the IP to frame->instr_ptr
1 parent 29da92e commit 511733c

File tree

11 files changed

+31
-15
lines changed

11 files changed

+31
-15
lines changed

Include/internal/pycore_opcode_metadata.h

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

Include/internal/pycore_uop_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
#define specializing
6161
#define split
6262
#define replicate(TIMES)
63+
#define tier1
64+
#define no_save_ip
6365

6466
// Dummy variables for stack effects.
6567
static PyObject *value, *value1, *value2, *left, *right, *res, *sum, *prod, *sub;
@@ -336,19 +338,18 @@ dummy_func(
336338
res = PyStackRef_NULL;
337339
}
338340

339-
inst(END_FOR, (value -- )) {
341+
no_save_ip inst(END_FOR, (value -- )) {
340342
/* Don't update instr_ptr, so that POP_ITER sees
341343
* the FOR_ITER as the previous instruction.
342344
* This has the benign side effect that if value is
343345
* finalized it will see the location as the FOR_ITER's.
344346
*/
345-
frame->instr_ptr = prev_instr;
346347
PyStackRef_CLOSE(value);
347348
}
348349

349350
macro(POP_ITER) = POP_TOP;
350351

351-
tier1 inst(INSTRUMENTED_END_FOR, (receiver, value -- receiver)) {
352+
no_save_ip tier1 inst(INSTRUMENTED_END_FOR, (receiver, value -- receiver)) {
352353
/* Need to create a fake StopIteration error here,
353354
* to conform to PEP 380 */
354355
if (PyStackRef_GenCheck(receiver)) {

Python/executor_cases.c.h

Lines changed: 0 additions & 1 deletion
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: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,14 @@ translate_bytecode_to_trace(
622622
goto done;
623623
}
624624
assert(opcode != ENTER_EXECUTOR && opcode != EXTENDED_ARG);
625-
RESERVE_RAW(2, "_CHECK_VALIDITY_AND_SET_IP");
626-
ADD_TO_TRACE(_CHECK_VALIDITY_AND_SET_IP, 0, (uintptr_t)instr, target);
625+
if (OPCODE_HAS_NO_SAVE_IP(opcode)) {
626+
RESERVE_RAW(2, "_CHECK_VALIDITY");
627+
ADD_TO_TRACE(_CHECK_VALIDITY, 0, 0, target);
628+
}
629+
else {
630+
RESERVE_RAW(2, "_CHECK_VALIDITY_AND_SET_IP");
631+
ADD_TO_TRACE(_CHECK_VALIDITY_AND_SET_IP, 0, (uintptr_t)instr, target);
632+
}
627633

628634
/* Special case the first instruction,
629635
* so that we can guarantee forward progress */

Tools/cases_generator/analyzer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Properties:
2727
oparg_and_1: bool = False
2828
const_oparg: int = -1
2929
needs_prev: bool = False
30+
no_save_ip: bool = False
3031

3132
def dump(self, indent: str) -> None:
3233
simple_properties = self.__dict__.copy()
@@ -60,6 +61,7 @@ def from_list(properties: list["Properties"]) -> "Properties":
6061
side_exit=any(p.side_exit for p in properties),
6162
pure=all(p.pure for p in properties),
6263
needs_prev=any(p.needs_prev for p in properties),
64+
no_save_ip=all(p.no_save_ip for p in properties),
6365
)
6466

6567
@property
@@ -87,6 +89,7 @@ def escapes(self) -> bool:
8789
has_free=False,
8890
side_exit=False,
8991
pure=True,
92+
no_save_ip=False,
9093
)
9194

9295

@@ -829,6 +832,7 @@ def compute_properties(op: parser.InstDef) -> Properties:
829832
and not has_free,
830833
has_free=has_free,
831834
pure="pure" in op.annotations,
835+
no_save_ip="no_save_ip" in op.annotations,
832836
tier=tier_variable(op),
833837
needs_prev=variable_used(op, "prev_instr"),
834838
)

Tools/cases_generator/generators_common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ def cflags(p: Properties) -> str:
583583
flags.append("HAS_ESCAPES_FLAG")
584584
if p.pure:
585585
flags.append("HAS_PURE_FLAG")
586+
if p.no_save_ip:
587+
flags.append("HAS_NO_SAVE_IP_FLAG")
586588
if p.oparg_and_1:
587589
flags.append("HAS_OPARG_AND_1_FLAG")
588590
if flags:

Tools/cases_generator/lexer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def choice(*opts: str) -> str:
226226
"replicate",
227227
"tier1",
228228
"tier2",
229+
"no_save_ip",
229230
}
230231

231232
__all__ = []

Tools/cases_generator/opcode_metadata_generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"PASSTHROUGH",
5454
"OPARG_AND_1",
5555
"ERROR_NO_POP",
56+
"NO_SAVE_IP",
5657
]
5758

5859

0 commit comments

Comments
 (0)