Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/tail-call.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- 'Python/ceval.c'
- 'Python/ceval_macros.h'
- 'Python/generated_cases.c.h'
- 'Python/ceval_tailcall.c'
push:
paths: *paths
workflow_dispatch:
Expand Down
40 changes: 40 additions & 0 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def run_cases_test(self, input: str, expected: str):
actual = instructions.strip() + "\n\n " + labels.strip()

self.assertEqual(actual.strip(), expected.strip())
return lines

def test_inst_no_args(self):
input = """
Expand Down Expand Up @@ -1791,6 +1792,45 @@ def test_spilled_label(self):
"""
self.run_cases_test(input, output)

def test_start_frame_separate_from_handlers(self):
input = """
spilled label(start_frame) {
RELOAD_STACK();
goto handler;
}

label(handler) {
SAVE_STACK();
goto start_frame;
}
"""
output = """
LABEL(handler)
{
_PyFrame_SetStackPointer(frame, stack_pointer);
_PyFrame_StackPointerValidate(frame);
JUMP_TO_LABEL(start_frame);
}
"""
generated = self.run_cases_test(input, output)
handlers, start_frame = generated.split(
"#if !_Py_TAIL_CALL_INTERP || defined(_Py_CEVAL_START_FRAME)\n"
)
self.assertNotIn("\n LABEL(start_frame)\n", handlers)
self.assertIn("#ifndef _Py_CEVAL_START_FRAME", handlers)
expected = """
LABEL(start_frame)
{
stack_pointer = _PyFrame_GetStackPointer(frame);
_PyFrame_StackPointerInvalidate(frame);
JUMP_TO_LABEL(handler);
}

#endif /* !_Py_TAIL_CALL_INTERP || defined(_Py_CEVAL_START_FRAME) */
#undef TIER_ONE
"""
self.assertEqual(start_frame.strip(), expected.strip())


