[clang] [CUDA] Treat function-scope statics in device code as device variables (PR #222985)

Ambrose Leeb via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 2 10:50:47 PDT 2026


================
@@ -764,8 +764,26 @@ void SemaCUDA::checkAllowedInitializer(VarDecl *VD) {
   if (VD->isInvalidDecl() || !VD->hasInit() || !VD->hasGlobalStorage() ||
       IsDependentVar(VD))
     return;
+
+  // A function-scope static is a device variable when it is emitted on the
+  // device side, and has the same initialization restrictions.
+  CUDAVariableTarget VT = IdentifyTarget(VD);
+  const FunctionDecl *FD = VD->getDeclContext()->getEnclosingFunction();
+
+  // CVT_Both means the enclosing function is __host__ __device__, so the
+  // variable is emitted on both sides and only the device-side copy is
+  // restricted. Lambdas are implicitly HD and their statics are host variables.
----------------
Sirraide wrote:

> Lambdas are implicitly HD and their statics are host variables.

Perhaps I’m missing something here, but how does this work? A `static` variable inside the body of a lambda call operator that is invoked on the device side should be a `__device__` variable shouldn’t it? How are lambda call operators different from regular functions in this respect?

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


More information about the cfe-commits mailing list