[clang] [CUDA][HIP] Fix CTAD for host/device constructors (PR #168711)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 26 00:09:03 PST 2025


================
@@ -7984,6 +7984,18 @@ void Sema::ProcessDeclAttributeList(
     }
   }
 
+  // CUDA/HIP: disallow explicit CUDA target attributes on deduction guides.
+  // Deduction guides are not callable functions and never participate in
+  // codegen; they are always treated as host+device for CUDA/HIP semantic
+  // checks, so explicit target attributes on them would be misleading noise.
+  if (getLangOpts().CUDA)
+    if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(D))
+      if (Guide->hasAttr<CUDAHostAttr>() || Guide->hasAttr<CUDADeviceAttr>() ||
+          Guide->hasAttr<CUDAGlobalAttr>()) {
+        Diag(Guide->getLocation(), diag::err_deduction_guide_target_attr);
+        Guide->setInvalidDecl();
+      }
----------------
cor3ntin wrote:

Can you have a `CUDAHostAttr` if getLangOpts().CUDA` is not set? I think we can simplify this code a bit. also :

```suggestion
  if (getLangOpts().CUDA)
    if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(D); 
        Guide && Guide->hasAttr<CUDAHostAttr>() || Guide->hasAttr<CUDADeviceAttr>() ||
          Guide->hasAttr<CUDAGlobalAttr>()) {
        Diag(Guide->getLocation(), diag::err_deduction_guide_target_attr);
        Guide->setInvalidDecl();
      }
```

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


More information about the cfe-commits mailing list