[clang] [CUDA][HIP] fix virtual dtor host/device attr (PR #128926)

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 28 18:56:43 PST 2025


================
@@ -32,22 +32,24 @@ public:
 template class B<float>;
 }
 
-// The implicit host/device attrs of virtual dtor B<float>::~B() is inferred to
-// have implicit device attr since dtors of its members and parent classes can
-// be executed on device. This causes a diagnostic since B<float>::~B() must
-// be emitted, and it eventually causes host_fun() called on device side.
+// The implicit host/device attrs of virtual dtor ~B() should be
+// conservatively inferred, where constexpr member dtor's should
+// not be considered device since they may call host functions.
+// Therefore B<float>::~B() should not have implicit device attr.
+// However C<float>::~C() should have implicit device attr since
+// it is trivial.
 namespace ExplicitInstantiationDtorNoAttr {
-void host_fun() // dev-note {{'host_fun' declared here}}
+void host_fun()
----------------
yxsamliu wrote:

no. deferred diag happens at the end of sema.

What this PR does is that for virtual dtor of explicitly instantiated template class, clang will not treat constexpr dtors as executable on device, therefore if there is a constexpr member dtor, the virtual dtor is host only. Then it won't be emitted on device side.

The reason is that users have no control of this virtual dtor since it must be emitted, and users may not have control of the class definition, so unless we are sure this dtor can be executed on device, let's do not infer it as device. This makes sure the code that works as C++ continue to work in CUDA/HIP.


https://github.com/llvm/llvm-project/pull/128926


More information about the cfe-commits mailing list