Skip to content

Commit f7830fb

Browse files
committed
Merge commit '7cde97423347' from llvm.org/main into next
# Conflicts: # clang/include/clang/Basic/Features.def
2 parents 199112b + 7cde974 commit f7830fb

19 files changed

+253
-93
lines changed

clang/docs/CXXTypeAwareAllocators.rst

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
=========================
2+
C++ Type Aware Allocators
3+
=========================
4+
5+
.. contents::
6+
:local:
7+
8+
Introduction
9+
============
10+
11+
Clang includes an implementation of P2719 "Type-aware allocation and deallocation
12+
functions".
13+
14+
This is a feature that extends the semantics of `new`, `new[]`, `delete` and
15+
`delete[]` operators to expose the type being allocated to the operator. This
16+
can be used to customize allocation of types without needing to modify the
17+
type declaration, or via template definitions fully generic type aware
18+
allocators.
19+
20+
P2719 introduces a type-identity tag as valid parameter type for all allocation
21+
operators. This tag is a default initialized value of type `std::type_identity<T>`
22+
where T is the type being allocated or deallocated. Unlike the other placement
23+
arguments this tag is passed as the first parameter to the operator.
24+
25+
The most basic use case is as follows
26+
27+
.. code-block:: c++
28+
29+
#include <new>
30+
#include <type_traits>
31+
32+
struct S {
33+
// ...
34+
};
35+
36+
void *operator new(std::type_identity<S>, size_t, std::align_val_t);
37+
void operator delete(std::type_identity<S>, void *, size_t, std::align_val_t);
38+
39+
void f() {
40+
S *s = new S; // calls ::operator new(std::type_identity<S>(), sizeof(S), alignof(S))
41+
delete s; // calls ::operator delete(std::type_identity<S>(), s, sizeof(S), alignof(S))
42+
}
43+
44+
While this functionality alone is powerful and useful, the true power comes
45+
by using templates. In addition to adding the type-identity tag, P2719 allows
46+
the tag parameter to be a dependent specialization of `std::type_identity`,
47+
updates the overload resolution rules to support full template deduction and
48+
constraint semantics, and updates the definition of usual deallocation functions
49+
to include `operator delete` definitions that are templatized on the
50+
type-identity tag.
51+
52+
This allows arbitrarily constrained definitions of the operators that resolve
53+
as would be expected for any other template function resolution, e.g (only
54+
showing `operator new` for brevity)
55+
56+
.. code-block:: c++
57+
58+
template <typename T, unsigned Size> struct Array {
59+
T buffer[Size];
60+
};
61+
62+
// Starting with a concrete type
63+
void *operator new(std::type_identity<Array<int, 5>>, size_t, std::align_val_t);
64+
65+
// Only care about five element arrays
66+
template <typename T>
67+
void *operator new(std::type_identity<Array<T, 5>>, size_t, std::align_val_t);
68+
69+
// An array of N floats
70+
template <unsigned N>
71+
void *operator new(std::type_identity<Array<float, N>>, size_t, std::align_val_t);
72+
73+
// Any array
74+
template <typename T, unsigned N>
75+
void *operator new(std::type_identity<Array<T, N>>, size_t, std::align_val_t);
76+
77+
// A handy concept
78+
template <typename T> concept Polymorphic = std::is_polymorphic_v<T>;
79+
80+
// Only applies is T is Polymorphic
81+
template <Polymorphic T, unsigned N>
82+
void *operator new(std::type_identity<Array<T, N>>, size_t, std::align_val_t);
83+
84+
// Any even length array
85+
template <typename T, unsigned N>
86+
void *operator new(std::type_identity<Array<T, N>>, size_t, std::align_val_t)
87+
requires(N%2 == 0);
88+
89+
Operator selection then proceeds according to the usual rules for choosing
90+
the best/most constrained match.
91+
92+
Any declaration of a type aware operator new or operator delete must include a
93+
matching complimentary operator defined in the same scope.
94+
95+
Notes
96+
=====
97+
98+
Unconstrained Global Operators
99+
------------------------------
100+
101+
Declaring an unconstrained type aware global operator `new` or `delete` (or
102+
`[]` variants) creates numerous hazards, similar to, but different from, those
103+
created by attempting to replace the non-type aware global operators. For that
104+
reason unconstrained operators are strongly discouraged.
105+
106+
Mismatching Constraints
107+
-----------------------
108+
109+
When declaring global type aware operators you should ensure the constraints
110+
applied to new and delete match exactly, and declare them together. This
111+
limits the risk of having mismatching operators selected due to differing
112+
constraints resulting in changes to prioritization when determining the most
113+
viable candidate.
114+
115+
Declarations Across Libraries
116+
-----------------------------
117+
118+
Declaring a typed allocator for a type in a separate TU or library creates
119+
similar hazards as different libraries and TUs may see (or select) different
120+
definitions.
121+
122+
Under this model something like this would be risky
123+
124+
.. code-block:: c++
125+
126+
template<typename T>
127+
void *operator new(std::type_identity<std::vector<T>>, size_t, std::align_val_t);
128+
129+
However this hazard is not present simply due to the use of the a type from
130+
another library:
131+
132+
.. code-block:: c++
133+
134+
template<typename T>
135+
struct MyType {
136+
T thing;
137+
};
138+
template<typename T>
139+
void *operator new(std::type_identity<MyType<std::vector<T>>>, size_t, std::align_val_t);
140+
141+
Here we see `std::vector` being used, but that is not the actual type being
142+
allocated.
143+
144+
Implicit and Placement Parameters
145+
---------------------------------
146+
147+
Type aware allocators are always passed both the implicit alignment and size
148+
parameters in all cases. Explicit placement parameters are supported after the
149+
mandatory implicit parameters.
150+
151+
Publication
152+
===========
153+
154+
`Type-aware allocation and deallocation functions <https://wg21.link/P2719>`_.
155+
Louis Dionne, Oliver Hunt.

