Skip to content

runtime: allow over-aligned types in the runtime #42143

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

Merged
merged 2 commits into from
Apr 6, 2022

Conversation

compnerd
Copy link
Member

@compnerd compnerd commented Apr 1, 2022

Not all targets have a 16-byte type alignment guarantee. For the types
which are not naturally aligned, provide a type specific operator new
overload to ensure that we are properly aligning the type on allocation
as we run the risk of under-aligned allocations otherwise.

This should no longer be needed with C++17 and newer which do a two
phase operator new lookup preferring
operator new(std::size, std::align_val_t) if needed. The base type
would be fully pre-processed away. The empty base class optimization
should help ensure that we do not pay any extra size costs for the
alignment fixes.

As we are a C++14 codebase, we must locally implement some of the
standard type_traits utilities, namely void_t. We take the minimal
definition here, assuming that the compiler is up-to-date with C++14 DR
reports which fixed an issue in SFINAE. We use the SFINAE for detecting
the presence of the operator new overload to guide the over-alignment,
which is inherited through the new swift::overaligned_type<> base
type.

Annotate the known classes which request explicit alignment which is
non-pointer alignment. This list was identified by
git grep ' alignas(.*) '.

@compnerd compnerd changed the title WIP: runtime: support over-aligned types without aligned operator new runtime: allow over-aligned types in the runtime Apr 2, 2022
@compnerd
Copy link
Member Author

compnerd commented Apr 2, 2022

CC: @rjmccall @mikeash

@compnerd compnerd force-pushed the malign branch 3 times, most recently from aa3db3e to 4ad6e2d Compare April 3, 2022 17:22
@compnerd
Copy link
Member Author

compnerd commented Apr 3, 2022

@swift-ci please test

@compnerd
Copy link
Member Author

compnerd commented Apr 3, 2022

@swift-ci please test

@compnerd
Copy link
Member Author

compnerd commented Apr 3, 2022

@swift-ci please clean test Linux platform

@compnerd
Copy link
Member Author

compnerd commented Apr 4, 2022

@swift-ci please test

@compnerd
Copy link
Member Author

compnerd commented Apr 4, 2022

@swift-ci please test

Copy link
Contributor

@mikeash mikeash left a comment

Choose a reason for hiding this comment

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

LGTM, but my c++ template knowledge isn't good enough to properly evaluate that part of it.

@compnerd
Copy link
Member Author

compnerd commented Apr 4, 2022

@swift-ci please test Windows platform

@compnerd
Copy link
Member Author

compnerd commented Apr 4, 2022

@swift-ci please test

@compnerd
Copy link
Member Author

compnerd commented Apr 4, 2022

@swift-ci please test Windows platform

@compnerd
Copy link
Member Author

compnerd commented Apr 4, 2022

@swift-ci please test

@compnerd
Copy link
Member Author

compnerd commented Apr 5, 2022

@swift-ci please test

Not all targets have a 16-byte type alignment guarantee.  For the types
which are not naturally aligned, provide a type specific `operator new`
overload to ensure that we are properly aligning the type on allocation
as we run the risk of under-aligned allocations otherwise.

This should no longer be needed with C++17 and newer which do a two
phase `operator new` lookup preferring
`operator new(std::size, std::align_val_t)` if needed.  The base type
would be fully pre-processed away.  The empty base class optimization
should help ensure that we do not pay any extra size costs for the
alignment fixes.

As we are a C++14 codebase, we must locally implement some of the
standard type_traits utilities, namely `void_t`.  We take the minimal
definition here, assuming that the compiler is up-to-date with C++14 DR
reports which fixed an issue in SFINAE.  We use the SFINAE for detecting
the presence of the `operator new` overload to guide the over-alignment,
which is inherited through the new `swift::overaligned_type<>`  base
type.

Annotate the known classes which request explicit alignment which is
non-pointer alignment.  This list was identified by
`git grep ' alignas(.*) '`.
@compnerd
Copy link
Member Author

compnerd commented Apr 5, 2022

@swift-ci please test Linux platform

@compnerd
Copy link
Member Author

compnerd commented Apr 5, 2022

@swift-ci please test macOS

@compnerd
Copy link
Member Author

compnerd commented Apr 5, 2022

@swift-ci please test Windows platform

@compnerd
Copy link
Member Author

compnerd commented Apr 5, 2022

@swift-ci please test macOS platform

@compnerd
Copy link
Member Author

compnerd commented Apr 5, 2022

@swift-ci please test Windows platform

@compnerd
Copy link
Member Author

compnerd commented Apr 5, 2022

@swift-ci please test macOS platform

@compnerd
Copy link
Member Author

compnerd commented Apr 6, 2022

@swift-ci please test

@compnerd
Copy link
Member Author

compnerd commented Apr 6, 2022

@swift-ci please test macOS platform

@compnerd
Copy link
Member Author

compnerd commented Apr 6, 2022

@swift-ci please test Windows platform

@compnerd compnerd merged commit d7a5e2b into swiftlang:main Apr 6, 2022
@compnerd compnerd deleted the malign branch April 6, 2022 14:58
@finagolfin
Copy link
Member

It appears this pull broke building the stdlib for Android armv7, both on the community CI and my Android CI that builds a standalone SDK.

@compnerd, what do you think, does this work for Win32?

@compnerd
Copy link
Member Author

compnerd commented Apr 13, 2022

This does work for Win32. Interesting failure - what happens if you add Android to the Win32 case (assuming you get to the rest before I do) for the invalid overload of the placement new operator?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants