-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix alignment issues when allocating in AtomicWaitQueue
#42355
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
Conversation
787eacd
to
c616d73
Compare
@swift-ci please test |
There are a couple of typos in this PR but they don't affect the correctness of the code, so I'm going to wait until the current CI builds complete before fixing. |
c616d73
to
580210e
Compare
@swift-ci please test |
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.
LGTM
LGTM. I'd like to suggest different names for the new functions. The "slow" in the existing functions is kind of leftover so I'd suggest we not bring that forward, and I'd like to make the names more clear about doing the same thing as new/delete. Perhaps |
…on even when the compiler does not support the C++17 over-aligned new feature (and avoid using new anyway since it might be overridden by something else in the process.)
580210e
to
3f24533
Compare
@swift-ci please test |
Mike and I discussed and settled on |
@swift-ci please test windows |
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 definitely is a step in the right direction! This should save us from the global operator new replacement shenanigans and also module specific allocators.
The Windows build failure appears unrelated to this PR. Will try a rebuild in case it's been resolved on main, but otherwise I don't think it holds up merging. |
@swift-ci please test windows |
The Windows issue is indeed present on main, so merging! |
This change ensures that
AtomicWaitQueue
allocates its inner queues in an aligned fashion even when the compiler does not support the C++17 "over-aligned new" feature, and avoids usingnew
anyway since it might be overridden by something else in the process.On some platforms, the compiler does not support the C++17 "over-aligned new" feature. Allocating an instance of type
T
withnew T()
will produce a naturally-aligned pointer, which appears to be insufficient for use withAtomicWaitQueue
on said platforms. Windows and Android in particular have been identified as affected.The solution is to explicitly callstd::aligned_alloc()
(or, if the type's size is somehow not a multiple of its alignment,std::malloc()
) and to use placement new to initialize the new value. Destruction is accomplished by directly calling the type's destructor and then passing the pointer tostd::free()
.Since
swift_slowAlloc()
andswift_slowDealloc()
handle alignment already and are the preferred allocation mechanisms in Swift, sub them in forstd::aligned_alloc()
andstd::free()
above.This fix has the added benefit of avoiding the global
operator new
, which can be overridden by other code in the process. If the globaloperator new
is overridden in such a way that it re-enters the Swift runtime, sadness will result.