Skip to content

Commit e918cb2

Browse files
Move unpredictable jump detection to the cases generator
1 parent 8adaf4d commit e918cb2

File tree

8 files changed

+22
-51
lines changed

8 files changed

+22
-51
lines changed

Include/internal/pycore_interp_structs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,6 @@ typedef _Py_CODEUNIT *(*_PyJitEntryFuncPtr)(struct _PyExecutorObject *exec, _PyI
759759

760760
typedef struct _PyJitTracerState {
761761
bool dependencies_still_valid;
762-
bool dynamic_jump_taken;
763762
bool prev_instr_is_super;
764763
int code_max_size;
765764
int code_curr_size;

Include/internal/pycore_opcode_metadata.h

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

Python/ceval_macros.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,6 @@ GETITEM(PyObject *v, Py_ssize_t i) {
333333
#define RECORD_BRANCH_TAKEN(bitset, flag)
334334
#endif
335335

336-
#if _Py_TIER2
337-
# define RECORD_DYNAMIC_JUMP_TAKEN() tstate->interp->jit_state.dynamic_jump_taken = true;
338-
#else
339-
# define RECORD_DYNAMIC_JUMP_TAKEN()
340-
#endif
341-
342336
#define UNBOUNDLOCAL_ERROR_MSG \
343337
"cannot access local variable '%s' where it is not associated with a value"
344338
#define UNBOUNDFREE_ERROR_MSG \

Python/generated_cases.c.h

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

Python/optimizer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,10 @@ _PyJit_translate_single_bytecode_to_trace(
632632
goto done;
633633
}
634634

635-
// Strange control-flow, unsupported opcode, etc.
636-
if (tstate->interp->jit_state.dynamic_jump_taken) {
635+
// Strange control-flow
636+
bool has_dynamic_jump_taken = OPCODE_HAS_UNPREDICTABLE_JUMP(opcode) &&
637+
(next_instr != this_instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]]);
638+
if (has_dynamic_jump_taken) {
637639
DPRINTF(2, "Unsupported: dynamic jump taken\n");
638640
goto unsupported;
639641
}
@@ -974,7 +976,6 @@ _PyJit_TryInitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame, _
974976
tstate->interp->jit_state.prev_instr_frame = frame;
975977
tstate->interp->jit_state.prev_instr_oparg = oparg;
976978
tstate->interp->jit_state.prev_instr_stacklevel = curr_stackdepth;
977-
tstate->interp->jit_state.dynamic_jump_taken = false;
978979
tstate->interp->jit_state.prev_instr_is_super = false;
979980
_Py_BloomFilter_Init(&tstate->interp->jit_state.dependencies);
980981
return 1;

Tools/cases_generator/analyzer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,11 @@ def stmt_has_jump_on_unpredictable_path(stmt: Stmt, branches_seen: int) -> tuple
910910
return True, branches_seen
911911
return False, branches_seen
912912
elif isinstance(stmt, IfStmt):
913-
return True, branches_seen + 1
913+
predict, seen = stmt_has_jump_on_unpredictable_path(stmt.body, branches_seen)
914+
if stmt.else_body:
915+
predict_else, seen_else = stmt_has_jump_on_unpredictable_path(stmt.else_body, branches_seen)
916+
return predict != predict_else, seen + seen_else + 1
917+
return predict, seen + 1
914918
elif isinstance(stmt, MacroIfStmt):
915919
predict, seen = stmt_has_jump_on_unpredictable_path_body(stmt.body, branches_seen)
916920
if stmt.else_body:

Tools/cases_generator/generators_common.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ def __init__(self, out: CWriter, labels: dict[str, Label], cannot_escape: bool =
129129
"DISPATCH": self.dispatch,
130130
"INSTRUCTION_SIZE": self.instruction_size,
131131
"stack_pointer": self.stack_pointer,
132-
"JUMPBY": self.jumpby,
133132
"DISPATCH_SAME_OPARG": self.dispatch_same_oparg,
134133
}
135134
self.out = out
@@ -422,30 +421,6 @@ def sync_sp(
422421
storage.stack.clear(self.out)
423422
return True
424423

425-
def jumpby(
426-
self,
427-
tkn: Token,
428-
tkn_iter: TokenIterator,
429-
uop: CodeSection,
430-
storage: Storage,
431-
inst: Instruction | None,
432-
) -> bool:
433-
self.out.start_line()
434-
self.emit(tkn)
435-
lparen = next(tkn_iter)
436-
self.emit(lparen)
437-
jump = next(tkn_iter)
438-
self.emit(jump)
439-
emit_to(self.out, tkn_iter, "RPAREN")
440-
next(tkn_iter)
441-
self.emit(");\n")
442-
443-
444-
if uop.properties.unpredictable_jump and jump.text != "0":
445-
self.out.start_line()
446-
self.emit("RECORD_DYNAMIC_JUMP_TAKEN();\n")
447-
return True
448-
449424
def stack_pointer(
450425
self,
451426
tkn: Token,
@@ -780,6 +755,8 @@ def cflags(p: Properties) -> str:
780755
flags.append("HAS_PURE_FLAG")
781756
if p.no_save_ip:
782757
flags.append("HAS_NO_SAVE_IP_FLAG")
758+
if p.unpredictable_jump:
759+
flags.append("HAS_UNPREDICTABLE_JUMP_FLAG")
783760
if flags:
784761
return " | ".join(flags)
785762
else:

Tools/cases_generator/opcode_metadata_generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"ERROR_NO_POP",
5757
"NO_SAVE_IP",
5858
"PERIODIC",
59+
"UNPREDICTABLE_JUMP",
5960
]
6061

6162

0 commit comments

Comments
 (0)