Skip to content

gh-150587: Factor non-negative compact int subscript guards#153004

Closed
KRRT7 wants to merge 3 commits into
python:mainfrom
KRRT7:list-int-compact-guard
Closed

gh-150587: Factor non-negative compact int subscript guards#153004
KRRT7 wants to merge 3 commits into
python:mainfrom
KRRT7:list-int-compact-guard

Conversation

@KRRT7

@KRRT7 KRRT7 commented Jul 4, 2026

Copy link
Copy Markdown

This PR is now a refactor/cleanup, not a bug fix.

The original version assumed there was a correctness issue around non-compact int indices reaching specialized subscript paths. That turned out to be incorrect: the existing specialization and guard structure already prevents that case from reaching _PyLong_CompactValue() unsafely.

What this PR does now is factor the "exact non-negative compact int" check into a shared guard uop and use it in the specialized subscript paths that already rely on that invariant:

  • BINARY_OP_SUBSCR_LIST_INT
  • BINARY_OP_SUBSCR_STR_INT
  • BINARY_OP_SUBSCR_USTR_INT
  • BINARY_OP_SUBSCR_TUPLE_INT
  • STORE_SUBSCR_LIST_INT

The refactor keeps the specialized opcode expansions at the same uop counts as before by replacing _GUARD_TOS_INT where appropriate, rather than stacking an additional guard on top.

Tests run:

  • env PYTHONPATH=build/lib.macosx-26.5-arm64-3.16 ./python.exe -m test test_opcache -m test_binary_subscr -m test_store_subscr -v
  • env PYTHONPATH=build/lib.macosx-26.5-arm64-3.16 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 Outdated
@markshannon

Copy link
Copy Markdown
Member

This PR is marked as fixing #150587, but that issue is closed as not planned.

What is this fixing?

@KRRT7

KRRT7 commented Jul 6, 2026

Copy link
Copy Markdown
Author

@markshannon that is my own issue, the original issue was for a much bigger PR and I've since then, scoped it down, will update the body / title.

@KRRT7 KRRT7 requested a review from Fidget-Spinner July 7, 2026 20:33
@Fidget-Spinner

Copy link
Copy Markdown
Member

Wait upon closer inspection, the current code already deopts in the subscript op if it sees a negative index. I think there's no bug here? Correct me if I'm wrong.

Also GUARD_TOS_INT checks for a compact long already, then the subscript op itself checks for the range .

@KRRT7

KRRT7 commented Jul 10, 2026

Copy link
Copy Markdown
Author

You were right about the original bug claim: after rechecking the guard/specialization path, the non-compact-index issue I described is not actually reachable.

I’ve kept the branch but narrowed it into a refactor only: factor the exact non-negative compact-int check into a shared guard uop for the specialized subscript/store-subscript paths that already depend on that invariant, without increasing their uop counts.

I reran the focused test_opcache coverage for test_binary_subscr and test_store_subscr, with and without PYTHON_JIT=1.

@KRRT7 KRRT7 changed the title gh-150587: Guard specialized list-int subscript indices gh-150587: Factor non-negative compact int subscript guards Jul 10, 2026
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.

3 participants