Debian Codegraph @

clang/test/SemaCUDA/dtor.cu

raw
1 // RUN: %clang_cc1 %s -std=c++20 -fsyntax-only -verify=host
2 // RUN: %clang_cc1 %s -std=c++20 -fcuda-is-device -fsyntax-only -verify=dev
3
4 // host-no-diagnostics
5
6 #include "Inputs/cuda.h"
7
8 // Virtual dtor ~B() of explicit instantiation B<float> must
9 // be emitted, which causes host_fun() called.
10 namespace ExplicitInstantiationExplicitDevDtor {
11 void host_fun() // dev-note {{'host_fun' declared here}}
12 {}
13
14 template <unsigned>
15 constexpr void hd_fun() {
16 host_fun(); // dev-error {{reference to __host__ function 'host_fun' in __host__ __device__ function}}
17 }
18
19 struct A {
20 constexpr ~A() { // dev-note {{called by '~B'}}
21 hd_fun<8>(); // dev-note {{called by '~A'}}
22 }
23 };
24
25 template <typename T>
26 struct B {
27 public:
28 virtual __device__ ~B() = default;
29 A _a;
30 };
31
32 template class B<float>;
33 }
34
35 // The implicit host/device attrs of virtual dtor ~B() should be
36 // conservatively inferred, where constexpr member dtor's should
37 // not be considered device since they may call host functions.
38 // Therefore B<float>::~B() should not have implicit device attr.
39 // However C<float>::~C() should have implicit device attr since
40 // it is trivial.
41 namespace ExplicitInstantiationDtorNoAttr {
42 void host_fun()
43 {}
44
45 template <unsigned>
46 constexpr void hd_fun() {
47 host_fun();
48 }
49
50 struct A {
51 constexpr ~A() {
52 hd_fun<8>();
53 }
54 };
55
56 template <typename T>
57 struct B {
58 public:
59 virtual ~B() = default;
60 A _a;
61 };
62
63 template <typename T>
64 struct C {
65 public:
66 virtual ~C() = default;
67 };
68
69 template class B<float>;
70 template class C<float>;
71 __device__ void foo() {
72 C<float> x;
73 }
74 }
75
76 // Dtors of implicit template class instantiation are not
77 // conservatively inferred because the invalid usage can
78 // be diagnosed.
79 namespace ImplicitInstantiation {
80 void host_fun() // dev-note {{'host_fun' declared here}}
81 {}
82
83 template <unsigned>
84 constexpr void hd_fun() {
85 host_fun(); // dev-error {{reference to __host__ function 'host_fun' in __host__ __device__ function}}
86 }
87
88 struct A {
89 constexpr ~A() { // dev-note {{called by '~B'}}
90 hd_fun<8>(); // dev-note {{called by '~A'}}
91 }
92 };
93
94 template <typename T>
95 struct B {
96 public:
97 ~B() = default; // dev-note {{called by 'foo'}}
98 A _a;
99 };
100
101 __device__ void foo() {
102 B<float> x;
103 }
104 }