clang 24.0.0git
SemaCUDA.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
12
13#include "clang/Sema/SemaCUDA.h"
15#include "clang/AST/Decl.h"
17#include "clang/AST/ExprCXX.h"
18#include "clang/Basic/Cuda.h"
21#include "clang/Sema/Lookup.h"
22#include "clang/Sema/Overload.h"
24#include "clang/Sema/Sema.h"
25#include "clang/Sema/Template.h"
26#include "llvm/ADT/SmallVector.h"
27#include <optional>
28using namespace clang;
29
31
32template <typename AttrT> static bool hasExplicitAttr(const VarDecl *D) {
33 if (!D)
34 return false;
35 if (auto *A = D->getAttr<AttrT>())
36 return !A->isImplicit();
37 return false;
38}
39
41 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
42 ForceHostDeviceDepth++;
43}
44
46 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
47 if (ForceHostDeviceDepth == 0)
48 return false;
49 ForceHostDeviceDepth--;
50 return true;
51}
52
54 MultiExprArg ExecConfig,
55 SourceLocation GGGLoc) {
56 bool IsDeviceKernelCall = false;
57 switch (CurrentTarget()) {
60 IsDeviceKernelCall = true;
61 break;
63 if (getLangOpts().CUDAIsDevice) {
64 IsDeviceKernelCall = true;
65 FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
66 if (Caller && isImplicitHostDeviceFunction(Caller)) {
67 // Under the device compilation, config call under an HD function should
68 // be treated as a device kernel call. But, for implicit HD ones (such
69 // 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 as
73 // the host-side kernel call.
74 if (getLangOpts().HIP)
75 IsDeviceKernelCall = false;
76 } else if (getLangOpts().HIP && getLangOpts().IncrementalExtensions &&
77 isa<TopLevelStmtDecl>(SemaRef.getCurLexicalContext())) {
78 IsDeviceKernelCall = false;
79 }
80 }
81 break;
82 default:
83 break;
84 }
85
86 if (IsDeviceKernelCall && getLangOpts().HIP)
87 return ExprError(
88 Diag(LLLLoc, diag::err_cuda_device_kernel_launch_not_supported));
89
90 FunctionDecl *ConfigDecl = IsDeviceKernelCall
93 if (!ConfigDecl)
94 return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use)
95 << (IsDeviceKernelCall ? getLaunchDeviceFuncName()
97 // Additional check on the launch function if it's a device kernel call.
98 if (IsDeviceKernelCall) {
99 auto *GetParamBuf = getASTContext().getcudaGetParameterBufferDecl();
100 if (!GetParamBuf)
101 return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use)
103 }
104
105 QualType ConfigQTy = ConfigDecl->getType();
106
107 DeclRefExpr *ConfigDR = new (getASTContext()) DeclRefExpr(
108 getASTContext(), ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc);
109 SemaRef.MarkFunctionReferenced(LLLLoc, ConfigDecl);
110
111 if (IsDeviceKernelCall) {
113 // Use a null pointer as the kernel function, which may not be resolvable
114 // here. For example, resolving that kernel function may need additional
115 // kernel arguments.
116 llvm::APInt Zero(SemaRef.Context.getTypeSize(SemaRef.Context.IntTy), 0);
117 Args.push_back(IntegerLiteral::Create(SemaRef.Context, Zero,
118 SemaRef.Context.IntTy, LLLLoc));
119 // Use a null pointer as the placeholder of the parameter buffer, which
120 // should be replaced with the actual allocation later, in the codegen.
121 Args.push_back(IntegerLiteral::Create(SemaRef.Context, Zero,
122 SemaRef.Context.IntTy, LLLLoc));
123 // Add the original config arguments.
124 llvm::append_range(Args, ExecConfig);
125 // Add the default blockDim if it's missing.
126 if (Args.size() < 4) {
127 llvm::APInt One(SemaRef.Context.getTypeSize(SemaRef.Context.IntTy), 1);
128 Args.push_back(IntegerLiteral::Create(SemaRef.Context, One,
129 SemaRef.Context.IntTy, LLLLoc));
130 }
131 // Add the default sharedMemSize if it's missing.
132 if (Args.size() < 5)
133 Args.push_back(IntegerLiteral::Create(SemaRef.Context, Zero,
134 SemaRef.Context.IntTy, LLLLoc));
135 // Add the default stream if it's missing.
136 if (Args.size() < 6)
137 Args.push_back(new (SemaRef.Context) CXXNullPtrLiteralExpr(
138 SemaRef.Context.NullPtrTy, LLLLoc));
139 return SemaRef.BuildCallExpr(S, ConfigDR, LLLLoc, Args, GGGLoc, nullptr,
140 /*IsExecConfig=*/true);
141 }
142 return SemaRef.BuildCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, nullptr,
143 /*IsExecConfig=*/true);
144}
145
147 bool HasHostAttr = false;
148 bool HasDeviceAttr = false;
149 bool HasGlobalAttr = false;
150 bool HasInvalidTargetAttr = false;
151 for (const ParsedAttr &AL : Attrs) {
152 switch (AL.getKind()) {
153 case ParsedAttr::AT_CUDAGlobal:
154 HasGlobalAttr = true;
155 break;
156 case ParsedAttr::AT_CUDAHost:
157 HasHostAttr = true;
158 break;
159 case ParsedAttr::AT_CUDADevice:
160 HasDeviceAttr = true;
161 break;
162 case ParsedAttr::AT_CUDAInvalidTarget:
163 HasInvalidTargetAttr = true;
164 break;
165 default:
166 break;
167 }
168 }
169
170 if (HasInvalidTargetAttr)
172
173 if (HasGlobalAttr)
175
176 if (HasHostAttr && HasDeviceAttr)
178
179 if (HasDeviceAttr)
181
183}
184
185template <typename A>
186static bool hasAttr(const Decl *D, bool IgnoreImplicitAttr) {
187 return D->hasAttrs() && llvm::any_of(D->getAttrs(), [&](Attr *Attribute) {
188 return isa<A>(Attribute) &&
189 !(IgnoreImplicitAttr && Attribute->isImplicit());
190 });
191}
192
195 : S(S_) {
196 SavedCtx = S.CurCUDATargetCtx;
197 assert(K == SemaCUDA::CTCK_InitGlobalVar);
198 auto *VD = dyn_cast_or_null<VarDecl>(D);
199 if (VD && VD->hasGlobalStorage() && !VD->isStaticLocal()) {
201 if ((hasAttr<CUDADeviceAttr>(VD, /*IgnoreImplicit=*/true) &&
202 !hasAttr<CUDAHostAttr>(VD, /*IgnoreImplicit=*/true)) ||
203 hasAttr<CUDASharedAttr>(VD, /*IgnoreImplicit=*/true) ||
204 hasAttr<CUDAConstantAttr>(VD, /*IgnoreImplicit=*/true))
206 S.CurCUDATargetCtx = {Target, K, VD};
207 }
208}
209
210/// IdentifyTarget - Determine the CUDA compilation target for this function
212 bool IgnoreImplicitHDAttr) {
213 // Code that lives outside a function gets the target from CurCUDATargetCtx.
214 if (D == nullptr)
215 return CurCUDATargetCtx.Target;
216
217 // C++ deduction guides are never codegen'ed and only participate in template
218 // argument deduction. Treat them as if they were always host+device so that
219 // CUDA/HIP target checking never rejects their use based solely on target.
222
223 if (D->hasAttr<CUDAInvalidTargetAttr>())
225
226 if (D->hasAttr<CUDAGlobalAttr>())
228
229 if (D->isConsteval())
231
232 if (hasAttr<CUDADeviceAttr>(D, IgnoreImplicitHDAttr)) {
233 if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr))
236 } else if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr)) {
238 } else if ((D->isImplicit() || !D->isUserProvided()) &&
239 !IgnoreImplicitHDAttr) {
240 // Some implicit declarations (like intrinsic functions) are not marked.
241 // Set the most lenient target on them for maximal flexibility.
243 }
244
246}
247
248/// IdentifyTarget - Determine the CUDA compilation target for this variable.
250 if (Var->hasAttr<HIPManagedAttr>())
251 return CVT_Unified;
252 // Only constexpr and const variabless with implicit constant attribute
253 // are emitted on both sides. Such variables are promoted to device side
254 // only if they have static constant initializers on device side.
255 if ((Var->isConstexpr() || Var->getType().isConstQualified()) &&
256 Var->hasAttr<CUDAConstantAttr>() &&
258 return CVT_Both;
259 if (Var->hasAttr<CUDADeviceAttr>() || Var->hasAttr<CUDAConstantAttr>() ||
260 Var->hasAttr<CUDASharedAttr>() ||
263 return CVT_Device;
264 // Function-scope static variable without explicit device or constant
265 // attribute are emitted
266 // - on both sides in host device functions
267 // - on device side in device or global functions
268 if (auto *FD = dyn_cast<FunctionDecl>(Var->getDeclContext())) {
269 switch (IdentifyTarget(FD)) {
271 return CVT_Both;
274 return CVT_Device;
275 default:
276 return CVT_Host;
277 }
278 }
279 return CVT_Host;
280}
281
282// * CUDA Call preference table
283//
284// F - from,
285// T - to
286// Ph - preference in host mode
287// Pd - preference in device mode
288// H - handled in (x)
289// Preferences: N:native, SS:same side, HD:host-device, WS:wrong side, --:never.
290//
291// | F | T | Ph | Pd | H |
292// |----+----+-----+-----+-----+
293// | d | d | N | N | (c) |
294// | d | g | -- | -- | (a) |
295// | d | h | -- | -- | (e) |
296// | d | hd | HD | HD | (b) |
297// | g | d | N | N | (c) |
298// | g | g | -- | -- | (a) |
299// | g | h | -- | -- | (e) |
300// | g | hd | HD | HD | (b) |
301// | h | d | -- | -- | (e) |
302// | h | g | N | N | (c) |
303// | h | h | N | N | (c) |
304// | h | hd | HD | HD | (b) |
305// | hd | d | WS | SS | (d) |
306// | hd | g | SS | -- |(d/a)|
307// | hd | h | SS | WS | (d) |
308// | hd | hd | HD | HD | (b) |
309
312 const FunctionDecl *Callee) {
313 assert(Callee && "Callee must be valid.");
314
315 // Treat ctor/dtor as host device function in device var initializer to allow
316 // trivial ctor/dtor without device attr to be used. Non-trivial ctor/dtor
317 // will be diagnosed by checkAllowedInitializer.
318 if (Caller == nullptr && CurCUDATargetCtx.Kind == CTCK_InitGlobalVar &&
321 return CFP_HostDevice;
322
323 CUDAFunctionTarget CallerTarget = IdentifyTarget(Caller);
324 CUDAFunctionTarget CalleeTarget = IdentifyTarget(Callee);
325
326 // If one of the targets is invalid, the check always fails, no matter what
327 // the other target is.
328 if (CallerTarget == CUDAFunctionTarget::InvalidTarget ||
329 CalleeTarget == CUDAFunctionTarget::InvalidTarget)
330 return CFP_Never;
331
332 // (a) Call global from either global or device contexts is allowed as part
333 // of CUDA's dynamic parallelism support.
334 if (CalleeTarget == CUDAFunctionTarget::Global &&
335 (CallerTarget == CUDAFunctionTarget::Global ||
336 CallerTarget == CUDAFunctionTarget::Device))
337 return CFP_Native;
338
339 // (b) Calling HostDevice is OK for everyone.
340 if (CalleeTarget == CUDAFunctionTarget::HostDevice)
341 return CFP_HostDevice;
342
343 // (c) Best case scenarios
344 if (CalleeTarget == CallerTarget ||
345 (CallerTarget == CUDAFunctionTarget::Host &&
346 CalleeTarget == CUDAFunctionTarget::Global) ||
347 (CallerTarget == CUDAFunctionTarget::Global &&
348 CalleeTarget == CUDAFunctionTarget::Device))
349 return CFP_Native;
350
351 // HipStdPar mode is special, in that assessing whether a device side call to
352 // a host target is deferred to a subsequent pass, and cannot unambiguously be
353 // adjudicated in the AST, hence we optimistically allow them to pass here.
354 if (getLangOpts().HIPStdPar &&
355 (CallerTarget == CUDAFunctionTarget::Global ||
356 CallerTarget == CUDAFunctionTarget::Device ||
357 CallerTarget == CUDAFunctionTarget::HostDevice) &&
358 CalleeTarget == CUDAFunctionTarget::Host)
359 return CFP_HostDevice;
360
361 // (d) HostDevice behavior depends on compilation mode.
362 if (CallerTarget == CUDAFunctionTarget::HostDevice) {
363 // It's OK to call a compilation-mode matching function from an HD one.
364 if ((getLangOpts().CUDAIsDevice &&
365 (CalleeTarget == CUDAFunctionTarget::Device ||
366 CalleeTarget == CUDAFunctionTarget::Global)) ||
367 (!getLangOpts().CUDAIsDevice &&
368 (CalleeTarget == CUDAFunctionTarget::Host ||
369 CalleeTarget == CUDAFunctionTarget::Global)))
370 return CFP_SameSide;
371
372 // Calls from HD to non-mode-matching functions (i.e., to host functions
373 // when compiling in device mode or to device functions when compiling in
374 // host mode) are allowed at the sema level, but eventually rejected if
375 // they're ever codegened. TODO: Reject said calls earlier.
376 return CFP_WrongSide;
377 }
378
379 // (e) Calling across device/host boundary is not something you should do.
380 if ((CallerTarget == CUDAFunctionTarget::Host &&
381 CalleeTarget == CUDAFunctionTarget::Device) ||
382 (CallerTarget == CUDAFunctionTarget::Device &&
383 CalleeTarget == CUDAFunctionTarget::Host) ||
384 (CallerTarget == CUDAFunctionTarget::Global &&
385 CalleeTarget == CUDAFunctionTarget::Host))
386 return CFP_Never;
387
388 llvm_unreachable("All cases should've been handled by now.");
389}
390
391template <typename AttrT> static bool hasImplicitAttr(const FunctionDecl *D) {
392 if (!D)
393 return false;
394 if (auto *A = D->getAttr<AttrT>())
395 return A->isImplicit();
396 return D->isImplicit();
397}
398
400 bool IsImplicitDevAttr = hasImplicitAttr<CUDADeviceAttr>(D);
401 bool IsImplicitHostAttr = hasImplicitAttr<CUDAHostAttr>(D);
402 return IsImplicitDevAttr && IsImplicitHostAttr;
403}
404
408
410 const FunctionDecl *Caller,
411 SmallVectorImpl<std::pair<DeclAccessPair, FunctionDecl *>> &Matches) {
412 if (Matches.size() <= 1)
413 return;
414
415 using Pair = std::pair<DeclAccessPair, FunctionDecl *>;
416
417 // Gets the CUDA function preference for a call from Caller to Match.
418 auto GetCFP = [&](const Pair &Match) {
419 return IdentifyPreference(Caller, Match.second);
420 };
421
422 // Find the best call preference among the functions in Matches.
423 CUDAFunctionPreference BestCFP =
424 GetCFP(*llvm::max_element(Matches, [&](const Pair &M1, const Pair &M2) {
425 return GetCFP(M1) < GetCFP(M2);
426 }));
427
428 // Erase all functions with lower priority.
429 llvm::erase_if(Matches,
430 [&](const Pair &Match) { return GetCFP(Match) < BestCFP; });
431}
432
433/// When an implicitly-declared special member has to invoke more than one
434/// base/field special member, conflicts may occur in the targets of these
435/// members. For example, if one base's member __host__ and another's is
436/// __device__, it's a conflict.
437/// This function figures out if the given targets \param Target1 and
438/// \param Target2 conflict, and if they do not it fills in
439/// \param ResolvedTarget with a target that resolves for both calls.
440/// \return true if there's a conflict, false otherwise.
441static bool
443 CUDAFunctionTarget Target2,
444 CUDAFunctionTarget *ResolvedTarget) {
445 // Only free functions and static member functions may be global.
446 assert(Target1 != CUDAFunctionTarget::Global);
447 assert(Target2 != CUDAFunctionTarget::Global);
448
449 if (Target1 == CUDAFunctionTarget::HostDevice) {
450 *ResolvedTarget = Target2;
451 } else if (Target2 == CUDAFunctionTarget::HostDevice) {
452 *ResolvedTarget = Target1;
453 } else if (Target1 != Target2) {
454 return true;
455 } else {
456 *ResolvedTarget = Target1;
457 }
458
459 return false;
460}
461
464 CXXMethodDecl *MemberDecl,
465 bool ConstRHS,
466 bool Diagnose) {
467 // If the defaulted special member is defined lexically outside of its
468 // owning class, or the special member already has explicit device or host
469 // attributes, do not infer.
470 bool InClass = MemberDecl->getLexicalParent() == MemberDecl->getParent();
471 bool HasH = MemberDecl->hasAttr<CUDAHostAttr>();
472 bool HasD = MemberDecl->hasAttr<CUDADeviceAttr>();
473 bool HasExplicitAttr =
474 (HasD && !MemberDecl->getAttr<CUDADeviceAttr>()->isImplicit()) ||
475 (HasH && !MemberDecl->getAttr<CUDAHostAttr>()->isImplicit());
476 if (!InClass || HasExplicitAttr)
477 return false;
478
479 std::optional<CUDAFunctionTarget> InferredTarget;
480
481 // We're going to invoke special member lookup; mark that these special
482 // members are called from this one, and not from its caller.
483 Sema::ContextRAII MethodContext(SemaRef, MemberDecl);
484
485 // Look for special members in base classes that should be invoked from here.
486 // Infer the target of this member base on the ones it should call.
487 // Skip direct and indirect virtual bases for abstract classes, except for
488 // destructors — the complete destructor variant destroys virtual bases
489 // regardless of whether the class is abstract.
491 for (const auto &B : ClassDecl->bases()) {
492 if (!B.isVirtual()) {
493 Bases.push_back(&B);
494 }
495 }
496
497 if (!ClassDecl->isAbstract() || CSM == CXXSpecialMemberKind::Destructor)
498 llvm::append_range(Bases, llvm::make_pointer_range(ClassDecl->vbases()));
499
500 for (const auto *B : Bases) {
501 auto *BaseClassDecl = B->getType()->getAsCXXRecordDecl();
502 if (!BaseClassDecl)
503 continue;
504
506 SemaRef.LookupSpecialMember(BaseClassDecl, CSM,
507 /* ConstArg */ ConstRHS,
508 /* VolatileArg */ false,
509 /* RValueThis */ false,
510 /* ConstThis */ false,
511 /* VolatileThis */ false);
512
513 if (!SMOR.getMethod())
514 continue;
515
516 CUDAFunctionTarget BaseMethodTarget = IdentifyTarget(SMOR.getMethod());
517
518 if (!InferredTarget) {
519 InferredTarget = BaseMethodTarget;
520 } else {
521 bool ResolutionError = resolveCalleeCUDATargetConflict(
522 *InferredTarget, BaseMethodTarget, &*InferredTarget);
523 if (ResolutionError) {
524 if (Diagnose) {
525 Diag(ClassDecl->getLocation(),
526 diag::note_implicit_member_target_infer_collision)
527 << (unsigned)CSM << *InferredTarget << BaseMethodTarget;
528 }
529 MemberDecl->addAttr(
530 CUDAInvalidTargetAttr::CreateImplicit(getASTContext()));
531 return true;
532 }
533 }
534 }
535
536 // Same as for bases, but now for special members of fields.
537 for (const auto *F : ClassDecl->fields()) {
538 if (F->isInvalidDecl()) {
539 continue;
540 }
541
542 auto *FieldRecDecl =
544 if (!FieldRecDecl)
545 continue;
546
548 SemaRef.LookupSpecialMember(FieldRecDecl, CSM,
549 /* ConstArg */ ConstRHS && !F->isMutable(),
550 /* VolatileArg */ false,
551 /* RValueThis */ false,
552 /* ConstThis */ false,
553 /* VolatileThis */ false);
554
555 if (!SMOR.getMethod())
556 continue;
557
558 CUDAFunctionTarget FieldMethodTarget = IdentifyTarget(SMOR.getMethod());
559
560 if (!InferredTarget) {
561 InferredTarget = FieldMethodTarget;
562 } else {
563 bool ResolutionError = resolveCalleeCUDATargetConflict(
564 *InferredTarget, FieldMethodTarget, &*InferredTarget);
565 if (ResolutionError) {
566 if (Diagnose) {
567 Diag(ClassDecl->getLocation(),
568 diag::note_implicit_member_target_infer_collision)
569 << (unsigned)CSM << *InferredTarget << FieldMethodTarget;
570 }
571 MemberDecl->addAttr(
572 CUDAInvalidTargetAttr::CreateImplicit(getASTContext()));
573 return true;
574 }
575 }
576 }
577
578 // If no target was inferred, mark this member as __host__ __device__;
579 // it's the least restrictive option that can be invoked from any target.
580 bool NeedsH = true, NeedsD = true;
581 if (InferredTarget) {
582 if (*InferredTarget == CUDAFunctionTarget::Device)
583 NeedsH = false;
584 else if (*InferredTarget == CUDAFunctionTarget::Host)
585 NeedsD = false;
586 }
587
588 // We either setting attributes first time, or the inferred ones must match
589 // previously set ones.
590 if (NeedsD && !HasD)
591 MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));
592 if (NeedsH && !HasH)
593 MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));
594
595 return false;
596}
597
599 if (!CD->isDefined() && CD->isTemplateInstantiation())
600 SemaRef.InstantiateFunctionDefinition(Loc, CD->getFirstDecl());
601
602 // (E.2.3.1, CUDA 7.5) A constructor for a class type is considered
603 // empty at a point in the translation unit, if it is either a
604 // trivial constructor
605 if (CD->isTrivial())
606 return true;
607
608 // ... or it satisfies all of the following conditions:
609 // The constructor function has been defined.
610 // The constructor function has no parameters,
611 // and the function body is an empty compound statement.
612 if (!(CD->hasTrivialBody() && CD->getNumParams() == 0))
613 return false;
614
615 // Its class has no virtual functions and no virtual base classes.
616 if (CD->getParent()->isDynamicClass())
617 return false;
618
619 // Union ctor does not call ctors of its data members.
620 if (CD->getParent()->isUnion())
621 return true;
622
623 // The only form of initializer allowed is an empty constructor.
624 // This will recursively check all base classes and member initializers
625 if (!llvm::all_of(CD->inits(), [&](const CXXCtorInitializer *CI) {
626 if (const CXXConstructExpr *CE =
627 dyn_cast<CXXConstructExpr>(CI->getInit()))
628 return isEmptyConstructor(Loc, CE->getConstructor());
629 return false;
630 }))
631 return false;
632
633 return true;
634}
635
637 // No destructor -> no problem.
638 if (!DD)
639 return true;
640
641 if (!DD->isDefined() && DD->isTemplateInstantiation())
642 SemaRef.InstantiateFunctionDefinition(Loc, DD->getFirstDecl());
643
644 // (E.2.3.1, CUDA 7.5) A destructor for a class type is considered
645 // empty at a point in the translation unit, if it is either a
646 // trivial constructor
647 if (DD->isTrivial())
648 return true;
649
650 // ... or it satisfies all of the following conditions:
651 // The destructor function has been defined.
652 // and the function body is an empty compound statement.
653 if (!DD->hasTrivialBody())
654 return false;
655
656 const CXXRecordDecl *ClassDecl = DD->getParent();
657
658 // Its class has no virtual functions and no virtual base classes.
659 if (ClassDecl->isDynamicClass())
660 return false;
661
662 // Union does not have base class and union dtor does not call dtors of its
663 // data members.
664 if (DD->getParent()->isUnion())
665 return true;
666
667 // Only empty destructors are allowed. This will recursively check
668 // destructors for all base classes...
669 if (!llvm::all_of(ClassDecl->bases(), [&](const CXXBaseSpecifier &BS) {
670 if (CXXRecordDecl *RD = BS.getType()->getAsCXXRecordDecl())
671 return isEmptyDestructor(Loc, RD->getDestructor());
672 return true;
673 }))
674 return false;
675
676 // ... and member fields.
677 if (!llvm::all_of(ClassDecl->fields(), [&](const FieldDecl *Field) {
678 if (CXXRecordDecl *RD = Field->getType()
679 ->getBaseElementTypeUnsafe()
680 ->getAsCXXRecordDecl())
681 return isEmptyDestructor(Loc, RD->getDestructor());
682 return true;
683 }))
684 return false;
685
686 return true;
687}
688
689namespace {
690enum CUDAInitializerCheckKind {
691 CICK_DeviceOrConstant, // Check initializer for device/constant variable
692 CICK_Shared, // Check initializer for shared variable
693};
694
695bool IsDependentVar(VarDecl *VD) {
696 if (VD->getType()->isDependentType())
697 return true;
698 if (const auto *Init = VD->getInit())
699 return Init->isValueDependent();
700 return false;
701}
702
703// Check whether a variable has an allowed initializer for a CUDA device side
704// variable with global storage. \p VD may be a host variable to be checked for
705// potential promotion to device side variable.
706//
707// CUDA/HIP allows only empty constructors as initializers for global
708// variables (see E.2.3.1, CUDA 7.5). The same restriction also applies to all
709// __shared__ variables whether they are local or not (they all are implicitly
710// static in CUDA). One exception is that CUDA allows constant initializers
711// for __constant__ and __device__ variables.
712bool HasAllowedCUDADeviceStaticInitializer(SemaCUDA &S, VarDecl *VD,
713 CUDAInitializerCheckKind CheckKind) {
714 assert(!VD->isInvalidDecl() && VD->hasGlobalStorage());
715 assert(!IsDependentVar(VD) && "do not check dependent var");
716 const Expr *Init = VD->getInit();
717 auto IsEmptyInit = [&](const Expr *Init) {
718 if (!Init)
719 return true;
720 if (const auto *CE = dyn_cast<CXXConstructExpr>(Init)) {
721 return S.isEmptyConstructor(VD->getLocation(), CE->getConstructor());
722 }
723 return false;
724 };
725 auto IsConstantInit = [&](const Expr *Init) {
726 assert(Init);
727 ASTContext::CUDAConstantEvalContextRAII EvalCtx(S.getASTContext(),
728 /*NoWronSidedVars=*/true);
729 return Init->isConstantInitializer(S.getASTContext(),
730 VD->getType()->isReferenceType());
731 };
732 auto HasEmptyDtor = [&](VarDecl *VD) {
733 if (const auto *RD = VD->getType()->getAsCXXRecordDecl())
734 return S.isEmptyDestructor(VD->getLocation(), RD->getDestructor());
735 return true;
736 };
737 if (CheckKind == CICK_Shared)
738 return IsEmptyInit(Init) && HasEmptyDtor(VD);
739 return S.getLangOpts().GPUAllowDeviceInit ||
740 ((IsEmptyInit(Init) || IsConstantInit(Init)) && HasEmptyDtor(VD));
741}
742} // namespace
743
745 // Return early if VD is inside a non-instantiated template function since
746 // the implicit constructor is not defined yet.
747 if (const FunctionDecl *FD =
748 dyn_cast_or_null<FunctionDecl>(VD->getDeclContext());
749 FD && FD->isDependentContext())
750 return;
751
752 bool IsSharedVar = VD->hasAttr<CUDASharedAttr>();
753 bool IsDeviceOrConstantVar =
754 !IsSharedVar &&
755 (VD->hasAttr<CUDADeviceAttr>() || VD->hasAttr<CUDAConstantAttr>());
756 if ((IsSharedVar || IsDeviceOrConstantVar) &&
758 Diag(VD->getLocation(), diag::err_cuda_address_space_gpuvar);
759 VD->setInvalidDecl();
760 return;
761 }
762 // Do not check dependent variables since the ctor/dtor/initializer are not
763 // determined. Do it after instantiation.
764 if (VD->isInvalidDecl() || !VD->hasInit() || !VD->hasGlobalStorage() ||
765 IsDependentVar(VD))
766 return;
767 const Expr *Init = VD->getInit();
768 if (IsDeviceOrConstantVar || IsSharedVar) {
769 if (HasAllowedCUDADeviceStaticInitializer(
770 *this, VD, IsSharedVar ? CICK_Shared : CICK_DeviceOrConstant))
771 return;
772 Diag(VD->getLocation(),
773 IsSharedVar ? diag::err_shared_var_init : diag::err_dynamic_var_init)
774 << Init->getSourceRange();
775 VD->setInvalidDecl();
776 } else {
777 // This is a host-side global variable. Check that the initializer is
778 // callable from the host side.
779
780 struct GlobVarInitChecker : ConstEvaluatedExprVisitor<GlobVarInitChecker> {
781 private:
783 SemaCUDA &SCRef;
784 VarDecl *VD;
785 void CheckForWrongSidedCall(const FunctionDecl *FD) {
786 CUDAFunctionTarget InitFnTarget = SCRef.IdentifyTarget(FD);
787 if (InitFnTarget != CUDAFunctionTarget::Host &&
788 InitFnTarget != CUDAFunctionTarget::HostDevice &&
789 !VD->isInvalidDecl()) {
790 SCRef.Diag(VD->getLocation(),
791 diag::err_ref_bad_target_global_initializer)
792 << InitFnTarget << FD;
793 SCRef.Diag(FD->getLocation(), diag::note_previous_decl) << FD;
794 VD->setInvalidDecl();
795 }
796 }
797
798 public:
799 GlobVarInitChecker(SemaCUDA &S, VarDecl *VD)
800 : Base(S.getASTContext()), SCRef(S), VD(VD) {}
801 void VisitDeclRefExpr(const DeclRefExpr *DRE) {
802 if (auto *VarD = dyn_cast<VarDecl>(DRE->getDecl());
803 VarD && VarD->hasAttr<HIPManagedAttr>()) {
804 SCRef.Diag(DRE->getLocation(),
805 diag::err_cuda_invalid_use_of_managedvar);
806 SCRef.Diag(VD->getLocation(),
807 diag::note_cuda_managed_var_in_glob_init);
808 }
809 }
810 void VisitCallExpr(const CallExpr *CE) {
811 const FunctionDecl *InitFn = CE->getDirectCallee();
812 if (InitFn)
813 CheckForWrongSidedCall(InitFn);
814 Base::VisitCallExpr(CE);
815 }
816
817 void VisitCXXConstructExpr(const CXXConstructExpr *CE) {
818 const CXXConstructorDecl *Ctor = CE->getConstructor();
819 if (Ctor) {
820 CheckForWrongSidedCall(Ctor);
821 for (auto *I : Ctor->inits())
822 Visit(I->getInit());
823 }
824 Base::VisitCXXConstructExpr(CE);
825 }
826
827 void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
828 Visit(E->getExpr());
829 }
830
831 void VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
832 Visit(E->getExpr());
833 }
834 };
835 GlobVarInitChecker Checker(*this, VD);
836 Checker.Visit(Init);
837 }
838}
839
841 const FunctionDecl *Callee) {
842 FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
843 if (!Caller)
844 return;
845
846 if (!isImplicitHostDeviceFunction(Callee))
847 return;
848
849 CUDAFunctionTarget CallerTarget = IdentifyTarget(Caller);
850
851 // Record whether an implicit host device function is used on device side.
852 if (CallerTarget != CUDAFunctionTarget::Device &&
853 CallerTarget != CUDAFunctionTarget::Global &&
854 (CallerTarget != CUDAFunctionTarget::HostDevice ||
856 !getASTContext().CUDAImplicitHostDeviceFunUsedByDevice.count(Caller))))
857 return;
858
860}
861
862// With -fcuda-host-device-constexpr, an unattributed constexpr function is
863// treated as implicitly __host__ __device__, unless:
864// * it is a variadic function (device-side variadic functions are not
865// allowed), or
866// * a __device__ function with this signature was already declared, in which
867// case in which case we output an error, unless the __device__ decl is in a
868// system header, in which case we leave the constexpr function unattributed.
869//
870// In addition, all function decls are treated as __host__ __device__ when
871// ForceHostDeviceDepth > 0 (corresponding to code within a
872// #pragma clang force_cuda_host_device_begin/end
873// pair).
875 const LookupResult &Previous) {
876 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
877
878 if (ForceHostDeviceDepth > 0) {
879 if (!NewD->hasAttr<CUDAHostAttr>())
880 NewD->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));
881 if (!NewD->hasAttr<CUDADeviceAttr>())
882 NewD->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));
883 return;
884 }
885
886 // If a template function has no host/device/global attributes,
887 // make it implicitly host device function.
888 if (getLangOpts().OffloadImplicitHostDeviceTemplates &&
889 !NewD->hasAttr<CUDAHostAttr>() && !NewD->hasAttr<CUDADeviceAttr>() &&
890 !NewD->hasAttr<CUDAGlobalAttr>() &&
893 NewD->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));
894 NewD->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));
895 return;
896 }
897
898 if (!getLangOpts().CUDAHostDeviceConstexpr || !NewD->isConstexpr() ||
899 NewD->isVariadic() || NewD->hasAttr<CUDAHostAttr>() ||
900 NewD->hasAttr<CUDADeviceAttr>() || NewD->hasAttr<CUDAGlobalAttr>())
901 return;
902
903 // Is D a __device__ function with the same signature as NewD, ignoring CUDA
904 // attributes?
905 auto IsMatchingDeviceFn = [&](NamedDecl *D) {
906 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(D))
907 D = Using->getTargetDecl();
908 FunctionDecl *OldD = D->getAsFunction();
909 return OldD && OldD->hasAttr<CUDADeviceAttr>() &&
910 !OldD->hasAttr<CUDAHostAttr>() &&
911 !SemaRef.IsOverload(NewD, OldD,
912 /* UseMemberUsingDeclRules = */ false,
913 /* ConsiderCudaAttrs = */ false);
914 };
915 auto It = llvm::find_if(Previous, IsMatchingDeviceFn);
916 if (It != Previous.end()) {
917 // We found a __device__ function with the same name and signature as NewD
918 // (ignoring CUDA attrs). This is an error unless that function is defined
919 // in a system header, in which case we simply return without making NewD
920 // host+device.
921 NamedDecl *Match = *It;
922 if (!SemaRef.getSourceManager().isInSystemHeader(Match->getLocation())) {
923 Diag(NewD->getLocation(),
924 diag::err_cuda_unattributed_constexpr_cannot_overload_device)
925 << NewD;
926 Diag(Match->getLocation(),
927 diag::note_cuda_conflicting_device_function_declared_here);
928 }
929 return;
930 }
931
932 NewD->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));
933 NewD->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));
934}
935
936// TODO: `__constant__` memory may be a limited resource for certain targets.
937// A safeguard may be needed at the end of compilation pipeline if
938// `__constant__` memory usage goes beyond limit.
940 // Do not promote dependent variables since the cotr/dtor/initializer are
941 // not determined. Do it after instantiation.
942 if (getLangOpts().CUDAIsDevice && !VD->hasAttr<CUDAConstantAttr>() &&
943 !VD->hasAttr<CUDASharedAttr>() &&
944 (VD->isFileVarDecl() || VD->isStaticDataMember()) &&
945 !IsDependentVar(VD) &&
946 ((VD->isConstexpr() || VD->getType().isConstQualified()) &&
947 HasAllowedCUDADeviceStaticInitializer(*this, VD,
948 CICK_DeviceOrConstant))) {
949 VD->addAttr(CUDAConstantAttr::CreateImplicit(getASTContext()));
950 }
951}
952
954 unsigned DiagID) {
955 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
956 FunctionDecl *CurFunContext =
957 SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
958 SemaDiagnosticBuilder::Kind DiagKind = [&] {
959 if (!CurFunContext)
960 return SemaDiagnosticBuilder::K_Nop;
961 switch (CurrentTarget()) {
964 return SemaDiagnosticBuilder::K_Immediate;
966 // An HD function counts as host code if we're compiling for host, and
967 // device code if we're compiling for device. Defer any errors in device
968 // mode until the function is known-emitted.
969 if (!getLangOpts().CUDAIsDevice)
970 return SemaDiagnosticBuilder::K_Nop;
971 if (SemaRef.IsLastErrorImmediate &&
972 getDiagnostics().getDiagnosticIDs()->isNote(DiagID))
973 return SemaDiagnosticBuilder::K_Immediate;
974 if (isImplicitHDExplicitInstantiation(CurFunContext))
975 return SemaDiagnosticBuilder::K_Deferred;
976 return (SemaRef.getEmissionStatus(CurFunContext) ==
978 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack
979 : SemaDiagnosticBuilder::K_Deferred;
980 default:
981 return SemaDiagnosticBuilder::K_Nop;
982 }
983 }();
984 return SemaDiagnosticBuilder(DiagKind, Loc, DiagID, CurFunContext, SemaRef);
985}
986
988 unsigned DiagID) {
989 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
990 FunctionDecl *CurFunContext =
991 SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
992 SemaDiagnosticBuilder::Kind DiagKind = [&] {
993 if (!CurFunContext)
994 return SemaDiagnosticBuilder::K_Nop;
995 switch (CurrentTarget()) {
997 return SemaDiagnosticBuilder::K_Immediate;
999 // An HD function counts as host code if we're compiling for host, and
1000 // device code if we're compiling for device. Defer any errors in device
1001 // mode until the function is known-emitted.
1002 if (getLangOpts().CUDAIsDevice)
1003 return SemaDiagnosticBuilder::K_Nop;
1004 if (SemaRef.IsLastErrorImmediate &&
1005 getDiagnostics().getDiagnosticIDs()->isNote(DiagID))
1006 return SemaDiagnosticBuilder::K_Immediate;
1007 return (SemaRef.getEmissionStatus(CurFunContext) ==
1009 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack
1010 : SemaDiagnosticBuilder::K_Deferred;
1011 default:
1012 return SemaDiagnosticBuilder::K_Nop;
1013 }
1014 }();
1015 return SemaDiagnosticBuilder(DiagKind, Loc, DiagID, CurFunContext, SemaRef);
1016}
1017
1019 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
1020 assert(Callee && "Callee may not be null.");
1021
1022 const auto &ExprEvalCtx = SemaRef.currentEvaluationContext();
1023 if (ExprEvalCtx.isUnevaluated() || ExprEvalCtx.isConstantEvaluated() ||
1024 ExprEvalCtx.isDiscardedStatementContext())
1025 return true;
1026
1027 // C++ deduction guides participate in overload resolution but are not
1028 // callable functions and are never codegen'ed. Treat them as always
1029 // allowed for CUDA/HIP compatibility checking.
1030 if (isa<CXXDeductionGuideDecl>(Callee))
1031 return true;
1032
1033 // FIXME: Is bailing out early correct here? Should we instead assume that
1034 // the caller is a global initializer?
1035 FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
1036 if (!Caller)
1037 return true;
1038
1039 // If the caller is known-emitted, mark the callee as known-emitted.
1040 // Otherwise, mark the call in our call graph so we can traverse it later.
1041 bool CallerKnownEmitted = SemaRef.getEmissionStatus(Caller) ==
1043 bool CallerIsImplicitHDExplicitInst =
1045 SemaDiagnosticBuilder::Kind DiagKind = [this, Caller, Callee,
1046 CallerKnownEmitted,
1047 CallerIsImplicitHDExplicitInst] {
1048 switch (IdentifyPreference(Caller, Callee)) {
1049 case CFP_Never:
1050 case CFP_WrongSide:
1051 assert(Caller && "Never/wrongSide calls require a non-null caller");
1052 // If we know the caller will be emitted, we know this wrong-side call
1053 // will be emitted, so it's an immediate error. Otherwise, defer the
1054 // error until we know the caller is emitted.
1055 return (CallerKnownEmitted && !CallerIsImplicitHDExplicitInst)
1056 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack
1057 : SemaDiagnosticBuilder::K_Deferred;
1058 default:
1059 return SemaDiagnosticBuilder::K_Nop;
1060 }
1061 }();
1062
1063 bool IsDeviceKernelCall = Callee == getASTContext().getcudaLaunchDeviceDecl();
1064 bool CallerHD = Caller && Caller->hasAttr<CUDAHostAttr>() &&
1065 Caller->hasAttr<CUDADeviceAttr>();
1066 bool CallerDiscard = SemaRef.getEmissionStatus(Caller) ==
1068 bool RDC = getLangOpts().GPURelocatableDeviceCode;
1069 if (IsDeviceKernelCall && !(CallerHD && CallerDiscard) && !RDC) {
1070 Diag(Loc, diag::err_cuda_device_kernel_launch_require_rdc);
1071 return false;
1072 }
1073
1074 if (DiagKind == SemaDiagnosticBuilder::K_Nop) {
1075 // For -fgpu-rdc, keep track of external kernels used by host functions.
1076 if (getLangOpts().CUDAIsDevice && RDC &&
1077 Callee->hasAttr<CUDAGlobalAttr>() && !Callee->isDefined() &&
1078 (!Caller || (!Caller->getDescribedFunctionTemplate() &&
1079 getASTContext().GetGVALinkageForFunction(Caller) ==
1082 return true;
1083 }
1084
1085 // Avoid emitting this error twice for the same location. Using a hashtable
1086 // like this is unfortunate, but because we must continue parsing as normal
1087 // after encountering a deferred error, it's otherwise very tricky for us to
1088 // ensure that we only emit this deferred error once.
1089 if (!LocsWithCUDACallDiags.insert({Caller, Loc}).second)
1090 return true;
1091
1092 SemaDiagnosticBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller,
1093 SemaRef)
1094 << IdentifyTarget(Callee) << /*function*/ 0 << Callee
1095 << IdentifyTarget(Caller);
1096 if (!Callee->getBuiltinID())
1097 SemaDiagnosticBuilder(DiagKind, Callee->getLocation(),
1098 diag::note_previous_decl, Caller, SemaRef)
1099 << Callee;
1100 return DiagKind != SemaDiagnosticBuilder::K_Immediate &&
1101 DiagKind != SemaDiagnosticBuilder::K_ImmediateWithCallStack;
1102}
1103
1104// Check the wrong-sided reference capture of lambda for CUDA/HIP.
1105// A lambda function may capture a stack variable by reference when it is
1106// defined and uses the capture by reference when the lambda is called. When
1107// the capture and use happen on different sides, the capture is invalid and
1108// should be diagnosed.
1110 const sema::Capture &Capture) {
1111 // In host compilation we only need to check lambda functions emitted on host
1112 // side. In such lambda functions, a reference capture is invalid only
1113 // if the lambda structure is populated by a device function or kernel then
1114 // is passed to and called by a host function. However that is impossible,
1115 // since a device function or kernel can only call a device function, also a
1116 // kernel cannot pass a lambda back to a host function since we cannot
1117 // define a kernel argument type which can hold the lambda before the lambda
1118 // itself is defined.
1119 if (!getLangOpts().CUDAIsDevice)
1120 return;
1121
1122 // File-scope lambda can only do init captures for global variables, which
1123 // results in passing by value for these global variables.
1124 FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
1125 if (!Caller)
1126 return;
1127
1128 // In device compilation, we only need to check lambda functions which are
1129 // emitted on device side. For such lambdas, a reference capture is invalid
1130 // only if the lambda structure is populated by a host function then passed
1131 // to and called in a device function or kernel.
1132 bool CalleeIsDevice = Callee->hasAttr<CUDADeviceAttr>();
1133 bool CallerIsHost =
1134 !Caller->hasAttr<CUDAGlobalAttr>() && !Caller->hasAttr<CUDADeviceAttr>();
1135 bool ShouldCheck = CalleeIsDevice && CallerIsHost;
1136 if (!ShouldCheck || !Capture.isReferenceCapture())
1137 return;
1138 auto DiagKind = SemaDiagnosticBuilder::K_Deferred;
1139 if (Capture.isVariableCapture() && !getLangOpts().HIPStdPar) {
1141 diag::err_capture_bad_target, Callee, SemaRef)
1142 << Capture.getVariable();
1143 } else if (Capture.isThisCapture()) {
1144 // Capture of this pointer is allowed since this pointer may be pointing to
1145 // managed memory which is accessible on both device and host sides. It only
1146 // results in invalid memory access if this pointer points to memory not
1147 // accessible on device side.
1149 diag::warn_maybe_capture_bad_target_this_ptr, Callee,
1150 SemaRef);
1151 }
1152}
1153
1155 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
1156 if (Method->hasAttr<CUDAHostAttr>() || Method->hasAttr<CUDADeviceAttr>())
1157 return;
1158 Method->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));
1159 Method->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));
1160}
1161
1163 const LookupResult &Previous) {
1164 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
1165 CUDAFunctionTarget NewTarget = IdentifyTarget(NewFD);
1166 for (NamedDecl *OldND : Previous) {
1167 FunctionDecl *OldFD = OldND->getAsFunction();
1168 if (!OldFD)
1169 continue;
1170
1171 CUDAFunctionTarget OldTarget = IdentifyTarget(OldFD);
1172 // Don't allow HD and global functions to overload other functions with the
1173 // same signature. We allow overloading based on CUDA attributes so that
1174 // functions can have different implementations on the host and device, but
1175 // HD/global functions "exist" in some sense on both the host and device, so
1176 // should have the same implementation on both sides.
1177 if (NewTarget != OldTarget &&
1178 !SemaRef.IsOverload(NewFD, OldFD, /* UseMemberUsingDeclRules = */ false,
1179 /* ConsiderCudaAttrs = */ false)) {
1180 if ((NewTarget == CUDAFunctionTarget::HostDevice &&
1181 !(getLangOpts().OffloadImplicitHostDeviceTemplates &&
1183 OldTarget == CUDAFunctionTarget::Device)) ||
1184 (OldTarget == CUDAFunctionTarget::HostDevice &&
1185 !(getLangOpts().OffloadImplicitHostDeviceTemplates &&
1187 NewTarget == CUDAFunctionTarget::Device)) ||
1188 (NewTarget == CUDAFunctionTarget::Global) ||
1189 (OldTarget == CUDAFunctionTarget::Global)) {
1190 Diag(NewFD->getLocation(), diag::err_cuda_ovl_target)
1191 << NewTarget << NewFD->getDeclName() << OldTarget << OldFD;
1192 Diag(OldFD->getLocation(), diag::note_previous_declaration);
1193 NewFD->setInvalidDecl();
1194 break;
1195 }
1196 if ((NewTarget == CUDAFunctionTarget::Host &&
1197 OldTarget == CUDAFunctionTarget::Device) ||
1198 (NewTarget == CUDAFunctionTarget::Device &&
1199 OldTarget == CUDAFunctionTarget::Host)) {
1200 Diag(NewFD->getLocation(), diag::warn_offload_incompatible_redeclare)
1201 << NewTarget << OldTarget;
1202 Diag(OldFD->getLocation(), diag::note_previous_declaration);
1203 }
1204 }
1205 }
1206}
1207
1208template <typename AttrTy>
1210 const FunctionDecl &TemplateFD) {
1211 if (FD->hasAttr<AttrTy>())
1212 return;
1213 if (AttrTy *Attribute = TemplateFD.getAttr<AttrTy>()) {
1214 AttrTy *Clone = Attribute->clone(S.Context);
1215 Clone->setInherited(true);
1216 FD->addAttr(Clone);
1217 }
1218}
1219
1221 const FunctionTemplateDecl &TD) {
1222 const FunctionDecl &TemplateFD = *TD.getTemplatedDecl();
1226}
1227
1229 if (getLangOpts().OffloadViaLLVM)
1230 return "__llvmPushCallConfiguration";
1231
1232 if (getLangOpts().HIP)
1233 return getLangOpts().HIPUseNewLaunchAPI ? "__hipPushCallConfiguration"
1234 : "hipConfigureCall";
1235
1236 // New CUDA kernel launch sequence.
1237 if (CudaFeatureEnabled(getASTContext().getTargetInfo().getSDKVersion(),
1239 return "__cudaPushCallConfiguration";
1240
1241 // Legacy CUDA kernel configuration call
1242 return "cudaConfigureCall";
1243}
1244
1246 return "cudaGetParameterBuffer";
1247}
1248
1250 return "cudaLaunchDevice";
1251}
1252
1253// Record any local constexpr variables that are passed one way on the host
1254// and another on the device.
1256 MultiExprArg Arguments, OverloadCandidateSet &Candidates) {
1257 sema::LambdaScopeInfo *LambdaInfo = SemaRef.getCurLambda();
1258 if (!LambdaInfo)
1259 return;
1260
1261 for (unsigned I = 0; I < Arguments.size(); ++I) {
1262 auto *DeclRef = dyn_cast<DeclRefExpr>(Arguments[I]);
1263 if (!DeclRef)
1264 continue;
1265 auto *Variable = dyn_cast<VarDecl>(DeclRef->getDecl());
1266 if (!Variable || !Variable->isLocalVarDecl() || !Variable->isConstexpr())
1267 continue;
1268
1269 bool HostByValue = false, HostByRef = false;
1270 bool DeviceByValue = false, DeviceByRef = false;
1271
1272 for (OverloadCandidate &Candidate : Candidates) {
1273 FunctionDecl *Callee = Candidate.Function;
1274 if (!Callee || I >= Callee->getNumParams())
1275 continue;
1276
1280 continue;
1281
1282 bool CoversHost = (Target == CUDAFunctionTarget::Host ||
1284 bool CoversDevice = (Target == CUDAFunctionTarget::Device ||
1286
1287 bool IsRef = Callee->getParamDecl(I)->getType()->isReferenceType();
1288 HostByValue |= CoversHost && !IsRef;
1289 HostByRef |= CoversHost && IsRef;
1290 DeviceByValue |= CoversDevice && !IsRef;
1291 DeviceByRef |= CoversDevice && IsRef;
1292 }
1293
1294 if ((HostByValue && DeviceByRef) || (HostByRef && DeviceByValue))
1295 LambdaInfo->CUDAPotentialODRUsedVars.insert(Variable);
1296 }
1297}
Defines the clang::ASTContext interface.
static bool hasImplicitAttr(const ValueDecl *decl)
Defines the clang::Expr interface and subclasses for C++ expressions.
FormatToken * Previous
The previous token in the unwrapped line.
Defines the clang::Preprocessor interface.
static bool resolveCalleeCUDATargetConflict(CUDAFunctionTarget Target1, CUDAFunctionTarget Target2, CUDAFunctionTarget *ResolvedTarget)
When an implicitly-declared special member has to invoke more than one base/field special member,...
Definition SemaCUDA.cpp:442
static bool hasAttr(const Decl *D, bool IgnoreImplicitAttr)
Definition SemaCUDA.cpp:186
static void copyAttrIfPresent(Sema &S, FunctionDecl *FD, const FunctionDecl &TemplateFD)
static bool hasExplicitAttr(const VarDecl *D)
Definition SemaCUDA.cpp:32
This file declares semantic analysis for CUDA constructs.
FunctionDecl * getcudaGetParameterBufferDecl()
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
llvm::SetVector< const ValueDecl * > CUDAExternalDeviceDeclODRUsedByHost
Keep track of CUDA/HIP external kernels or device variables ODR-used by host code.
llvm::DenseSet< const FunctionDecl * > CUDAImplicitHostDeviceFunUsedByDevice
Keep track of CUDA/HIP implicit host device functions used on device side in device compilation.
FunctionDecl * getcudaConfigureCallDecl()
FunctionDecl * getcudaLaunchDeviceDecl()
Attr - This represents one attribute.
Definition Attr.h:46
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents a call to a C++ constructor.
Definition ExprCXX.h:1552
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition ExprCXX.h:1615
Represents a C++ constructor within a class.
Definition DeclCXX.h:2641
Represents a C++ base or member initializer.
Definition DeclCXX.h:2406
A default argument (C++ [dcl.fct.default]).
Definition ExprCXX.h:1274
A use of a default initializer in a constructor or in aggregate initialization.
Definition ExprCXX.h:1381
Expr * getExpr()
Get the initialization expression that will be used.
Definition ExprCXX.cpp:1138
Represents a C++ destructor within a class.
Definition DeclCXX.h:2906
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2149
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2292
The null pointer literal (C++11 [lex.nullptr])
Definition ExprCXX.h:772
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
base_class_range bases()
Definition DeclCXX.h:608
base_class_range vbases()
Definition DeclCXX.h:625
bool isAbstract() const
Determine whether this class has a pure virtual function.
Definition DeclCXX.h:1230
bool isDynamicClass() const
Definition DeclCXX.h:574
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2987
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3170
ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
DeclContext * getLexicalParent()
getLexicalParent - Returns the containing lexical DeclContext.
Definition DeclBase.h:2142
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1290
ValueDecl * getDecl()
Definition Expr.h:1358
SourceLocation getLocation() const
Definition Expr.h:1366
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
T * getAttr() const
Definition DeclBase.h:581
bool hasAttrs() const
Definition DeclBase.h:526
void addAttr(Attr *A)
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition DeclBase.h:601
void setInvalidDecl(bool Invalid=true)
setInvalidDecl - Indicates the Decl had a semantic error.
Definition DeclBase.cpp:178
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition DeclBase.cpp:273
bool isInvalidDecl() const
Definition DeclBase.h:596
SourceLocation getLocation() const
Definition DeclBase.h:447
DeclContext * getDeclContext()
Definition DeclBase.h:456
AttrVec & getAttrs()
Definition DeclBase.h:532
bool hasAttr() const
Definition DeclBase.h:585
This represents one expression.
Definition Expr.h:113
Represents a member of a struct/union/class.
Definition Decl.h:3295
Represents a function declaration or definition.
Definition Decl.h:2059
bool hasTrivialBody() const
Returns whether the function has a trivial body that does not require any specific codegen.
Definition Decl.cpp:3199
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition Decl.cpp:4246
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition Decl.cpp:4234
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition Decl.h:2504
bool isVariadic() const
Whether this function is variadic.
Definition Decl.cpp:3121
bool isTemplateInstantiation() const
Determines if the given function was instantiated from a function template.
Definition Decl.cpp:4298
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition Decl.h:2597
bool isConsteval() const
Definition Decl.h:2609
bool isUserProvided() const
True if this method is user-declared and was not deleted or defaulted on its first declaration.
Definition Decl.h:2537
bool isImplicitHDExplicitInstantiation() const
True if both host and device are implicit attributes and this is (or is a member of) an explicit temp...
Definition Decl.cpp:4555
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3870
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
Definition Decl.cpp:3235
Declaration of a template function.
FunctionDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
static IntegerLiteral * Create(const ASTContext &C, const llvm::APInt &V, QualType type, SourceLocation l)
Returns a new integer literal with value 'V' and type 'type'.
Definition Expr.cpp:981
Represents the results of name lookup.
Definition Lookup.h:147
This represents a decl that may have a name.
Definition Decl.h:275
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:341
OverloadCandidateSet - A set of overload candidates, used in C++ overload resolution (C++ 13....
Definition Overload.h:1161
ParsedAttr - Represents a syntactic attribute.
Definition ParsedAttr.h:119
A (possibly-)qualified type.
Definition TypeBase.h:938
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition TypeBase.h:8461
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8494
LangAS getAddressSpace() const
Definition TypeBase.h:572
field_range fields() const
Definition Decl.h:4663
decl_type * getFirstDecl()
Return the first declaration of this declaration or itself if this is the only declaration.
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
A generic diagnostic builder for errors which may or may not be deferred.
Definition SemaBase.h:111
SemaBase(Sema &S)
Definition SemaBase.cpp:7
ASTContext & getASTContext() const
Definition SemaBase.cpp:9
Sema & SemaRef
Definition SemaBase.h:40
const LangOptions & getLangOpts() const
Definition SemaBase.cpp:11
DiagnosticsEngine & getDiagnostics() const
Definition SemaBase.cpp:10
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition SemaBase.cpp:61
std::string getLaunchDeviceFuncName() const
Return the name of the device kernel launch function.
void PushForceHostDevice()
Increments our count of the number of times we've seen a pragma forcing functions to be host device.
Definition SemaCUDA.cpp:40
void checkAllowedInitializer(VarDecl *VD)
Definition SemaCUDA.cpp:744
void RecordImplicitHostDeviceFuncUsedByDevice(const FunctionDecl *FD)
Record FD if it is a CUDA/HIP implicit host device function used on device side in device compilation...
Definition SemaCUDA.cpp:840
std::string getConfigureFuncName() const
Returns the name of the launch configuration function.
bool PopForceHostDevice()
Decrements our count of the number of times we've seen a pragma forcing functions to be host device.
Definition SemaCUDA.cpp:45
CUDAFunctionTarget IdentifyTarget(const FunctionDecl *D, bool IgnoreImplicitHDAttr=false)
Determines whether the given function is a CUDA device/host/kernel/etc.
Definition SemaCUDA.cpp:211
static bool isImplicitHDExplicitInstantiation(const FunctionDecl *FD)
Null-tolerant wrapper for FunctionDecl::isImplicitHDExplicitInstantiation.
Definition SemaCUDA.cpp:405
void maybeAddHostDeviceAttrs(FunctionDecl *FD, const LookupResult &Previous)
May add implicit CUDAHostAttr and CUDADeviceAttr attributes to FD, depending on FD and the current co...
Definition SemaCUDA.cpp:874
ExprResult ActOnExecConfigExpr(Scope *S, SourceLocation LLLLoc, MultiExprArg ExecConfig, SourceLocation GGGLoc)
Definition SemaCUDA.cpp:53
bool isEmptyConstructor(SourceLocation Loc, CXXConstructorDecl *CD)
Definition SemaCUDA.cpp:598
std::string getGetParameterBufferFuncName() const
Return the name of the parameter buffer allocation function for the device kernel launch.
bool isEmptyDestructor(SourceLocation Loc, CXXDestructorDecl *CD)
Definition SemaCUDA.cpp:636
void checkTargetOverload(FunctionDecl *NewFD, const LookupResult &Previous)
Check whether NewFD is a valid overload for CUDA.
CUDAFunctionTarget CurrentTarget()
Gets the CUDA target for the current context.
Definition SemaCUDA.h:153
SemaDiagnosticBuilder DiagIfHostCode(SourceLocation Loc, unsigned DiagID)
Creates a SemaDiagnosticBuilder that emits the diagnostic if the current context is "used as host cod...
Definition SemaCUDA.cpp:987
bool inferTargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl, CXXSpecialMemberKind CSM, CXXMethodDecl *MemberDecl, bool ConstRHS, bool Diagnose)
Given a implicit special member, infer its CUDA target from the calls it needs to make to underlying ...
Definition SemaCUDA.cpp:462
struct clang::SemaCUDA::CUDATargetContext CurCUDATargetCtx
CUDATargetContextKind
Defines kinds of CUDA global host/device context where a function may be called.
Definition SemaCUDA.h:130
@ CTCK_InitGlobalVar
Unknown context.
Definition SemaCUDA.h:132
SemaDiagnosticBuilder DiagIfDeviceCode(SourceLocation Loc, unsigned DiagID)
Creates a SemaDiagnosticBuilder that emits the diagnostic if the current context is "used as device c...
Definition SemaCUDA.cpp:953
llvm::DenseSet< FunctionDeclAndLoc > LocsWithCUDACallDiags
FunctionDecls and SourceLocations for which CheckCall has emitted a (maybe deferred) "bad call" diagn...
Definition SemaCUDA.h:73
bool CheckCall(SourceLocation Loc, FunctionDecl *Callee)
Check whether we're allowed to call Callee from the current context.
void inheritTargetAttrs(FunctionDecl *FD, const FunctionTemplateDecl &TD)
Copies target attributes from the template TD to the function FD.
static bool isImplicitHostDeviceFunction(const FunctionDecl *D)
Definition SemaCUDA.cpp:399
void CheckLambdaCapture(CXXMethodDecl *D, const sema::Capture &Capture)
void MaybeAddConstantAttr(VarDecl *VD)
May add implicit CUDAConstantAttr attribute to VD, depending on VD and current compilation settings.
Definition SemaCUDA.cpp:939
void EraseUnwantedMatches(const FunctionDecl *Caller, llvm::SmallVectorImpl< std::pair< DeclAccessPair, FunctionDecl * > > &Matches)
Finds a function in Matches with highest calling priority from Caller context and erases all function...
Definition SemaCUDA.cpp:409
SemaCUDA(Sema &S)
Definition SemaCUDA.cpp:30
void SetLambdaAttrs(CXXMethodDecl *Method)
Set device or host device attributes on the given lambda operator() method.
CUDAFunctionPreference IdentifyPreference(const FunctionDecl *Caller, const FunctionDecl *Callee)
Identifies relative preference of a given Caller/Callee combination, based on their host/device attri...
Definition SemaCUDA.cpp:311
void recordPotentialODRUsedVariable(MultiExprArg Args, OverloadCandidateSet &CandidateSet)
Record variables that are potentially ODR-used in CUDA/HIP.
@ CVT_Host
Emitted on device side with a shadow variable on host side.
Definition SemaCUDA.h:121
@ CVT_Both
Emitted on host side only.
Definition SemaCUDA.h:122
@ CVT_Unified
Emitted on both sides with different addresses.
Definition SemaCUDA.h:123
A RAII object to temporarily push a declaration context.
Definition Sema.h:3532
SpecialMemberOverloadResult - The overloading result for a special member function.
Definition Sema.h:9332
CXXMethodDecl * getMethod() const
Definition Sema.h:9344
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:863
ASTContext & Context
Definition Sema.h:1304
Encodes a location in the source.
bool isUnion() const
Definition Decl.h:4063
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isReferenceType() const
Definition TypeBase.h:8682
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
Definition Type.cpp:5503
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition TypeBase.h:2859
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
Definition Type.cpp:5512
Represents a shadow declaration implicitly introduced into a scope by a (resolved) using-declaration ...
Definition DeclCXX.h:3428
QualType getType() const
Definition Decl.h:724
Represents a variable declaration or definition.
Definition Decl.h:933
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition Decl.h:1594
bool hasInit() const
Definition Decl.cpp:2380
bool isStaticDataMember() const
Determines whether this is a static data member.
Definition Decl.h:1307
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1248
bool isFileVarDecl() const
Returns true for file scoped variable declaration.
Definition Decl.h:1366
const Expr * getInit() const
Definition Decl.h:1392
ValueDecl * getVariable() const
Definition ScopeInfo.h:676
bool isVariableCapture() const
Definition ScopeInfo.h:651
SourceLocation getLocation() const
Retrieve the location at which this variable was captured.
Definition ScopeInfo.h:687
bool isThisCapture() const
Definition ScopeInfo.h:650
bool isReferenceCapture() const
Definition ScopeInfo.h:656
llvm::SmallPtrSet< VarDecl *, 4 > CUDAPotentialODRUsedVars
Variables that are potentially ODR-used in CUDA/HIP.
Definition ScopeInfo.h:956
Defines the clang::TargetInfo interface.
Top level wrappers for InstallAPI frontend operations.
@ Match
This is not an overload because the signature exactly matches an existing declaration.
Definition Sema.h:824
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ GVA_StrongExternal
Definition Linkage.h:76
CUDAFunctionTarget
Definition Cuda.h:65
MutableArrayRef< Expr * > MultiExprArg
Definition Ownership.h:259
bool CudaFeatureEnabled(llvm::VersionTuple, CudaFeature)
Definition Cuda.cpp:119
ExprResult ExprError()
Definition Ownership.h:265
@ CUDA_USES_NEW_LAUNCH
Definition Cuda.h:82
CXXSpecialMemberKind
Kinds of C++ special members.
Definition Decl.h:2019
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition Specifiers.h:140
ActionResult< Expr * > ExprResult
Definition Ownership.h:249
OverloadCandidate - A single candidate in an overload set (C++ 13.3).
Definition Overload.h:934
SemaCUDA::CUDATargetContext SavedCtx
Definition SemaCUDA.h:146
CUDATargetContextRAII(SemaCUDA &S_, SemaCUDA::CUDATargetContextKind K, Decl *D)
Definition SemaCUDA.cpp:193