def test_incorrect_spills(self):
input1 = """
Expand Down
23 changes: 20 additions & 3 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ PY_CORE_EXE_LDFLAGS:= $(if $(CONFIGURE_EXE_LDFLAGS), $(CONFIGURE_EXE_LDFLAGS) $(
CFLAGS_ALIASING=@CFLAGS_ALIASING@
# Compilation flags only for ceval.c.
CFLAGS_CEVAL=@CFLAGS_CEVAL@
# Compilation flags only for ceval_tailcall.c.
CFLAGS_CEVAL_TAILCALL=@CFLAGS_CEVAL_TAILCALL@


# Machine-dependent subdirectories
Expand Down Expand Up @@ -445,6 +447,7 @@ PYTHON_OBJS= \
Python/bltinmodule.o \
Python/brc.o \
Python/ceval.o \
Python/ceval_tailcall.o \

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 also need ceval_tailcall.o in DTRACE_DEPS. Its probe-enabled checks require dtrace -G processing on FreeBSD, otherwise tail-call builds with DTrace fail to link.

Python/codecs.o \
Python/codegen.o \
Python/compile.o \
Expand Down Expand Up @@ -612,7 +615,11 @@ LINK_PYTHON_OBJS=@LINK_PYTHON_OBJS@
# On some systems, object files that reference DTrace probes need to be modified
# in-place by dtrace(1).
DTRACE_DEPS = \
Python/ceval.o Python/gc.o Python/import.o Python/sysmodule.o
Python/ceval.o \
Python/gc.o \
Python/import.o \
Python/sysmodule.o \
Python/ceval_tailcall.o

##########################################################################
# pyexpat's expat library
Expand Down Expand Up @@ -2152,7 +2159,7 @@ regen-test-cases:

.PHONY: regen-test-opcode-targets
regen-test-opcode-targets:
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/cases_generator/target_generator.py \
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/cases_generator/target_generator.py --static \
-o $(srcdir)/Modules/_testinternalcapi/test_targets.h.new $(srcdir)/Python/bytecodes.c \
$(srcdir)/Modules/_testinternalcapi/testbytecodes.c
$(UPDATE_FILE) $(srcdir)/Modules/_testinternalcapi/test_targets.h $(srcdir)/Modules/_testinternalcapi/test_targets.h.new
Expand Down Expand Up @@ -2198,6 +2205,13 @@ Python/ceval.o: \
$(srcdir)/Python/opcode_targets.h \
$(srcdir)/Python/record_functions.c.h

Python/ceval_tailcall.o: \
$(srcdir)/Python/ceval.h \
$(srcdir)/Python/ceval_macros.h \
$(srcdir)/Python/generated_cases.c.h \
$(srcdir)/Python/opcode_targets.h \
$(srcdir)/Python/record_functions.c.h

Python/flowgraph.o: \
$(srcdir)/Include/internal/pycore_opcode_metadata.h

Expand Down Expand Up @@ -3204,6 +3218,9 @@ Python/dtoa.o: Python/dtoa.c
Python/ceval.o: Python/ceval.c
$(CC) -c $(PY_CORE_CFLAGS) $(CFLAGS_CEVAL) -o $@ $<

Python/ceval_tailcall.o: Python/ceval_tailcall.c
$(CC) -c $(PY_CORE_CFLAGS) $(CFLAGS_CEVAL_TAILCALL) -o $@ $<

# Run reindent on the library
.PHONY: reindent
reindent:
Expand Down Expand Up @@ -3462,7 +3479,7 @@ MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h $(srcdir)/Modules/addrinfo.
MODULE__SSL_DEPS=$(srcdir)/Modules/_ssl.h $(srcdir)/Modules/_openssl_mem.h $(srcdir)/Modules/_ssl/cert.c $(srcdir)/Modules/_ssl/debughelpers.c $(srcdir)/Modules/_ssl/misc.c $(srcdir)/Modules/_ssl_data_111.h $(srcdir)/Modules/_ssl_data_300.h $(srcdir)/Modules/socketmodule.h
MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/parts.h $(srcdir)/Modules/_testcapi/util.h
MODULE__TESTLIMITEDCAPI_DEPS=$(srcdir)/Modules/_testlimitedcapi/testcapi_long.h $(srcdir)/Modules/_testlimitedcapi/parts.h $(srcdir)/Modules/_testlimitedcapi/util.h
MODULE__TESTINTERNALCAPI_DEPS=$(srcdir)/Modules/_testinternalcapi/parts.h $(srcdir)/Parser/tokenizer/source.h $(srcdir)/Parser/tokenizer/types.h $(srcdir)/Python/ceval.h $(srcdir)/Modules/_testinternalcapi/test_targets.h $(srcdir)/Modules/_testinternalcapi/test_cases.c.h
MODULE__TESTINTERNALCAPI_DEPS=$(srcdir)/Modules/_testinternalcapi/parts.h $(srcdir)/Parser/tokenizer/source.h $(srcdir)/Parser/tokenizer/types.h $(srcdir)/Python/ceval.h $(srcdir)/Python/ceval_macros.h $(srcdir)/Modules/_testinternalcapi/test_targets.h $(srcdir)/Modules/_testinternalcapi/test_cases.c.h
MODULE__SQLITE3_DEPS=$(srcdir)/Modules/_sqlite/connection.h $(srcdir)/Modules/_sqlite/cursor.h $(srcdir)/Modules/_sqlite/microprotocols.h $(srcdir)/Modules/_sqlite/module.h $(srcdir)/Modules/_sqlite/prepare_protocol.h $(srcdir)/Modules/_sqlite/row.h $(srcdir)/Modules/_sqlite/util.h
MODULE__ZSTD_DEPS=$(srcdir)/Modules/_zstd/_zstdmodule.h $(srcdir)/Modules/_zstd/buffer.h $(srcdir)/Modules/_zstd/zstddict.h

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reserve frame pointer on :option:`--with-tail-call-interp` builds using Clang 21 or newer. This recovers most of the performance hit for those builds.
12 changes: 8 additions & 4 deletions Modules/_testinternalcapi/interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ stop_tracing_and_jit(PyThreadState *tstate, _PyInterpreterFrame *frame)
#endif

#if _Py_TAIL_CALL_INTERP
/* The declarations give the generated handlers internal linkage. */
#include "test_targets.h"
#include "test_cases.c.h"
#define _Py_CEVAL_START_FRAME
#include "test_cases.c.h"
#undef _Py_CEVAL_START_FRAME
#endif

PyObject* _Py_HOT_FUNCTION
Expand Down Expand Up @@ -124,9 +128,9 @@ Test_EvalFrame(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
_PyFrame_StackPointerInvalidate(frame);
#if _Py_TAIL_CALL_INTERP
# if Py_STATS
return _TAIL_CALL_error(frame, stack_pointer, tstate, next_instr, instruction_funcptr_handler_table, 0, lastopcode);
return _Py_TAIL_CALL_error(frame, stack_pointer, tstate, next_instr, instruction_funcptr_handler_table, 0, lastopcode);
# else
return _TAIL_CALL_error(frame, stack_pointer, tstate, next_instr, instruction_funcptr_handler_table, 0);
return _Py_TAIL_CALL_error(frame, stack_pointer, tstate, next_instr, instruction_funcptr_handler_table, 0);
# endif
#else
goto error;
Expand All @@ -135,9 +139,9 @@ Test_EvalFrame(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)

#if _Py_TAIL_CALL_INTERP
# if Py_STATS
return _TAIL_CALL_start_frame(frame, NULL, tstate, NULL, instruction_funcptr_handler_table, 0, lastopcode);
return _Py_TAIL_CALL_start_frame(frame, NULL, tstate, NULL, instruction_funcptr_handler_table, 0, lastopcode);
# else
return _TAIL_CALL_start_frame(frame, NULL, tstate, NULL, instruction_funcptr_handler_table, 0);
return _Py_TAIL_CALL_start_frame(frame, NULL, tstate, NULL, instruction_funcptr_handler_table, 0);
# endif
#else
goto start_frame;
Expand Down
59 changes: 32 additions & 27 deletions Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading