Skip to content

gh-148219: Fix JIT+pystats crash: is_terminator fails for base opcodes - #148220

Open
eendebakpt wants to merge 6 commits into
python:mainfrom
eendebakpt:fix-pystats-jit-terminator
Open

eendebakpt wants to merge 6 commits into
python:mainfrom
eendebakpt:fix-pystats-jit-terminator

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Two bugs when building with --enable-experimental-jit --enable-pystats:

  1. is_terminator() uses _PyUop_Uncached[opcode] to map replicated variants back to base opcodes, but the array only contains entries for replicated variants (e.g. _JUMP_TO_TOP_r00). For base opcodes like _JUMP_TO_TOP itself, it returns 0, so the terminator is not recognized.

  2. jit.c references jit_got_size in OPT_STAT_ADD but the field is missing from OptimizationStats in pystats.h, causing a build failure.

Two bugs when building with --enable-experimental-jit --enable-pystats:

1. `is_terminator()` uses `_PyUop_Uncached[opcode]` to map replicated
   variants back to base opcodes, but the array only contains entries
   for replicated variants (e.g. `_JUMP_TO_TOP_r00`). For base opcodes
   like `_JUMP_TO_TOP` itself, it returns 0, so the terminator is not
   recognized. This causes `effective_trace_length()` to hit
   `Py_FatalError("No terminating instruction")`.

   Fix: fall back to the raw opcode when `_PyUop_Uncached` returns 0.

2. `jit.c` references `jit_got_size` in `OPT_STAT_ADD` but the field
   is missing from `OptimizationStats` in `pystats.h`, causing a build
   failure.

   Fix: add the missing `jit_got_size` field.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Comment thread Include/cpython/pystats.h Resolved
Comment thread Python/optimizer.c Outdated
is_terminator(const _PyUOpInstruction *uop)
{
int opcode = _PyUop_Uncached[uop->opcode];
if (opcode == 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should remove the line int opcode = _PyUop_Uncached[uop->opcode]; as the only time we should see a TOS caching uop is in the sanity_check function.

Maybe move that line there, and just assert(opcode <= MAX_UOP_ID) here?

is_terminator() is only called before stack_allocate, where opcodes
are always base opcodes. Use the raw opcode directly with an assert.

In sanity_check (which sees replicated opcodes post-stack_allocate),
inline the terminator check using the already-computed base_opcode.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@eendebakpt
eendebakpt requested a review from markshannon April 11, 2026 21:21
eendebakpt and others added 2 commits April 27, 2026 21:56
Mirrors the wiring of the other jit_*_size counters: emit the value in
print_optimization_stats and accumulate it in merge_optimization_stats
so merged stats include the GOT size.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@eendebakpt

Copy link
Copy Markdown
Contributor Author

@cocolato The changes from #148958 I merged in here.

@cocolato cocolato left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should merge this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants