GeezerVison is showing github.com — gh-101525: Use only safe identical code folding with BOLT by geofft · Pull Request #134642 · python/cpython
Conversation
geofft
commented
May 25, 2025
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
"Identical code folding" (ICF) is the feature of an optimizer to find that two functions have the same code and that they can therefore be deduplicated in the binary. While this is usually safe, it can cause observable behavior differences if the program relies on the fact that the two functions have different addresses.
CPython relies on this in (at least) Objects/typeobject.c, which defines two functions wrap_binaryfunc() and wrap_binaryfunc_l() with the same implementation, and stores their addresses in the slotdefs array. If these two functions have the same address, update_one_slot() in that file will fill in slots it shouldn't, causing, for instances, classes defined in Python that inherit from some built-in types to misbehave.
As of LLVM 20 (llvm/llvm-project#116275), BOLT has a "safe ICF" mode, where it looks to see if there are any uses of a function symbol outside function calls (e.g., relocations in data sections) and skips ICF on such functions. The intent is that this avoids observable behavior differences but still saves storage as much as possible.
This version is about two months old at the time of writing. To support older LLVM versions, we have to turn off ICF entirely.
This problem was previously noticed for Windows/MSVC in #53093 (and again in #24098), where the default behavior of PGO is to enable ICF (which they expand to "identical COMDAT folding") and we had to turn it off.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
corona10 and erlend-aasland reacted with thumbs up emoji
"Identical code folding" (ICF) is the feature of an optimizer to find that two
functions have the same code and that they can therefore be deduplicated
in the binary. While this is usually safe, it can cause observable
behavior differences if the program relies on the fact that the two
functions have different addresses.
CPython relies on this in (at least) Objects/typeobject.c, which defines
two functions wrap_binaryfunc() and wrap_binaryfunc_l() with the same
implementation, and stores their addresses in the slotdefs array. If
these two functions have the same address, update_one_slot() in that
file will fill in slots it shouldn't, causing, for instances,
classes defined in Python that inherit from some built-in types to
misbehave.
As of LLVM 20 (llvm/llvm-project#116275), BOLT has a "safe ICF" mode,
where it looks to see if there are any uses of a function symbol outside
function calls (e.g., relocations in data sections) and skips ICF on
such functions. The intent is that this avoids observable behavior
differences but still saves storage as much as possible.
This version is about two months old at the time of writing. To support
older LLVM versions, we have to turn off ICF entirely.
This problem was previously noticed for Windows/MSVC in python#53093 (and
again in python#24098), where the default behavior of PGO is to enable ICF
(which they expand to "identical COMDAT folding") and we had to turn it
off.
|
cc @zanieb
fyi @kmod who originally added -icf=1 to the BOLT flags
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced May 25, 2025
corona10
changed the title
Use only safe identical code folding with BOLT
gh-101525: Use only safe identical code folding with BOLT
May 26, 2025
Uh oh!
There was an error while loading. Please reload this page.
geofft
added a commit
to astral-sh/python-build-standalone
that referenced
this pull request
May 30, 2025
astral-sh/uv#13610 reported a misbehavior that is the result of a
subclass of str incorrectly having its ->tp_as_number->nb_add slot
filled in with the value of PyUnicode_Type->tp_as_sequence->sq_concat.
There are some times when this is an appropriate thing to do iwhen
subclassing, but this is not one of them. The logic to prevent it in
this case relies on two helper functions in the file, wrap_binaryfunc
and wrap_binaryfunc_l, having different addresses, even though they
contain identical code.
For some reason BOLT does not do this optimization in the shared library
(even though those are static functions and not exported), so we only
started seeing this in the static build.
BOLT in LLVM 20+ supports "safe" code folding, which uses heuristics about
relocations to determine whether a function's address is used in any way other
than a call. This seems to be enough to fix the issue. Add a patch to switch
to -icf=safe, submitted upstream as python/cpython#134642
corona10
left a comment
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for digging this issue, Now BOLT build be more stable :)
LGTM.
I like this change and it's real issue: astral-sh/python-build-standalone#622
But I am not sure this is best autoconf that we add, I will wait @erlend-aasland 's review also.
Erlend, the change looks good. Can you review, focusing on the autoconf code itself?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
erlend-aasland reacted with thumbs up emoji
Uh oh!
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 30 days with no activity.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@geofft do you want to pick this back up? I believe applying Erlend's suggestion should be sufficient to move this forward.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 90 days with no activity.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@geofft Are you okay if I pick up this work? I can create a new PR from my fork or add commits to your branch.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Merged with main and applied Erlend's suggestion, I think this is ready for review now, sorry for the delay! (Also in general feel free to pick up any of my branches)
@erlend-aasland, I assume that I used the [[]] syntax because I copied from a few other spots in configure.ac that use AC_LANG_PROGRAM([[]], [[]]). Should all of those also be AC_LANG_PROGRAM([], [])?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Also I added a blurb because #148833 which switched to -icf=0 (which landed in the meantime) added a blurb. Originally I figured this was an internal / non-default feature and didn't add a blurb at all. I'm not sure what's expected here, if I ought to edit or delete the other blurb I can do that.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks good, except for the issue with the CFLAGS and LDFLAGS (see inline comment). also need to merge main.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Comment on lines
+2240
to
+2241
|
|
CFLAGS="$CFLAGS_NODIST"
|
|
|
LDFLAGS="$LDFLAGS_NODIST"
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this discards the existing CFLAGS and LDFLAGS - they should be preserved when adding the _NODIST flags.
|
CFLAGS="$CFLAGS_NODIST" |
|
LDFLAGS="$LDFLAGS_NODIST" |
|
CFLAGS="$CFLAGS $CFLAGS_NODIST" |
|
LDFLAGS="$LDFLAGS $LDFLAGS_NODIST" |
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.
Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
corona10
corona10 approved these changes
AA-Turner
Awaiting requested review from AA-Turner
AA-Turner is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|