[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / Sema / SemaCUDA.cpp
blobd993499cf4a6e6e9f1a53a795185f89b9598832c
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file implements semantic analysis for CUDA constructs.
10 ///
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTContext.h"
14 #include "clang/AST/Decl.h"
15 #include "clang/AST/ExprCXX.h"
16 #include "clang/Basic/Cuda.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/Lex/Preprocessor.h"
19 #include "clang/Sema/Lookup.h"
20 #include "clang/Sema/ScopeInfo.h"
21 #include "clang/Sema/Sema.h"
22 #include "clang/Sema/SemaDiagnostic.h"
23 #include "clang/Sema/SemaInternal.h"
24 #include "clang/Sema/Template.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include <optional>
27 using namespace clang;
29 template <typename AttrT> static bool hasExplicitAttr(const VarDecl *D) {
30 if (!D)
31 return false;
32 if (auto *A = D->getAttr<AttrT>())
33 return !A->isImplicit();
34 return false;
37 void Sema::PushForceCUDAHostDevice() {
38 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
39 ForceCUDAHostDeviceDepth++;
42 bool Sema::PopForceCUDAHostDevice() {
43 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
44 if (ForceCUDAHostDeviceDepth == 0)
45 return false;
46 ForceCUDAHostDeviceDepth--;
47 return true;
50 ExprResult Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc,
51 MultiExprArg ExecConfig,
52 SourceLocation GGGLoc) {
53 FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl();
54 if (!ConfigDecl)
55 return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use)
56 << getCudaConfigureFuncName());
57 QualType ConfigQTy = ConfigDecl->getType();
59 DeclRefExpr *ConfigDR = new (Context)
60 DeclRefExpr(Context, ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc);
61 MarkFunctionReferenced(LLLLoc, ConfigDecl);
63 return BuildCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, nullptr,
64 /*IsExecConfig=*/true);
67 Sema::CUDAFunctionTarget
68 Sema::IdentifyCUDATarget(const ParsedAttributesView &Attrs) {
69 bool HasHostAttr = false;
70 bool HasDeviceAttr = false;
71 bool HasGlobalAttr = false;
72 bool HasInvalidTargetAttr = false;
73 for (const ParsedAttr &AL : Attrs) {
74 switch (AL.getKind()) {
75 case ParsedAttr::AT_CUDAGlobal:
76 HasGlobalAttr = true;
77 break;
78 case ParsedAttr::AT_CUDAHost:
79 HasHostAttr = true;
80 break;
81 case ParsedAttr::AT_CUDADevice:
82 HasDeviceAttr = true;
83 break;
84 case ParsedAttr::AT_CUDAInvalidTarget:
85 HasInvalidTargetAttr = true;
86 break;
87 default:
88 break;
92 if (HasInvalidTargetAttr)
93 return CFT_InvalidTarget;
95 if (HasGlobalAttr)
96 return CFT_Global;
98 if (HasHostAttr && HasDeviceAttr)
99 return CFT_HostDevice;
101 if (HasDeviceAttr)
102 return CFT_Device;
104 return CFT_Host;
107 template <typename A>
108 static bool hasAttr(const Decl *D, bool IgnoreImplicitAttr) {
109 return D->hasAttrs() && llvm::any_of(D->getAttrs(), [&](Attr *Attribute) {
110 return isa<A>(Attribute) &&
111 !(IgnoreImplicitAttr && Attribute->isImplicit());
115 Sema::CUDATargetContextRAII::CUDATargetContextRAII(Sema &S_,
116 CUDATargetContextKind K,
117 Decl *D)
118 : S(S_) {
119 SavedCtx = S.CurCUDATargetCtx;
120 assert(K == CTCK_InitGlobalVar);
121 auto *VD = dyn_cast_or_null<VarDecl>(D);
122 if (VD && VD->hasGlobalStorage() && !VD->isStaticLocal()) {
123 auto Target = CFT_Host;
124 if ((hasAttr<CUDADeviceAttr>(VD, /*IgnoreImplicit=*/true) &&
125 !hasAttr<CUDAHostAttr>(VD, /*IgnoreImplicit=*/true)) ||
126 hasAttr<CUDASharedAttr>(VD, /*IgnoreImplicit=*/true) ||
127 hasAttr<CUDAConstantAttr>(VD, /*IgnoreImplicit=*/true))
128 Target = CFT_Device;
129 S.CurCUDATargetCtx = {Target, K, VD};
133 /// IdentifyCUDATarget - Determine the CUDA compilation target for this function
134 Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D,
135 bool IgnoreImplicitHDAttr) {
136 // Code that lives outside a function gets the target from CurCUDATargetCtx.
137 if (D == nullptr)
138 return CurCUDATargetCtx.Target;
140 if (D->hasAttr<CUDAInvalidTargetAttr>())
141 return CFT_InvalidTarget;
143 if (D->hasAttr<CUDAGlobalAttr>())
144 return CFT_Global;
146 if (hasAttr<CUDADeviceAttr>(D, IgnoreImplicitHDAttr)) {
147 if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr))
148 return CFT_HostDevice;
149 return CFT_Device;
150 } else if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr)) {
151 return CFT_Host;
152 } else if ((D->isImplicit() || !D->isUserProvided()) &&
153 !IgnoreImplicitHDAttr) {
154 // Some implicit declarations (like intrinsic functions) are not marked.
155 // Set the most lenient target on them for maximal flexibility.
156 return CFT_HostDevice;
159 return CFT_Host;
162 /// IdentifyTarget - Determine the CUDA compilation target for this variable.
163 Sema::CUDAVariableTarget Sema::IdentifyCUDATarget(const VarDecl *Var) {
164 if (Var->hasAttr<HIPManagedAttr>())
165 return CVT_Unified;
166 // Only constexpr and const variabless with implicit constant attribute
167 // are emitted on both sides. Such variables are promoted to device side
168 // only if they have static constant intializers on device side.
169 if ((Var->isConstexpr() || Var->getType().isConstQualified()) &&
170 Var->hasAttr<CUDAConstantAttr>() &&
171 !hasExplicitAttr<CUDAConstantAttr>(Var))
172 return CVT_Both;
173 if (Var->hasAttr<CUDADeviceAttr>() || Var->hasAttr<CUDAConstantAttr>() ||
174 Var->hasAttr<CUDASharedAttr>() ||
175 Var->getType()->isCUDADeviceBuiltinSurfaceType() ||
176 Var->getType()->isCUDADeviceBuiltinTextureType())
177 return CVT_Device;
178 // Function-scope static variable without explicit device or constant
179 // attribute are emitted
180 // - on both sides in host device functions
181 // - on device side in device or global functions
182 if (auto *FD = dyn_cast<FunctionDecl>(Var->getDeclContext())) {
183 switch (IdentifyCUDATarget(FD)) {
184 case CFT_HostDevice:
185 return CVT_Both;
186 case CFT_Device:
187 case CFT_Global:
188 return CVT_Device;
189 default:
190 return CVT_Host;
193 return CVT_Host;
196 // * CUDA Call preference table
198 // F - from,
199 // T - to
200 // Ph - preference in host mode
201 // Pd - preference in device mode
202 // H - handled in (x)
203 // Preferences: N:native, SS:same side, HD:host-device, WS:wrong side, --:never.
205 // | F | T | Ph | Pd | H |
206 // |----+----+-----+-----+-----+
207 // | d | d | N | N | (c) |
208 // | d | g | -- | -- | (a) |
209 // | d | h | -- | -- | (e) |
210 // | d | hd | HD | HD | (b) |
211 // | g | d | N | N | (c) |
212 // | g | g | -- | -- | (a) |
213 // | g | h | -- | -- | (e) |
214 // | g | hd | HD | HD | (b) |
215 // | h | d | -- | -- | (e) |
216 // | h | g | N | N | (c) |
217 // | h | h | N | N | (c) |
218 // | h | hd | HD | HD | (b) |
219 // | hd | d | WS | SS | (d) |
220 // | hd | g | SS | -- |(d/a)|
221 // | hd | h | SS | WS | (d) |
222 // | hd | hd | HD | HD | (b) |
224 Sema::CUDAFunctionPreference
225 Sema::IdentifyCUDAPreference(const FunctionDecl *Caller,
226 const FunctionDecl *Callee) {
227 assert(Callee && "Callee must be valid.");
228 CUDAFunctionTarget CallerTarget = IdentifyCUDATarget(Caller);
229 CUDAFunctionTarget CalleeTarget = IdentifyCUDATarget(Callee);
231 // If one of the targets is invalid, the check always fails, no matter what
232 // the other target is.
233 if (CallerTarget == CFT_InvalidTarget || CalleeTarget == CFT_InvalidTarget)
234 return CFP_Never;
236 // (a) Can't call global from some contexts until we support CUDA's
237 // dynamic parallelism.
238 if (CalleeTarget == CFT_Global &&
239 (CallerTarget == CFT_Global || CallerTarget == CFT_Device))
240 return CFP_Never;
242 // (b) Calling HostDevice is OK for everyone.
243 if (CalleeTarget == CFT_HostDevice)
244 return CFP_HostDevice;
246 // (c) Best case scenarios
247 if (CalleeTarget == CallerTarget ||
248 (CallerTarget == CFT_Host && CalleeTarget == CFT_Global) ||
249 (CallerTarget == CFT_Global && CalleeTarget == CFT_Device))
250 return CFP_Native;
252 // HipStdPar mode is special, in that assessing whether a device side call to
253 // a host target is deferred to a subsequent pass, and cannot unambiguously be
254 // adjudicated in the AST, hence we optimistically allow them to pass here.
255 if (getLangOpts().HIPStdPar &&
256 (CallerTarget == CFT_Global || CallerTarget == CFT_Device ||
257 CallerTarget == CFT_HostDevice) &&
258 CalleeTarget == CFT_Host)
259 return CFP_HostDevice;
261 // (d) HostDevice behavior depends on compilation mode.
262 if (CallerTarget == CFT_HostDevice) {
263 // It's OK to call a compilation-mode matching function from an HD one.
264 if ((getLangOpts().CUDAIsDevice && CalleeTarget == CFT_Device) ||
265 (!getLangOpts().CUDAIsDevice &&
266 (CalleeTarget == CFT_Host || CalleeTarget == CFT_Global)))
267 return CFP_SameSide;
269 // Calls from HD to non-mode-matching functions (i.e., to host functions
270 // when compiling in device mode or to device functions when compiling in
271 // host mode) are allowed at the sema level, but eventually rejected if
272 // they're ever codegened. TODO: Reject said calls earlier.
273 return CFP_WrongSide;
276 // (e) Calling across device/host boundary is not something you should do.
277 if ((CallerTarget == CFT_Host && CalleeTarget == CFT_Device) ||
278 (CallerTarget == CFT_Device && CalleeTarget == CFT_Host) ||
279 (CallerTarget == CFT_Global && CalleeTarget == CFT_Host))
280 return CFP_Never;
282 llvm_unreachable("All cases should've been handled by now.");
285 template <typename AttrT> static bool hasImplicitAttr(const FunctionDecl *D) {
286 if (!D)
287 return false;
288 if (auto *A = D->getAttr<AttrT>())
289 return A->isImplicit();
290 return D->isImplicit();
293 bool Sema::isCUDAImplicitHostDeviceFunction(const FunctionDecl *D) {
294 bool IsImplicitDevAttr = hasImplicitAttr<CUDADeviceAttr>(D);
295 bool IsImplicitHostAttr = hasImplicitAttr<CUDAHostAttr>(D);
296 return IsImplicitDevAttr && IsImplicitHostAttr;
299 void Sema::EraseUnwantedCUDAMatches(
300 const FunctionDecl *Caller,
301 SmallVectorImpl<std::pair<DeclAccessPair, FunctionDecl *>> &Matches) {
302 if (Matches.size() <= 1)
303 return;
305 using Pair = std::pair<DeclAccessPair, FunctionDecl*>;
307 // Gets the CUDA function preference for a call from Caller to Match.
308 auto GetCFP = [&](const Pair &Match) {
309 return IdentifyCUDAPreference(Caller, Match.second);
312 // Find the best call preference among the functions in Matches.
313 CUDAFunctionPreference BestCFP = GetCFP(*std::max_element(
314 Matches.begin(), Matches.end(),
315 [&](const Pair &M1, const Pair &M2) { return GetCFP(M1) < GetCFP(M2); }));
317 // Erase all functions with lower priority.
318 llvm::erase_if(Matches,
319 [&](const Pair &Match) { return GetCFP(Match) < BestCFP; });
322 /// When an implicitly-declared special member has to invoke more than one
323 /// base/field special member, conflicts may occur in the targets of these
324 /// members. For example, if one base's member __host__ and another's is
325 /// __device__, it's a conflict.
326 /// This function figures out if the given targets \param Target1 and
327 /// \param Target2 conflict, and if they do not it fills in
328 /// \param ResolvedTarget with a target that resolves for both calls.
329 /// \return true if there's a conflict, false otherwise.
330 static bool
331 resolveCalleeCUDATargetConflict(Sema::CUDAFunctionTarget Target1,
332 Sema::CUDAFunctionTarget Target2,
333 Sema::CUDAFunctionTarget *ResolvedTarget) {
334 // Only free functions and static member functions may be global.
335 assert(Target1 != Sema::CFT_Global);
336 assert(Target2 != Sema::CFT_Global);
338 if (Target1 == Sema::CFT_HostDevice) {
339 *ResolvedTarget = Target2;
340 } else if (Target2 == Sema::CFT_HostDevice) {
341 *ResolvedTarget = Target1;
342 } else if (Target1 != Target2) {
343 return true;
344 } else {
345 *ResolvedTarget = Target1;
348 return false;
351 bool Sema::inferCUDATargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl,
352 CXXSpecialMember CSM,
353 CXXMethodDecl *MemberDecl,
354 bool ConstRHS,
355 bool Diagnose) {
356 // If the defaulted special member is defined lexically outside of its
357 // owning class, or the special member already has explicit device or host
358 // attributes, do not infer.
359 bool InClass = MemberDecl->getLexicalParent() == MemberDecl->getParent();
360 bool HasH = MemberDecl->hasAttr<CUDAHostAttr>();
361 bool HasD = MemberDecl->hasAttr<CUDADeviceAttr>();
362 bool HasExplicitAttr =
363 (HasD && !MemberDecl->getAttr<CUDADeviceAttr>()->isImplicit()) ||
364 (HasH && !MemberDecl->getAttr<CUDAHostAttr>()->isImplicit());
365 if (!InClass || HasExplicitAttr)
366 return false;
368 std::optional<CUDAFunctionTarget> InferredTarget;
370 // We're going to invoke special member lookup; mark that these special
371 // members are called from this one, and not from its caller.
372 ContextRAII MethodContext(*this, MemberDecl);
374 // Look for special members in base classes that should be invoked from here.
375 // Infer the target of this member base on the ones it should call.
376 // Skip direct and indirect virtual bases for abstract classes.
377 llvm::SmallVector<const CXXBaseSpecifier *, 16> Bases;
378 for (const auto &B : ClassDecl->bases()) {
379 if (!B.isVirtual()) {
380 Bases.push_back(&B);
384 if (!ClassDecl->isAbstract()) {
385 llvm::append_range(Bases, llvm::make_pointer_range(ClassDecl->vbases()));
388 for (const auto *B : Bases) {
389 const RecordType *BaseType = B->getType()->getAs<RecordType>();
390 if (!BaseType) {
391 continue;
394 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl());
395 Sema::SpecialMemberOverloadResult SMOR =
396 LookupSpecialMember(BaseClassDecl, CSM,
397 /* ConstArg */ ConstRHS,
398 /* VolatileArg */ false,
399 /* RValueThis */ false,
400 /* ConstThis */ false,
401 /* VolatileThis */ false);
403 if (!SMOR.getMethod())
404 continue;
406 CUDAFunctionTarget BaseMethodTarget = IdentifyCUDATarget(SMOR.getMethod());
407 if (!InferredTarget) {
408 InferredTarget = BaseMethodTarget;
409 } else {
410 bool ResolutionError = resolveCalleeCUDATargetConflict(
411 *InferredTarget, BaseMethodTarget, &*InferredTarget);
412 if (ResolutionError) {
413 if (Diagnose) {
414 Diag(ClassDecl->getLocation(),
415 diag::note_implicit_member_target_infer_collision)
416 << (unsigned)CSM << *InferredTarget << BaseMethodTarget;
418 MemberDecl->addAttr(CUDAInvalidTargetAttr::CreateImplicit(Context));
419 return true;
424 // Same as for bases, but now for special members of fields.
425 for (const auto *F : ClassDecl->fields()) {
426 if (F->isInvalidDecl()) {
427 continue;
430 const RecordType *FieldType =
431 Context.getBaseElementType(F->getType())->getAs<RecordType>();
432 if (!FieldType) {
433 continue;
436 CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(FieldType->getDecl());
437 Sema::SpecialMemberOverloadResult SMOR =
438 LookupSpecialMember(FieldRecDecl, CSM,
439 /* ConstArg */ ConstRHS && !F->isMutable(),
440 /* VolatileArg */ false,
441 /* RValueThis */ false,
442 /* ConstThis */ false,
443 /* VolatileThis */ false);
445 if (!SMOR.getMethod())
446 continue;
448 CUDAFunctionTarget FieldMethodTarget =
449 IdentifyCUDATarget(SMOR.getMethod());
450 if (!InferredTarget) {
451 InferredTarget = FieldMethodTarget;
452 } else {
453 bool ResolutionError = resolveCalleeCUDATargetConflict(
454 *InferredTarget, FieldMethodTarget, &*InferredTarget);
455 if (ResolutionError) {
456 if (Diagnose) {
457 Diag(ClassDecl->getLocation(),
458 diag::note_implicit_member_target_infer_collision)
459 << (unsigned)CSM << *InferredTarget << FieldMethodTarget;
461 MemberDecl->addAttr(CUDAInvalidTargetAttr::CreateImplicit(Context));
462 return true;
468 // If no target was inferred, mark this member as __host__ __device__;
469 // it's the least restrictive option that can be invoked from any target.
470 bool NeedsH = true, NeedsD = true;
471 if (InferredTarget) {
472 if (*InferredTarget == CFT_Device)
473 NeedsH = false;
474 else if (*InferredTarget == CFT_Host)
475 NeedsD = false;
478 // We either setting attributes first time, or the inferred ones must match
479 // previously set ones.
480 if (NeedsD && !HasD)
481 MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
482 if (NeedsH && !HasH)
483 MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
485 return false;
488 bool Sema::isEmptyCudaConstructor(SourceLocation Loc, CXXConstructorDecl *CD) {
489 if (!CD->isDefined() && CD->isTemplateInstantiation())
490 InstantiateFunctionDefinition(Loc, CD->getFirstDecl());
492 // (E.2.3.1, CUDA 7.5) A constructor for a class type is considered
493 // empty at a point in the translation unit, if it is either a
494 // trivial constructor
495 if (CD->isTrivial())
496 return true;
498 // ... or it satisfies all of the following conditions:
499 // The constructor function has been defined.
500 // The constructor function has no parameters,
501 // and the function body is an empty compound statement.
502 if (!(CD->hasTrivialBody() && CD->getNumParams() == 0))
503 return false;
505 // Its class has no virtual functions and no virtual base classes.
506 if (CD->getParent()->isDynamicClass())
507 return false;
509 // Union ctor does not call ctors of its data members.
510 if (CD->getParent()->isUnion())
511 return true;
513 // The only form of initializer allowed is an empty constructor.
514 // This will recursively check all base classes and member initializers
515 if (!llvm::all_of(CD->inits(), [&](const CXXCtorInitializer *CI) {
516 if (const CXXConstructExpr *CE =
517 dyn_cast<CXXConstructExpr>(CI->getInit()))
518 return isEmptyCudaConstructor(Loc, CE->getConstructor());
519 return false;
521 return false;
523 return true;
526 bool Sema::isEmptyCudaDestructor(SourceLocation Loc, CXXDestructorDecl *DD) {
527 // No destructor -> no problem.
528 if (!DD)
529 return true;
531 if (!DD->isDefined() && DD->isTemplateInstantiation())
532 InstantiateFunctionDefinition(Loc, DD->getFirstDecl());
534 // (E.2.3.1, CUDA 7.5) A destructor for a class type is considered
535 // empty at a point in the translation unit, if it is either a
536 // trivial constructor
537 if (DD->isTrivial())
538 return true;
540 // ... or it satisfies all of the following conditions:
541 // The destructor function has been defined.
542 // and the function body is an empty compound statement.
543 if (!DD->hasTrivialBody())
544 return false;
546 const CXXRecordDecl *ClassDecl = DD->getParent();
548 // Its class has no virtual functions and no virtual base classes.
549 if (ClassDecl->isDynamicClass())
550 return false;
552 // Union does not have base class and union dtor does not call dtors of its
553 // data members.
554 if (DD->getParent()->isUnion())
555 return true;
557 // Only empty destructors are allowed. This will recursively check
558 // destructors for all base classes...
559 if (!llvm::all_of(ClassDecl->bases(), [&](const CXXBaseSpecifier &BS) {
560 if (CXXRecordDecl *RD = BS.getType()->getAsCXXRecordDecl())
561 return isEmptyCudaDestructor(Loc, RD->getDestructor());
562 return true;
564 return false;
566 // ... and member fields.
567 if (!llvm::all_of(ClassDecl->fields(), [&](const FieldDecl *Field) {
568 if (CXXRecordDecl *RD = Field->getType()
569 ->getBaseElementTypeUnsafe()
570 ->getAsCXXRecordDecl())
571 return isEmptyCudaDestructor(Loc, RD->getDestructor());
572 return true;
574 return false;
576 return true;
579 namespace {
580 enum CUDAInitializerCheckKind {
581 CICK_DeviceOrConstant, // Check initializer for device/constant variable
582 CICK_Shared, // Check initializer for shared variable
585 bool IsDependentVar(VarDecl *VD) {
586 if (VD->getType()->isDependentType())
587 return true;
588 if (const auto *Init = VD->getInit())
589 return Init->isValueDependent();
590 return false;
593 // Check whether a variable has an allowed initializer for a CUDA device side
594 // variable with global storage. \p VD may be a host variable to be checked for
595 // potential promotion to device side variable.
597 // CUDA/HIP allows only empty constructors as initializers for global
598 // variables (see E.2.3.1, CUDA 7.5). The same restriction also applies to all
599 // __shared__ variables whether they are local or not (they all are implicitly
600 // static in CUDA). One exception is that CUDA allows constant initializers
601 // for __constant__ and __device__ variables.
602 bool HasAllowedCUDADeviceStaticInitializer(Sema &S, VarDecl *VD,
603 CUDAInitializerCheckKind CheckKind) {
604 assert(!VD->isInvalidDecl() && VD->hasGlobalStorage());
605 assert(!IsDependentVar(VD) && "do not check dependent var");
606 const Expr *Init = VD->getInit();
607 auto IsEmptyInit = [&](const Expr *Init) {
608 if (!Init)
609 return true;
610 if (const auto *CE = dyn_cast<CXXConstructExpr>(Init)) {
611 return S.isEmptyCudaConstructor(VD->getLocation(), CE->getConstructor());
613 return false;
615 auto IsConstantInit = [&](const Expr *Init) {
616 assert(Init);
617 ASTContext::CUDAConstantEvalContextRAII EvalCtx(S.Context,
618 /*NoWronSidedVars=*/true);
619 return Init->isConstantInitializer(S.Context,
620 VD->getType()->isReferenceType());
622 auto HasEmptyDtor = [&](VarDecl *VD) {
623 if (const auto *RD = VD->getType()->getAsCXXRecordDecl())
624 return S.isEmptyCudaDestructor(VD->getLocation(), RD->getDestructor());
625 return true;
627 if (CheckKind == CICK_Shared)
628 return IsEmptyInit(Init) && HasEmptyDtor(VD);
629 return S.LangOpts.GPUAllowDeviceInit ||
630 ((IsEmptyInit(Init) || IsConstantInit(Init)) && HasEmptyDtor(VD));
632 } // namespace
634 void Sema::checkAllowedCUDAInitializer(VarDecl *VD) {
635 // Return early if VD is inside a non-instantiated template function since
636 // the implicit constructor is not defined yet.
637 if (const FunctionDecl *FD =
638 dyn_cast_or_null<FunctionDecl>(VD->getDeclContext()))
639 if (FD->isDependentContext())
640 return;
642 // Do not check dependent variables since the ctor/dtor/initializer are not
643 // determined. Do it after instantiation.
644 if (VD->isInvalidDecl() || !VD->hasInit() || !VD->hasGlobalStorage() ||
645 IsDependentVar(VD))
646 return;
647 const Expr *Init = VD->getInit();
648 bool IsSharedVar = VD->hasAttr<CUDASharedAttr>();
649 bool IsDeviceOrConstantVar =
650 !IsSharedVar &&
651 (VD->hasAttr<CUDADeviceAttr>() || VD->hasAttr<CUDAConstantAttr>());
652 if (IsDeviceOrConstantVar || IsSharedVar) {
653 if (HasAllowedCUDADeviceStaticInitializer(
654 *this, VD, IsSharedVar ? CICK_Shared : CICK_DeviceOrConstant))
655 return;
656 Diag(VD->getLocation(),
657 IsSharedVar ? diag::err_shared_var_init : diag::err_dynamic_var_init)
658 << Init->getSourceRange();
659 VD->setInvalidDecl();
660 } else {
661 // This is a host-side global variable. Check that the initializer is
662 // callable from the host side.
663 const FunctionDecl *InitFn = nullptr;
664 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Init)) {
665 InitFn = CE->getConstructor();
666 } else if (const CallExpr *CE = dyn_cast<CallExpr>(Init)) {
667 InitFn = CE->getDirectCallee();
669 if (InitFn) {
670 CUDAFunctionTarget InitFnTarget = IdentifyCUDATarget(InitFn);
671 if (InitFnTarget != CFT_Host && InitFnTarget != CFT_HostDevice) {
672 Diag(VD->getLocation(), diag::err_ref_bad_target_global_initializer)
673 << InitFnTarget << InitFn;
674 Diag(InitFn->getLocation(), diag::note_previous_decl) << InitFn;
675 VD->setInvalidDecl();
681 // With -fcuda-host-device-constexpr, an unattributed constexpr function is
682 // treated as implicitly __host__ __device__, unless:
683 // * it is a variadic function (device-side variadic functions are not
684 // allowed), or
685 // * a __device__ function with this signature was already declared, in which
686 // case in which case we output an error, unless the __device__ decl is in a
687 // system header, in which case we leave the constexpr function unattributed.
689 // In addition, all function decls are treated as __host__ __device__ when
690 // ForceCUDAHostDeviceDepth > 0 (corresponding to code within a
691 // #pragma clang force_cuda_host_device_begin/end
692 // pair).
693 void Sema::maybeAddCUDAHostDeviceAttrs(FunctionDecl *NewD,
694 const LookupResult &Previous) {
695 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
697 if (ForceCUDAHostDeviceDepth > 0) {
698 if (!NewD->hasAttr<CUDAHostAttr>())
699 NewD->addAttr(CUDAHostAttr::CreateImplicit(Context));
700 if (!NewD->hasAttr<CUDADeviceAttr>())
701 NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context));
702 return;
705 if (!getLangOpts().CUDAHostDeviceConstexpr || !NewD->isConstexpr() ||
706 NewD->isVariadic() || NewD->hasAttr<CUDAHostAttr>() ||
707 NewD->hasAttr<CUDADeviceAttr>() || NewD->hasAttr<CUDAGlobalAttr>())
708 return;
710 // Is D a __device__ function with the same signature as NewD, ignoring CUDA
711 // attributes?
712 auto IsMatchingDeviceFn = [&](NamedDecl *D) {
713 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(D))
714 D = Using->getTargetDecl();
715 FunctionDecl *OldD = D->getAsFunction();
716 return OldD && OldD->hasAttr<CUDADeviceAttr>() &&
717 !OldD->hasAttr<CUDAHostAttr>() &&
718 !IsOverload(NewD, OldD, /* UseMemberUsingDeclRules = */ false,
719 /* ConsiderCudaAttrs = */ false);
721 auto It = llvm::find_if(Previous, IsMatchingDeviceFn);
722 if (It != Previous.end()) {
723 // We found a __device__ function with the same name and signature as NewD
724 // (ignoring CUDA attrs). This is an error unless that function is defined
725 // in a system header, in which case we simply return without making NewD
726 // host+device.
727 NamedDecl *Match = *It;
728 if (!getSourceManager().isInSystemHeader(Match->getLocation())) {
729 Diag(NewD->getLocation(),
730 diag::err_cuda_unattributed_constexpr_cannot_overload_device)
731 << NewD;
732 Diag(Match->getLocation(),
733 diag::note_cuda_conflicting_device_function_declared_here);
735 return;
738 NewD->addAttr(CUDAHostAttr::CreateImplicit(Context));
739 NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context));
742 // TODO: `__constant__` memory may be a limited resource for certain targets.
743 // A safeguard may be needed at the end of compilation pipeline if
744 // `__constant__` memory usage goes beyond limit.
745 void Sema::MaybeAddCUDAConstantAttr(VarDecl *VD) {
746 // Do not promote dependent variables since the cotr/dtor/initializer are
747 // not determined. Do it after instantiation.
748 if (getLangOpts().CUDAIsDevice && !VD->hasAttr<CUDAConstantAttr>() &&
749 !VD->hasAttr<CUDASharedAttr>() &&
750 (VD->isFileVarDecl() || VD->isStaticDataMember()) &&
751 !IsDependentVar(VD) &&
752 ((VD->isConstexpr() || VD->getType().isConstQualified()) &&
753 HasAllowedCUDADeviceStaticInitializer(*this, VD,
754 CICK_DeviceOrConstant))) {
755 VD->addAttr(CUDAConstantAttr::CreateImplicit(getASTContext()));
759 Sema::SemaDiagnosticBuilder Sema::CUDADiagIfDeviceCode(SourceLocation Loc,
760 unsigned DiagID) {
761 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
762 FunctionDecl *CurFunContext = getCurFunctionDecl(/*AllowLambda=*/true);
763 SemaDiagnosticBuilder::Kind DiagKind = [&] {
764 if (!CurFunContext)
765 return SemaDiagnosticBuilder::K_Nop;
766 switch (CurrentCUDATarget()) {
767 case CFT_Global:
768 case CFT_Device:
769 return SemaDiagnosticBuilder::K_Immediate;
770 case CFT_HostDevice:
771 // An HD function counts as host code if we're compiling for host, and
772 // device code if we're compiling for device. Defer any errors in device
773 // mode until the function is known-emitted.
774 if (!getLangOpts().CUDAIsDevice)
775 return SemaDiagnosticBuilder::K_Nop;
776 if (IsLastErrorImmediate && Diags.getDiagnosticIDs()->isBuiltinNote(DiagID))
777 return SemaDiagnosticBuilder::K_Immediate;
778 return (getEmissionStatus(CurFunContext) ==
779 FunctionEmissionStatus::Emitted)
780 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack
781 : SemaDiagnosticBuilder::K_Deferred;
782 default:
783 return SemaDiagnosticBuilder::K_Nop;
785 }();
786 return SemaDiagnosticBuilder(DiagKind, Loc, DiagID, CurFunContext, *this);
789 Sema::SemaDiagnosticBuilder Sema::CUDADiagIfHostCode(SourceLocation Loc,
790 unsigned DiagID) {
791 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
792 FunctionDecl *CurFunContext = getCurFunctionDecl(/*AllowLambda=*/true);
793 SemaDiagnosticBuilder::Kind DiagKind = [&] {
794 if (!CurFunContext)
795 return SemaDiagnosticBuilder::K_Nop;
796 switch (CurrentCUDATarget()) {
797 case CFT_Host:
798 return SemaDiagnosticBuilder::K_Immediate;
799 case CFT_HostDevice:
800 // An HD function counts as host code if we're compiling for host, and
801 // device code if we're compiling for device. Defer any errors in device
802 // mode until the function is known-emitted.
803 if (getLangOpts().CUDAIsDevice)
804 return SemaDiagnosticBuilder::K_Nop;
805 if (IsLastErrorImmediate && Diags.getDiagnosticIDs()->isBuiltinNote(DiagID))
806 return SemaDiagnosticBuilder::K_Immediate;
807 return (getEmissionStatus(CurFunContext) ==
808 FunctionEmissionStatus::Emitted)
809 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack
810 : SemaDiagnosticBuilder::K_Deferred;
811 default:
812 return SemaDiagnosticBuilder::K_Nop;
814 }();
815 return SemaDiagnosticBuilder(DiagKind, Loc, DiagID, CurFunContext, *this);
818 bool Sema::CheckCUDACall(SourceLocation Loc, FunctionDecl *Callee) {
819 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
820 assert(Callee && "Callee may not be null.");
822 const auto &ExprEvalCtx = currentEvaluationContext();
823 if (ExprEvalCtx.isUnevaluated() || ExprEvalCtx.isConstantEvaluated())
824 return true;
826 // FIXME: Is bailing out early correct here? Should we instead assume that
827 // the caller is a global initializer?
828 FunctionDecl *Caller = getCurFunctionDecl(/*AllowLambda=*/true);
829 if (!Caller)
830 return true;
832 // If the caller is known-emitted, mark the callee as known-emitted.
833 // Otherwise, mark the call in our call graph so we can traverse it later.
834 bool CallerKnownEmitted =
835 getEmissionStatus(Caller) == FunctionEmissionStatus::Emitted;
836 SemaDiagnosticBuilder::Kind DiagKind = [this, Caller, Callee,
837 CallerKnownEmitted] {
838 switch (IdentifyCUDAPreference(Caller, Callee)) {
839 case CFP_Never:
840 case CFP_WrongSide:
841 assert(Caller && "Never/wrongSide calls require a non-null caller");
842 // If we know the caller will be emitted, we know this wrong-side call
843 // will be emitted, so it's an immediate error. Otherwise, defer the
844 // error until we know the caller is emitted.
845 return CallerKnownEmitted
846 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack
847 : SemaDiagnosticBuilder::K_Deferred;
848 default:
849 return SemaDiagnosticBuilder::K_Nop;
851 }();
853 if (DiagKind == SemaDiagnosticBuilder::K_Nop) {
854 // For -fgpu-rdc, keep track of external kernels used by host functions.
855 if (LangOpts.CUDAIsDevice && LangOpts.GPURelocatableDeviceCode &&
856 Callee->hasAttr<CUDAGlobalAttr>() && !Callee->isDefined())
857 getASTContext().CUDAExternalDeviceDeclODRUsedByHost.insert(Callee);
858 return true;
861 // Avoid emitting this error twice for the same location. Using a hashtable
862 // like this is unfortunate, but because we must continue parsing as normal
863 // after encountering a deferred error, it's otherwise very tricky for us to
864 // ensure that we only emit this deferred error once.
865 if (!LocsWithCUDACallDiags.insert({Caller, Loc}).second)
866 return true;
868 SemaDiagnosticBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller, *this)
869 << IdentifyCUDATarget(Callee) << /*function*/ 0 << Callee
870 << IdentifyCUDATarget(Caller);
871 if (!Callee->getBuiltinID())
872 SemaDiagnosticBuilder(DiagKind, Callee->getLocation(),
873 diag::note_previous_decl, Caller, *this)
874 << Callee;
875 return DiagKind != SemaDiagnosticBuilder::K_Immediate &&
876 DiagKind != SemaDiagnosticBuilder::K_ImmediateWithCallStack;
879 // Check the wrong-sided reference capture of lambda for CUDA/HIP.
880 // A lambda function may capture a stack variable by reference when it is
881 // defined and uses the capture by reference when the lambda is called. When
882 // the capture and use happen on different sides, the capture is invalid and
883 // should be diagnosed.
884 void Sema::CUDACheckLambdaCapture(CXXMethodDecl *Callee,
885 const sema::Capture &Capture) {
886 // In host compilation we only need to check lambda functions emitted on host
887 // side. In such lambda functions, a reference capture is invalid only
888 // if the lambda structure is populated by a device function or kernel then
889 // is passed to and called by a host function. However that is impossible,
890 // since a device function or kernel can only call a device function, also a
891 // kernel cannot pass a lambda back to a host function since we cannot
892 // define a kernel argument type which can hold the lambda before the lambda
893 // itself is defined.
894 if (!LangOpts.CUDAIsDevice)
895 return;
897 // File-scope lambda can only do init captures for global variables, which
898 // results in passing by value for these global variables.
899 FunctionDecl *Caller = getCurFunctionDecl(/*AllowLambda=*/true);
900 if (!Caller)
901 return;
903 // In device compilation, we only need to check lambda functions which are
904 // emitted on device side. For such lambdas, a reference capture is invalid
905 // only if the lambda structure is populated by a host function then passed
906 // to and called in a device function or kernel.
907 bool CalleeIsDevice = Callee->hasAttr<CUDADeviceAttr>();
908 bool CallerIsHost =
909 !Caller->hasAttr<CUDAGlobalAttr>() && !Caller->hasAttr<CUDADeviceAttr>();
910 bool ShouldCheck = CalleeIsDevice && CallerIsHost;
911 if (!ShouldCheck || !Capture.isReferenceCapture())
912 return;
913 auto DiagKind = SemaDiagnosticBuilder::K_Deferred;
914 if (Capture.isVariableCapture() && !getLangOpts().HIPStdPar) {
915 SemaDiagnosticBuilder(DiagKind, Capture.getLocation(),
916 diag::err_capture_bad_target, Callee, *this)
917 << Capture.getVariable();
918 } else if (Capture.isThisCapture()) {
919 // Capture of this pointer is allowed since this pointer may be pointing to
920 // managed memory which is accessible on both device and host sides. It only
921 // results in invalid memory access if this pointer points to memory not
922 // accessible on device side.
923 SemaDiagnosticBuilder(DiagKind, Capture.getLocation(),
924 diag::warn_maybe_capture_bad_target_this_ptr, Callee,
925 *this);
929 void Sema::CUDASetLambdaAttrs(CXXMethodDecl *Method) {
930 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
931 if (Method->hasAttr<CUDAHostAttr>() || Method->hasAttr<CUDADeviceAttr>())
932 return;
933 Method->addAttr(CUDADeviceAttr::CreateImplicit(Context));
934 Method->addAttr(CUDAHostAttr::CreateImplicit(Context));
937 void Sema::checkCUDATargetOverload(FunctionDecl *NewFD,
938 const LookupResult &Previous) {
939 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
940 CUDAFunctionTarget NewTarget = IdentifyCUDATarget(NewFD);
941 for (NamedDecl *OldND : Previous) {
942 FunctionDecl *OldFD = OldND->getAsFunction();
943 if (!OldFD)
944 continue;
946 CUDAFunctionTarget OldTarget = IdentifyCUDATarget(OldFD);
947 // Don't allow HD and global functions to overload other functions with the
948 // same signature. We allow overloading based on CUDA attributes so that
949 // functions can have different implementations on the host and device, but
950 // HD/global functions "exist" in some sense on both the host and device, so
951 // should have the same implementation on both sides.
952 if (NewTarget != OldTarget &&
953 ((NewTarget == CFT_HostDevice) || (OldTarget == CFT_HostDevice) ||
954 (NewTarget == CFT_Global) || (OldTarget == CFT_Global)) &&
955 !IsOverload(NewFD, OldFD, /* UseMemberUsingDeclRules = */ false,
956 /* ConsiderCudaAttrs = */ false)) {
957 Diag(NewFD->getLocation(), diag::err_cuda_ovl_target)
958 << NewTarget << NewFD->getDeclName() << OldTarget << OldFD;
959 Diag(OldFD->getLocation(), diag::note_previous_declaration);
960 NewFD->setInvalidDecl();
961 break;
966 template <typename AttrTy>
967 static void copyAttrIfPresent(Sema &S, FunctionDecl *FD,
968 const FunctionDecl &TemplateFD) {
969 if (AttrTy *Attribute = TemplateFD.getAttr<AttrTy>()) {
970 AttrTy *Clone = Attribute->clone(S.Context);
971 Clone->setInherited(true);
972 FD->addAttr(Clone);
976 void Sema::inheritCUDATargetAttrs(FunctionDecl *FD,
977 const FunctionTemplateDecl &TD) {
978 const FunctionDecl &TemplateFD = *TD.getTemplatedDecl();
979 copyAttrIfPresent<CUDAGlobalAttr>(*this, FD, TemplateFD);
980 copyAttrIfPresent<CUDAHostAttr>(*this, FD, TemplateFD);
981 copyAttrIfPresent<CUDADeviceAttr>(*this, FD, TemplateFD);
984 std::string Sema::getCudaConfigureFuncName() const {
985 if (getLangOpts().HIP)
986 return getLangOpts().HIPUseNewLaunchAPI ? "__hipPushCallConfiguration"
987 : "hipConfigureCall";
989 // New CUDA kernel launch sequence.
990 if (CudaFeatureEnabled(Context.getTargetInfo().getSDKVersion(),
991 CudaFeature::CUDA_USES_NEW_LAUNCH))
992 return "__cudaPushCallConfiguration";
994 // Legacy CUDA kernel configuration call
995 return "cudaConfigureCall";