Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-132508: Use tagged integers on the evaluation stack for the last instruction offset#132545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
markshannon merged 14 commits intopython:mainfromfaster-cpython:fix-132508
Apr 29, 2025

Conversation

markshannon
Copy link
Member

@markshannonmarkshannon commentedApr 15, 2025
edited by bedevere-appbot
Loading

When reraising in afinally block, the exception needs to look as if it were raised from an earlier point in the code.
To do this we save the earlier instruction offset as an integer on the evaluation stack.
Currently, this requires boxing the integer, which can (extremely rarely) fail.
By using a tagged integer we can avoid that failure mode.

This is might seem like an elaborate fix for a very minor issue, and it is, but we will want tagged integers/pointers for many other things and this is a nice small step to that larger change.

See#132509

Copy link
Member

@Fidget-SpinnerFidget-Spinner left a comment

Choose a reason for hiding this comment

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

2 comments

@markshannon
Copy link
MemberAuthor

As expected,performance is neutral.

Copy link
Member

@brandtbucherbrandtbucher left a comment

Choose a reason for hiding this comment

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

I believe the magic number needs to be updated (and everything regenerated too) since theexception_unwind label now pushes tagged ints.

@bedevere-app
Copy link

When you're done making the requested changes, leave the comment:I have made the requested changes; please review again.

@markshannon
Copy link
MemberAuthor

I believe the magic number needs to be updated (and everything regenerated too) since the exception_unwind label now pushes tagged ints.

Nothing has changed in terms of stack effects. The only change is that we push a tagged int instead of a boxed one.

@markshannon
Copy link
MemberAuthor

The failure on JIT/ARM64 look like a JIT bug. The assertion errors seem impossible and do not occur on any other platform.
Neither the ARM64 (JIT) interpreter or the other JIT builds show any of the failures.

@Fidget-Spinner
Copy link
Member

I think I've seen that JIT failure before on my old PR when I used a different tagging scheme.

@markshannon
Copy link
MemberAuthor

The JIT ARM 64 windows failure is just a timeout for the int repr test that sometimes happens.

@brandtbucher
Copy link
Member

This isn't a JIT problem, it's a tier two problem (I can reproduce on an M2 Mac with--with-pydebug --enable-experimental-jit=interpreter). Digging in deeper...

@brandtbucher
Copy link
Member

When entering tier two, the current frame'sf_executable is0x0000000000000002. Which is, uh, not a valid stackref. It looks suspiciously likeBITS_TO_PTR_MASKED(PyStackRef_TagInt(0)).

@brandtbucher
Copy link
Member

Never mind, it looks like the bugis in the JIT code. I believe this is because the Clang is not actually inlining the some_PyFrame_GetCode calls used forstack_pointer sanity checks on this build. Instead, it’s emitting the code for the function alongside the normal bytecode, and making calls tothat. My guess is it’s using some clever calling convention or something and we’re messing up how it’s being called.

@brandtbucher
Copy link
Member

@markshannon, this fixes it:

diff --git a/Tools/jit/_stencils.py b/Tools/jit/_stencils.pyindex 8faa9e8cac2..639e4bcc793 100644--- a/Tools/jit/_stencils.py+++ b/Tools/jit/_stencils.py@@ -291,6 +291,7 @@ def process_relocations(                 hole.kind                 in {"R_AARCH64_CALL26", "R_AARCH64_JUMP26", "ARM64_RELOC_BRANCH26"}                 and hole.value is HoleValue.ZERO+                and hole.symbol not in self.symbols             ):                 hole.func = "patch_aarch64_trampoline"                 hole.need_state = True

@brandtbucher
Copy link
Member

For anyone curious: on this particular build, Clang doesn't inline one of the calls to_PyFrame_GetCode, and instead emits it alongside our_JIT_ENTRY function. This is fine, but our handling of jumps on AArch64 macOS (which inserts trampolines for out-of-range jumps) meant that we tried to "link against" the_PyFrame_GetCode function in the main executable, rather than the (slightly different!) version the compiler emitted as part of the stencil.

Though they bothshould be identical, my guess is that Clang realized that the function was static and only called in one place by the bytecode, and didn't use an ABI-conforming calling convention. If we change the logic to check for duplicate local symbols in the template before trying to link to the main executable (which is what my change does), then we end up calling the intended version of the function.

(And, in case anyone was wondering:lldb is useless in our JIT code, andprintf-debugging didn't help at all in this situation. It was basically just looking at the template we generated alongside the disassembly that led to the "aha" moment.)

Zheaoli reacted with thumbs up emoji

@brandtbucher
Copy link
Member

@markshannon, you applied my patch to the wrong line.

PyObject *obj = _Py_stackref_get_object(ref);
Py_INCREF(obj);
return _Py_stackref_create(obj, filename, linenumber);
if (ref.index & 1) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (ref.index&1) {
if (is_tagged_int(ref)) {

@markshannonmarkshannon merged commitccf1b0b intopython:mainApr 29, 2025
72 checks passed
@markshannonmarkshannon deleted the fix-132508 branchApril 29, 2025 17:00
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@iritkatrieliritkatrieliritkatriel approved these changes

@Fidget-SpinnerFidget-SpinnerFidget-Spinner approved these changes

@brandtbucherbrandtbucherbrandtbucher left review comments

@ericsnowcurrentlyericsnowcurrentlyAwaiting requested review from ericsnowcurrentlyericsnowcurrently is a code owner

@savannahostrowskisavannahostrowskiAwaiting requested review from savannahostrowskisavannahostrowski is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@markshannon@Fidget-Spinner@brandtbucher@iritkatriel

[8]ページ先頭

©2009-2025 Movatter.jp