Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-126204: Add a sys._jit_enabled helper #126247

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

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
43740f7
Add `Py_JIT_ENABLED` variable to `pyconfig.h`
Eclips4 Oct 31, 2024
65e6cfa
Changes for Windows build
Eclips4 Oct 31, 2024
718b21e
fix typo
Eclips4 Oct 31, 2024
f4f1748
Another changes for Windows build
Eclips4 Oct 31, 2024
20d51dc
Update _sysconfig.c
Eclips4 Oct 31, 2024
a3e6dda
Don't set Py_JIT_ENABLED to 1 if --enable-experimental-jit=no
Eclips4 Oct 31, 2024
40c001c
Fixes for Windows build..
Eclips4 Oct 31, 2024
81994a9
Set Py_JIT_ENABLED to 2 insetad of 'interpreter'
Eclips4 Oct 31, 2024
1618433
PyLong_FromInt -> PyLong_FromLong
Eclips4 Oct 31, 2024
1773d9e
Fix typo in pyconfigs
Eclips4 Oct 31, 2024
9a50db7
Add a NEWS entry
Eclips4 Oct 31, 2024
4b4e212
Update documentation
Eclips4 Oct 31, 2024
19ddff2
Fix typo
Eclips4 Oct 31, 2024
91b7a78
Move NEWS entry
Eclips4 Oct 31, 2024
e79e0df
Fix typo in path
Eclips4 Oct 31, 2024
759321b
Fix doc formatting
Eclips4 Oct 31, 2024
49e4fdc
Fix configure
Eclips4 Oct 31, 2024
169831e
Revert previous changes
Eclips4 Nov 5, 2024
c83ea49
Revert other part of previous changes
Eclips4 Nov 5, 2024
1dec18a
Merge branch 'main' into add-py-jit
Eclips4 Nov 9, 2024
02d1f60
Add ``sys._jit_enabled``
Eclips4 Nov 10, 2024
a9c5b41
Add a ``NEWS`` entry
Eclips4 Nov 10, 2024
5423c71
Fix typo
Eclips4 Nov 10, 2024
49336ab
Apply suggestions from code review
Eclips4 Nov 10, 2024
67123ee
Add documentation for sys._jit_enabled and what's new entry.
Eclips4 Nov 10, 2024
4ddcd62
``availability`` only works for platforms :(
Eclips4 Nov 10, 2024
aa8f5bb
Apply suggestions from code review
Eclips4 Nov 10, 2024
0b5bce8
Align indentation
Eclips4 Nov 10, 2024
0ffcd9f
Remove unnecessary check
Eclips4 Nov 10, 2024
b6c097b
Update Python/pylifecycle.c
Eclips4 Nov 10, 2024
b4dc9dc
Update Python/sysmodule.c
Eclips4 Nov 11, 2024
46a4a7f
Fix reference leak in error path
Eclips4 Nov 11, 2024
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
Prev Previous commit
Next Next commit
Add sys._jit_enabled
  • Loading branch information
Eclips4 committed Nov 10, 2024
commit 02d1f6049fb07ee10c784ee6837c0f2b8652761b
4 changes: 4 additions & 0 deletions Include/internal/pycore_sysmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ extern int _PySys_ClearAttrString(PyInterpreterState *interp,
extern int _PySys_SetFlagObj(Py_ssize_t pos, PyObject *new_value);
extern int _PySys_SetIntMaxStrDigits(int maxdigits);

#if defined(_Py_TIER2) && (_Py_TIER2 % 2 != 0)
extern int _PySys_JITEnabled(void);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 1 addition & 3 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1782,10 +1782,8 @@ def test_initconfig_api(self):
'perf_profiling': 2,
}
config_dev_mode(preconfig, config)
# Temporarily enable ignore_stderr=True to ignore warnings on JIT builds
# See gh-126255 for more information
self.check_all_configs("test_initconfig_api", config, preconfig,
api=API_ISOLATED, ignore_stderr=True)
Comment on lines -1785 to -1788
Copy link
Member Author

Choose a reason for hiding this comment

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

That's fun. Due to incorrect assumptions, where JIT is actually enabled or not, this hack is no longer needed. Just setting PYTHON_JIT=0 is enough here.

api=API_ISOLATED, env={'PYTHON_JIT': '0'})

def test_initconfig_get_api(self):
self.run_embedded_interpreter("test_initconfig_get_api")
Expand Down
38 changes: 37 additions & 1 deletion Python/clinic/sysmodule.c.h

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

53 changes: 22 additions & 31 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1296,43 +1296,34 @@ init_interp_main(PyThreadState *tstate)
#endif
}

