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