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"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/EvaluatedExprVisitor.h"
17#include "clang/AST/ExprCXX.h"
18#include "clang/Basic/Cuda.h"
19#include "clang/Basic/TargetInfo.h"
20#include "clang/Lex/Preprocessor.h"
21#include "clang/Sema/Lookup.h"
22#include "clang/Sema/Overload.h"
23#include "clang/Sema/ScopeInfo.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
30SemaCUDA::SemaCUDA(Sema &S) : SemaBase(S) {}
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
40void SemaCUDA::PushForceHostDevice() {
41 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
42 ForceHostDeviceDepth++;
43}
44
45bool SemaCUDA::PopForceHostDevice() {
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
53ExprResult SemaCUDA::ActOnExecConfigExpr(Scope *S, SourceLocation LLLLoc,
54 MultiExprArg ExecConfig,
55 SourceLocation GGGLoc) {
56 bool IsDeviceKernelCall = false;
57 switch (CurrentTarget()) {
58 case CUDAFunctionTarget::Global:
59 case CUDAFunctionTarget::Device:
60 IsDeviceKernelCall = true;
61 break;
62 case CUDAFunctionTarget::HostDevice:
63 if (getLangOpts().CUDAIsDevice) {
64 IsDeviceKernelCall = true;
65 FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
66 if (Caller && isImplicitHostDeviceFunction(D: 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>(Val: 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(Loc: LLLLoc, DiagID: diag::err_cuda_device_kernel_launch_not_supported));
89
90 FunctionDecl *ConfigDecl = IsDeviceKernelCall
91 ? getASTContext().getcudaLaunchDeviceDecl()
92 : getASTContext().getcudaConfigureCallDecl();
93 if (!ConfigDecl)
94 return ExprError(Diag(Loc: LLLLoc, DiagID: diag::err_undeclared_var_use)
95 << (IsDeviceKernelCall ? getLaunchDeviceFuncName()
96 : getConfigureFuncName()));
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(Loc: LLLLoc, DiagID: diag::err_undeclared_var_use)
102 << getGetParameterBufferFuncName());
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(Loc: LLLLoc, Func: ConfigDecl);
110
111 if (IsDeviceKernelCall) {
112 SmallVector<Expr *> Args;
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(T: SemaRef.Context.IntTy), 0);
117 Args.push_back(Elt: IntegerLiteral::Create(C: SemaRef.Context, V: Zero,
118 type: SemaRef.Context.IntTy, l: 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(Elt: IntegerLiteral::Create(C: SemaRef.Context, V: Zero,
122 type: SemaRef.Context.IntTy, l: LLLLoc));
123 // Add the original config arguments.
124 llvm::append_range(C&: Args, R&: ExecConfig);
125 // Add the default blockDim if it's missing.
126 if (Args.size() < 4) {
127 llvm::APInt One(SemaRef.Context.getTypeSize(T: SemaRef.Context.IntTy), 1);
128 Args.push_back(Elt: IntegerLiteral::Create(C: SemaRef.Context, V: One,
129 type: SemaRef.Context.IntTy, l: LLLLoc));
130 }
131 // Add the default sharedMemSize if it's missing.
132 if (Args.size() < 5)
133 Args.push_back(Elt: IntegerLiteral::Create(C: SemaRef.Context, V: Zero,
134 type: SemaRef.Context.IntTy, l: LLLLoc));
135 // Add the default stream if it's missing.
136 if (Args.size() < 6)
137 Args.push_back(Elt: new (SemaRef.Context) CXXNullPtrLiteralExpr(
138 SemaRef.Context.NullPtrTy, LLLLoc));
139 return SemaRef.BuildCallExpr(S, Fn: ConfigDR, LParenLoc: LLLLoc, ArgExprs: Args, RParenLoc: GGGLoc, ExecConfig: nullptr,
140 /*IsExecConfig=*/true);
141 }
142 return SemaRef.BuildCallExpr(S, Fn: ConfigDR, LParenLoc: LLLLoc, ArgExprs: ExecConfig, RParenLoc: GGGLoc, ExecConfig: nullptr,
143 /*IsExecConfig=*/true);
144}
145
146CUDAFunctionTarget SemaCUDA::IdentifyTarget(const ParsedAttributesView &Attrs) {
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)
171 return CUDAFunctionTarget::InvalidTarget;
172
173 if (HasGlobalAttr)
174 return CUDAFunctionTarget::Global;
175
176 if (HasHostAttr && HasDeviceAttr)
177 return CUDAFunctionTarget::HostDevice;
178
179 if (HasDeviceAttr)
180 return CUDAFunctionTarget::Device;
181
182 return CUDAFunctionTarget::Host;
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
193SemaCUDA::CUDATargetContextRAII::CUDATargetContextRAII(
194 SemaCUDA &S_, SemaCUDA::CUDATargetContextKind K, Decl *D)
195 : S(S_) {
196 SavedCtx = S.CurCUDATargetCtx;
197 assert(K == SemaCUDA::CTCK_InitGlobalVar);
198 auto *VD = dyn_cast_or_null<VarDecl>(Val: D);
199 if (VD && VD->hasGlobalStorage() && !VD->isStaticLocal()) {
200 auto Target = CUDAFunctionTarget::Host;
201 if ((hasAttr<CUDADeviceAttr>(D: VD, /*IgnoreImplicit=*/IgnoreImplicitAttr: true) &&
202 !hasAttr<CUDAHostAttr>(D: VD, /*IgnoreImplicit=*/IgnoreImplicitAttr: true)) ||
203 hasAttr<CUDASharedAttr>(D: VD, /*IgnoreImplicit=*/IgnoreImplicitAttr: true) ||
204 hasAttr<CUDAConstantAttr>(D: VD, /*IgnoreImplicit=*/IgnoreImplicitAttr: true))
205 Target = CUDAFunctionTarget::Device;
206 S.CurCUDATargetCtx = {.Target: Target, .Kind: K, .D: VD};
207 }
208}
209
210/// IdentifyTarget - Determine the CUDA compilation target for this function
211CUDAFunctionTarget SemaCUDA::IdentifyTarget(const FunctionDecl *D,
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.
220 if (isa<CXXDeductionGuideDecl>(Val: D))
221 return CUDAFunctionTarget::HostDevice;
222
223 if (D->hasAttr<CUDAInvalidTargetAttr>())
224 return CUDAFunctionTarget::InvalidTarget;
225
226 if (D->hasAttr<CUDAGlobalAttr>())
227 return CUDAFunctionTarget::Global;
228
229 if (D->isConsteval())
230 return CUDAFunctionTarget::HostDevice;
231
232 if (hasAttr<CUDADeviceAttr>(D, IgnoreImplicitAttr: IgnoreImplicitHDAttr)) {
233 if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitAttr: IgnoreImplicitHDAttr))
234 return CUDAFunctionTarget::HostDevice;
235 return CUDAFunctionTarget::Device;
236 } else if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitAttr: IgnoreImplicitHDAttr)) {
237 return CUDAFunctionTarget::Host;
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.
242 return CUDAFunctionTarget::HostDevice;
243 }
244
245 return CUDAFunctionTarget::Host;
246}
247
248/// IdentifyTarget - Determine the CUDA compilation target for this variable.
249SemaCUDA::CUDAVariableTarget SemaCUDA::IdentifyTarget(const VarDecl *Var) {
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>() &&
257 !hasExplicitAttr<CUDAConstantAttr>(D: Var))
258 return CVT_Both;
259 if (Var->hasAttr<CUDADeviceAttr>() || Var->hasAttr<CUDAConstantAttr>() ||
260 Var->hasAttr<CUDASharedAttr>() ||
261 Var->getType()->isCUDADeviceBuiltinSurfaceType() ||
262 Var->getType()->isCUDADeviceBuiltinTextureType())
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>(Val: Var->getDeclContext())) {
269 switch (IdentifyTarget(D: FD)) {
270 case CUDAFunctionTarget::HostDevice:
271 return CVT_Both;
272 case CUDAFunctionTarget::Device:
273 case CUDAFunctionTarget::Global:
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
310SemaCUDA::CUDAFunctionPreference
311SemaCUDA::IdentifyPreference(const FunctionDecl *Caller,
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 &&
319 CurCUDATargetCtx.Target == CUDAFunctionTarget::Device &&
320 (isa<CXXConstructorDecl>(Val: Callee) || isa<CXXDestructorDecl>(Val: Callee)))
321 return CFP_HostDevice;
322
323 CUDAFunctionTarget CallerTarget = IdentifyTarget(D: Caller);
324 CUDAFunctionTarget CalleeTarget = IdentifyTarget(D: 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
399bool SemaCUDA::isImplicitHostDeviceFunction(const FunctionDecl *D) {
400 bool IsImplicitDevAttr = hasImplicitAttr<CUDADeviceAttr>(D);
401 bool IsImplicitHostAttr = hasImplicitAttr<CUDAHostAttr>(D);
402 return IsImplicitDevAttr && IsImplicitHostAttr;
403}
404
405bool SemaCUDA::isImplicitHDExplicitInstantiation(const FunctionDecl *FD) {
406 return FD && FD->isImplicitHDExplicitInstantiation();
407}
408
409void SemaCUDA::EraseUnwantedMatches(
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, Callee: Match.second);
420 };
421
422 // Find the best call preference among the functions in Matches.
423 CUDAFunctionPreference BestCFP =
424 GetCFP(*llvm::max_element(Range&: Matches, C: [&](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(C&: Matches,
430 P: [&](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
442resolveCalleeCUDATargetConflict(CUDAFunctionTarget Target1,
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
462bool SemaCUDA::inferTargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl,
463 CXXSpecialMemberKind CSM,
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.
490 llvm::SmallVector<const CXXBaseSpecifier *, 16> Bases;
491 for (const auto &B : ClassDecl->bases()) {
492 if (!B.isVirtual()) {
493 Bases.push_back(Elt: &B);
494 }
495 }
496
497 if (!ClassDecl->isAbstract() || CSM == CXXSpecialMemberKind::Destructor)
498 llvm::append_range(C&: Bases, R: llvm::make_pointer_range(Range: ClassDecl->vbases()));
499
500 for (const auto *B : Bases) {
501 auto *BaseClassDecl = B->getType()->getAsCXXRecordDecl();
502 if (!BaseClassDecl)
503 continue;
504
505 Sema::SpecialMemberOverloadResult SMOR =
506 SemaRef.LookupSpecialMember(D: BaseClassDecl, SM: 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(D: SMOR.getMethod());
517
518 if (!InferredTarget) {
519 InferredTarget = BaseMethodTarget;
520 } else {
521 bool ResolutionError = resolveCalleeCUDATargetConflict(
522 Target1: *InferredTarget, Target2: BaseMethodTarget, ResolvedTarget: &*InferredTarget);
523 if (ResolutionError) {
524 if (Diagnose) {
525 Diag(Loc: ClassDecl->getLocation(),
526 DiagID: diag::note_implicit_member_target_infer_collision)
527 << (unsigned)CSM << *InferredTarget << BaseMethodTarget;
528 }
529 MemberDecl->addAttr(
530 A: CUDAInvalidTargetAttr::CreateImplicit(Ctx&: 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 =
543 getASTContext().getBaseElementType(QT: F->getType())->getAsCXXRecordDecl();
544 if (!FieldRecDecl)
545 continue;
546
547 Sema::SpecialMemberOverloadResult SMOR =
548 SemaRef.LookupSpecialMember(D: FieldRecDecl, SM: 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(D: SMOR.getMethod());
559
560 if (!InferredTarget) {
561 InferredTarget = FieldMethodTarget;
562 } else {
563 bool ResolutionError = resolveCalleeCUDATargetConflict(
564 Target1: *InferredTarget, Target2: FieldMethodTarget, ResolvedTarget: &*InferredTarget);
565 if (ResolutionError) {
566 if (Diagnose) {
567 Diag(Loc: ClassDecl->getLocation(),
568 DiagID: diag::note_implicit_member_target_infer_collision)
569 << (unsigned)CSM << *InferredTarget << FieldMethodTarget;
570 }
571 MemberDecl->addAttr(
572 A: CUDAInvalidTargetAttr::CreateImplicit(Ctx&: 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(A: CUDADeviceAttr::CreateImplicit(Ctx&: getASTContext()));
592 if (NeedsH && !HasH)
593 MemberDecl->addAttr(A: CUDAHostAttr::CreateImplicit(Ctx&: getASTContext()));
594
595 return false;
596}
597
598bool SemaCUDA::isEmptyConstructor(SourceLocation Loc, CXXConstructorDecl *CD) {
599 if (!CD->isDefined() && CD->isTemplateInstantiation())
600 SemaRef.InstantiateFunctionDefinition(PointOfInstantiation: Loc, Function: 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(Range: CD->inits(), P: [&](const CXXCtorInitializer *CI) {
626 if (const CXXConstructExpr *CE =
627 dyn_cast<CXXConstructExpr>(Val: CI->getInit()))
628 return isEmptyConstructor(Loc, CD: CE->getConstructor());
629 return false;
630 }))
631 return false;
632
633 return true;
634}
635
636bool SemaCUDA::isEmptyDestructor(SourceLocation Loc, CXXDestructorDecl *DD) {
637 // No destructor -> no problem.
638 if (!DD)
639 return true;
640
641 if (!DD->isDefined() && DD->isTemplateInstantiation())
642 SemaRef.InstantiateFunctionDefinition(PointOfInstantiation: Loc, Function: 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(Range: ClassDecl->bases(), P: [&](const CXXBaseSpecifier &BS) {
670 if (CXXRecordDecl *RD = BS.getType()->getAsCXXRecordDecl())
671 return isEmptyDestructor(Loc, DD: RD->getDestructor());
672 return true;
673 }))
674 return false;
675
676 // ... and member fields.
677 if (!llvm::all_of(Range: ClassDecl->fields(), P: [&](const FieldDecl *Field) {
678 if (CXXRecordDecl *RD = Field->getType()
679 ->getBaseElementTypeUnsafe()
680 ->getAsCXXRecordDecl())
681 return isEmptyDestructor(Loc, DD: 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>(Val: Init)) {
721 return S.isEmptyConstructor(Loc: VD->getLocation(), CD: 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(Ctx&: S.getASTContext(),
730 ForRef: VD->getType()->isReferenceType());
731 };
732 auto HasEmptyDtor = [&](VarDecl *VD) {
733 if (const auto *RD = VD->getType()->getAsCXXRecordDecl())
734 return S.isEmptyDestructor(Loc: VD->getLocation(), DD: 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
744void SemaCUDA::checkAllowedInitializer(VarDecl *VD) {
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>(Val: 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) &&
757 VD->getType().getQualifiers().getAddressSpace() != LangAS::Default) {
758 Diag(Loc: VD->getLocation(), DiagID: 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 S&: *this, VD, CheckKind: IsSharedVar ? CICK_Shared : CICK_DeviceOrConstant))
771 return;
772 Diag(Loc: VD->getLocation(),
773 DiagID: 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:
782 using Base = ConstEvaluatedExprVisitor<GlobVarInitChecker>;
783 SemaCUDA &SCRef;
784 VarDecl *VD;
785 void CheckForWrongSidedCall(const FunctionDecl *FD) {
786 CUDAFunctionTarget InitFnTarget = SCRef.IdentifyTarget(D: FD);
787 if (InitFnTarget != CUDAFunctionTarget::Host &&
788 InitFnTarget != CUDAFunctionTarget::HostDevice &&
789 !VD->isInvalidDecl()) {
790 SCRef.Diag(Loc: VD->getLocation(),
791 DiagID: diag::err_ref_bad_target_global_initializer)
792 << InitFnTarget << FD;
793 SCRef.Diag(Loc: FD->getLocation(), DiagID: 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>(Val: DRE->getDecl());
803 VarD && VarD->hasAttr<HIPManagedAttr>()) {
804 SCRef.Diag(Loc: DRE->getLocation(),
805 DiagID: diag::err_cuda_invalid_use_of_managedvar);
806 SCRef.Diag(Loc: VD->getLocation(),
807 DiagID: 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(FD: InitFn);
814 Base::VisitCallExpr(CE);
815 }
816
817 void VisitCXXConstructExpr(const CXXConstructExpr *CE) {
818 const CXXConstructorDecl *Ctor = CE->getConstructor();
819 if (Ctor) {
820 CheckForWrongSidedCall(FD: Ctor);
821 for (auto *I : Ctor->inits())
822 Visit(S: I->getInit());
823 }
824 Base::VisitCXXConstructExpr(S: CE);
825 }
826
827 void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
828 Visit(S: E->getExpr());
829 }
830
831 void VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
832 Visit(S: E->getExpr());
833 }
834 };
835 GlobVarInitChecker Checker(*this, VD);
836 Checker.Visit(S: Init);
837 }
838}
839
840void SemaCUDA::RecordImplicitHostDeviceFuncUsedByDevice(
841 const FunctionDecl *Callee) {
842 FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
843 if (!Caller)
844 return;
845
846 if (!isImplicitHostDeviceFunction(D: Callee))
847 return;
848
849 CUDAFunctionTarget CallerTarget = IdentifyTarget(D: 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 ||
855 (isImplicitHostDeviceFunction(D: Caller) &&
856 !getASTContext().CUDAImplicitHostDeviceFunUsedByDevice.count(V: Caller))))
857 return;
858
859 getASTContext().CUDAImplicitHostDeviceFunUsedByDevice.insert(V: Callee);
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).
874void SemaCUDA::maybeAddHostDeviceAttrs(FunctionDecl *NewD,
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(A: CUDAHostAttr::CreateImplicit(Ctx&: getASTContext()));
881 if (!NewD->hasAttr<CUDADeviceAttr>())
882 NewD->addAttr(A: CUDADeviceAttr::CreateImplicit(Ctx&: 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>() &&
891 (NewD->getDescribedFunctionTemplate() ||
892 NewD->isFunctionTemplateSpecialization())) {
893 NewD->addAttr(A: CUDAHostAttr::CreateImplicit(Ctx&: getASTContext()));
894 NewD->addAttr(A: CUDADeviceAttr::CreateImplicit(Ctx&: 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>(Val: D))
907 D = Using->getTargetDecl();
908 FunctionDecl *OldD = D->getAsFunction();
909 return OldD && OldD->hasAttr<CUDADeviceAttr>() &&
910 !OldD->hasAttr<CUDAHostAttr>() &&
911 !SemaRef.IsOverload(New: NewD, Old: OldD,
912 /* UseMemberUsingDeclRules = */ false,
913 /* ConsiderCudaAttrs = */ false);
914 };
915 auto It = llvm::find_if(Range: Previous, P: 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(Loc: Match->getLocation())) {
923 Diag(Loc: NewD->getLocation(),
924 DiagID: diag::err_cuda_unattributed_constexpr_cannot_overload_device)
925 << NewD;
926 Diag(Loc: Match->getLocation(),
927 DiagID: diag::note_cuda_conflicting_device_function_declared_here);
928 }
929 return;
930 }
931
932 NewD->addAttr(A: CUDAHostAttr::CreateImplicit(Ctx&: getASTContext()));
933 NewD->addAttr(A: CUDADeviceAttr::CreateImplicit(Ctx&: 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.
939void SemaCUDA::MaybeAddConstantAttr(VarDecl *VD) {
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(S&: *this, VD,
948 CheckKind: CICK_DeviceOrConstant))) {
949 VD->addAttr(A: CUDAConstantAttr::CreateImplicit(Ctx&: getASTContext()));
950 }
951}
952
953SemaBase::SemaDiagnosticBuilder SemaCUDA::DiagIfDeviceCode(SourceLocation Loc,
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()) {
962 case CUDAFunctionTarget::Global:
963 case CUDAFunctionTarget::Device:
964 return SemaDiagnosticBuilder::K_Immediate;
965 case CUDAFunctionTarget::HostDevice:
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(FD: CurFunContext))
975 return SemaDiagnosticBuilder::K_Deferred;
976 return (SemaRef.getEmissionStatus(Decl: CurFunContext) ==
977 Sema::FunctionEmissionStatus::Emitted)
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
987Sema::SemaDiagnosticBuilder SemaCUDA::DiagIfHostCode(SourceLocation Loc,
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()) {
996 case CUDAFunctionTarget::Host:
997 return SemaDiagnosticBuilder::K_Immediate;
998 case CUDAFunctionTarget::HostDevice:
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(Decl: CurFunContext) ==
1008 Sema::FunctionEmissionStatus::Emitted)
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
1018bool SemaCUDA::CheckCall(SourceLocation Loc, FunctionDecl *Callee) {
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>(Val: 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(Decl: Caller) ==
1042 Sema::FunctionEmissionStatus::Emitted;
1043 bool CallerIsImplicitHDExplicitInst =
1044 isImplicitHDExplicitInstantiation(FD: Caller);
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(Decl: Caller) ==
1067 Sema::FunctionEmissionStatus::TemplateDiscarded;
1068 bool RDC = getLangOpts().GPURelocatableDeviceCode;
1069 if (IsDeviceKernelCall && !(CallerHD && CallerDiscard) && !RDC) {
1070 Diag(Loc, DiagID: 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(FD: Caller) ==
1080 GVA_StrongExternal)))
1081 getASTContext().CUDAExternalDeviceDeclODRUsedByHost.insert(X: Callee);
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(V: {.FD: Caller, .Loc: Loc}).second)
1090 return true;
1091
1092 SemaDiagnosticBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller,
1093 SemaRef)
1094 << IdentifyTarget(D: Callee) << /*function*/ 0 << Callee
1095 << IdentifyTarget(D: 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.
1109void SemaCUDA::CheckLambdaCapture(CXXMethodDecl *Callee,
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) {
1140 SemaDiagnosticBuilder(DiagKind, Capture.getLocation(),
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.
1148 SemaDiagnosticBuilder(DiagKind, Capture.getLocation(),
1149 diag::warn_maybe_capture_bad_target_this_ptr, Callee,
1150 SemaRef);
1151 }
1152}
1153
1154void SemaCUDA::SetLambdaAttrs(CXXMethodDecl *Method) {
1155 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
1156 if (Method->hasAttr<CUDAHostAttr>() || Method->hasAttr<CUDADeviceAttr>())
1157 return;
1158 Method->addAttr(A: CUDADeviceAttr::CreateImplicit(Ctx&: getASTContext()));
1159 Method->addAttr(A: CUDAHostAttr::CreateImplicit(Ctx&: getASTContext()));
1160}
1161
1162void SemaCUDA::checkTargetOverload(FunctionDecl *NewFD,
1163 const LookupResult &Previous) {
1164 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
1165 CUDAFunctionTarget NewTarget = IdentifyTarget(D: NewFD);
1166 for (NamedDecl *OldND : Previous) {
1167 FunctionDecl *OldFD = OldND->getAsFunction();
1168 if (!OldFD)
1169 continue;
1170
1171 CUDAFunctionTarget OldTarget = IdentifyTarget(D: 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(New: NewFD, Old: OldFD, /* UseMemberUsingDeclRules = */ false,
1179 /* ConsiderCudaAttrs = */ false)) {
1180 if ((NewTarget == CUDAFunctionTarget::HostDevice &&
1181 !(getLangOpts().OffloadImplicitHostDeviceTemplates &&
1182 isImplicitHostDeviceFunction(D: NewFD) &&
1183 OldTarget == CUDAFunctionTarget::Device)) ||
1184 (OldTarget == CUDAFunctionTarget::HostDevice &&
1185 !(getLangOpts().OffloadImplicitHostDeviceTemplates &&
1186 isImplicitHostDeviceFunction(D: OldFD) &&
1187 NewTarget == CUDAFunctionTarget::Device)) ||
1188 (NewTarget == CUDAFunctionTarget::Global) ||
1189 (OldTarget == CUDAFunctionTarget::Global)) {
1190 Diag(Loc: NewFD->getLocation(), DiagID: diag::err_cuda_ovl_target)
1191 << NewTarget << NewFD->getDeclName() << OldTarget << OldFD;
1192 Diag(Loc: OldFD->getLocation(), DiagID: 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(Loc: NewFD->getLocation(), DiagID: diag::warn_offload_incompatible_redeclare)
1201 << NewTarget << OldTarget;
1202 Diag(Loc: OldFD->getLocation(), DiagID: diag::note_previous_declaration);
1203 }
1204 }
1205 }
1206}
1207
1208template <typename AttrTy>
1209static void copyAttrIfPresent(Sema &S, FunctionDecl *FD,
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(A: Clone);
1217 }
1218}
1219
1220void SemaCUDA::inheritTargetAttrs(FunctionDecl *FD,
1221 const FunctionTemplateDecl &TD) {
1222 const FunctionDecl &TemplateFD = *TD.getTemplatedDecl();
1223 copyAttrIfPresent<CUDAGlobalAttr>(S&: SemaRef, FD, TemplateFD);
1224 copyAttrIfPresent<CUDAHostAttr>(S&: SemaRef, FD, TemplateFD);
1225 copyAttrIfPresent<CUDADeviceAttr>(S&: SemaRef, FD, TemplateFD);
1226}
1227
1228std::string SemaCUDA::getConfigureFuncName() const {
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(),
1238 CudaFeature::CUDA_USES_NEW_LAUNCH))
1239 return "__cudaPushCallConfiguration";
1240
1241 // Legacy CUDA kernel configuration call
1242 return "cudaConfigureCall";
1243}
1244
1245std::string SemaCUDA::getGetParameterBufferFuncName() const {
1246 return "cudaGetParameterBuffer";
1247}
1248
1249std::string SemaCUDA::getLaunchDeviceFuncName() const {
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.
1255void SemaCUDA::recordPotentialODRUsedVariable(
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>(Val: Arguments[I]);
1263 if (!DeclRef)
1264 continue;
1265 auto *Variable = dyn_cast<VarDecl>(Val: 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
1277 CUDAFunctionTarget Target = IdentifyTarget(D: Callee);
1278 if (Target == CUDAFunctionTarget::InvalidTarget ||
1279 Target == CUDAFunctionTarget::Global)
1280 continue;
1281
1282 bool CoversHost = (Target == CUDAFunctionTarget::Host ||
1283 Target == CUDAFunctionTarget::HostDevice);
1284 bool CoversDevice = (Target == CUDAFunctionTarget::Device ||
1285 Target == CUDAFunctionTarget::HostDevice);
1286
1287 bool IsRef = Callee->getParamDecl(i: 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(Ptr: Variable);
1296 }
1297}
1298