Skip to content

gh-150587: Guard specialized list-int subscript indices#153004

Open
KRRT7 wants to merge 1 commit into
python:mainfrom
KRRT7:list-int-compact-guard
Open

gh-150587: Guard specialized list-int subscript indices#153004
KRRT7 wants to merge 1 commit into
python:mainfrom
KRRT7:list-int-compact-guard

Conversation

@KRRT7

@KRRT7 KRRT7 commented Jul 4, 2026

Copy link
Copy Markdown

Fixes gh-150587.

This supersedes the earlier closed PR gh-150588. That patch was moved into gh-150425 at the time, but the broad performance PR has not landed, so this keeps the correctness fix as a focused standalone change.

This makes the runtime invariant explicit for the specialized list/int subscript and store-subscript paths. Both opcodes are selected only after seeing a compact non-negative int index, but their runtime guard only ensures an exact int. If the same specialized code later sees a non-compact exact int, it must fall back before calling _PyLong_CompactValue().

Changes:

  • Add compact non-negative int fallback checks before _PyLong_CompactValue() in BINARY_OP_SUBSCR_LIST_INT and STORE_SUBSCR_LIST_INT.
  • Regenerate the tier-1, tier-2, and _testinternalcapi generated case headers.
  • Add regression coverage that warms the specialized opcode with index 0, then calls it with 2**30 + 2 and verifies generic IndexError behavior instead of using the low digit as the list index.

Tests:

  • ./python.exe -m test test_opcache -v
  • ./python.exe -m test test_opcache -m test_binary_subscr -m test_store_subscr -v
  • env PYTHON_JIT=1 ./python.exe -m test test_opcache -m test_binary_subscr -m test_store_subscr -v

@KRRT7 KRRT7 force-pushed the list-int-compact-guard branch from 24dd20d to f1a230b Compare July 4, 2026 07:45
@KRRT7 KRRT7 changed the title gh-153002: Guard specialized list-int subscript indices gh-150587: Guard specialized list-int subscript indices Jul 4, 2026

@Fidget-Spinner Fidget-Spinner 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.

LGTM, thanks for catching this bug, just one request.

Comment thread Python/bytecodes.c

assert(PyLong_CheckExact(sub));
assert(PyList_CheckExact(list));
DEOPT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub));

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.

Why not use EXIT_IF here?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BINARY_OP_SUBSCR_LIST_INT / STORE_SUBSCR_LIST_INT silently depend on _GUARD_TOS_INT enforcing compactness

2 participants