diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index b8be6732b758f..6e20eb5b99dde 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -17126,7 +17126,7 @@ MarkVarDeclODRUsed(VarDecl *Var, SourceLocation Loc, Sema &SemaRef, CaptureType, DeclRefType, FunctionScopeIndexToStopAt); - if (SemaRef.LangOpts.CUDA) { + if (SemaRef.LangOpts.CUDA && Var && Var->hasGlobalStorage()) { auto *FD = dyn_cast_or_null(SemaRef.CurContext); auto Target = SemaRef.IdentifyCUDATarget(FD); auto IsEmittedOnDeviceSide = [](VarDecl *Var) { @@ -17142,28 +17142,26 @@ MarkVarDeclODRUsed(VarDecl *Var, SourceLocation Loc, Sema &SemaRef, } return false; }; - if (Var && Var->hasGlobalStorage()) { - if (!IsEmittedOnDeviceSide(Var)) { - // Diagnose ODR-use of host global variables in device functions. - // Reference of device global variables in host functions is allowed - // through shadow variables therefore it is not diagnosed. - if (SemaRef.LangOpts.CUDAIsDevice) - SemaRef.targetDiag(Loc, diag::err_ref_bad_target) - << /*host*/ 2 << /*variable*/ 1 << Var << Target; - } else if ((Target == Sema::CFT_Host || Target == Sema::CFT_HostDevice) && - !Var->hasExternalStorage()) { - // Record a CUDA/HIP device side variable if it is ODR-used - // by host code. This is done conservatively, when the variable is - // referenced in any of the following contexts: - // - a non-function context - // - a host function - // - a host device function - // This makes the ODR-use of the device side variable by host code to - // be visible in the device compilation for the compiler to be able to - // emit template variables instantiated by host code only and to - // externalize the static device side variable ODR-used by host code. - SemaRef.getASTContext().CUDADeviceVarODRUsedByHost.insert(Var); - } + if (!IsEmittedOnDeviceSide(Var)) { + // Diagnose ODR-use of host global variables in device functions. + // Reference of device global variables in host functions is allowed + // through shadow variables therefore it is not diagnosed. + if (SemaRef.LangOpts.CUDAIsDevice) + SemaRef.targetDiag(Loc, diag::err_ref_bad_target) + << /*host*/ 2 << /*variable*/ 1 << Var << Target; + } else if ((Target == Sema::CFT_Host || Target == Sema::CFT_HostDevice) && + !Var->hasExternalStorage()) { + // Record a CUDA/HIP device side variable if it is ODR-used + // by host code. This is done conservatively, when the variable is + // referenced in any of the following contexts: + // - a non-function context + // - a host function + // - a host device function + // This makes the ODR-use of the device side variable by host code to + // be visible in the device compilation for the compiler to be able to + // emit template variables instantiated by host code only and to + // externalize the static device side variable ODR-used by host code. + SemaRef.getASTContext().CUDADeviceVarODRUsedByHost.insert(Var); } }