Debian Codegraph @

clang/test/CodeGenCXX/type-aware-allocators.cpp

raw
1 // RUN: %clang_cc1 %s -triple arm64-apple-macosx -fsized-deallocation -faligned-allocation -emit-llvm -fcxx-exceptions -fexceptions -std=c++23 -o - -Wno-c++26-extensions | FileCheck --check-prefixes=CHECK,CHECK_SIZED_ALIGNED %s
2 // RUN: %clang_cc1 %s -triple arm64-apple-macosx -fno-sized-deallocation -faligned-allocation -emit-llvm -fcxx-exceptions -fexceptions -std=c++23 -o - -Wno-c++26-extensions | FileCheck --check-prefixes=CHECK,CHECK_NO_SIZE_ALIGNED %s
3 // RUN: %clang_cc1 %s -triple arm64-apple-macosx -fno-sized-deallocation -fno-aligned-allocation -emit-llvm -fcxx-exceptions -fexceptions -std=c++23 -o - -Wno-c++26-extensions | FileCheck --check-prefixes=CHECK,CHECK_NO_SIZE_NO_ALIGN %s
4 // RUN: %clang_cc1 %s -triple arm64-apple-macosx -fsized-deallocation -fno-aligned-allocation -emit-llvm -fcxx-exceptions -fexceptions -std=c++23 -o - -Wno-c++26-extensions | FileCheck --check-prefixes=CHECK,CHECK_SIZED_NO_ALIGN %s
5 // Test default behaviour with c++26
6 // RUN: %clang_cc1 %s -triple arm64-apple-macosx -fsized-deallocation -faligned-allocation -emit-llvm -fcxx-exceptions -fexceptions -std=c++26 -o - | FileCheck --check-prefixes=CHECK,CHECK_SIZED_ALIGNED %s
7 // RUN: %clang_cc1 %s -triple arm64-apple-macosx -fno-sized-deallocation -faligned-allocation -emit-llvm -fcxx-exceptions -fexceptions -std=c++26 -o - | FileCheck --check-prefixes=CHECK,CHECK_NO_SIZE_ALIGNED %s
8 // RUN: %clang_cc1 %s -triple arm64-apple-macosx -fno-sized-deallocation -fno-aligned-allocation -emit-llvm -fcxx-exceptions -fexceptions -std=c++26 -o - | FileCheck --check-prefixes=CHECK,CHECK_NO_SIZE_NO_ALIGN %s
9 // RUN: %clang_cc1 %s -triple arm64-apple-macosx -fsized-deallocation -fno-aligned-allocation -emit-llvm -fcxx-exceptions -fexceptions -std=c++26 -o - | FileCheck --check-prefixes=CHECK,CHECK_SIZED_NO_ALIGN %s
10
11
12 namespace std {
13 template <class T> struct type_identity {
14 typedef T type;
15 };
16 enum class align_val_t : __SIZE_TYPE__ {};
17 struct destroying_delete_t {};
18 }
19
20 using size_t = __SIZE_TYPE__;
21
22 // Sanity check to esure the semantics of the selected compiler mode
23 // will trigger the exception handlers we are expecting, before
24 // involving type aware allocation.
25 // We duplicate the struct definitions so we don't trigger diagnostics
26 // for changing operator resolution on the same type, and we do the
27 // untyped test before specifying the typed operators rather than using
28 // template constraints so we don't have to deal with monstrous mangling.
29 struct S1 {
30 S1();
31 };
32
33 struct __attribute__((aligned(128))) S2 {
34 S2();
35 };
36
37 struct S3 {
38 S3();
39 };
40
41 struct __attribute__((aligned(128))) S4 {
42 S4();
43 char buffer[130];
44 };
45
46 extern "C" void test_no_type_aware_allocator() {
47 S1 *s1 = new S1;
48 delete s1;
49 S2 *s2 = new S2;
50 delete s2;
51 }
52 // CHECK-LABEL: test_no_type_aware_allocator
53 // CHECK: [[ALLOC_RESULT:%.*]] = call {{.*}} @_Znwm(
54 // CHECK: @_ZN2S1C1Ev({{.*}} [[ALLOC_RESULT]])
55 // CHECK-NEXT: unwind label %[[S1LPAD:lpad]]
56 // CHECK_SIZED_ALIGNED: @_ZdlPvm(
57 // CHECK_SIZED_NO_ALIGN: @_ZdlPvm(
58 // CHECK_NO_SIZE_ALIGNED: @_ZdlPv(
59 // CHECK_NO_SIZE_NO_ALIGN: @_ZdlPv(
60 // CHECK_SIZED_ALIGNED: [[ALIGNED_ALLOC_RESULT:%.*]] = call {{.*}} @_ZnwmSt11align_val_t(
61 // CHECK_NO_SIZE_ALIGNED: [[ALIGNED_ALLOC_RESULT:%.*]] = call {{.*}} @_ZnwmSt11align_val_t(
62 // CHECK_SIZED_NO_ALIGN: [[ALIGNED_ALLOC_RESULT:%.*]] = call {{.*}} @_Znwm(
63 // CHECK_NO_SIZE_NO_ALIGN: [[ALIGNED_ALLOC_RESULT:%.*]] = call {{.*}} @_Znwm(
64 // CHECK: _ZN2S2C1Ev({{.*}} [[ALIGNED_ALLOC_RESULT]])
65 // CHECK-NEXT: unwind label %[[S2LPAD:lpad3]]
66 // CHECK_SIZED_ALIGNED: _ZdlPvmSt11align_val_t(
67 // CHECK_NO_SIZE_ALIGNED: _ZdlPvSt11align_val_t(
68 // CHECK_SIZED_NO_ALIGN: _ZdlPvm(
69 // CHECK_NO_SIZE_NO_ALIGN: _ZdlPv(
70 // CHECK: [[S1LPAD]]:{{.*}};
71 // CHECK_SIZED_ALIGNED: @_ZdlPvm({{.*}}[[ALLOC_RESULT]], {{.*}})
72 // CHECK_SIZED_NO_ALIGN: @_ZdlPvm({{.*}}[[ALLOC_RESULT]], {{.*}})
73 // CHECK_NO_SIZE_ALIGNED: @_ZdlPv({{.*}}[[ALLOC_RESULT]])
74 // CHECK_NO_SIZE_NO_ALIGN: @_ZdlPv({{.*}}[[ALLOC_RESULT]])
75 // CHECK: [[S2LPAD]]:
76 // CHECK_SIZED_ALIGNED: _ZdlPvSt11align_val_t({{.*}}[[ALIGNED_ALLOC_RESULT]], {{.*}})
77 // CHECK_NO_SIZE_ALIGNED: _ZdlPvSt11align_val_t({{.*}}[[ALIGNED_ALLOC_RESULT]], {{.*}})
78 // CHECK_SIZED_NO_ALIGN: _ZdlPvm({{.*}}[[ALIGNED_ALLOC_RESULT]], {{.*}})
79 // CHECK_NO_SIZE_NO_ALIGN: _ZdlPv({{.*}}[[ALIGNED_ALLOC_RESULT]])
80
81 template <typename T> void *operator new(std::type_identity<T>, size_t, std::align_val_t);
82 template <typename T> void operator delete(std::type_identity<T>, void *, size_t, std::align_val_t);
83
84 extern "C" void test_free_type_aware_allocator() {
85 S3 *s3 = new S3;
86 delete s3;
87 S4 *s4 = new S4;
88 delete s4;
89 }
90 // CHECK-LABEL: test_free_type_aware_allocator
91 // CHECK: [[ALLOC_RESULT:%.*]] = call {{.*}} @_ZnwI2S3EPvSt13type_identityIT_EmSt11align_val_t(
92 // CHECK: @_ZN2S3C1Ev({{.*}}[[ALLOC_RESULT]])
93 // CHECK-NEXT: unwind label %[[S3LPAD:.*]]
94 // CHECK: @_ZdlI2S3EvSt13type_identityIT_EPvmSt11align_val_t(
95 // CHECK: [[ALIGNED_ALLOC_RESULT:%.*]] = call {{.*}} @_ZnwI2S4EPvSt13type_identityIT_EmSt11align_val_t(
96 // CHECK: @_ZN2S4C1Ev({{.*}}[[ALIGNED_ALLOC_RESULT]])
97 // CHECK-NEXT: unwind label %[[S4LPAD:.*]]
98 // CHECK: @_ZdlI2S4EvSt13type_identityIT_EPvmSt11align_val_t({{.*}}, {{.*}}, {{.*}} 256, {{.*}} 128)
99 // CHECK: [[S3LPAD]]:
100 // CHECK: @_ZdlI2S3EvSt13type_identityIT_EPvmSt11align_val_t({{.*}}, {{.*}}[[ALLOC_RESULT]], {{.*}} 1, {{.*}} 1)
101 // CHECK: [[S4LPAD]]:
102 // CHECK: @_ZdlI2S4EvSt13type_identityIT_EPvmSt11align_val_t({{.*}}, {{.*}}[[ALIGNED_ALLOC_RESULT]], {{.*}} 256, {{.*}} 128)
103
104 struct S5 {
105 S5();
106 template <typename T> void *operator new(std::type_identity<T>, size_t, std::align_val_t);
107 template <typename T> void operator delete(std::type_identity<T>, void*, size_t, std::align_val_t);
108 void operator delete(S5*, std::destroying_delete_t);
109 };
110
111 extern "C" void test_ensure_type_aware_cleanup() {
112 S5 *s5 = new S5;
113 delete s5;
114 }
115 // CHECK-LABEL: test_ensure_type_aware_cleanup
116 // CHECK: [[ALLOC_RESULT:%.*]] = call {{.*}} @_ZN2S5nwIS_EEPvSt13type_identityIT_EmSt11align_val_t(
117 // CHECK: @_ZN2S5C1Ev({{.*}}[[ALLOC_RESULT]])
118 // CHECK-NEXT: unwind label %[[S5LPAD:.*]]
119 // CHECK: @_ZN2S5dlEPS_St19destroying_delete_t(
120 // CHECK: [[S5LPAD]]:
121 // CHECK: @_ZN2S5dlIS_EEvSt13type_identityIT_EPvmSt11align_val_t({{.*}}, {{.*}} [[ALLOC_RESULT]]
122
123 struct S6 {
124 S6();
125 virtual ~S6();
126 template <typename T> void *operator new(std::type_identity<T>, size_t, std::align_val_t);
127 template <typename T> void operator delete(std::type_identity<T>, void*, size_t, std::align_val_t);
128 };
129
130 S6::~S6(){
131 }
132 // CHECK-LABEL: _ZN2S6D0Ev
133 // CHECK: _ZN2S6dlIS_EEvSt13type_identityIT_EPvmSt11align_val_t(
134
135 struct __attribute__((aligned(128))) S7 : S6 {
136
137 };
138
139
140 struct S8 : S6 {
141 S8();
142 void *operator new(size_t);
143 void operator delete(void*);
144 };
145
146 S8::S8(){}
147
148 extern "C" void test_ensure_type_aware_overrides() {
149 S6 *s6 = new S6;
150 delete s6;
151 S7 *s7 = new S7;
152 delete s7;
153 S8 *s8 = new S8;
154 delete s8;
155 }
156 // CHECK-LABEL: test_ensure_type_aware_overrides
157 // CHECK: [[S6_ALLOC:%.*]] = call {{.*}} @_ZN2S6nwIS_EEPvSt13type_identityIT_EmSt11align_val_t(
158 // CHECK: @_ZN2S6C1Ev({{.*}}[[S6_ALLOC]])
159 // CHECK-NEXT: unwind label %[[S6LPAD:.*]]
160 // CHECK: [[S6_VTABLE:%vtable.*]] = load
161 // CHECK: [[S6_DFN_ADDR:%.*]] = getelementptr inbounds ptr, ptr [[S6_VTABLE]], i64 1
162 // CHECK: [[S6_DFN:%.*]] = load ptr, ptr [[S6_DFN_ADDR]]
163 // CHECK: call void [[S6_DFN]](
164 // CHECK: [[S7_ALLOC:%.*]] = call {{.*}} @_ZN2S6nwI2S7EEPvSt13type_identityIT_EmSt11align_val_t(
165 // CHECK: @_ZN2S7C1Ev({{.*}}[[S7_ALLOC]])
166 // CHECK-NEXT: unwind label %[[S7LPAD:.*]]
167 // CHECK: [[S7_VTABLE:%vtable.*]] = load
168 // CHECK: [[S7_DFN_ADDR:%.*]] = getelementptr inbounds ptr, ptr [[S7_VTABLE]], i64 1
169 // CHECK: [[S7_DFN:%.*]] = load ptr, ptr [[S7_DFN_ADDR]]
170 // CHECK: call void [[S7_DFN]](
171 // CHECK: [[S8_ALLOC:%.*]] = call {{.*}} @_ZN2S8nwEm(
172 // CHECK: @_ZN2S8C1Ev({{.*}}[[S8_ALLOC]])
173 // CHECK-NEXT: unwind label %[[S8LPAD:.*]]
174 // CHECK: [[S8_VTABLE:%vtable.*]] = load
175 // CHECK: [[S8_DFN_ADDR:%.*]] = getelementptr inbounds ptr, ptr [[S8_VTABLE]], i64 1
176 // CHECK: [[S8_DFN:%.*]] = load ptr, ptr [[S8_DFN_ADDR]]
177 // CHECK: call void [[S8_DFN]](
178 // CHECK: [[S6LPAD]]:
179 // CHECK: @_ZN2S6dlIS_EEvSt13type_identityIT_EPvmSt11align_val_t({{.*}}, {{.*}} [[S6_ALLOC]], {{.*}}, {{.*}})
180 // CHECK: [[S7LPAD]]:
181 // CHECK: @_ZN2S6dlI2S7EEvSt13type_identityIT_EPvmSt11align_val_t({{.*}}, {{.*}} [[S7_ALLOC]], {{.*}}, {{.*}})
182 // CHECK: [[S8LPAD]]:
183 // CHECK: @_ZN2S8dlEPv({{.*}} [[S8_ALLOC]])
184
185 struct __attribute__((aligned(128))) S11 {
186 S11();
187 template <typename T> void *operator new(std::type_identity<T>, size_t, std::align_val_t);
188 template <typename T> void operator delete(std::type_identity<T>, void*, size_t, std::align_val_t);
189 void operator delete(S11*, std::destroying_delete_t, std::align_val_t);
190 };
191
192
193 struct S12 {
194 template <typename T> void *operator new(std::type_identity<T>, size_t, std::align_val_t, unsigned line = __builtin_LINE());
195 template <typename T> void operator delete(std::type_identity<T>, void*, size_t, std::align_val_t, unsigned line = __builtin_LINE());
196 template <typename T> void operator delete(std::type_identity<T>, void*, size_t, std::align_val_t);
197 };
198
199 extern "C" void test_ensure_type_aware_resolution_includes_location() {
200 S12 *s12 = new S12(); // test line
201 delete s12;
202 }
203
204 // CHECK-LABEL: test_ensure_type_aware_resolution_includes_location
205 // `180` in the next line is the line number from the test line in above
206 // CHECK: %call = call noundef ptr @_ZN3S12nwIS_EEPvSt13type_identityIT_EmSt11align_val_tj({{.*}}, {{.*}}, {{.*}}, {{.*}})
207
208 // CHECK-LABEL: @_ZN2S8D0Ev
209 // CHECK: @_ZN2S8dlEPv(
210
211 // CHECK-LABEL: _ZN2S7D0Ev
212 // CHECK: _ZN2S6dlI2S7EEvSt13type_identityIT_EPvmSt11align_val_t(