Visitar URL original
gh-128563: A new tail-calling interpreter by Fidget-Spinner · Pull Request #128718 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20250208211416/https://github.com/python/cpython/pull/128718
Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-128563: A new tail-calling interpreter #128718

Merged
merged 127 commits into from
Feb 6, 2025

Conversation

Fidget-Spinner
Copy link
Member

@Fidget-Spinner Fidget-Spinner commented Jan 10, 2025 •

Features:

  • Significantly better performance on all 64-bit platforms that we care about.
  • Better debugging experience, locals don't get optimized out anymore in tail call handlers. If you want to see a trace of instructions, you can just disable tail calls too.

Preliminary benchmark results here https://github.com/faster-cpython/benchmarking-public/tree/main/results/bm-20250107-3.14.0a3+-f1d3190-CLANG

TLDR (all results are pyperformance, clang-19, with PGO + ThinLTO unless stated otherwise):

  • 9.2% geomean faster AArch64 Ubuntu 22.04 ARM Neoverse N1. Up to 26% faster on Python workloads.
  • 7.4% geomean faster x86_64 Ubuntu 20.04 with Xeon W-2255. Up to 24% faster on Python workloads.
  • 10.8% geomean faster on x86_64 Windows 64-bit with i9-12900. Up to 37% faster on Python workloads. [No PGO]
  • 14.4% geomean slower on x86 Windows 32-bit with i9-12900 (same machine). Will turn off this option.
  • 14.7% geomean faster on AArch64 with macOS M1. Up to 45% faster on Python workloads.

More recent benchmark results:
https://github.com/faster-cpython/benchmarking-public/tree/main/results/bm-20250116-3.14.0a4+-df5d01c-CLANG

  • 8.5% geomean faster AArch64 with Ubuntu 22.04 ARM Neoverse N1. Up to 24% faster on Python workloads.
  • 9.3% geomean faster x86_64 Ubuntu 22.04 with i9-12900. Up to 24% faster on Python workloads.
  • 11.7% geomean faster on AArch64 with macOS M1. Up to 49% faster on Python workloads.

This initial implementation focuses on correctness. There's still room to improve performance even further. I've detailed performance plans in the original issue.

Changset:

  • Added to configure.ac to auto-detect when we can do this.
  • We need opcode in the call arguments because it might be modified by instrumented instructions.
  • Autogenerated opcode tailcall handlers using existing bytecode DSL generator.

Credits also to Brandt and Savannah for the JIT workflow file.

Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

Looking good in general.
Quite a few code quality issues, but nothing difficult to fix.

Instead of the preamble

            #ifdef Py_TAIL_CALL_INTERP
            int opcode = next_instr->op.code;
            (void)(opcode);
            #endif /* Py_TAIL_CALL_INTERP */
            #ifdef Py_TAIL_CALL_INTERP
            _Py_CODEUNIT* const this_instr = next_instr;
            (void)this_instr;
            #endif /* Py_TAIL_CALL_INTERP */
            frame->instr_ptr = next_instr;

we could leave it to the C compiler to determine whether this_instr or opcode are used and take advantage of the fact that we know statically what the opcode is.
Then we just need to emit this:

            _Py_CODEUNIT* const this_instr = next_instr;
            frame->instr_ptr = this_instr;
            int opcode = {OPCODE};
            (void)(opcode);

Which should be a tad quicker and easier to read.

Python/bytecodes.c Outdated Show resolved Hide resolved
Python/ceval.c Show resolved Hide resolved
Python/ceval.c Show resolved Hide resolved
Tools/cases_generator/generators_common.py Outdated Show resolved Hide resolved
Tools/cases_generator/generators_common.py Outdated Show resolved Hide resolved
Tools/cases_generator/tier2_generator.py Outdated Show resolved Hide resolved
Tools/cases_generator/generators_common.py Outdated Show resolved Hide resolved
Tools/cases_generator/generators_common.py Outdated Show resolved Hide resolved
Tools/cases_generator/generators_common.py Outdated Show resolved Hide resolved
Tools/cases_generator/tier1_generator.py Outdated Show resolved Hide resolved
@bedevere-app
Copy link

bedevere-app bot commented Feb 6, 2025

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

@Fidget-Spinner
Copy link
Member Author

I have made the requested changes; please review again.

@bedevere-app
Copy link

bedevere-app bot commented Feb 6, 2025

Thanks for making the requested changes!

@markshannon: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from markshannon February 6, 2025 14:02
Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

Looks good.
The dispatching mechanism is now distinct from the actions, even in the generated code, which is nice.

Just one quibble, but that can be done in another PR if you want.

Do we have up to date performance numbers?
No need to do that before merging, but it would be good to have the performance impact on record.

Tools/cases_generator/tier1_generator.py Outdated Show resolved Hide resolved
@@ -304,6 +304,10 @@ def test_inst_no_args(self):
"""
output = """
TARGET(OP) {
int opcode = OP;
(void)(opcode);
_Py_CODEUNIT* const this_instr = next_instr;
Copy link
Member

Choose a reason for hiding this comment

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

The generated sequence

            _Py_CODEUNIT* const this_instr = next_instr;
            (void)this_instr;
            frame->instr_ptr = next_instr;

would be neater as:

            _Py_CODEUNIT* const this_instr = next_instr;
            frame->instr_ptr = this_instr;

@Fidget-Spinner
Copy link
Member Author

Fidget-Spinner commented Feb 6, 2025 •

Do we have up to date performance numbers? No need to do that before merging, but it would be good to have the performance impact on record.

No, but the benchmarking infra can't benchmark this anyways. Because it's opt-in. @mdboom perhaps we could add the configure option now?

@Fidget-Spinner
Copy link
Member Author

@markshannon we can't do the int opcode plan because it breaks platforms without computed gotos it seems (like MSVC). So I'll just put an ifdef around it.

@Fidget-Spinner Fidget-Spinner merged commit cb640b6 into python:main Feb 6, 2025
70 checks passed
@Fidget-Spinner Fidget-Spinner deleted the tail-call branch February 6, 2025 15:22
@brandtbucher
Copy link
Member

Great work on this, @Fidget-Spinner!

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Feb 7, 2025
Co-authored-by: Garrett Gu <garrettgu777@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants