AK: Add abstraction for forcing empty base optimization on Windows

Without this annotation, the MSVC ABI is reluctant to apply EBO even
in cases that are basically guaranteed on the Itanium ABI by modern
compilers. This fixes an UBSAN issue with Variant on Windows.
This commit is contained in:
ayeteadoe 2025-06-16 15:08:50 -06:00 committed by Andrew Kaster
commit 3fd05306fc
Notes: github-actions[bot] 2025-06-17 21:35:02 +00:00
3 changed files with 10 additions and 3 deletions

View file

@ -249,6 +249,13 @@
# define NO_UNIQUE_ADDRESS [[no_unique_address]]
#endif
#if defined(AK_OS_WINDOWS)
// https://learn.microsoft.com/en-us/cpp/cpp/empty-bases?view=msvc-170
# define AK_COMPACT_EMPTY_BASES __declspec(empty_bases)
#else
# define AK_COMPACT_EMPTY_BASES
#endif
// GCC doesn't have __has_feature but clang does
#ifndef __has_feature
# define __has_feature(...) 0

View file

@ -59,7 +59,7 @@ private:
};
template<typename T, typename... TRest>
struct Tuple<T, TRest...> : Tuple<TRest...> {
struct AK_COMPACT_EMPTY_BASES Tuple<T, TRest...> : Tuple<TRest...> {
template<typename FirstT, typename... RestT>
Tuple(FirstT&& first, RestT&&... rest)
@ -113,7 +113,7 @@ private:
namespace AK {
template<typename... Ts>
struct Tuple : Detail::Tuple<Ts...> {
struct AK_COMPACT_EMPTY_BASES Tuple : Detail::Tuple<Ts...> {
using Types = TypeList<Ts...>;
using Detail::Tuple<Ts...>::Tuple;
using Indices = MakeIndexSequence<sizeof...(Ts)>;

View file

@ -478,7 +478,7 @@ private:
}
template<typename... Fs>
struct Visitor : Fs... {
struct AK_COMPACT_EMPTY_BASES Visitor : Fs... {
using Types = TypeList<Fs...>;
Visitor(Fs&&... args)