// Turn on experimental tier 2 (uops-based) optimizer
// This is also needed when the JIT is enabled
// Turn on experimental tier 2 (uops-based) optimizer
// This is also needed when the JIT is enabled
#ifdef _Py_TIER2
if (is_main_interp) {
int enabled = 1;
#if _Py_TIER2 & 2
enabled = 0;
// perf profiler works fine with tier 2 interpreter, so
// only checking for a "real JIT".
#if _Py_TIER % 2 != 0
int enabled = _PySys_JITEnabled();
printf("enabled = %d\n", enabled);
if (enabled && config->perf_profiling > 0) {
(void)PyErr_WarnEx(
PyExc_RuntimeWarning,
"JIT deactivated as perf profiling support is active",
0);
} else
#endif
char *env = Py_GETENV("PYTHON_JIT");
if (env && *env != '\0') {
// PYTHON_JIT=0|1 overrides the default
enabled = *env != '0';
{
PyObject *opt = _PyOptimizer_NewUOpOptimizer();
if (opt == NULL) {
return _PyStatus_ERR("can't initialize optimizer");
}
if (enabled) {
#ifdef _Py_JIT
// perf profiler works fine with tier 2 interpreter, so
// only checking for a "real JIT".
if (config->perf_profiling > 0) {
(void)PyErr_WarnEx(
PyExc_RuntimeWarning,
"JIT deactivated as perf profiling support is active",
0);
} else
#endif
{
PyObject *opt = _PyOptimizer_NewUOpOptimizer();
if (opt == NULL) {
return _PyStatus_ERR("can't initialize optimizer");
}
if (_Py_SetTier2Optimizer((_PyOptimizerObject *)opt)) {
return _PyStatus_ERR("can't install optimizer");
}
Py_DECREF(opt);
}
if (_Py_SetTier2Optimizer((_PyOptimizerObject *)opt)) {
return _PyStatus_ERR("can't install optimizer");
}
Py_DECREF(opt);
}
#endif
}
#endif // Py_TIER2

if (!is_main_interp) {
// The main interpreter is handled in Py_Main(), for now.
Expand Down
57 changes: 51 additions & 6 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2264,12 +2264,14 @@ sys_activate_stack_trampoline_impl(PyObject *module, const char *backend)
/*[clinic end generated code: output=5783cdeb51874b43 input=a12df928758a82b4]*/
{
#ifdef PY_HAVE_PERF_TRAMPOLINE
#ifdef _Py_JIT
_PyOptimizerObject* optimizer = _Py_GetOptimizer();
if (optimizer != NULL) {
Py_DECREF(optimizer);
PyErr_SetString(PyExc_ValueError, "Cannot activate the perf trampoline if the JIT is active");
return NULL;
#if defined(_Py_TIER2) && (_Py_TIER2 % 2 != 0)
if (_PySys_JITEnabled()) {
_PyOptimizerObject* optimizer = _Py_GetOptimizer();
if (optimizer != NULL) {
Py_DECREF(optimizer);
PyErr_SetString(PyExc_ValueError, "Cannot activate the perf trampoline if the JIT is active");
return NULL;
}
}
#endif

Expand Down Expand Up @@ -2533,6 +2535,46 @@ PyAPI_FUNC(int) PyUnstable_CopyPerfMapFile(const char* parent_filename) {
}


#ifdef _Py_TIER2
// _Py_TIER2 is set to 4 or 6 in tier 2 builds.
#if _Py_TIER2 % 2 != 0
int
_PySys_JITEnabled(void)
{
char *env = Py_GETENV("PYTHON_JIT");
if (_Py_TIER2 == 1) {
// Interpreter was built with enabled jit, but it can be disabled via PYTHON_JIT=0
if (env && *env != '\0') {
return *env != '0';
}
return 1;
}
else if (_Py_TIER2 == 3) {
// Interpreter was built with disabled jit, but it can be enabled via PYTHON_JIT=1
if (env && *env != '\0') {
return *env == '1';
}
return 0;
}
Py_UNREACHABLE();
}

/*[clinic input]
sys._jit_enabled -> bool

Returns True if JIT is enabled, False otherwise.
[clinic start generated code]*/

static int
sys__jit_enabled_impl(PyObject *module)
/*[clinic end generated code: output=e8adca3fa5011880 input=f2bdfa820c11f65b]*/
{
return _PySys_JITEnabled();
}
#endif
#endif // _Py_TIER2


static PyMethodDef sys_methods[] = {
/* Might as well keep this in alphabetic order */
SYS_ADDAUDITHOOK_METHODDEF
Expand Down Expand Up @@ -2603,6 +2645,9 @@ static PyMethodDef sys_methods[] = {
#endif
SYS__GET_CPU_COUNT_CONFIG_METHODDEF
SYS__IS_GIL_ENABLED_METHODDEF
#ifdef _Py_TIER2
SYS__JIT_ENABLED_METHODDEF
#endif
{NULL, NULL} // sentinel
};

Expand Down
Loading