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