Skip to content

gh-143732: add specialization for FOR_ITER - #148745

Merged
markshannon merged 15 commits into
python:mainfrom
NekoAsakura:gh-143732/for-iter-type-recording
May 4, 2026
Merged

gh-143732: add specialization for FOR_ITER#148745
markshannon merged 15 commits into
python:mainfrom
NekoAsakura:gh-143732/for-iter-type-recording

Conversation

@NekoAsakura

@NekoAsakuraNekoAsakura commented Apr 19, 2026

Copy link
Copy Markdown
Member
mainbranchratio
for_iter_dict_items110.23 ms ± 1.74 ms99.29 ms ± 1.24 ms1.11× faster
for_iter_dict_keys85.20 ms ± 1.03 ms76.20 ms ± 1.02 ms1.12× faster
for_iter_dict_values84.91 ms ± 1.15 ms76.27 ms ± 1.24 ms1.11× faster
for_iter_set96.88 ms ± 1.05 ms87.46 ms ± 1.16 ms1.11× faster
for_iter_reversed79.99 ms ± 0.77 ms72.03 ms ± 0.79 ms1.11× faster
for_iter_enumerate123.65 ms ± 1.94 ms110.14 ms ± 1.82 ms1.12× faster
for_iter_zip131.47 ms ± 1.38 ms122.17 ms ± 2.87 ms1.08× faster
for_iter_list59.21 ms ± 1.17 ms59.48 ms ± 1.06 ms1.00× slower
for_iter_tuple53.78 ms ± 0.61 ms53.77 ms ± 0.74 ms1.00× faster
for_iter_range72.62 ms ± 0.80 ms72.55 ms ± 1.08 ms1.00× faster

@cocolatococolato 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 doing this!

Comment threadPython/bytecodes.c Outdated
}

macro(FOR_ITER) = _SPECIALIZE_FOR_ITER + _FOR_ITER;
macro(FOR_ITER) = _SPECIALIZE_FOR_ITER + _RECORD_NOS_GEN_FUNC + _RECORD_NOS_TYPE + _FOR_ITER;

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.

caseJIT_SYM_RECORDED_GEN_FUNC_TAG:
return&PyGen_Type;

We don't need _RECORD_NOS_GEN_FUNC here, because the GEN_FUNC_TAG recorded here does not contribute to the current optimization.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Every FOR_ITER specialisation's record list must be a prefix of FOR_ITER's.
_RECORD_NOS_GEN_FUNC writes a gen func or NULL to slot 0, matching what FOR_ITER_GEN reads from it.
https://github.com/python/cpython/actions/runs/24623062259/job/71997168333

PyType_Watch(TYPE_WATCHER_ID, (PyObject *)probable);
_Py_BloomFilter_Add(dependencies, probable);
sym_set_type(iter, probable);
int32_t orig_target = (this_instr - 1)->target;

@cocolatococolatoApr 19, 2026

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 should add an assert to make sure the last uop is _RECORD_NOS_TYPE

@markshannonmarkshannon 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've a few suggestions inline.

Comment threadPython/optimizer_bytecodes.c Outdated
Comment threadPython/optimizer_bytecodes.c Outdated
Comment threadPython/bytecodes.c
}

tier2 op(_ITER_NEXT_INLINE, (iternext_fn/4, iter, null_or_index -- iter, null_or_index, next)) {
volatile iternextfunc iternext_v = (iternextfunc)iternext_fn;

@markshannonmarkshannonMay 1, 2026

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 volatile? It shouldn't be necessary.
Also function pointers may not be the same size as normal pointers.
Can you add assert(sizeof(iternextfunc) == sizeof(uintptr_t)); to be on the safe side.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Because mmap pages usually sit too far from tp_iternext for the offset to reach, we have to use volatile to force compiler to emit callq *%rax (target read from a register) instead of callq <rel32> (target baked in as a fixed offset). Otherwise the call jumps to the wrong address and segfaults.

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.

Does this fail just for x86 or for AArch64 as well?

This is a bug in the JIT and we fix it, rather than add workarounds.
I think we should be adding a trampoline here, but it seems that we are not.
If you're looking at the stencils, can you see how it is being patched (what patch function is being called)?

@brandtbucher

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Both. (aarch64 was tested under qemu-user)
Trampoline path doesn't appear to be designed for _JIT_* symbols:

defsymbol_to_value(symbol: str) ->tuple[HoleValue, str|None]:
"""
Convert a symbol name to a HoleValue and a symbol name.
Some symbols (starting with "_JIT_") are special and are converted to their
own HoleValues.
"""
ifsymbol.startswith("_JIT_"):
try:
returnHoleValue[symbol.removeprefix("_JIT_")], None
exceptKeyError:
pass
returnHoleValue.ZERO, symbol

if (
hole.kind
in {"R_AARCH64_CALL26", "R_AARCH64_JUMP26", "ARM64_RELOC_BRANCH26"}
andhole.valueisHoleValue.ZERO
andhole.symbolnotinself.symbols

# x86_64 Darwin trampolines for external symbols
elif (
hole.kind=="X86_64_RELOC_BRANCH"
andhole.valueisHoleValue.ZERO
andhole.symbolnotinself.symbols

Even if we extended it to those, we'd need a stub per instance. I don't think it gains us anything.

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 only need a trampoline if the jitted code is too far from the executable. See #148822

@bedevere-app

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be poked with soft cushions!

@read-the-docs-community

read-the-docs-communityBot commented May 2, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32525858 | 📁 Comparing a0551fe against main (4b33308)

🔍 Preview build

81 files changed · + 1 added · ± 80 modified

+Added

±Modified

@markshannon

Copy link
Copy Markdown
Member

OK, well let's go with your approach for now. We already do the same here

I've created #149316 so we won't need the "volatile"

@markshannon
markshannon self-requested a review May 3, 2026 09:54
Comment threadPython/bytecodes.c Outdated
Comment threadPython/optimizer_bytecodes.c Outdated
op(_FOR_ITER_TIER_TWO, (iter, null_or_index -- iter, null_or_index, next)) {
PyTypeObject *type = sym_get_type(iter);
if (type != NULL && type != &PyGen_Type && type->tp_iternext != NULL) {
ADD_OP(_ITER_NEXT_INLINE, 0, (uintptr_t)type->tp_iternext);

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 need the watcher here as well.

What we do elsewhere is something like this:

booldefinite= true;
PyTypeObject*type=sym_get_type(iter);
if (type==NULL) {
type=sym_get_probable_type(iter);
definite= false;
}
if (type!=NULL) {
// Add if not definite, otherwise NOP// Add watcher(s)
}

Comment threadPython/bytecodes.c Outdated

macro(FOR_ITER_VIRTUAL) =
unused/1 + // Skip over the counter
_RECORD_NOS + // Required for family-uniform recording (gh-148571).

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.

The analyzer should take the union of all the family members when considering what to record.
Can you try removing this?

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 create an pr to resolve this: #149359

@markshannon
markshannon self-requested a review May 4, 2026 16:26

@markshannonmarkshannon 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.

This looks good now, thanks.

We can improve the tooling later. The generated code is good with the workarounds.

@markshannon
markshannon self-requested a review May 4, 2026 16:27
@markshannon
markshannon merged commit 9846407 into python:mainMay 4, 2026
86 of 87 checks passed
@NekoAsakura
NekoAsakura deleted the gh-143732/for-iter-type-recording branch May 4, 2026 17:34
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@NekoAsakura@markshannon@cocolato@jalaheshammohamed-sketch