Skip to content

Commit f968f1a

Browse files
committed
[probably not for upstream] CodeGen, libcxx: Introduce __has_non_relocatable_fields.
In order to properly benchmark PFP we need to be able to do an A/B comparison and make A and B as equal as possible. This includes not disabling libc++'s non-standard trivially relocatable optimizations wherever possible. In the upstream code we just made trivially_relocatable equivalent to trivially_copyable but in the benchmarking version of the code (also more aligned to what will be possible in C++26) restore trivially_relocatable logic but also check whether any of the fields are non-relocatable using a temporarily introduced builtin.
1 parent 9bbf4a7 commit f968f1a

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

clang/include/clang/Basic/TokenKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ TYPE_TRAIT_2(__is_pointer_interconvertible_base_of, IsPointerInterconvertibleBas
544544
#include "clang/Basic/TransformTypeTraits.def"
545545

546546
// Clang-only C++ Type Traits
547+
TYPE_TRAIT_1(__has_non_relocatable_fields, HasNonRelocatableFields, KEYCXX)
547548
TYPE_TRAIT_1(__is_trivially_equality_comparable, IsTriviallyEqualityComparable, KEYCXX)
548549
TYPE_TRAIT_1(__is_bounded_array, IsBoundedArray, KEYCXX)
549550
TYPE_TRAIT_1(__is_unbounded_array, IsUnboundedArray, KEYCXX)

clang/lib/Sema/SemaTypeTraits.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT,
533533
case UTT_IsTriviallyEqualityComparable:
534534
case UTT_IsCppTriviallyRelocatable:
535535
case UTT_IsReplaceable:
536+
case UTT_HasNonRelocatableFields:
536537
case UTT_CanPassInRegs:
537538
// Per the GCC type traits documentation, T shall be a complete type, cv void,
538539
// or an array of unknown bound. But GCC actually imposes the same constraints
@@ -1132,6 +1133,10 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
11321133
return C.hasUniqueObjectRepresentations(T);
11331134
case UTT_IsTriviallyRelocatable:
11341135
return IsTriviallyRelocatableType(Self, T);
1136+
case UTT_HasNonRelocatableFields:
1137+
return T->getAsCXXRecordDecl() &&
1138+
!C.arePFPFieldsTriviallyRelocatable(T->getAsCXXRecordDecl()) &&
1139+
C.hasPFPFields(T);
11351140
case UTT_IsBitwiseCloneable:
11361141
return T.isBitwiseCloneableType(C);
11371142
case UTT_IsCppTriviallyRelocatable:

libcxx/include/__type_traits/is_trivially_relocatable.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ template <class _Tp, class = void>
3434
struct __libcpp_is_trivially_relocatable : is_trivially_copyable<_Tp> {};
3535
#endif
3636

37-
// __trivially_relocatable on libc++'s builtin types does not currently return the right answer with PFP.
38-
#if !__has_feature(pointer_field_protection)
3937
template <class _Tp>
4038
struct __libcpp_is_trivially_relocatable<_Tp,
4139
__enable_if_t<is_same<_Tp, typename _Tp::__trivially_relocatable>::value
42-
__enable_if_t<is_same<_Tp, typename _Tp::__trivially_relocatable>::value> >
43-
: true_type {};
40+
#if __has_builtin(__has_non_relocatable_fields)
41+
&& !__has_non_relocatable_fields(_Tp)
4442
#endif
43+
> > : true_type {
44+
};
4545

4646
_LIBCPP_END_NAMESPACE_STD
4747

0 commit comments

Comments
 (0)