Skip to content

gh-161467: Enable __attribute__((hot)) for Clang - #161468

Open
chenshanzhi wants to merge 4 commits into
python:mainfrom
chenshanzhi:enable-hot-attr-for-clang
Open

chenshanzhi wants to merge 4 commits into
python:mainfrom
chenshanzhi:enable-hot-attr-for-clang

Conversation

@chenshanzhi

@chenshanzhi chenshanzhi commented Nov 6, 2026 •

Copy link
Copy Markdown

The history of compiler support for the relevant features is as follows.

Therefore, __attribute__((hot)) is not enabled when building CPython with Clang's default configuration. And we can use _Py__has_attribute(hot) or GCC version >= 4.3 to determine whether the hot attribute is supported, and enable this attribute for Clang and other compilers that support it.

The history of compiler support for the relevant features is as follows.
* GCC introduced `__attribute__((hot))` in GCC 4.3.0.
  * https://gcc.gnu.org/pipermail/gcc-patches/2017-July/478580.html
* GCC has supported for `__has_attribute()` since GCC 5.
  * https://gcc.gnu.org/gcc-5/changes.html
* Clang has supported `__attribute__((hot))` since Clang 3.2.
  * llvm/llvm-project@29c2b43
* Clang has supported `__has_attribute()` since Clang 2.9.
  * llvm/llvm-project@274a70e
* By default, Clang defines `__GNUC__` as 4, `__GNUC_MINOR__` as 2, and
  `__GNUC_PATCHLEVEL__` as 1.
  * llvm/llvm-project#99804

Therefore, `__attribute__((hot))` is not enabled when building CPython
with Clang's default configuration. And we can use
`_Py__has_attribute(hot)` or `GCC version >= 4.3` to determine whether
the hot attribute is supported, and enable this attribute for Clang and
other compilers that support it.
@python-cla-bot

python-cla-bot Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Nov 6, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@StanFromIreland

Copy link
Copy Markdown
Member

Can you please show PyPerformance changes with this, both on a release build and on a PGO build?

@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 6, 2026
Comment thread Include/pyport.h
Comment on lines +314 to 319
#if _Py__has_attribute(hot) || defined(__GNUC__) \
&& ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))
#define _Py_HOT_FUNCTION __attribute__((hot))
#else
#define _Py_HOT_FUNCTION
#endif

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.

GCC 5 was releasesd 11 years, IMO it's ok to drop this optimization on GCC 5 which shouldn't be used in the wild anymore. So I suggest to simplify the code (and add indentation for readability):

Suggested change
#if _Py__has_attribute(hot)
# define _Py_HOT_FUNCTION __attribute__((hot))
#else
# define _Py_HOT_FUNCTION
#endif

Note: I'm the one who added _Py_HOT_FUNCTION to Python a few years ago :-)

@vstinner

vstinner commented Nov 6, 2026

Copy link
Copy Markdown
Member

Can you please show PyPerformance changes with this, both on a release build and on a PGO build?

When using PGO, __attribute__((hot)) is not really interesting (I think that the compiler can ignore it, but I'm not sure).

I'm not sure that __attribute__((hot)) has a major impact on performance. I added it but later, I mostly relied on PGO when I really wanted to measure performance and test an optimization.

__attribute__((hot)) helps a little bit for quick & dirty benchmark session using ./configure (gcc -O3).

@StanFromIreland

Copy link
Copy Markdown
Member

The comment above says:

Issue #28618: This attribute must not be abused, otherwise it can have a negative effect on performance.

We don't know how Clang will behave/optimise, so I think we should be cautious and verify there is no regression before merging.

Comment thread Include/pyport.h
* Usage:
* int _Py_HOT_FUNCTION x(void) { return 3; }
*
* Issue #28618: This attribute must not be abused, otherwise it can have a

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.

While you're here, can you update the issue number to issue gh-72804 please? See: #72804

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants