brintos

brintos / llvm-project-archived public Read only

0
0
Text · 48.8 KiB · dd9bcab Raw
1238 lines · cpp
1//===--- SemaCUDA.cpp - Semantic Analysis for CUDA constructs -------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8/// \file9/// This file implements semantic analysis for CUDA constructs.10///11//===----------------------------------------------------------------------===//12 13#include "clang/Sema/SemaCUDA.h"14#include "clang/AST/ASTContext.h"15#include "clang/AST/Decl.h"16#include "clang/AST/ExprCXX.h"17#include "clang/Basic/Cuda.h"18#include "clang/Basic/TargetInfo.h"19#include "clang/Lex/Preprocessor.h"20#include "clang/Sema/Lookup.h"21#include "clang/Sema/Overload.h"22#include "clang/Sema/ScopeInfo.h"23#include "clang/Sema/Sema.h"24#include "clang/Sema/Template.h"25#include "llvm/ADT/SmallVector.h"26#include <optional>27using namespace clang;28 29SemaCUDA::SemaCUDA(Sema &S) : SemaBase(S) {}30 31template <typename AttrT> static bool hasExplicitAttr(const VarDecl *D) {32  if (!D)33    return false;34  if (auto *A = D->getAttr<AttrT>())35    return !A->isImplicit();36  return false;37}38 39void SemaCUDA::PushForceHostDevice() {40  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");41  ForceHostDeviceDepth++;42}43 44bool SemaCUDA::PopForceHostDevice() {45  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");46  if (ForceHostDeviceDepth == 0)47    return false;48  ForceHostDeviceDepth--;49  return true;50}51 52ExprResult SemaCUDA::ActOnExecConfigExpr(Scope *S, SourceLocation LLLLoc,53                                         MultiExprArg ExecConfig,54                                         SourceLocation GGGLoc) {55  bool IsDeviceKernelCall = false;56  switch (CurrentTarget()) {57  case CUDAFunctionTarget::Global:58  case CUDAFunctionTarget::Device:59    IsDeviceKernelCall = true;60    break;61  case CUDAFunctionTarget::HostDevice:62    if (getLangOpts().CUDAIsDevice) {63      IsDeviceKernelCall = true;64      if (FunctionDecl *Caller =65              SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);66          Caller && isImplicitHostDeviceFunction(Caller)) {67        // Under the device compilation, config call under an HD function should68        // be treated as a device kernel call. But, for implicit HD ones (such69        // as lambdas), need to check whether RDC is enabled or not.70        if (!getLangOpts().GPURelocatableDeviceCode)71          IsDeviceKernelCall = false;72        // HIP doesn't support device-side kernel call yet. Still treat it as73        // the host-side kernel call.74        if (getLangOpts().HIP)75          IsDeviceKernelCall = false;76      }77    }78    break;79  default:80    break;81  }82 83  if (IsDeviceKernelCall && getLangOpts().HIP)84    return ExprError(85        Diag(LLLLoc, diag::err_cuda_device_kernel_launch_not_supported));86 87  if (IsDeviceKernelCall && !getLangOpts().GPURelocatableDeviceCode)88    return ExprError(89        Diag(LLLLoc, diag::err_cuda_device_kernel_launch_require_rdc));90 91  FunctionDecl *ConfigDecl = IsDeviceKernelCall92                                 ? getASTContext().getcudaLaunchDeviceDecl()93                                 : getASTContext().getcudaConfigureCallDecl();94  if (!ConfigDecl)95    return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use)96                     << (IsDeviceKernelCall ? getLaunchDeviceFuncName()97                                            : getConfigureFuncName()));98  // Additional check on the launch function if it's a device kernel call.99  if (IsDeviceKernelCall) {100    auto *GetParamBuf = getASTContext().getcudaGetParameterBufferDecl();101    if (!GetParamBuf)102      return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use)103                       << getGetParameterBufferFuncName());104  }105 106  QualType ConfigQTy = ConfigDecl->getType();107 108  DeclRefExpr *ConfigDR = new (getASTContext()) DeclRefExpr(109      getASTContext(), ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc);110  SemaRef.MarkFunctionReferenced(LLLLoc, ConfigDecl);111 112  if (IsDeviceKernelCall) {113    SmallVector<Expr *> Args;114    // Use a null pointer as the kernel function, which may not be resolvable115    // here. For example, resolving that kernel function may need additional116    // kernel arguments.117    llvm::APInt Zero(SemaRef.Context.getTypeSize(SemaRef.Context.IntTy), 0);118    Args.push_back(IntegerLiteral::Create(SemaRef.Context, Zero,119                                          SemaRef.Context.IntTy, LLLLoc));120    // Use a null pointer as the placeholder of the parameter buffer, which121    // should be replaced with the actual allocation later, in the codegen.122    Args.push_back(IntegerLiteral::Create(SemaRef.Context, Zero,123                                          SemaRef.Context.IntTy, LLLLoc));124    // Add the original config arguments.125    llvm::append_range(Args, ExecConfig);126    // Add the default blockDim if it's missing.127    if (Args.size() < 4) {128      llvm::APInt One(SemaRef.Context.getTypeSize(SemaRef.Context.IntTy), 1);129      Args.push_back(IntegerLiteral::Create(SemaRef.Context, One,130                                            SemaRef.Context.IntTy, LLLLoc));131    }132    // Add the default sharedMemSize if it's missing.133    if (Args.size() < 5)134      Args.push_back(IntegerLiteral::Create(SemaRef.Context, Zero,135                                            SemaRef.Context.IntTy, LLLLoc));136    // Add the default stream if it's missing.137    if (Args.size() < 6)138      Args.push_back(new (SemaRef.Context) CXXNullPtrLiteralExpr(139          SemaRef.Context.NullPtrTy, LLLLoc));140    return SemaRef.BuildCallExpr(S, ConfigDR, LLLLoc, Args, GGGLoc, nullptr,141                                 /*IsExecConfig=*/true);142  }143  return SemaRef.BuildCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, nullptr,144                               /*IsExecConfig=*/true);145}146 147CUDAFunctionTarget SemaCUDA::IdentifyTarget(const ParsedAttributesView &Attrs) {148  bool HasHostAttr = false;149  bool HasDeviceAttr = false;150  bool HasGlobalAttr = false;151  bool HasInvalidTargetAttr = false;152  for (const ParsedAttr &AL : Attrs) {153    switch (AL.getKind()) {154    case ParsedAttr::AT_CUDAGlobal:155      HasGlobalAttr = true;156      break;157    case ParsedAttr::AT_CUDAHost:158      HasHostAttr = true;159      break;160    case ParsedAttr::AT_CUDADevice:161      HasDeviceAttr = true;162      break;163    case ParsedAttr::AT_CUDAInvalidTarget:164      HasInvalidTargetAttr = true;165      break;166    default:167      break;168    }169  }170 171  if (HasInvalidTargetAttr)172    return CUDAFunctionTarget::InvalidTarget;173 174  if (HasGlobalAttr)175    return CUDAFunctionTarget::Global;176 177  if (HasHostAttr && HasDeviceAttr)178    return CUDAFunctionTarget::HostDevice;179 180  if (HasDeviceAttr)181    return CUDAFunctionTarget::Device;182 183  return CUDAFunctionTarget::Host;184}185 186template <typename A>187static bool hasAttr(const Decl *D, bool IgnoreImplicitAttr) {188  return D->hasAttrs() && llvm::any_of(D->getAttrs(), [&](Attr *Attribute) {189           return isa<A>(Attribute) &&190                  !(IgnoreImplicitAttr && Attribute->isImplicit());191         });192}193 194SemaCUDA::CUDATargetContextRAII::CUDATargetContextRAII(195    SemaCUDA &S_, SemaCUDA::CUDATargetContextKind K, Decl *D)196    : S(S_) {197  SavedCtx = S.CurCUDATargetCtx;198  assert(K == SemaCUDA::CTCK_InitGlobalVar);199  auto *VD = dyn_cast_or_null<VarDecl>(D);200  if (VD && VD->hasGlobalStorage() && !VD->isStaticLocal()) {201    auto Target = CUDAFunctionTarget::Host;202    if ((hasAttr<CUDADeviceAttr>(VD, /*IgnoreImplicit=*/true) &&203         !hasAttr<CUDAHostAttr>(VD, /*IgnoreImplicit=*/true)) ||204        hasAttr<CUDASharedAttr>(VD, /*IgnoreImplicit=*/true) ||205        hasAttr<CUDAConstantAttr>(VD, /*IgnoreImplicit=*/true))206      Target = CUDAFunctionTarget::Device;207    S.CurCUDATargetCtx = {Target, K, VD};208  }209}210 211/// IdentifyTarget - Determine the CUDA compilation target for this function212CUDAFunctionTarget SemaCUDA::IdentifyTarget(const FunctionDecl *D,213                                            bool IgnoreImplicitHDAttr) {214  // Code that lives outside a function gets the target from CurCUDATargetCtx.215  if (D == nullptr)216    return CurCUDATargetCtx.Target;217 218  if (D->hasAttr<CUDAInvalidTargetAttr>())219    return CUDAFunctionTarget::InvalidTarget;220 221  if (D->hasAttr<CUDAGlobalAttr>())222    return CUDAFunctionTarget::Global;223 224  if (D->isConsteval())225    return CUDAFunctionTarget::HostDevice;226 227  if (hasAttr<CUDADeviceAttr>(D, IgnoreImplicitHDAttr)) {228    if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr))229      return CUDAFunctionTarget::HostDevice;230    return CUDAFunctionTarget::Device;231  } else if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr)) {232    return CUDAFunctionTarget::Host;233  } else if ((D->isImplicit() || !D->isUserProvided()) &&234             !IgnoreImplicitHDAttr) {235    // Some implicit declarations (like intrinsic functions) are not marked.236    // Set the most lenient target on them for maximal flexibility.237    return CUDAFunctionTarget::HostDevice;238  }239 240  return CUDAFunctionTarget::Host;241}242 243/// IdentifyTarget - Determine the CUDA compilation target for this variable.244SemaCUDA::CUDAVariableTarget SemaCUDA::IdentifyTarget(const VarDecl *Var) {245  if (Var->hasAttr<HIPManagedAttr>())246    return CVT_Unified;247  // Only constexpr and const variabless with implicit constant attribute248  // are emitted on both sides. Such variables are promoted to device side249  // only if they have static constant intializers on device side.250  if ((Var->isConstexpr() || Var->getType().isConstQualified()) &&251      Var->hasAttr<CUDAConstantAttr>() &&252      !hasExplicitAttr<CUDAConstantAttr>(Var))253    return CVT_Both;254  if (Var->hasAttr<CUDADeviceAttr>() || Var->hasAttr<CUDAConstantAttr>() ||255      Var->hasAttr<CUDASharedAttr>() ||256      Var->getType()->isCUDADeviceBuiltinSurfaceType() ||257      Var->getType()->isCUDADeviceBuiltinTextureType())258    return CVT_Device;259  // Function-scope static variable without explicit device or constant260  // attribute are emitted261  //  - on both sides in host device functions262  //  - on device side in device or global functions263  if (auto *FD = dyn_cast<FunctionDecl>(Var->getDeclContext())) {264    switch (IdentifyTarget(FD)) {265    case CUDAFunctionTarget::HostDevice:266      return CVT_Both;267    case CUDAFunctionTarget::Device:268    case CUDAFunctionTarget::Global:269      return CVT_Device;270    default:271      return CVT_Host;272    }273  }274  return CVT_Host;275}276 277// * CUDA Call preference table278//279// F - from,280// T - to281// Ph - preference in host mode282// Pd - preference in device mode283// H  - handled in (x)284// Preferences: N:native, SS:same side, HD:host-device, WS:wrong side, --:never.285//286// | F  | T  | Ph  | Pd  |  H  |287// |----+----+-----+-----+-----+288// | d  | d  | N   | N   | (c) |289// | d  | g  | --  | --  | (a) |290// | d  | h  | --  | --  | (e) |291// | d  | hd | HD  | HD  | (b) |292// | g  | d  | N   | N   | (c) |293// | g  | g  | --  | --  | (a) |294// | g  | h  | --  | --  | (e) |295// | g  | hd | HD  | HD  | (b) |296// | h  | d  | --  | --  | (e) |297// | h  | g  | N   | N   | (c) |298// | h  | h  | N   | N   | (c) |299// | h  | hd | HD  | HD  | (b) |300// | hd | d  | WS  | SS  | (d) |301// | hd | g  | SS  | --  |(d/a)|302// | hd | h  | SS  | WS  | (d) |303// | hd | hd | HD  | HD  | (b) |304 305SemaCUDA::CUDAFunctionPreference306SemaCUDA::IdentifyPreference(const FunctionDecl *Caller,307                             const FunctionDecl *Callee) {308  assert(Callee && "Callee must be valid.");309 310  // Treat ctor/dtor as host device function in device var initializer to allow311  // trivial ctor/dtor without device attr to be used. Non-trivial ctor/dtor312  // will be diagnosed by checkAllowedInitializer.313  if (Caller == nullptr && CurCUDATargetCtx.Kind == CTCK_InitGlobalVar &&314      CurCUDATargetCtx.Target == CUDAFunctionTarget::Device &&315      (isa<CXXConstructorDecl>(Callee) || isa<CXXDestructorDecl>(Callee)))316    return CFP_HostDevice;317 318  CUDAFunctionTarget CallerTarget = IdentifyTarget(Caller);319  CUDAFunctionTarget CalleeTarget = IdentifyTarget(Callee);320 321  // If one of the targets is invalid, the check always fails, no matter what322  // the other target is.323  if (CallerTarget == CUDAFunctionTarget::InvalidTarget ||324      CalleeTarget == CUDAFunctionTarget::InvalidTarget)325    return CFP_Never;326 327  // (a) Call global from either global or device contexts is allowed as part328  // of CUDA's dynamic parallelism support.329  if (CalleeTarget == CUDAFunctionTarget::Global &&330      (CallerTarget == CUDAFunctionTarget::Global ||331       CallerTarget == CUDAFunctionTarget::Device))332    return CFP_Native;333 334  // (b) Calling HostDevice is OK for everyone.335  if (CalleeTarget == CUDAFunctionTarget::HostDevice)336    return CFP_HostDevice;337 338  // (c) Best case scenarios339  if (CalleeTarget == CallerTarget ||340      (CallerTarget == CUDAFunctionTarget::Host &&341       CalleeTarget == CUDAFunctionTarget::Global) ||342      (CallerTarget == CUDAFunctionTarget::Global &&343       CalleeTarget == CUDAFunctionTarget::Device))344    return CFP_Native;345 346  // HipStdPar mode is special, in that assessing whether a device side call to347  // a host target is deferred to a subsequent pass, and cannot unambiguously be348  // adjudicated in the AST, hence we optimistically allow them to pass here.349  if (getLangOpts().HIPStdPar &&350      (CallerTarget == CUDAFunctionTarget::Global ||351       CallerTarget == CUDAFunctionTarget::Device ||352       CallerTarget == CUDAFunctionTarget::HostDevice) &&353      CalleeTarget == CUDAFunctionTarget::Host)354    return CFP_HostDevice;355 356  // (d) HostDevice behavior depends on compilation mode.357  if (CallerTarget == CUDAFunctionTarget::HostDevice) {358    // It's OK to call a compilation-mode matching function from an HD one.359    if ((getLangOpts().CUDAIsDevice &&360         (CalleeTarget == CUDAFunctionTarget::Device ||361          CalleeTarget == CUDAFunctionTarget::Global)) ||362        (!getLangOpts().CUDAIsDevice &&363         (CalleeTarget == CUDAFunctionTarget::Host ||364          CalleeTarget == CUDAFunctionTarget::Global)))365      return CFP_SameSide;366 367    // Calls from HD to non-mode-matching functions (i.e., to host functions368    // when compiling in device mode or to device functions when compiling in369    // host mode) are allowed at the sema level, but eventually rejected if370    // they're ever codegened.  TODO: Reject said calls earlier.371    return CFP_WrongSide;372  }373 374  // (e) Calling across device/host boundary is not something you should do.375  if ((CallerTarget == CUDAFunctionTarget::Host &&376       CalleeTarget == CUDAFunctionTarget::Device) ||377      (CallerTarget == CUDAFunctionTarget::Device &&378       CalleeTarget == CUDAFunctionTarget::Host) ||379      (CallerTarget == CUDAFunctionTarget::Global &&380       CalleeTarget == CUDAFunctionTarget::Host))381    return CFP_Never;382 383  llvm_unreachable("All cases should've been handled by now.");384}385 386template <typename AttrT> static bool hasImplicitAttr(const FunctionDecl *D) {387  if (!D)388    return false;389  if (auto *A = D->getAttr<AttrT>())390    return A->isImplicit();391  return D->isImplicit();392}393 394bool SemaCUDA::isImplicitHostDeviceFunction(const FunctionDecl *D) {395  bool IsImplicitDevAttr = hasImplicitAttr<CUDADeviceAttr>(D);396  bool IsImplicitHostAttr = hasImplicitAttr<CUDAHostAttr>(D);397  return IsImplicitDevAttr && IsImplicitHostAttr;398}399 400void SemaCUDA::EraseUnwantedMatches(401    const FunctionDecl *Caller,402    SmallVectorImpl<std::pair<DeclAccessPair, FunctionDecl *>> &Matches) {403  if (Matches.size() <= 1)404    return;405 406  using Pair = std::pair<DeclAccessPair, FunctionDecl *>;407 408  // Gets the CUDA function preference for a call from Caller to Match.409  auto GetCFP = [&](const Pair &Match) {410    return IdentifyPreference(Caller, Match.second);411  };412 413  // Find the best call preference among the functions in Matches.414  CUDAFunctionPreference BestCFP =415      GetCFP(*llvm::max_element(Matches, [&](const Pair &M1, const Pair &M2) {416        return GetCFP(M1) < GetCFP(M2);417      }));418 419  // Erase all functions with lower priority.420  llvm::erase_if(Matches,421                 [&](const Pair &Match) { return GetCFP(Match) < BestCFP; });422}423 424/// When an implicitly-declared special member has to invoke more than one425/// base/field special member, conflicts may occur in the targets of these426/// members. For example, if one base's member __host__ and another's is427/// __device__, it's a conflict.428/// This function figures out if the given targets \param Target1 and429/// \param Target2 conflict, and if they do not it fills in430/// \param ResolvedTarget with a target that resolves for both calls.431/// \return true if there's a conflict, false otherwise.432static bool433resolveCalleeCUDATargetConflict(CUDAFunctionTarget Target1,434                                CUDAFunctionTarget Target2,435                                CUDAFunctionTarget *ResolvedTarget) {436  // Only free functions and static member functions may be global.437  assert(Target1 != CUDAFunctionTarget::Global);438  assert(Target2 != CUDAFunctionTarget::Global);439 440  if (Target1 == CUDAFunctionTarget::HostDevice) {441    *ResolvedTarget = Target2;442  } else if (Target2 == CUDAFunctionTarget::HostDevice) {443    *ResolvedTarget = Target1;444  } else if (Target1 != Target2) {445    return true;446  } else {447    *ResolvedTarget = Target1;448  }449 450  return false;451}452 453bool SemaCUDA::inferTargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl,454                                                   CXXSpecialMemberKind CSM,455                                                   CXXMethodDecl *MemberDecl,456                                                   bool ConstRHS,457                                                   bool Diagnose) {458  // If MemberDecl is virtual destructor of an explicit template class459  // instantiation, it must be emitted, therefore it needs to be inferred460  // conservatively by ignoring implicit host/device attrs of member and parent461  // dtors called by it. Also, it needs to be checed by deferred diag visitor.462  bool IsExpVDtor = false;463  if (isa<CXXDestructorDecl>(MemberDecl) && MemberDecl->isVirtual()) {464    if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(ClassDecl)) {465      TemplateSpecializationKind TSK = Spec->getTemplateSpecializationKind();466      IsExpVDtor = TSK == TSK_ExplicitInstantiationDeclaration ||467                   TSK == TSK_ExplicitInstantiationDefinition;468    }469  }470  if (IsExpVDtor)471    SemaRef.DeclsToCheckForDeferredDiags.insert(MemberDecl);472 473  // If the defaulted special member is defined lexically outside of its474  // owning class, or the special member already has explicit device or host475  // attributes, do not infer.476  bool InClass = MemberDecl->getLexicalParent() == MemberDecl->getParent();477  bool HasH = MemberDecl->hasAttr<CUDAHostAttr>();478  bool HasD = MemberDecl->hasAttr<CUDADeviceAttr>();479  bool HasExplicitAttr =480      (HasD && !MemberDecl->getAttr<CUDADeviceAttr>()->isImplicit()) ||481      (HasH && !MemberDecl->getAttr<CUDAHostAttr>()->isImplicit());482  if (!InClass || HasExplicitAttr)483    return false;484 485  std::optional<CUDAFunctionTarget> InferredTarget;486 487  // We're going to invoke special member lookup; mark that these special488  // members are called from this one, and not from its caller.489  Sema::ContextRAII MethodContext(SemaRef, MemberDecl);490 491  // Look for special members in base classes that should be invoked from here.492  // Infer the target of this member base on the ones it should call.493  // Skip direct and indirect virtual bases for abstract classes.494  llvm::SmallVector<const CXXBaseSpecifier *, 16> Bases;495  for (const auto &B : ClassDecl->bases()) {496    if (!B.isVirtual()) {497      Bases.push_back(&B);498    }499  }500 501  if (!ClassDecl->isAbstract()) {502    llvm::append_range(Bases, llvm::make_pointer_range(ClassDecl->vbases()));503  }504 505  for (const auto *B : Bases) {506    auto *BaseClassDecl = B->getType()->getAsCXXRecordDecl();507    if (!BaseClassDecl)508      continue;509 510    Sema::SpecialMemberOverloadResult SMOR =511        SemaRef.LookupSpecialMember(BaseClassDecl, CSM,512                                    /* ConstArg */ ConstRHS,513                                    /* VolatileArg */ false,514                                    /* RValueThis */ false,515                                    /* ConstThis */ false,516                                    /* VolatileThis */ false);517 518    if (!SMOR.getMethod())519      continue;520 521    CUDAFunctionTarget BaseMethodTarget =522        IdentifyTarget(SMOR.getMethod(), IsExpVDtor);523 524    if (!InferredTarget) {525      InferredTarget = BaseMethodTarget;526    } else {527      bool ResolutionError = resolveCalleeCUDATargetConflict(528          *InferredTarget, BaseMethodTarget, &*InferredTarget);529      if (ResolutionError) {530        if (Diagnose) {531          Diag(ClassDecl->getLocation(),532               diag::note_implicit_member_target_infer_collision)533              << (unsigned)CSM << *InferredTarget << BaseMethodTarget;534        }535        MemberDecl->addAttr(536            CUDAInvalidTargetAttr::CreateImplicit(getASTContext()));537        return true;538      }539    }540  }541 542  // Same as for bases, but now for special members of fields.543  for (const auto *F : ClassDecl->fields()) {544    if (F->isInvalidDecl()) {545      continue;546    }547 548    auto *FieldRecDecl =549        getASTContext().getBaseElementType(F->getType())->getAsCXXRecordDecl();550    if (!FieldRecDecl)551      continue;552 553    Sema::SpecialMemberOverloadResult SMOR =554        SemaRef.LookupSpecialMember(FieldRecDecl, CSM,555                                    /* ConstArg */ ConstRHS && !F->isMutable(),556                                    /* VolatileArg */ false,557                                    /* RValueThis */ false,558                                    /* ConstThis */ false,559                                    /* VolatileThis */ false);560 561    if (!SMOR.getMethod())562      continue;563 564    CUDAFunctionTarget FieldMethodTarget =565        IdentifyTarget(SMOR.getMethod(), IsExpVDtor);566 567    if (!InferredTarget) {568      InferredTarget = FieldMethodTarget;569    } else {570      bool ResolutionError = resolveCalleeCUDATargetConflict(571          *InferredTarget, FieldMethodTarget, &*InferredTarget);572      if (ResolutionError) {573        if (Diagnose) {574          Diag(ClassDecl->getLocation(),575               diag::note_implicit_member_target_infer_collision)576              << (unsigned)CSM << *InferredTarget << FieldMethodTarget;577        }578        MemberDecl->addAttr(579            CUDAInvalidTargetAttr::CreateImplicit(getASTContext()));580        return true;581      }582    }583  }584 585  // If no target was inferred, mark this member as __host__ __device__;586  // it's the least restrictive option that can be invoked from any target.587  bool NeedsH = true, NeedsD = true;588  if (InferredTarget) {589    if (*InferredTarget == CUDAFunctionTarget::Device)590      NeedsH = false;591    else if (*InferredTarget == CUDAFunctionTarget::Host)592      NeedsD = false;593  }594 595  // We either setting attributes first time, or the inferred ones must match596  // previously set ones.597  if (NeedsD && !HasD)598    MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));599  if (NeedsH && !HasH)600    MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));601 602  return false;603}604 605bool SemaCUDA::isEmptyConstructor(SourceLocation Loc, CXXConstructorDecl *CD) {606  if (!CD->isDefined() && CD->isTemplateInstantiation())607    SemaRef.InstantiateFunctionDefinition(Loc, CD->getFirstDecl());608 609  // (E.2.3.1, CUDA 7.5) A constructor for a class type is considered610  // empty at a point in the translation unit, if it is either a611  // trivial constructor612  if (CD->isTrivial())613    return true;614 615  // ... or it satisfies all of the following conditions:616  // The constructor function has been defined.617  // The constructor function has no parameters,618  // and the function body is an empty compound statement.619  if (!(CD->hasTrivialBody() && CD->getNumParams() == 0))620    return false;621 622  // Its class has no virtual functions and no virtual base classes.623  if (CD->getParent()->isDynamicClass())624    return false;625 626  // Union ctor does not call ctors of its data members.627  if (CD->getParent()->isUnion())628    return true;629 630  // The only form of initializer allowed is an empty constructor.631  // This will recursively check all base classes and member initializers632  if (!llvm::all_of(CD->inits(), [&](const CXXCtorInitializer *CI) {633        if (const CXXConstructExpr *CE =634                dyn_cast<CXXConstructExpr>(CI->getInit()))635          return isEmptyConstructor(Loc, CE->getConstructor());636        return false;637      }))638    return false;639 640  return true;641}642 643bool SemaCUDA::isEmptyDestructor(SourceLocation Loc, CXXDestructorDecl *DD) {644  // No destructor -> no problem.645  if (!DD)646    return true;647 648  if (!DD->isDefined() && DD->isTemplateInstantiation())649    SemaRef.InstantiateFunctionDefinition(Loc, DD->getFirstDecl());650 651  // (E.2.3.1, CUDA 7.5) A destructor for a class type is considered652  // empty at a point in the translation unit, if it is either a653  // trivial constructor654  if (DD->isTrivial())655    return true;656 657  // ... or it satisfies all of the following conditions:658  // The destructor function has been defined.659  // and the function body is an empty compound statement.660  if (!DD->hasTrivialBody())661    return false;662 663  const CXXRecordDecl *ClassDecl = DD->getParent();664 665  // Its class has no virtual functions and no virtual base classes.666  if (ClassDecl->isDynamicClass())667    return false;668 669  // Union does not have base class and union dtor does not call dtors of its670  // data members.671  if (DD->getParent()->isUnion())672    return true;673 674  // Only empty destructors are allowed. This will recursively check675  // destructors for all base classes...676  if (!llvm::all_of(ClassDecl->bases(), [&](const CXXBaseSpecifier &BS) {677        if (CXXRecordDecl *RD = BS.getType()->getAsCXXRecordDecl())678          return isEmptyDestructor(Loc, RD->getDestructor());679        return true;680      }))681    return false;682 683  // ... and member fields.684  if (!llvm::all_of(ClassDecl->fields(), [&](const FieldDecl *Field) {685        if (CXXRecordDecl *RD = Field->getType()686                                    ->getBaseElementTypeUnsafe()687                                    ->getAsCXXRecordDecl())688          return isEmptyDestructor(Loc, RD->getDestructor());689        return true;690      }))691    return false;692 693  return true;694}695 696namespace {697enum CUDAInitializerCheckKind {698  CICK_DeviceOrConstant, // Check initializer for device/constant variable699  CICK_Shared,           // Check initializer for shared variable700};701 702bool IsDependentVar(VarDecl *VD) {703  if (VD->getType()->isDependentType())704    return true;705  if (const auto *Init = VD->getInit())706    return Init->isValueDependent();707  return false;708}709 710// Check whether a variable has an allowed initializer for a CUDA device side711// variable with global storage. \p VD may be a host variable to be checked for712// potential promotion to device side variable.713//714// CUDA/HIP allows only empty constructors as initializers for global715// variables (see E.2.3.1, CUDA 7.5). The same restriction also applies to all716// __shared__ variables whether they are local or not (they all are implicitly717// static in CUDA). One exception is that CUDA allows constant initializers718// for __constant__ and __device__ variables.719bool HasAllowedCUDADeviceStaticInitializer(SemaCUDA &S, VarDecl *VD,720                                           CUDAInitializerCheckKind CheckKind) {721  assert(!VD->isInvalidDecl() && VD->hasGlobalStorage());722  assert(!IsDependentVar(VD) && "do not check dependent var");723  const Expr *Init = VD->getInit();724  auto IsEmptyInit = [&](const Expr *Init) {725    if (!Init)726      return true;727    if (const auto *CE = dyn_cast<CXXConstructExpr>(Init)) {728      return S.isEmptyConstructor(VD->getLocation(), CE->getConstructor());729    }730    return false;731  };732  auto IsConstantInit = [&](const Expr *Init) {733    assert(Init);734    ASTContext::CUDAConstantEvalContextRAII EvalCtx(S.getASTContext(),735                                                    /*NoWronSidedVars=*/true);736    return Init->isConstantInitializer(S.getASTContext(),737                                       VD->getType()->isReferenceType());738  };739  auto HasEmptyDtor = [&](VarDecl *VD) {740    if (const auto *RD = VD->getType()->getAsCXXRecordDecl())741      return S.isEmptyDestructor(VD->getLocation(), RD->getDestructor());742    return true;743  };744  if (CheckKind == CICK_Shared)745    return IsEmptyInit(Init) && HasEmptyDtor(VD);746  return S.getLangOpts().GPUAllowDeviceInit ||747         ((IsEmptyInit(Init) || IsConstantInit(Init)) && HasEmptyDtor(VD));748}749} // namespace750 751void SemaCUDA::checkAllowedInitializer(VarDecl *VD) {752  // Return early if VD is inside a non-instantiated template function since753  // the implicit constructor is not defined yet.754  if (const FunctionDecl *FD =755          dyn_cast_or_null<FunctionDecl>(VD->getDeclContext());756      FD && FD->isDependentContext())757    return;758 759  bool IsSharedVar = VD->hasAttr<CUDASharedAttr>();760  bool IsDeviceOrConstantVar =761      !IsSharedVar &&762      (VD->hasAttr<CUDADeviceAttr>() || VD->hasAttr<CUDAConstantAttr>());763  if ((IsSharedVar || IsDeviceOrConstantVar) &&764      VD->getType().getQualifiers().getAddressSpace() != LangAS::Default) {765    Diag(VD->getLocation(), diag::err_cuda_address_space_gpuvar);766    VD->setInvalidDecl();767    return;768  }769  // Do not check dependent variables since the ctor/dtor/initializer are not770  // determined. Do it after instantiation.771  if (VD->isInvalidDecl() || !VD->hasInit() || !VD->hasGlobalStorage() ||772      IsDependentVar(VD))773    return;774  const Expr *Init = VD->getInit();775  if (IsDeviceOrConstantVar || IsSharedVar) {776    if (HasAllowedCUDADeviceStaticInitializer(777            *this, VD, IsSharedVar ? CICK_Shared : CICK_DeviceOrConstant))778      return;779    Diag(VD->getLocation(),780         IsSharedVar ? diag::err_shared_var_init : diag::err_dynamic_var_init)781        << Init->getSourceRange();782    VD->setInvalidDecl();783  } else {784    // This is a host-side global variable.  Check that the initializer is785    // callable from the host side.786    const FunctionDecl *InitFn = nullptr;787    if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Init)) {788      InitFn = CE->getConstructor();789    } else if (const CallExpr *CE = dyn_cast<CallExpr>(Init)) {790      InitFn = CE->getDirectCallee();791    }792    if (InitFn) {793      CUDAFunctionTarget InitFnTarget = IdentifyTarget(InitFn);794      if (InitFnTarget != CUDAFunctionTarget::Host &&795          InitFnTarget != CUDAFunctionTarget::HostDevice) {796        Diag(VD->getLocation(), diag::err_ref_bad_target_global_initializer)797            << InitFnTarget << InitFn;798        Diag(InitFn->getLocation(), diag::note_previous_decl) << InitFn;799        VD->setInvalidDecl();800      }801    }802  }803}804 805void SemaCUDA::RecordImplicitHostDeviceFuncUsedByDevice(806    const FunctionDecl *Callee) {807  FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);808  if (!Caller)809    return;810 811  if (!isImplicitHostDeviceFunction(Callee))812    return;813 814  CUDAFunctionTarget CallerTarget = IdentifyTarget(Caller);815 816  // Record whether an implicit host device function is used on device side.817  if (CallerTarget != CUDAFunctionTarget::Device &&818      CallerTarget != CUDAFunctionTarget::Global &&819      (CallerTarget != CUDAFunctionTarget::HostDevice ||820       (isImplicitHostDeviceFunction(Caller) &&821        !getASTContext().CUDAImplicitHostDeviceFunUsedByDevice.count(Caller))))822    return;823 824  getASTContext().CUDAImplicitHostDeviceFunUsedByDevice.insert(Callee);825}826 827// With -fcuda-host-device-constexpr, an unattributed constexpr function is828// treated as implicitly __host__ __device__, unless:829//  * it is a variadic function (device-side variadic functions are not830//    allowed), or831//  * a __device__ function with this signature was already declared, in which832//    case in which case we output an error, unless the __device__ decl is in a833//    system header, in which case we leave the constexpr function unattributed.834//835// In addition, all function decls are treated as __host__ __device__ when836// ForceHostDeviceDepth > 0 (corresponding to code within a837//   #pragma clang force_cuda_host_device_begin/end838// pair).839void SemaCUDA::maybeAddHostDeviceAttrs(FunctionDecl *NewD,840                                       const LookupResult &Previous) {841  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");842 843  if (ForceHostDeviceDepth > 0) {844    if (!NewD->hasAttr<CUDAHostAttr>())845      NewD->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));846    if (!NewD->hasAttr<CUDADeviceAttr>())847      NewD->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));848    return;849  }850 851  // If a template function has no host/device/global attributes,852  // make it implicitly host device function.853  if (getLangOpts().OffloadImplicitHostDeviceTemplates &&854      !NewD->hasAttr<CUDAHostAttr>() && !NewD->hasAttr<CUDADeviceAttr>() &&855      !NewD->hasAttr<CUDAGlobalAttr>() &&856      (NewD->getDescribedFunctionTemplate() ||857       NewD->isFunctionTemplateSpecialization())) {858    NewD->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));859    NewD->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));860    return;861  }862 863  if (!getLangOpts().CUDAHostDeviceConstexpr || !NewD->isConstexpr() ||864      NewD->isVariadic() || NewD->hasAttr<CUDAHostAttr>() ||865      NewD->hasAttr<CUDADeviceAttr>() || NewD->hasAttr<CUDAGlobalAttr>())866    return;867 868  // Is D a __device__ function with the same signature as NewD, ignoring CUDA869  // attributes?870  auto IsMatchingDeviceFn = [&](NamedDecl *D) {871    if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(D))872      D = Using->getTargetDecl();873    FunctionDecl *OldD = D->getAsFunction();874    return OldD && OldD->hasAttr<CUDADeviceAttr>() &&875           !OldD->hasAttr<CUDAHostAttr>() &&876           !SemaRef.IsOverload(NewD, OldD,877                               /* UseMemberUsingDeclRules = */ false,878                               /* ConsiderCudaAttrs = */ false);879  };880  auto It = llvm::find_if(Previous, IsMatchingDeviceFn);881  if (It != Previous.end()) {882    // We found a __device__ function with the same name and signature as NewD883    // (ignoring CUDA attrs).  This is an error unless that function is defined884    // in a system header, in which case we simply return without making NewD885    // host+device.886    NamedDecl *Match = *It;887    if (!SemaRef.getSourceManager().isInSystemHeader(Match->getLocation())) {888      Diag(NewD->getLocation(),889           diag::err_cuda_unattributed_constexpr_cannot_overload_device)890          << NewD;891      Diag(Match->getLocation(),892           diag::note_cuda_conflicting_device_function_declared_here);893    }894    return;895  }896 897  NewD->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));898  NewD->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));899}900 901// TODO: `__constant__` memory may be a limited resource for certain targets.902// A safeguard may be needed at the end of compilation pipeline if903// `__constant__` memory usage goes beyond limit.904void SemaCUDA::MaybeAddConstantAttr(VarDecl *VD) {905  // Do not promote dependent variables since the cotr/dtor/initializer are906  // not determined. Do it after instantiation.907  if (getLangOpts().CUDAIsDevice && !VD->hasAttr<CUDAConstantAttr>() &&908      !VD->hasAttr<CUDASharedAttr>() &&909      (VD->isFileVarDecl() || VD->isStaticDataMember()) &&910      !IsDependentVar(VD) &&911      ((VD->isConstexpr() || VD->getType().isConstQualified()) &&912       HasAllowedCUDADeviceStaticInitializer(*this, VD,913                                             CICK_DeviceOrConstant))) {914    VD->addAttr(CUDAConstantAttr::CreateImplicit(getASTContext()));915  }916}917 918SemaBase::SemaDiagnosticBuilder SemaCUDA::DiagIfDeviceCode(SourceLocation Loc,919                                                           unsigned DiagID) {920  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");921  FunctionDecl *CurFunContext =922      SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);923  SemaDiagnosticBuilder::Kind DiagKind = [&] {924    if (!CurFunContext)925      return SemaDiagnosticBuilder::K_Nop;926    switch (CurrentTarget()) {927    case CUDAFunctionTarget::Global:928    case CUDAFunctionTarget::Device:929      return SemaDiagnosticBuilder::K_Immediate;930    case CUDAFunctionTarget::HostDevice:931      // An HD function counts as host code if we're compiling for host, and932      // device code if we're compiling for device.  Defer any errors in device933      // mode until the function is known-emitted.934      if (!getLangOpts().CUDAIsDevice)935        return SemaDiagnosticBuilder::K_Nop;936      if (SemaRef.IsLastErrorImmediate &&937          getDiagnostics().getDiagnosticIDs()->isNote(DiagID))938        return SemaDiagnosticBuilder::K_Immediate;939      return (SemaRef.getEmissionStatus(CurFunContext) ==940              Sema::FunctionEmissionStatus::Emitted)941                 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack942                 : SemaDiagnosticBuilder::K_Deferred;943    default:944      return SemaDiagnosticBuilder::K_Nop;945    }946  }();947  return SemaDiagnosticBuilder(DiagKind, Loc, DiagID, CurFunContext, SemaRef);948}949 950Sema::SemaDiagnosticBuilder SemaCUDA::DiagIfHostCode(SourceLocation Loc,951                                                     unsigned DiagID) {952  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");953  FunctionDecl *CurFunContext =954      SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);955  SemaDiagnosticBuilder::Kind DiagKind = [&] {956    if (!CurFunContext)957      return SemaDiagnosticBuilder::K_Nop;958    switch (CurrentTarget()) {959    case CUDAFunctionTarget::Host:960      return SemaDiagnosticBuilder::K_Immediate;961    case CUDAFunctionTarget::HostDevice:962      // An HD function counts as host code if we're compiling for host, and963      // device code if we're compiling for device.  Defer any errors in device964      // mode until the function is known-emitted.965      if (getLangOpts().CUDAIsDevice)966        return SemaDiagnosticBuilder::K_Nop;967      if (SemaRef.IsLastErrorImmediate &&968          getDiagnostics().getDiagnosticIDs()->isNote(DiagID))969        return SemaDiagnosticBuilder::K_Immediate;970      return (SemaRef.getEmissionStatus(CurFunContext) ==971              Sema::FunctionEmissionStatus::Emitted)972                 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack973                 : SemaDiagnosticBuilder::K_Deferred;974    default:975      return SemaDiagnosticBuilder::K_Nop;976    }977  }();978  return SemaDiagnosticBuilder(DiagKind, Loc, DiagID, CurFunContext, SemaRef);979}980 981bool SemaCUDA::CheckCall(SourceLocation Loc, FunctionDecl *Callee) {982  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");983  assert(Callee && "Callee may not be null.");984 985  const auto &ExprEvalCtx = SemaRef.currentEvaluationContext();986  if (ExprEvalCtx.isUnevaluated() || ExprEvalCtx.isConstantEvaluated())987    return true;988 989  // FIXME: Is bailing out early correct here?  Should we instead assume that990  // the caller is a global initializer?991  FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);992  if (!Caller)993    return true;994 995  // If the caller is known-emitted, mark the callee as known-emitted.996  // Otherwise, mark the call in our call graph so we can traverse it later.997  bool CallerKnownEmitted = SemaRef.getEmissionStatus(Caller) ==998                            Sema::FunctionEmissionStatus::Emitted;999  SemaDiagnosticBuilder::Kind DiagKind = [this, Caller, Callee,1000                                          CallerKnownEmitted] {1001    switch (IdentifyPreference(Caller, Callee)) {1002    case CFP_Never:1003    case CFP_WrongSide:1004      assert(Caller && "Never/wrongSide calls require a non-null caller");1005      // If we know the caller will be emitted, we know this wrong-side call1006      // will be emitted, so it's an immediate error.  Otherwise, defer the1007      // error until we know the caller is emitted.1008      return CallerKnownEmitted1009                 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack1010                 : SemaDiagnosticBuilder::K_Deferred;1011    default:1012      return SemaDiagnosticBuilder::K_Nop;1013    }1014  }();1015 1016  if (DiagKind == SemaDiagnosticBuilder::K_Nop) {1017    // For -fgpu-rdc, keep track of external kernels used by host functions.1018    if (getLangOpts().CUDAIsDevice && getLangOpts().GPURelocatableDeviceCode &&1019        Callee->hasAttr<CUDAGlobalAttr>() && !Callee->isDefined() &&1020        (!Caller || (!Caller->getDescribedFunctionTemplate() &&1021                     getASTContext().GetGVALinkageForFunction(Caller) ==1022                         GVA_StrongExternal)))1023      getASTContext().CUDAExternalDeviceDeclODRUsedByHost.insert(Callee);1024    return true;1025  }1026 1027  // Avoid emitting this error twice for the same location.  Using a hashtable1028  // like this is unfortunate, but because we must continue parsing as normal1029  // after encountering a deferred error, it's otherwise very tricky for us to1030  // ensure that we only emit this deferred error once.1031  if (!LocsWithCUDACallDiags.insert({Caller, Loc}).second)1032    return true;1033 1034  SemaDiagnosticBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller,1035                        SemaRef)1036      << IdentifyTarget(Callee) << /*function*/ 0 << Callee1037      << IdentifyTarget(Caller);1038  if (!Callee->getBuiltinID())1039    SemaDiagnosticBuilder(DiagKind, Callee->getLocation(),1040                          diag::note_previous_decl, Caller, SemaRef)1041        << Callee;1042  return DiagKind != SemaDiagnosticBuilder::K_Immediate &&1043         DiagKind != SemaDiagnosticBuilder::K_ImmediateWithCallStack;1044}1045 1046// Check the wrong-sided reference capture of lambda for CUDA/HIP.1047// A lambda function may capture a stack variable by reference when it is1048// defined and uses the capture by reference when the lambda is called. When1049// the capture and use happen on different sides, the capture is invalid and1050// should be diagnosed.1051void SemaCUDA::CheckLambdaCapture(CXXMethodDecl *Callee,1052                                  const sema::Capture &Capture) {1053  // In host compilation we only need to check lambda functions emitted on host1054  // side. In such lambda functions, a reference capture is invalid only1055  // if the lambda structure is populated by a device function or kernel then1056  // is passed to and called by a host function. However that is impossible,1057  // since a device function or kernel can only call a device function, also a1058  // kernel cannot pass a lambda back to a host function since we cannot1059  // define a kernel argument type which can hold the lambda before the lambda1060  // itself is defined.1061  if (!getLangOpts().CUDAIsDevice)1062    return;1063 1064  // File-scope lambda can only do init captures for global variables, which1065  // results in passing by value for these global variables.1066  FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);1067  if (!Caller)1068    return;1069 1070  // In device compilation, we only need to check lambda functions which are1071  // emitted on device side. For such lambdas, a reference capture is invalid1072  // only if the lambda structure is populated by a host function then passed1073  // to and called in a device function or kernel.1074  bool CalleeIsDevice = Callee->hasAttr<CUDADeviceAttr>();1075  bool CallerIsHost =1076      !Caller->hasAttr<CUDAGlobalAttr>() && !Caller->hasAttr<CUDADeviceAttr>();1077  bool ShouldCheck = CalleeIsDevice && CallerIsHost;1078  if (!ShouldCheck || !Capture.isReferenceCapture())1079    return;1080  auto DiagKind = SemaDiagnosticBuilder::K_Deferred;1081  if (Capture.isVariableCapture() && !getLangOpts().HIPStdPar) {1082    SemaDiagnosticBuilder(DiagKind, Capture.getLocation(),1083                          diag::err_capture_bad_target, Callee, SemaRef)1084        << Capture.getVariable();1085  } else if (Capture.isThisCapture()) {1086    // Capture of this pointer is allowed since this pointer may be pointing to1087    // managed memory which is accessible on both device and host sides. It only1088    // results in invalid memory access if this pointer points to memory not1089    // accessible on device side.1090    SemaDiagnosticBuilder(DiagKind, Capture.getLocation(),1091                          diag::warn_maybe_capture_bad_target_this_ptr, Callee,1092                          SemaRef);1093  }1094}1095 1096void SemaCUDA::SetLambdaAttrs(CXXMethodDecl *Method) {1097  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");1098  if (Method->hasAttr<CUDAHostAttr>() || Method->hasAttr<CUDADeviceAttr>())1099    return;1100  Method->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));1101  Method->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));1102}1103 1104void SemaCUDA::checkTargetOverload(FunctionDecl *NewFD,1105                                   const LookupResult &Previous) {1106  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");1107  CUDAFunctionTarget NewTarget = IdentifyTarget(NewFD);1108  for (NamedDecl *OldND : Previous) {1109    FunctionDecl *OldFD = OldND->getAsFunction();1110    if (!OldFD)1111      continue;1112 1113    CUDAFunctionTarget OldTarget = IdentifyTarget(OldFD);1114    // Don't allow HD and global functions to overload other functions with the1115    // same signature.  We allow overloading based on CUDA attributes so that1116    // functions can have different implementations on the host and device, but1117    // HD/global functions "exist" in some sense on both the host and device, so1118    // should have the same implementation on both sides.1119    if (NewTarget != OldTarget &&1120        !SemaRef.IsOverload(NewFD, OldFD, /* UseMemberUsingDeclRules = */ false,1121                            /* ConsiderCudaAttrs = */ false)) {1122      if ((NewTarget == CUDAFunctionTarget::HostDevice &&1123           !(getLangOpts().OffloadImplicitHostDeviceTemplates &&1124             isImplicitHostDeviceFunction(NewFD) &&1125             OldTarget == CUDAFunctionTarget::Device)) ||1126          (OldTarget == CUDAFunctionTarget::HostDevice &&1127           !(getLangOpts().OffloadImplicitHostDeviceTemplates &&1128             isImplicitHostDeviceFunction(OldFD) &&1129             NewTarget == CUDAFunctionTarget::Device)) ||1130          (NewTarget == CUDAFunctionTarget::Global) ||1131          (OldTarget == CUDAFunctionTarget::Global)) {1132        Diag(NewFD->getLocation(), diag::err_cuda_ovl_target)1133            << NewTarget << NewFD->getDeclName() << OldTarget << OldFD;1134        Diag(OldFD->getLocation(), diag::note_previous_declaration);1135        NewFD->setInvalidDecl();1136        break;1137      }1138      if ((NewTarget == CUDAFunctionTarget::Host &&1139           OldTarget == CUDAFunctionTarget::Device) ||1140          (NewTarget == CUDAFunctionTarget::Device &&1141           OldTarget == CUDAFunctionTarget::Host)) {1142        Diag(NewFD->getLocation(), diag::warn_offload_incompatible_redeclare)1143            << NewTarget << OldTarget;1144        Diag(OldFD->getLocation(), diag::note_previous_declaration);1145      }1146    }1147  }1148}1149 1150template <typename AttrTy>1151static void copyAttrIfPresent(Sema &S, FunctionDecl *FD,1152                              const FunctionDecl &TemplateFD) {1153  if (AttrTy *Attribute = TemplateFD.getAttr<AttrTy>()) {1154    AttrTy *Clone = Attribute->clone(S.Context);1155    Clone->setInherited(true);1156    FD->addAttr(Clone);1157  }1158}1159 1160void SemaCUDA::inheritTargetAttrs(FunctionDecl *FD,1161                                  const FunctionTemplateDecl &TD) {1162  const FunctionDecl &TemplateFD = *TD.getTemplatedDecl();1163  copyAttrIfPresent<CUDAGlobalAttr>(SemaRef, FD, TemplateFD);1164  copyAttrIfPresent<CUDAHostAttr>(SemaRef, FD, TemplateFD);1165  copyAttrIfPresent<CUDADeviceAttr>(SemaRef, FD, TemplateFD);1166}1167 1168std::string SemaCUDA::getConfigureFuncName() const {1169  if (getLangOpts().OffloadViaLLVM)1170    return "__llvmPushCallConfiguration";1171 1172  if (getLangOpts().HIP)1173    return getLangOpts().HIPUseNewLaunchAPI ? "__hipPushCallConfiguration"1174                                            : "hipConfigureCall";1175 1176  // New CUDA kernel launch sequence.1177  if (CudaFeatureEnabled(getASTContext().getTargetInfo().getSDKVersion(),1178                         CudaFeature::CUDA_USES_NEW_LAUNCH))1179    return "__cudaPushCallConfiguration";1180 1181  // Legacy CUDA kernel configuration call1182  return "cudaConfigureCall";1183}1184 1185std::string SemaCUDA::getGetParameterBufferFuncName() const {1186  return "cudaGetParameterBuffer";1187}1188 1189std::string SemaCUDA::getLaunchDeviceFuncName() const {1190  return "cudaLaunchDevice";1191}1192 1193// Record any local constexpr variables that are passed one way on the host1194// and another on the device.1195void SemaCUDA::recordPotentialODRUsedVariable(1196    MultiExprArg Arguments, OverloadCandidateSet &Candidates) {1197  sema::LambdaScopeInfo *LambdaInfo = SemaRef.getCurLambda();1198  if (!LambdaInfo)1199    return;1200 1201  for (unsigned I = 0; I < Arguments.size(); ++I) {1202    auto *DeclRef = dyn_cast<DeclRefExpr>(Arguments[I]);1203    if (!DeclRef)1204      continue;1205    auto *Variable = dyn_cast<VarDecl>(DeclRef->getDecl());1206    if (!Variable || !Variable->isLocalVarDecl() || !Variable->isConstexpr())1207      continue;1208 1209    bool HostByValue = false, HostByRef = false;1210    bool DeviceByValue = false, DeviceByRef = false;1211 1212    for (OverloadCandidate &Candidate : Candidates) {1213      FunctionDecl *Callee = Candidate.Function;1214      if (!Callee || I >= Callee->getNumParams())1215        continue;1216 1217      CUDAFunctionTarget Target = IdentifyTarget(Callee);1218      if (Target == CUDAFunctionTarget::InvalidTarget ||1219          Target == CUDAFunctionTarget::Global)1220        continue;1221 1222      bool CoversHost = (Target == CUDAFunctionTarget::Host ||1223                         Target == CUDAFunctionTarget::HostDevice);1224      bool CoversDevice = (Target == CUDAFunctionTarget::Device ||1225                           Target == CUDAFunctionTarget::HostDevice);1226 1227      bool IsRef = Callee->getParamDecl(I)->getType()->isReferenceType();1228      HostByValue |= CoversHost && !IsRef;1229      HostByRef |= CoversHost && IsRef;1230      DeviceByValue |= CoversDevice && !IsRef;1231      DeviceByRef |= CoversDevice && IsRef;1232    }1233 1234    if ((HostByValue && DeviceByRef) || (HostByRef && DeviceByValue))1235      LambdaInfo->CUDAPotentialODRUsedVars.insert(Variable);1236  }1237}1238