Skip to content

Commit b420f6b

Browse files
authored
pythonGH-139109: Support switch/case dispatch with the tracing interpreter. (pythonGH-141703)
1 parent b87613f commit b420f6b

18 files changed

+617
-585
lines changed

.github/workflows/jit.yml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ jobs:
5757
fail-fast: false
5858
matrix:
5959
target:
60-
# To re-enable later when we support these.
61-
# - i686-pc-windows-msvc/msvc
62-
# - x86_64-pc-windows-msvc/msvc
63-
# - aarch64-pc-windows-msvc/msvc
60+
- i686-pc-windows-msvc/msvc
61+
- x86_64-pc-windows-msvc/msvc
62+
- aarch64-pc-windows-msvc/msvc
6463
- x86_64-apple-darwin/clang
6564
- aarch64-apple-darwin/clang
6665
- x86_64-unknown-linux-gnu/gcc
@@ -71,16 +70,15 @@ jobs:
7170
llvm:
7271
- 21
7372
include:
74-
# To re-enable later when we support these.
75-
# - target: i686-pc-windows-msvc/msvc
76-
# architecture: Win32
77-
# runner: windows-2022
78-
# - target: x86_64-pc-windows-msvc/msvc
79-
# architecture: x64
80-
# runner: windows-2022
81-
# - target: aarch64-pc-windows-msvc/msvc
82-
# architecture: ARM64
83-
# runner: windows-11-arm
73+
- target: i686-pc-windows-msvc/msvc
74+
architecture: Win32
75+
runner: windows-2022
76+
- target: x86_64-pc-windows-msvc/msvc
77+
architecture: x64
78+
runner: windows-2022
79+
- target: aarch64-pc-windows-msvc/msvc
80+
architecture: ARM64
81+
runner: windows-11-arm
8482
- target: x86_64-apple-darwin/clang
8583
architecture: x86_64
8684
runner: macos-15-intel

Include/internal/pycore_magic_number.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ Known values:
286286
Python 3.15a1 3653 (Fix handling of opcodes that may leave operands on the stack when optimizing LOAD_FAST)
287287
Python 3.15a1 3654 (Fix missing exception handlers in logical expression)
288288
Python 3.15a1 3655 (Fix miscompilation of some module-level annotations)
289+
Python 3.15a1 3656 (Add TRACE_RECORD instruction, for platforms with switch based interpreter)
289290
290291
291292
Python 3.16 will start with 3700
@@ -299,7 +300,7 @@ PC/launcher.c must also be updated.
299300
300301
*/
301302

302-
#define PYC_MAGIC_NUMBER 3655
303+
#define PYC_MAGIC_NUMBER 3656
303304
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
304305
(little-endian) and then appending b'\r\n'. */
305306
#define PYC_MAGIC_NUMBER_TOKEN \

Include/internal/pycore_opcode_metadata.h

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

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ extern void _Py_ClearExecutorDeletionList(PyInterpreterState *interp);
364364

365365
int _PyJit_translate_single_bytecode_to_trace(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *next_instr, int stop_tracing_opcode);
366366

367-
int
367+
PyAPI_FUNC(int)
368368
_PyJit_TryInitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame,
369369
_Py_CODEUNIT *curr_instr, _Py_CODEUNIT *start_instr,
370370
_Py_CODEUNIT *close_loop_instr, int curr_stackdepth, int chain_depth, _PyExitData *exit,

Include/internal/pycore_uop_ids.h

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

Include/opcode_ids.h

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

Lib/_opcode_metadata.py

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

Python/bytecodes.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5636,10 +5636,12 @@ dummy_func(
56365636
DISPATCH();
56375637
}
56385638

5639-
label(record_previous_inst) {
5639+
inst(TRACE_RECORD, (--)) {
56405640
#if _Py_TIER2
56415641
assert(IS_JIT_TRACING());
5642-
int opcode = next_instr->op.code;
5642+
next_instr = this_instr;
5643+
frame->instr_ptr = prev_instr;
5644+
opcode = next_instr->op.code;
56435645
bool stop_tracing = (opcode == WITH_EXCEPT_START ||
56445646
opcode == RERAISE || opcode == CLEANUP_THROW ||
56455647
opcode == PUSH_EXC_INFO || opcode == INTERPRETER_EXIT);
@@ -5675,7 +5677,8 @@ dummy_func(
56755677
}
56765678
DISPATCH_GOTO_NON_TRACING();
56775679
#else
5678-
Py_FatalError("JIT label executed in non-jit build.");
5680+
(void)prev_instr;
5681+
Py_FatalError("JIT instruction executed in non-jit build.");
56795682
#endif
56805683
}
56815684

Python/ceval.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,10 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
11791179
uint8_t opcode; /* Current opcode */
11801180
int oparg; /* Current opcode argument, if any */
11811181
assert(tstate->current_frame == NULL || tstate->current_frame->stackpointer != NULL);
1182+
#if !USE_COMPUTED_GOTOS
1183+
uint8_t tracing_mode = 0;
1184+
uint8_t dispatch_code;
1185+
#endif
11821186
#endif
11831187
_PyEntryFrame entry;
11841188

Python/ceval_macros.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@
134134
# define LABEL(name) name:
135135
#else
136136
# define TARGET(op) case op: TARGET_##op:
137-
# define DISPATCH_GOTO() goto dispatch_opcode
138-
# define DISPATCH_GOTO_NON_TRACING() goto dispatch_opcode
137+
# define DISPATCH_GOTO() dispatch_code = opcode | tracing_mode ; goto dispatch_opcode
138+
# define DISPATCH_GOTO_NON_TRACING() dispatch_code = opcode; goto dispatch_opcode
139139
# define JUMP_TO_LABEL(name) goto name;
140140
# define JUMP_TO_PREDICTED(name) goto PREDICTED_##name;
141141
# define LABEL(name) name:
@@ -148,9 +148,9 @@
148148
# define LEAVE_TRACING() \
149149
DISPATCH_TABLE_VAR = DISPATCH_TABLE;
150150
#else
151-
# define IS_JIT_TRACING() (0)
152-
# define ENTER_TRACING()
153-
# define LEAVE_TRACING()
151+
# define IS_JIT_TRACING() (tracing_mode != 0)
152+
# define ENTER_TRACING() tracing_mode = 255
153+
# define LEAVE_TRACING() tracing_mode = 0
154154
#endif
155155

156156
/* PRE_DISPATCH_GOTO() does lltrace if enabled. Normally a no-op */

0 commit comments

Comments
 (0)