clang/docs/LanguageExtensions.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Clang Language Extensions
1515
AutomaticReferenceCounting
1616
PointerAuthentication
1717
MatrixTypes
18+
CXXTypeAwareAllocators
1819

1920
Introduction
2021
============
@@ -1540,6 +1541,13 @@ Use ``__has_feature(cxx_variable_templates)`` or
15401541
``__has_extension(cxx_variable_templates)`` to determine if support for
15411542
templated variable declarations is enabled.
15421543

1544+
C++ type aware allocators
1545+
^^^^^^^^^^^^^^^^^^^^^^^^^
1546+
1547+
Use ``__has_extension(cxx_type_aware_allocators)`` to determine the existence of
1548+
support for the future C++2d type aware allocator feature. For full details see
1549+
:doc:`C++ Type Aware Allocators <CXXTypeAwareAllocators>` for additional details.
1550+
15431551
C11
15441552
---
15451553

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ C++2c Feature Support
133133

134134
- Implemented `P0963R3 Structured binding declaration as a condition <https://wg21.link/P0963R3>`_.
135135

136-
- Implemented `P2719R4 Type-aware allocation and deallocation functions <https://wg21.link/P2719>`_.
137-
138136
- Implemented `P3618R0 Allow attaching main to the global module <https://wg21.link/P3618>`_.
139137

140138
C++23 Feature Support
@@ -1196,6 +1194,9 @@ New features
11961194
so frequent 'not yet implemented' diagnostics should be expected. Also, the
11971195
ACC MLIR dialect does not currently implement any lowering to LLVM-IR, so no
11981196
code generation is possible for OpenACC.
1197+
- Implemented `P2719R5 Type-aware allocation and deallocation functions <https://wg21.link/P2719>`_
1198+
as an extension in all C++ language modes.
1199+
11991200

12001201
Crash and bug fixes
12011202
^^^^^^^^^^^^^^^^^^^

clang/docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Using Clang as a Compiler
6161
APINotes
6262
DebuggingCoroutines
6363
AMDGPUSupport
64+
CXXTypeAwareAllocators
6465
CommandGuide/index
6566
FAQ
6667

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10153,11 +10153,8 @@ def err_destroying_operator_delete_not_usual : Error<
1015310153
def err_type_aware_destroying_operator_delete : Error<
1015410154
"destroying delete is not permitted to be type aware">;
1015510155

10156-
def ext_cxx26_type_aware_allocators : ExtWarn<
10157-
"type aware allocators are a C++2c extension">, InGroup<CXX26>;
10158-
def warn_cxx26_type_aware_allocators : Warning<
10159-
"type aware allocators are incompatible with C++ standards before C++2c">,
10160-
DefaultIgnore, InGroup<CXXPre26Compat>;
10156+
def warn_ext_type_aware_allocators : ExtWarn<
10157+
"type aware allocators are a Clang extension">, InGroup<DiagGroup<"ext-cxx-type-aware-allocators">>;
1016110158
def err_type_aware_allocator_missing_matching_operator : Error<
1016210159
"declaration of type aware %0 in %1 must have matching type aware %2"
1016310160
>;

clang/include/clang/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ FEATURE(clang_atomic_attributes, true)
374374
FEATURE(cuda_noinline_keyword, LangOpts.CUDA)
375375
EXTENSION(cuda_implicit_host_device_templates, LangOpts.CUDA && LangOpts.OffloadImplicitHostDeviceTemplates)
376376

377+
// C++2d type-aware allocators
378+
EXTENSION(cxx_type_aware_allocators, true)
379+
377380
// TODO(BoundsSafety): Deprecate this feature name
378381
FEATURE(bounds_attributes, LangOpts.BoundsSafety)
379382
/* TO_UPSTREAM(BoundsSafety) ON*/

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,6 @@ static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
779779
if (LangOpts.Char8)
780780
Builder.defineMacro("__cpp_char8_t", "202207L");
781781
Builder.defineMacro("__cpp_impl_destroying_delete", "201806L");
782-
783-
// TODO: Final number?
784-
Builder.defineMacro("__cpp_type_aware_allocators", "202500L");
785782
}
786783

787784
/// InitializeOpenCLFeatureTestMacros - Define OpenCL macros based on target

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16541,12 +16541,8 @@ static inline bool CheckOperatorNewDeleteTypes(
1654116541
if (IsPotentiallyTypeAware) {
1654216542
// We don't emit this diagnosis for template instantiations as we will
1654316543
// have already emitted it for the original template declaration.
16544-
if (!FnDecl->isTemplateInstantiation()) {
16545-
unsigned DiagID = SemaRef.getLangOpts().CPlusPlus26
16546-
? diag::warn_cxx26_type_aware_allocators
16547-
: diag::ext_cxx26_type_aware_allocators;
16548-
SemaRef.Diag(FnDecl->getLocation(), DiagID);
16549-
}
16544+
if (!FnDecl->isTemplateInstantiation())
16545+
SemaRef.Diag(FnDecl->getLocation(), diag::warn_ext_type_aware_allocators);
1655016546

1655116547
if (OperatorKind == AllocationOperatorKind::New) {
1655216548
SizeParameterIndex = 1;

clang/test/SemaCXX/type-aware-class-scoped-mismatched-constraints.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
// RUN: %clang_cc1 -triple arm64-apple-macosx -fsyntax-only -verify %s -std=c++26 -fexceptions -fcxx-exceptions -fsized-deallocation -faligned-allocation -Wno-non-c-typedef-for-linkage -DDEFAULT_DELETE=1
2-
// RUN: %clang_cc1 -triple arm64-apple-macosx -fsyntax-only -verify %s -std=c++26 -fexceptions -fcxx-exceptions -fno-sized-deallocation -faligned-allocation -Wno-non-c-typedef-for-linkage -DDEFAULT_DELETE=0
3-
// RUN: %clang_cc1 -triple arm64-apple-macosx -fsyntax-only -verify %s -std=c++26 -fexceptions -fcxx-exceptions -fsized-deallocation -fno-aligned-allocation -Wno-non-c-typedef-for-linkage -DDEFAULT_DELETE=1
4-
// RUN: %clang_cc1 -triple arm64-apple-macosx -fsyntax-only -verify %s -std=c++26 -fexceptions -fcxx-exceptions -fno-sized-deallocation -fno-aligned-allocation -Wno-non-c-typedef-for-linkage -DDEFAULT_DELETE=0
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fcxx-exceptions -fsized-deallocation -faligned-allocation -Wno-non-c-typedef-for-linkage -DDEFAULT_DELETE=1
2+
// RUN: %clang_cc1 -triple arm64-apple-macosx -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fcxx-exceptions -fno-sized-deallocation -faligned-allocation -Wno-non-c-typedef-for-linkage -DDEFAULT_DELETE=0
3+
// RUN: %clang_cc1 -triple arm64-apple-macosx -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fcxx-exceptions -fsized-deallocation -fno-aligned-allocation -Wno-non-c-typedef-for-linkage -DDEFAULT_DELETE=1
4+
// RUN: %clang_cc1 -triple arm64-apple-macosx -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fcxx-exceptions -fno-sized-deallocation -fno-aligned-allocation -Wno-non-c-typedef-for-linkage -DDEFAULT_DELETE=0
55

66
namespace std {
77
template <class T> struct type_identity {};
88
enum class align_val_t : __SIZE_TYPE__ {};
99
}
1010

11+
static_assert(__has_extension(cxx_type_aware_allocators), "Verifying the type aware extension flag is set");
12+
1113
using size_t = __SIZE_TYPE__;
1214

1315
void *operator new(size_t); // #default_operator_new

clang/test/SemaCXX/type-aware-coroutines.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple arm64-apple-macosx -fsyntax-only -verify %s -std=c++26 -fcoroutines -fexceptions -Wall -Wpedantic
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fcoroutines -fexceptions -Wall -Wpedantic
22

33

44
#include "Inputs/std-coroutine.h"

0 commit comments

Comments
 (0)