From 89b98fe205f8371139e37e3dc852059561a2d9ef Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 6 Nov 2023 02:30:54 -0500 Subject: [PATCH 1/8] Turn off optimize --- Python/ceval.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/ceval.c b/Python/ceval.c index 670f312f126201..82ca46308c312e 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -664,6 +664,7 @@ extern const struct _PyCode_DEF(8) _Py_InitCleanup; * so consume 3 units of C stack */ #define PY_EVAL_C_STACK_UNITS 2 +#pragma optimize(off) PyObject* _Py_HOT_FUNCTION _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag) { From f87df622d9e4d11a8315f171a7dbf13312c895ff Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 6 Nov 2023 02:35:08 -0500 Subject: [PATCH 2/8] Fix pragmas --- Python/ceval.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index 82ca46308c312e..32f263a3567255 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -664,7 +664,7 @@ extern const struct _PyCode_DEF(8) _Py_InitCleanup; * so consume 3 units of C stack */ #define PY_EVAL_C_STACK_UNITS 2 -#pragma optimize(off) +#pragma optimize("", off) PyObject* _Py_HOT_FUNCTION _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag) { @@ -1082,6 +1082,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int #elif defined(_MSC_VER) /* MS_WINDOWS */ # pragma warning(pop) #endif +#pragma optimize("", on) static void format_missing(PyThreadState *tstate, const char *kind, From 54d99b86dcad50fa159f3288b1dfbe94f7a25757 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 6 Nov 2023 03:57:48 -0500 Subject: [PATCH 3/8] Disable "t" --- Python/ceval.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index 32f263a3567255..430a9a86c441a9 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -664,7 +664,7 @@ extern const struct _PyCode_DEF(8) _Py_InitCleanup; * so consume 3 units of C stack */ #define PY_EVAL_C_STACK_UNITS 2 -#pragma optimize("", off) +#pragma optimize("t", off) PyObject* _Py_HOT_FUNCTION _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag) { From b393cd1ec07c5568e36833c9c8a77721778e0b9d Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 6 Nov 2023 04:47:31 -0500 Subject: [PATCH 4/8] Turn off s --- Python/ceval.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index 430a9a86c441a9..f3143121232884 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -664,7 +664,7 @@ extern const struct _PyCode_DEF(8) _Py_InitCleanup; * so consume 3 units of C stack */ #define PY_EVAL_C_STACK_UNITS 2 -#pragma optimize("t", off) +#pragma optimize("s", off) PyObject* _Py_HOT_FUNCTION _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag) { From eadfe93138c23925eaec802ffa020ab3cef3f85d Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 6 Nov 2023 06:26:04 -0500 Subject: [PATCH 5/8] gh-111786: Do pragmas only in MSC block --- Python/ceval.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index f3143121232884..b90665db064a9f 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -657,6 +657,11 @@ extern const struct _PyCode_DEF(8) _Py_InitCleanup; #elif defined(_MSC_VER) /* MS_WINDOWS */ # pragma warning(push) # pragma warning(disable:4102) +/* gh-111786: _PyEval_EvalFrameDefault is too large to optimize for speed on MSVC. + Disable that optimization temporarily. If this is fixed upstream, we should gate + this on the version of MSVC. + */ +# pragma optimize("t", off) #endif @@ -664,7 +669,6 @@ extern const struct _PyCode_DEF(8) _Py_InitCleanup; * so consume 3 units of C stack */ #define PY_EVAL_C_STACK_UNITS 2 -#pragma optimize("s", off) PyObject* _Py_HOT_FUNCTION _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag) { @@ -1081,8 +1085,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int # pragma GCC diagnostic pop #elif defined(_MSC_VER) /* MS_WINDOWS */ # pragma warning(pop) +# pragma optimize("", on) #endif -#pragma optimize("", on) static void format_missing(PyThreadState *tstate, const char *kind, From 81f7c23899640cec0779581f75120d4c2858cf43 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 7 Nov 2023 21:56:09 -0500 Subject: [PATCH 6/8] Only disable the optimization when doing a PGO build --- PCbuild/build.bat | 1 + PCbuild/pyproject.props | 1 + Python/ceval.c | 14 +++++++++----- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/PCbuild/build.bat b/PCbuild/build.bat index e61267b5852a8f..a84367c2b190c7 100644 --- a/PCbuild/build.bat +++ b/PCbuild/build.bat @@ -171,6 +171,7 @@ rem batch is, shall we say, "lackluster" echo on %MSBUILD% "%dir%pcbuild.proj" /t:%target% %parallel% %verbose%^ /p:Configuration=%conf% /p:Platform=%platf%^ + /p:UsePGO=%do_pgo%^ /p:IncludeExternals=%IncludeExternals%^ /p:IncludeCTypes=%IncludeCTypes%^ /p:IncludeSSL=%IncludeSSL% /p:IncludeTkinter=%IncludeTkinter%^ diff --git a/PCbuild/pyproject.props b/PCbuild/pyproject.props index fb12f2b0766d9b..414fff9821d8e0 100644 --- a/PCbuild/pyproject.props +++ b/PCbuild/pyproject.props @@ -41,6 +41,7 @@ $(PySourcePath)Include;$(PySourcePath)Include\internal;$(PySourcePath)Include\internal\mimalloc;$(PySourcePath)PC;$(IntDir);%(AdditionalIncludeDirectories) WIN32;$(_Py3NamePreprocessorDefinition);$(_PlatformPreprocessorDefinition)$(_DebugPreprocessorDefinition)$(_PydPreprocessorDefinition)%(PreprocessorDefinitions) Py_NOGIL=1;%(PreprocessorDefinitions) + _Py_USING_PGO=1;%(PreprocessorDefinitions) MaxSpeed true diff --git a/Python/ceval.c b/Python/ceval.c index b90665db064a9f..4265bdf2b0692b 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -657,11 +657,6 @@ extern const struct _PyCode_DEF(8) _Py_InitCleanup; #elif defined(_MSC_VER) /* MS_WINDOWS */ # pragma warning(push) # pragma warning(disable:4102) -/* gh-111786: _PyEval_EvalFrameDefault is too large to optimize for speed on MSVC. - Disable that optimization temporarily. If this is fixed upstream, we should gate - this on the version of MSVC. - */ -# pragma optimize("t", off) #endif @@ -669,6 +664,15 @@ extern const struct _PyCode_DEF(8) _Py_InitCleanup; * so consume 3 units of C stack */ #define PY_EVAL_C_STACK_UNITS 2 +#if defined(_MSC_VER) && defined(_Py_USING_PGO) +/* gh-111786: _PyEval_EvalFrameDefault is too large to optimize for speed with + PGO on MSVC. Disable that optimization temporarily. If this is fixed + upstream, we should gate this on the version of MSVC. + */ +# pragma optimize("t", off) +/* This setting is reversed below following _PyEval_EvalFrameDefault */ +#endif + PyObject* _Py_HOT_FUNCTION _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag) { From 009e3b12b8180a2264aca84d78b0836855e9799c Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 7 Nov 2023 22:26:13 -0500 Subject: [PATCH 7/8] No need to pass an extra flag from the batch file --- PCbuild/build.bat | 1 - PCbuild/pyproject.props | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/PCbuild/build.bat b/PCbuild/build.bat index a84367c2b190c7..e61267b5852a8f 100644 --- a/PCbuild/build.bat +++ b/PCbuild/build.bat @@ -171,7 +171,6 @@ rem batch is, shall we say, "lackluster" echo on %MSBUILD% "%dir%pcbuild.proj" /t:%target% %parallel% %verbose%^ /p:Configuration=%conf% /p:Platform=%platf%^ - /p:UsePGO=%do_pgo%^ /p:IncludeExternals=%IncludeExternals%^ /p:IncludeCTypes=%IncludeCTypes%^ /p:IncludeSSL=%IncludeSSL% /p:IncludeTkinter=%IncludeTkinter%^ diff --git a/PCbuild/pyproject.props b/PCbuild/pyproject.props index 414fff9821d8e0..5c63192032c5d5 100644 --- a/PCbuild/pyproject.props +++ b/PCbuild/pyproject.props @@ -41,7 +41,7 @@ $(PySourcePath)Include;$(PySourcePath)Include\internal;$(PySourcePath)Include\internal\mimalloc;$(PySourcePath)PC;$(IntDir);%(AdditionalIncludeDirectories) WIN32;$(_Py3NamePreprocessorDefinition);$(_PlatformPreprocessorDefinition)$(_DebugPreprocessorDefinition)$(_PydPreprocessorDefinition)%(PreprocessorDefinitions) Py_NOGIL=1;%(PreprocessorDefinitions) - _Py_USING_PGO=1;%(PreprocessorDefinitions) + _Py_USING_PGO=1;%(PreprocessorDefinitions) MaxSpeed true From fb2908964aeb9774102faf0d33dcf51d4aae87f7 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 7 Nov 2023 22:31:03 -0500 Subject: [PATCH 8/8] Fix condition --- PCbuild/pyproject.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PCbuild/pyproject.props b/PCbuild/pyproject.props index 5c63192032c5d5..bb3555bd123089 100644 --- a/PCbuild/pyproject.props +++ b/PCbuild/pyproject.props @@ -41,7 +41,7 @@ $(PySourcePath)Include;$(PySourcePath)Include\internal;$(PySourcePath)Include\internal\mimalloc;$(PySourcePath)PC;$(IntDir);%(AdditionalIncludeDirectories) WIN32;$(_Py3NamePreprocessorDefinition);$(_PlatformPreprocessorDefinition)$(_DebugPreprocessorDefinition)$(_PydPreprocessorDefinition)%(PreprocessorDefinitions) Py_NOGIL=1;%(PreprocessorDefinitions) - _Py_USING_PGO=1;%(PreprocessorDefinitions) + _Py_USING_PGO=1;%(PreprocessorDefinitions) MaxSpeed true