clang 20.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"
22#include "clang/Sema/Sema.h"
25#include "clang/Sema/Template.h"
26#include "llvm/ADT/STLForwardCompat.h"
27#include "llvm/ADT/SmallVector.h"
28#include <optional>
29using namespace clang;
30
32
33template <typename AttrT> static bool hasExplicitAttr(const VarDecl *D) {
34 if (!D)
35 return false;
36 if (auto *A = D->getAttr<AttrT>())
37 return !A->isImplicit();
38 return false;
39}
40
42 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
43 ForceHostDeviceDepth++;
44}
45
47 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
48 if (ForceHostDeviceDepth == 0)
49 return false;
50 ForceHostDeviceDepth--;
51 return true;
52}
53
55 MultiExprArg ExecConfig,
56 SourceLocation GGGLoc) {
58 if (!ConfigDecl)
59 return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use)
61 QualType ConfigQTy = ConfigDecl->getType();
62
63 DeclRefExpr *ConfigDR = new (getASTContext()) DeclRefExpr(
64 getASTContext(), ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc);
65 SemaRef.MarkFunctionReferenced(LLLLoc, ConfigDecl);
66
67 return SemaRef.BuildCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, nullptr,
68 /*IsExecConfig=*/true);
69}
70
72 bool HasHostAttr = false;
73 bool HasDeviceAttr = false;
74 bool HasGlobalAttr = false;
75 bool HasInvalidTargetAttr = false;
76 for (const ParsedAttr &AL : Attrs) {
77 switch (AL.getKind()) {
78 case ParsedAttr::AT_CUDAGlobal:
79 HasGlobalAttr = true;
80 break;
81 case ParsedAttr::AT_CUDAHost:
82 HasHostAttr = true;
83 break;
84 case ParsedAttr::AT_CUDADevice:
85 HasDeviceAttr = true;
86 break;
87 case ParsedAttr::AT_CUDAInvalidTarget:
88 HasInvalidTargetAttr = true;
89 break;
90 default:
91 break;
92 }
93 }
94
95 if (HasInvalidTargetAttr)
97
98 if (HasGlobalAttr)
100
101 if (HasHostAttr && HasDeviceAttr)
103
104 if (HasDeviceAttr)
106
108}
109
110template <typename A>
111static bool hasAttr(const Decl *D, bool IgnoreImplicitAttr) {
112 return D->hasAttrs() && llvm::any_of(D->getAttrs(), [&](Attr *Attribute) {
113 return isa<A>(Attribute) &&
114 !(IgnoreImplicitAttr && Attribute->isImplicit());
115 });
116}
117
120 : S(S_) {
122 assert(K == SemaCUDA::CTCK_InitGlobalVar);
123 auto *VD = dyn_cast_or_null<VarDecl>(D);
124 if (VD && VD->hasGlobalStorage() && !VD->isStaticLocal()) {
126 if ((hasAttr<CUDADeviceAttr>(VD, /*IgnoreImplicit=*/true) &&
127 !hasAttr<CUDAHostAttr>(VD, /*IgnoreImplicit=*/true)) ||
128 hasAttr<CUDASharedAttr>(VD, /*IgnoreImplicit=*/true) ||
129 hasAttr<CUDAConstantAttr>(VD, /*IgnoreImplicit=*/true))
131 S.CurCUDATargetCtx = {Target, K, VD};
132 }
133}
134
135/// IdentifyTarget - Determine the CUDA compilation target for this function
137 bool IgnoreImplicitHDAttr) {
138 // Code that lives outside a function gets the target from CurCUDATargetCtx.
139 if (D == nullptr)
141
142 if (D->hasAttr<CUDAInvalidTargetAttr>())
144
145 if (D->hasAttr<CUDAGlobalAttr>())
147
148 if (hasAttr<CUDADeviceAttr>(D, IgnoreImplicitHDAttr)) {
149 if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr))
152 } else if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr)) {
154 } else if ((D->isImplicit() || !D->isUserProvided()) &&
155 !IgnoreImplicitHDAttr) {
156 // Some implicit declarations (like intrinsic functions) are not marked.
157 // Set the most lenient target on them for maximal flexibility.
159 }
160
162}
163
164/// IdentifyTarget - Determine the CUDA compilation target for this variable.
166 if (Var->hasAttr<HIPManagedAttr>())
167 return CVT_Unified;
168 // Only constexpr and const variabless with implicit constant attribute
169 // are emitted on both sides. Such variables are promoted to device side
170 // only if they have static constant intializers on device side.
171 if ((Var->isConstexpr() || Var->getType().isConstQualified()) &&
172 Var->hasAttr<CUDAConstantAttr>() &&
173 !hasExplicitAttr<CUDAConstantAttr>(Var))
174 return CVT_Both;
175 if (Var->hasAttr<CUDADeviceAttr>() || Var->hasAttr<CUDAConstantAttr>() ||
176 Var->hasAttr<CUDASharedAttr>() ||
179 return CVT_Device;
180 // Function-scope static variable without explicit device or constant
181 // attribute are emitted
182 // - on both sides in host device functions
183 // - on device side in device or global functions
184 if (auto *FD = dyn_cast<FunctionDecl>(Var->getDeclContext())) {
185 switch (IdentifyTarget(FD)) {
187 return CVT_Both;
190 return CVT_Device;
191 default:
192 return CVT_Host;
193 }
194 }
195 return CVT_Host;
196}
197
198// * CUDA Call preference table
199//
200// F - from,
201// T - to
202// Ph - preference in host mode
203// Pd - preference in device mode
204// H - handled in (x)
205// Preferences: N:native, SS:same side, HD:host-device, WS:wrong side, --:never.
206//
207// | F | T | Ph | Pd | H |
208// |----+----+-----+-----+-----+
209// | d | d | N | N | (c) |
210// | d | g | -- | -- | (a) |
211// | d | h | -- | -- | (e) |
212// | d | hd | HD | HD | (b) |
213// | g | d | N | N | (c) |
214// | g | g | -- | -- | (a) |
215// | g | h | -- | -- | (e) |
216// | g | hd | HD | HD | (b) |
217// | h | d | -- | -- | (e) |
218// | h | g | N | N | (c) |
219// | h | h | N | N | (c) |
220// | h | hd | HD | HD | (b) |
221// | hd | d | WS | SS | (d) |
222// | hd | g | SS | -- |(d/a)|
223// | hd | h | SS | WS | (d) |
224// | hd | hd | HD | HD | (b) |
225
228 const FunctionDecl *Callee) {
229 assert(Callee && "Callee must be valid.");
230
231 // Treat ctor/dtor as host device function in device var initializer to allow
232 // trivial ctor/dtor without device attr to be used. Non-trivial ctor/dtor
233 // will be diagnosed by checkAllowedInitializer.
234 if (Caller == nullptr && CurCUDATargetCtx.Kind == CTCK_InitGlobalVar &&
236 (isa<CXXConstructorDecl>(Callee) || isa<CXXDestructorDecl>(Callee)))
237 return CFP_HostDevice;
238
239 CUDAFunctionTarget CallerTarget = IdentifyTarget(Caller);
240 CUDAFunctionTarget CalleeTarget = IdentifyTarget(Callee);
241
242 // If one of the targets is invalid, the check always fails, no matter what
243 // the other target is.
244 if (CallerTarget == CUDAFunctionTarget::InvalidTarget ||
245 CalleeTarget == CUDAFunctionTarget::InvalidTarget)
246 return CFP_Never;
247
248 // (a) Can't call global from some contexts until we support CUDA's
249 // dynamic parallelism.
250 if (CalleeTarget == CUDAFunctionTarget::Global &&
251 (CallerTarget == CUDAFunctionTarget::Global ||
252 CallerTarget == CUDAFunctionTarget::Device))
253 return CFP_Never;
254
255 // (b) Calling HostDevice is OK for everyone.
256 if (CalleeTarget == CUDAFunctionTarget::HostDevice)
257 return CFP_HostDevice;
258
259 // (c) Best case scenarios
260 if (CalleeTarget == CallerTarget ||
261 (CallerTarget == CUDAFunctionTarget::Host &&
262 CalleeTarget == CUDAFunctionTarget::Global) ||
263 (CallerTarget == CUDAFunctionTarget::Global &&
264 CalleeTarget == CUDAFunctionTarget::Device))
265 return CFP_Native;
266
267 // HipStdPar mode is special, in that assessing whether a device side call to
268 // a host target is deferred to a subsequent pass, and cannot unambiguously be
269 // adjudicated in the AST, hence we optimistically allow them to pass here.
270 if (getLangOpts().HIPStdPar &&
271 (CallerTarget == CUDAFunctionTarget::Global ||
272 CallerTarget == CUDAFunctionTarget::Device ||
273 CallerTarget == CUDAFunctionTarget::HostDevice) &&
274 CalleeTarget == CUDAFunctionTarget::Host)
275 return CFP_HostDevice;
276
277 // (d) HostDevice behavior depends on compilation mode.
278 if (CallerTarget == CUDAFunctionTarget::HostDevice) {
279 // It's OK to call a compilation-mode matching function from an HD one.
280 if ((getLangOpts().CUDAIsDevice &&
281 CalleeTarget == CUDAFunctionTarget::Device) ||
282 (!getLangOpts().CUDAIsDevice &&
283 (CalleeTarget == CUDAFunctionTarget::Host ||
284 CalleeTarget == CUDAFunctionTarget::Global)))
285 return CFP_SameSide;
286
287 // Calls from HD to non-mode-matching functions (i.e., to host functions
288 // when compiling in device mode or to device functions when compiling in
289 // host mode) are allowed at the sema level, but eventually rejected if
290 // they're ever codegened. TODO: Reject said calls earlier.
291 return CFP_WrongSide;
292 }
293
294 // (e) Calling across device/host boundary is not something you should do.
295 if ((CallerTarget == CUDAFunctionTarget::Host &&
296 CalleeTarget == CUDAFunctionTarget::Device) ||
297 (CallerTarget == CUDAFunctionTarget::Device &&
298 CalleeTarget == CUDAFunctionTarget::Host) ||
299 (CallerTarget == CUDAFunctionTarget::Global &&
300 CalleeTarget == CUDAFunctionTarget::Host))
301 return CFP_Never;
302
303 llvm_unreachable("All cases should've been handled by now.");
304}
305
306template <typename AttrT> static bool hasImplicitAttr(const FunctionDecl *D) {
307 if (!D)
308 return false;
309 if (auto *A = D->getAttr<AttrT>())
310 return A->isImplicit();
311 return D->isImplicit();
312}
313
315 bool IsImplicitDevAttr = hasImplicitAttr<CUDADeviceAttr>(D);
316 bool IsImplicitHostAttr = hasImplicitAttr<CUDAHostAttr>(D);
317 return IsImplicitDevAttr && IsImplicitHostAttr;
318}
319
321 const FunctionDecl *Caller,
322 SmallVectorImpl<std::pair<DeclAccessPair, FunctionDecl *>> &Matches) {
323 if (Matches.size() <= 1)
324 return;
325
326 using Pair = std::pair<DeclAccessPair, FunctionDecl*>;
327
328 // Gets the CUDA function preference for a call from Caller to Match.
329 auto GetCFP = [&](const Pair &Match) {
330 return IdentifyPreference(Caller, Match.second);
331 };
332
333 // Find the best call preference among the functions in Matches.
334 CUDAFunctionPreference BestCFP = GetCFP(*std::max_element(
335 Matches.begin(), Matches.end(),
336 [&](const Pair &M1, const Pair &M2) { return GetCFP(M1) < GetCFP(M2); }));
337
338 // Erase all functions with lower priority.
339 llvm::erase_if(Matches,
340 [&](const Pair &Match) { return GetCFP(Match) < BestCFP; });
341}
342
343/// When an implicitly-declared special member has to invoke more than one
344/// base/field special member, conflicts may occur in the targets of these
345/// members. For example, if one base's member __host__ and another's is
346/// __device__, it's a conflict.
347/// This function figures out if the given targets \param Target1 and
348/// \param Target2 conflict, and if they do not it fills in
349/// \param ResolvedTarget with a target that resolves for both calls.
350/// \return true if there's a conflict, false otherwise.
351static bool
353 CUDAFunctionTarget Target2,
354 CUDAFunctionTarget *ResolvedTarget) {
355 // Only free functions and static member functions may be global.
356 assert(Target1 != CUDAFunctionTarget::Global);
357 assert(Target2 != CUDAFunctionTarget::Global);
358
359 if (Target1 == CUDAFunctionTarget::HostDevice) {
360 *ResolvedTarget = Target2;
361 } else if (Target2 == CUDAFunctionTarget::HostDevice) {
362 *ResolvedTarget = Target1;
363 } else if (Target1 != Target2) {
364 return true;
365 } else {
366 *ResolvedTarget = Target1;
367 }
368
369 return false;
370}
371
374 CXXMethodDecl *MemberDecl,
375 bool ConstRHS,
376 bool Diagnose) {
377 // If the defaulted special member is defined lexically outside of its
378 // owning class, or the special member already has explicit device or host
379 // attributes, do not infer.
380 bool InClass = MemberDecl->getLexicalParent() == MemberDecl->getParent();
381 bool HasH = MemberDecl->hasAttr<CUDAHostAttr>();
382 bool HasD = MemberDecl->hasAttr<CUDADeviceAttr>();
383 bool HasExplicitAttr =
384 (HasD && !MemberDecl->getAttr<CUDADeviceAttr>()->isImplicit()) ||
385 (HasH && !MemberDecl->getAttr<CUDAHostAttr>()->isImplicit());
386 if (!InClass || HasExplicitAttr)
387 return false;
388
389 std::optional<CUDAFunctionTarget> InferredTarget;
390
391 // We're going to invoke special member lookup; mark that these special
392 // members are called from this one, and not from its caller.
393 Sema::ContextRAII MethodContext(SemaRef, MemberDecl);
394
395 // Look for special members in base classes that should be invoked from here.
396 // Infer the target of this member base on the ones it should call.
397 // Skip direct and indirect virtual bases for abstract classes.
399 for (const auto &B : ClassDecl->bases()) {
400 if (!B.isVirtual()) {
401 Bases.push_back(&B);
402 }
403 }
404
405 if (!ClassDecl->isAbstract()) {
406 llvm::append_range(Bases, llvm::make_pointer_range(ClassDecl->vbases()));
407 }
408
409 for (const auto *B : Bases) {
410 const RecordType *BaseType = B->getType()->getAs<RecordType>();
411 if (!BaseType) {
412 continue;
413 }
414
415 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl());
417 SemaRef.LookupSpecialMember(BaseClassDecl, CSM,
418 /* ConstArg */ ConstRHS,
419 /* VolatileArg */ false,
420 /* RValueThis */ false,
421 /* ConstThis */ false,
422 /* VolatileThis */ false);
423
424 if (!SMOR.getMethod())
425 continue;
426
427 CUDAFunctionTarget BaseMethodTarget = IdentifyTarget(SMOR.getMethod());
428 if (!InferredTarget) {
429 InferredTarget = BaseMethodTarget;
430 } else {
431 bool ResolutionError = resolveCalleeCUDATargetConflict(
432 *InferredTarget, BaseMethodTarget, &*InferredTarget);
433 if (ResolutionError) {
434 if (Diagnose) {
435 Diag(ClassDecl->getLocation(),
436 diag::note_implicit_member_target_infer_collision)
437 << (unsigned)CSM << llvm::to_underlying(*InferredTarget)
438 << llvm::to_underlying(BaseMethodTarget);
439 }
440 MemberDecl->addAttr(
441 CUDAInvalidTargetAttr::CreateImplicit(getASTContext()));
442 return true;
443 }
444 }
445 }
446
447 // Same as for bases, but now for special members of fields.
448 for (const auto *F : ClassDecl->fields()) {
449 if (F->isInvalidDecl()) {
450 continue;
451 }
452
453 const RecordType *FieldType =
455 if (!FieldType) {
456 continue;
457 }
458
459 CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(FieldType->getDecl());
461 SemaRef.LookupSpecialMember(FieldRecDecl, CSM,
462 /* ConstArg */ ConstRHS && !F->isMutable(),
463 /* VolatileArg */ false,
464 /* RValueThis */ false,
465 /* ConstThis */ false,
466 /* VolatileThis */ false);
467
468 if (!SMOR.getMethod())
469 continue;
470
471 CUDAFunctionTarget FieldMethodTarget = IdentifyTarget(SMOR.getMethod());
472 if (!InferredTarget) {
473 InferredTarget = FieldMethodTarget;
474 } else {
475 bool ResolutionError = resolveCalleeCUDATargetConflict(
476 *InferredTarget, FieldMethodTarget, &*InferredTarget);
477 if (ResolutionError) {
478 if (Diagnose) {
479 Diag(ClassDecl->getLocation(),
480 diag::note_implicit_member_target_infer_collision)
481 << (unsigned)CSM << llvm::to_underlying(*InferredTarget)
482 << llvm::to_underlying(FieldMethodTarget);
483 }
484 MemberDecl->addAttr(
485 CUDAInvalidTargetAttr::CreateImplicit(getASTContext()));
486 return true;
487 }
488 }
489 }
490
491
492 // If no target was inferred, mark this member as __host__ __device__;
493 // it's the least restrictive option that can be invoked from any target.
494 bool NeedsH = true, NeedsD = true;
495 if (InferredTarget) {
496 if (*InferredTarget == CUDAFunctionTarget::Device)
497 NeedsH = false;
498 else if (*InferredTarget == CUDAFunctionTarget::Host)
499 NeedsD = false;
500 }
501
502 // We either setting attributes first time, or the inferred ones must match
503 // previously set ones.
504 if (NeedsD && !HasD)
505 MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));
506 if (NeedsH && !HasH)
507 MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));
508
509 return false;
510}
511
513 if (!CD->isDefined() && CD->isTemplateInstantiation())
515
516 // (E.2.3.1, CUDA 7.5) A constructor for a class type is considered
517 // empty at a point in the translation unit, if it is either a
518 // trivial constructor
519 if (CD->isTrivial())
520 return true;
521
522 // ... or it satisfies all of the following conditions:
523 // The constructor function has been defined.
524 // The constructor function has no parameters,
525 // and the function body is an empty compound statement.
526 if (!(CD->hasTrivialBody() && CD->getNumParams() == 0))
527 return false;
528
529 // Its class has no virtual functions and no virtual base classes.
530 if (CD->getParent()->isDynamicClass())
531 return false;
532
533 // Union ctor does not call ctors of its data members.
534 if (CD->getParent()->isUnion())
535 return true;
536
537 // The only form of initializer allowed is an empty constructor.
538 // This will recursively check all base classes and member initializers
539 if (!llvm::all_of(CD->inits(), [&](const CXXCtorInitializer *CI) {
540 if (const CXXConstructExpr *CE =
541 dyn_cast<CXXConstructExpr>(CI->getInit()))
542 return isEmptyConstructor(Loc, CE->getConstructor());
543 return false;
544 }))
545 return false;
546
547 return true;
548}
549
551 // No destructor -> no problem.
552 if (!DD)
553 return true;
554
555 if (!DD->isDefined() && DD->isTemplateInstantiation())
557
558 // (E.2.3.1, CUDA 7.5) A destructor for a class type is considered
559 // empty at a point in the translation unit, if it is either a
560 // trivial constructor
561 if (DD->isTrivial())
562 return true;
563
564 // ... or it satisfies all of the following conditions:
565 // The destructor function has been defined.
566 // and the function body is an empty compound statement.
567 if (!DD->hasTrivialBody())
568 return false;
569
570 const CXXRecordDecl *ClassDecl = DD->getParent();
571
572 // Its class has no virtual functions and no virtual base classes.
573 if (ClassDecl->isDynamicClass())
574 return false;
575
576 // Union does not have base class and union dtor does not call dtors of its
577 // data members.
578 if (DD->getParent()->isUnion())
579 return true;
580
581 // Only empty destructors are allowed. This will recursively check
582 // destructors for all base classes...
583 if (!llvm::all_of(ClassDecl->bases(), [&](const CXXBaseSpecifier &BS) {
584 if (CXXRecordDecl *RD = BS.getType()->getAsCXXRecordDecl())
585 return isEmptyDestructor(Loc, RD->getDestructor());
586 return true;
587 }))
588 return false;
589
590 // ... and member fields.
591 if (!llvm::all_of(ClassDecl->fields(), [&](const FieldDecl *Field) {
592 if (CXXRecordDecl *RD = Field->getType()
593 ->getBaseElementTypeUnsafe()
594 ->getAsCXXRecordDecl())
595 return isEmptyDestructor(Loc, RD->getDestructor());
596 return true;
597 }))
598 return false;
599
600 return true;
601}
602
603namespace {
604enum CUDAInitializerCheckKind {
605 CICK_DeviceOrConstant, // Check initializer for device/constant variable
606 CICK_Shared, // Check initializer for shared variable
607};
608
609bool IsDependentVar(VarDecl *VD) {
610 if (VD->getType()->isDependentType())
611 return true;
612 if (const auto *Init = VD->getInit())
613 return Init->isValueDependent();
614 return false;
615}
616
617// Check whether a variable has an allowed initializer for a CUDA device side
618// variable with global storage. \p VD may be a host variable to be checked for
619// potential promotion to device side variable.
620//
621// CUDA/HIP allows only empty constructors as initializers for global
622// variables (see E.2.3.1, CUDA 7.5). The same restriction also applies to all
623// __shared__ variables whether they are local or not (they all are implicitly
624// static in CUDA). One exception is that CUDA allows constant initializers
625// for __constant__ and __device__ variables.
626bool HasAllowedCUDADeviceStaticInitializer(SemaCUDA &S, VarDecl *VD,
627 CUDAInitializerCheckKind CheckKind) {
628 assert(!VD->isInvalidDecl() && VD->hasGlobalStorage());
629 assert(!IsDependentVar(VD) && "do not check dependent var");
630 const Expr *Init = VD->getInit();
631 auto IsEmptyInit = [&](const Expr *Init) {
632 if (!Init)
633 return true;
634 if (const auto *CE = dyn_cast<CXXConstructExpr>(Init)) {
635 return S.isEmptyConstructor(VD->getLocation(), CE->getConstructor());
636 }
637 return false;
638 };
639 auto IsConstantInit = [&](const Expr *Init) {
640 assert(Init);
642 /*NoWronSidedVars=*/true);
643 return Init->isConstantInitializer(S.getASTContext(),
644 VD->getType()->isReferenceType());
645 };
646 auto HasEmptyDtor = [&](VarDecl *VD) {
647 if (const auto *RD = VD->getType()->getAsCXXRecordDecl())
648 return S.isEmptyDestructor(VD->getLocation(), RD->getDestructor());
649 return true;
650 };
651 if (CheckKind == CICK_Shared)
652 return IsEmptyInit(Init) && HasEmptyDtor(VD);
653 return S.getLangOpts().GPUAllowDeviceInit ||
654 ((IsEmptyInit(Init) || IsConstantInit(Init)) && HasEmptyDtor(VD));
655}
656} // namespace
657
659 // Return early if VD is inside a non-instantiated template function since
660 // the implicit constructor is not defined yet.
661 if (const FunctionDecl *FD =
662 dyn_cast_or_null<FunctionDecl>(VD->getDeclContext()))
663 if (FD->isDependentContext())
664 return;
665
666 // Do not check dependent variables since the ctor/dtor/initializer are not
667 // determined. Do it after instantiation.
668 if (VD->isInvalidDecl() || !VD->hasInit() || !VD->hasGlobalStorage() ||
669 IsDependentVar(VD))
670 return;
671 const Expr *Init = VD->getInit();
672 bool IsSharedVar = VD->hasAttr<CUDASharedAttr>();
673 bool IsDeviceOrConstantVar =
674 !IsSharedVar &&
675 (VD->hasAttr<CUDADeviceAttr>() || VD->hasAttr<CUDAConstantAttr>());
676 if (IsDeviceOrConstantVar || IsSharedVar) {
677 if (HasAllowedCUDADeviceStaticInitializer(
678 *this, VD, IsSharedVar ? CICK_Shared : CICK_DeviceOrConstant))
679 return;
680 Diag(VD->getLocation(),
681 IsSharedVar ? diag::err_shared_var_init : diag::err_dynamic_var_init)
682 << Init->getSourceRange();
683 VD->setInvalidDecl();
684 } else {
685 // This is a host-side global variable. Check that the initializer is
686 // callable from the host side.
687 const FunctionDecl *InitFn = nullptr;
688 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Init)) {
689 InitFn = CE->getConstructor();
690 } else if (const CallExpr *CE = dyn_cast<CallExpr>(Init)) {
691 InitFn = CE->getDirectCallee();
692 }
693 if (InitFn) {
694 CUDAFunctionTarget InitFnTarget = IdentifyTarget(InitFn);
695 if (InitFnTarget != CUDAFunctionTarget::Host &&
696 InitFnTarget != CUDAFunctionTarget::HostDevice) {
697 Diag(VD->getLocation(), diag::err_ref_bad_target_global_initializer)
698 << llvm::to_underlying(InitFnTarget) << InitFn;
699 Diag(InitFn->getLocation(), diag::note_previous_decl) << InitFn;
700 VD->setInvalidDecl();
701 }
702 }
703 }
704}
705
707 const FunctionDecl *Callee) {
708 FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
709 if (!Caller)
710 return;
711
712 if (!isImplicitHostDeviceFunction(Callee))
713 return;
714
715 CUDAFunctionTarget CallerTarget = IdentifyTarget(Caller);
716
717 // Record whether an implicit host device function is used on device side.
718 if (CallerTarget != CUDAFunctionTarget::Device &&
719 CallerTarget != CUDAFunctionTarget::Global &&
720 (CallerTarget != CUDAFunctionTarget::HostDevice ||
722 !getASTContext().CUDAImplicitHostDeviceFunUsedByDevice.count(Caller))))
723 return;
724
726}
727
728// With -fcuda-host-device-constexpr, an unattributed constexpr function is
729// treated as implicitly __host__ __device__, unless:
730// * it is a variadic function (device-side variadic functions are not
731// allowed), or
732// * a __device__ function with this signature was already declared, in which
733// case in which case we output an error, unless the __device__ decl is in a
734// system header, in which case we leave the constexpr function unattributed.
735//
736// In addition, all function decls are treated as __host__ __device__ when
737// ForceHostDeviceDepth > 0 (corresponding to code within a
738// #pragma clang force_cuda_host_device_begin/end
739// pair).
741 const LookupResult &Previous) {
742 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
743
744 if (ForceHostDeviceDepth > 0) {
745 if (!NewD->hasAttr<CUDAHostAttr>())
746 NewD->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));
747 if (!NewD->hasAttr<CUDADeviceAttr>())
748 NewD->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));
749 return;
750 }
751
752 // If a template function has no host/device/global attributes,
753 // make it implicitly host device function.
754 if (getLangOpts().OffloadImplicitHostDeviceTemplates &&
755 !NewD->hasAttr<CUDAHostAttr>() && !NewD->hasAttr<CUDADeviceAttr>() &&
756 !NewD->hasAttr<CUDAGlobalAttr>() &&
759 NewD->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));
760 NewD->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));
761 return;
762 }
763
764 if (!getLangOpts().CUDAHostDeviceConstexpr || !NewD->isConstexpr() ||
765 NewD->isVariadic() || NewD->hasAttr<CUDAHostAttr>() ||
766 NewD->hasAttr<CUDADeviceAttr>() || NewD->hasAttr<CUDAGlobalAttr>())
767 return;
768
769 // Is D a __device__ function with the same signature as NewD, ignoring CUDA
770 // attributes?
771 auto IsMatchingDeviceFn = [&](NamedDecl *D) {
772 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(D))
773 D = Using->getTargetDecl();
774 FunctionDecl *OldD = D->getAsFunction();
775 return OldD && OldD->hasAttr<CUDADeviceAttr>() &&
776 !OldD->hasAttr<CUDAHostAttr>() &&
777 !SemaRef.IsOverload(NewD, OldD,
778 /* UseMemberUsingDeclRules = */ false,
779 /* ConsiderCudaAttrs = */ false);
780 };
781 auto It = llvm::find_if(Previous, IsMatchingDeviceFn);
782 if (It != Previous.end()) {
783 // We found a __device__ function with the same name and signature as NewD
784 // (ignoring CUDA attrs). This is an error unless that function is defined
785 // in a system header, in which case we simply return without making NewD
786 // host+device.
787 NamedDecl *Match = *It;
789 Diag(NewD->getLocation(),
790 diag::err_cuda_unattributed_constexpr_cannot_overload_device)
791 << NewD;
792 Diag(Match->getLocation(),
793 diag::note_cuda_conflicting_device_function_declared_here);
794 }
795 return;
796 }
797
798 NewD->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));
799 NewD->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));
800}
801
802// TODO: `__constant__` memory may be a limited resource for certain targets.
803// A safeguard may be needed at the end of compilation pipeline if
804// `__constant__` memory usage goes beyond limit.
806 // Do not promote dependent variables since the cotr/dtor/initializer are
807 // not determined. Do it after instantiation.
808 if (getLangOpts().CUDAIsDevice && !VD->hasAttr<CUDAConstantAttr>() &&
809 !VD->hasAttr<CUDASharedAttr>() &&
810 (VD->isFileVarDecl() || VD->isStaticDataMember()) &&
811 !IsDependentVar(VD) &&
812 ((VD->isConstexpr() || VD->getType().isConstQualified()) &&
813 HasAllowedCUDADeviceStaticInitializer(*this, VD,
814 CICK_DeviceOrConstant))) {
815 VD->addAttr(CUDAConstantAttr::CreateImplicit(getASTContext()));
816 }
817}
818
820 unsigned DiagID) {
821 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
822 FunctionDecl *CurFunContext =
823 SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
824 SemaDiagnosticBuilder::Kind DiagKind = [&] {
825 if (!CurFunContext)
826 return SemaDiagnosticBuilder::K_Nop;
827 switch (CurrentTarget()) {
830 return SemaDiagnosticBuilder::K_Immediate;
832 // An HD function counts as host code if we're compiling for host, and
833 // device code if we're compiling for device. Defer any errors in device
834 // mode until the function is known-emitted.
835 if (!getLangOpts().CUDAIsDevice)
836 return SemaDiagnosticBuilder::K_Nop;
838 getDiagnostics().getDiagnosticIDs()->isBuiltinNote(DiagID))
839 return SemaDiagnosticBuilder::K_Immediate;
840 return (SemaRef.getEmissionStatus(CurFunContext) ==
842 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack
843 : SemaDiagnosticBuilder::K_Deferred;
844 default:
845 return SemaDiagnosticBuilder::K_Nop;
846 }
847 }();
848 return SemaDiagnosticBuilder(DiagKind, Loc, DiagID, CurFunContext, SemaRef);
849}
850
852 unsigned DiagID) {
853 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
854 FunctionDecl *CurFunContext =
855 SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
856 SemaDiagnosticBuilder::Kind DiagKind = [&] {
857 if (!CurFunContext)
858 return SemaDiagnosticBuilder::K_Nop;
859 switch (CurrentTarget()) {
861 return SemaDiagnosticBuilder::K_Immediate;
863 // An HD function counts as host code if we're compiling for host, and
864 // device code if we're compiling for device. Defer any errors in device
865 // mode until the function is known-emitted.
866 if (getLangOpts().CUDAIsDevice)
867 return SemaDiagnosticBuilder::K_Nop;
869 getDiagnostics().getDiagnosticIDs()->isBuiltinNote(DiagID))
870 return SemaDiagnosticBuilder::K_Immediate;
871 return (SemaRef.getEmissionStatus(CurFunContext) ==
873 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack
874 : SemaDiagnosticBuilder::K_Deferred;
875 default:
876 return SemaDiagnosticBuilder::K_Nop;
877 }
878 }();
879 return SemaDiagnosticBuilder(DiagKind, Loc, DiagID, CurFunContext, SemaRef);
880}
881
883 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
884 assert(Callee && "Callee may not be null.");
885
886 const auto &ExprEvalCtx = SemaRef.currentEvaluationContext();
887 if (ExprEvalCtx.isUnevaluated() || ExprEvalCtx.isConstantEvaluated())
888 return true;
889
890 // FIXME: Is bailing out early correct here? Should we instead assume that
891 // the caller is a global initializer?
892 FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
893 if (!Caller)
894 return true;
895
896 // If the caller is known-emitted, mark the callee as known-emitted.
897 // Otherwise, mark the call in our call graph so we can traverse it later.
898 bool CallerKnownEmitted = SemaRef.getEmissionStatus(Caller) ==
900 SemaDiagnosticBuilder::Kind DiagKind = [this, Caller, Callee,
901 CallerKnownEmitted] {
902 switch (IdentifyPreference(Caller, Callee)) {
903 case CFP_Never:
904 case CFP_WrongSide:
905 assert(Caller && "Never/wrongSide calls require a non-null caller");
906 // If we know the caller will be emitted, we know this wrong-side call
907 // will be emitted, so it's an immediate error. Otherwise, defer the
908 // error until we know the caller is emitted.
909 return CallerKnownEmitted
910 ? SemaDiagnosticBuilder::K_ImmediateWithCallStack
911 : SemaDiagnosticBuilder::K_Deferred;
912 default:
913 return SemaDiagnosticBuilder::K_Nop;
914 }
915 }();
916
917 if (DiagKind == SemaDiagnosticBuilder::K_Nop) {
918 // For -fgpu-rdc, keep track of external kernels used by host functions.
919 if (getLangOpts().CUDAIsDevice && getLangOpts().GPURelocatableDeviceCode &&
920 Callee->hasAttr<CUDAGlobalAttr>() && !Callee->isDefined() &&
921 (!Caller || (!Caller->getDescribedFunctionTemplate() &&
925 return true;
926 }
927
928 // Avoid emitting this error twice for the same location. Using a hashtable
929 // like this is unfortunate, but because we must continue parsing as normal
930 // after encountering a deferred error, it's otherwise very tricky for us to
931 // ensure that we only emit this deferred error once.
932 if (!LocsWithCUDACallDiags.insert({Caller, Loc}).second)
933 return true;
934
935 SemaDiagnosticBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller,
936 SemaRef)
937 << llvm::to_underlying(IdentifyTarget(Callee)) << /*function*/ 0 << Callee
938 << llvm::to_underlying(IdentifyTarget(Caller));
939 if (!Callee->getBuiltinID())
940 SemaDiagnosticBuilder(DiagKind, Callee->getLocation(),
941 diag::note_previous_decl, Caller, SemaRef)
942 << Callee;
943 return DiagKind != SemaDiagnosticBuilder::K_Immediate &&
944 DiagKind != SemaDiagnosticBuilder::K_ImmediateWithCallStack;
945}
946
947// Check the wrong-sided reference capture of lambda for CUDA/HIP.
948// A lambda function may capture a stack variable by reference when it is
949// defined and uses the capture by reference when the lambda is called. When
950// the capture and use happen on different sides, the capture is invalid and
951// should be diagnosed.
953 const sema::Capture &Capture) {
954 // In host compilation we only need to check lambda functions emitted on host
955 // side. In such lambda functions, a reference capture is invalid only
956 // if the lambda structure is populated by a device function or kernel then
957 // is passed to and called by a host function. However that is impossible,
958 // since a device function or kernel can only call a device function, also a
959 // kernel cannot pass a lambda back to a host function since we cannot
960 // define a kernel argument type which can hold the lambda before the lambda
961 // itself is defined.
962 if (!getLangOpts().CUDAIsDevice)
963 return;
964
965 // File-scope lambda can only do init captures for global variables, which
966 // results in passing by value for these global variables.
967 FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
968 if (!Caller)
969 return;
970
971 // In device compilation, we only need to check lambda functions which are
972 // emitted on device side. For such lambdas, a reference capture is invalid
973 // only if the lambda structure is populated by a host function then passed
974 // to and called in a device function or kernel.
975 bool CalleeIsDevice = Callee->hasAttr<CUDADeviceAttr>();
976 bool CallerIsHost =
977 !Caller->hasAttr<CUDAGlobalAttr>() && !Caller->hasAttr<CUDADeviceAttr>();
978 bool ShouldCheck = CalleeIsDevice && CallerIsHost;
979 if (!ShouldCheck || !Capture.isReferenceCapture())
980 return;
981 auto DiagKind = SemaDiagnosticBuilder::K_Deferred;
982 if (Capture.isVariableCapture() && !getLangOpts().HIPStdPar) {
984 diag::err_capture_bad_target, Callee, SemaRef)
985 << Capture.getVariable();
986 } else if (Capture.isThisCapture()) {
987 // Capture of this pointer is allowed since this pointer may be pointing to
988 // managed memory which is accessible on both device and host sides. It only
989 // results in invalid memory access if this pointer points to memory not
990 // accessible on device side.
992 diag::warn_maybe_capture_bad_target_this_ptr, Callee,
993 SemaRef);
994 }
995}
996
998 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
999 if (Method->hasAttr<CUDAHostAttr>() || Method->hasAttr<CUDADeviceAttr>())
1000 return;
1001 Method->addAttr(CUDADeviceAttr::CreateImplicit(getASTContext()));
1002 Method->addAttr(CUDAHostAttr::CreateImplicit(getASTContext()));
1003}
1004
1006 const LookupResult &Previous) {
1007 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
1008 CUDAFunctionTarget NewTarget = IdentifyTarget(NewFD);
1009 for (NamedDecl *OldND : Previous) {
1010 FunctionDecl *OldFD = OldND->getAsFunction();
1011 if (!OldFD)
1012 continue;
1013
1014 CUDAFunctionTarget OldTarget = IdentifyTarget(OldFD);
1015 // Don't allow HD and global functions to overload other functions with the
1016 // same signature. We allow overloading based on CUDA attributes so that
1017 // functions can have different implementations on the host and device, but
1018 // HD/global functions "exist" in some sense on both the host and device, so
1019 // should have the same implementation on both sides.
1020 if (NewTarget != OldTarget &&
1021 !SemaRef.IsOverload(NewFD, OldFD, /* UseMemberUsingDeclRules = */ false,
1022 /* ConsiderCudaAttrs = */ false)) {
1023 if ((NewTarget == CUDAFunctionTarget::HostDevice &&
1024 !(getLangOpts().OffloadImplicitHostDeviceTemplates &&
1026 OldTarget == CUDAFunctionTarget::Device)) ||
1027 (OldTarget == CUDAFunctionTarget::HostDevice &&
1028 !(getLangOpts().OffloadImplicitHostDeviceTemplates &&
1030 NewTarget == CUDAFunctionTarget::Device)) ||
1031 (NewTarget == CUDAFunctionTarget::Global) ||
1032 (OldTarget == CUDAFunctionTarget::Global)) {
1033 Diag(NewFD->getLocation(), diag::err_cuda_ovl_target)
1034 << llvm::to_underlying(NewTarget) << NewFD->getDeclName()
1035 << llvm::to_underlying(OldTarget) << OldFD;
1036 Diag(OldFD->getLocation(), diag::note_previous_declaration);
1037 NewFD->setInvalidDecl();
1038 break;
1039 }
1040 if ((NewTarget == CUDAFunctionTarget::Host &&
1041 OldTarget == CUDAFunctionTarget::Device) ||
1042 (NewTarget == CUDAFunctionTarget::Device &&
1043 OldTarget == CUDAFunctionTarget::Host)) {
1044 Diag(NewFD->getLocation(), diag::warn_offload_incompatible_redeclare)
1045 << llvm::to_underlying(NewTarget) << llvm::to_underlying(OldTarget);
1046 Diag(OldFD->getLocation(), diag::note_previous_declaration);
1047 }
1048 }
1049 }
1050}
1051
1052template <typename AttrTy>
1054 const FunctionDecl &TemplateFD) {
1055 if (AttrTy *Attribute = TemplateFD.getAttr<AttrTy>()) {
1056 AttrTy *Clone = Attribute->clone(S.Context);
1057 Clone->setInherited(true);
1058 FD->addAttr(Clone);
1059 }
1060}
1061
1063 const FunctionTemplateDecl &TD) {
1064 const FunctionDecl &TemplateFD = *TD.getTemplatedDecl();
1065 copyAttrIfPresent<CUDAGlobalAttr>(SemaRef, FD, TemplateFD);
1066 copyAttrIfPresent<CUDAHostAttr>(SemaRef, FD, TemplateFD);
1067 copyAttrIfPresent<CUDADeviceAttr>(SemaRef, FD, TemplateFD);
1068}
1069
1071 if (getLangOpts().OffloadViaLLVM)
1072 return "__llvmPushCallConfiguration";
1073
1074 if (getLangOpts().HIP)
1075 return getLangOpts().HIPUseNewLaunchAPI ? "__hipPushCallConfiguration"
1076 : "hipConfigureCall";
1077
1078 // New CUDA kernel launch sequence.
1079 if (CudaFeatureEnabled(getASTContext().getTargetInfo().getSDKVersion(),
1081 return "__cudaPushCallConfiguration";
1082
1083 // Legacy CUDA kernel configuration call
1084 return "cudaConfigureCall";
1085}
Defines the clang::ASTContext interface.
const Decl * D
static bool hasImplicitAttr(const ValueDecl *D)
Defines the clang::Expr interface and subclasses for C++ expressions.
llvm::MachO::Target Target
Definition: MachO.h:51
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:352
static bool hasAttr(const Decl *D, bool IgnoreImplicitAttr)
Definition: SemaCUDA.cpp:111
static void copyAttrIfPresent(Sema &S, FunctionDecl *FD, const FunctionDecl &TemplateFD)
Definition: SemaCUDA.cpp:1053
static bool hasExplicitAttr(const VarDecl *D)
Definition: SemaCUDA.cpp:33
This file declares semantic analysis for CUDA constructs.
SourceLocation Loc
Definition: SemaObjC.cpp:759
StateNode * Previous
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.
Definition: ASTContext.h:1199
GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD) const
llvm::DenseSet< const FunctionDecl * > CUDAImplicitHostDeviceFunUsedByDevice
Keep track of CUDA/HIP implicit host device functions used on device side in device compilation.
Definition: ASTContext.h:1203
FunctionDecl * getcudaConfigureCallDecl()
Definition: ASTContext.h:1471
Attr - This represents one attribute.
Definition: Attr.h:42
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1546
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2539
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2304
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2803
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2064
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition: DeclCXX.h:2190
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
base_class_range bases()
Definition: DeclCXX.h:620
base_class_range vbases()
Definition: DeclCXX.h:637
bool isAbstract() const
Determine whether this class has a pure virtual function.
Definition: DeclCXX.h:1226
bool isDynamicClass() const
Definition: DeclCXX.h:586
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2830
DeclContext * getLexicalParent()
getLexicalParent - Returns the containing lexical DeclContext.
Definition: DeclBase.h:2106
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1265
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
T * getAttr() const
Definition: DeclBase.h:580
bool hasAttrs() const
Definition: DeclBase.h:525
void addAttr(Attr *A)
Definition: DeclBase.cpp:1013
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition: DeclBase.h:600
void setInvalidDecl(bool Invalid=true)
setInvalidDecl - Indicates the Decl had a semantic error.
Definition: DeclBase.cpp:154
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition: DeclBase.cpp:249
bool isInvalidDecl() const
Definition: DeclBase.h:595
SourceLocation getLocation() const
Definition: DeclBase.h:446
DeclContext * getDeclContext()
Definition: DeclBase.h:455
AttrVec & getAttrs()
Definition: DeclBase.h:531
bool hasAttr() const
Definition: DeclBase.h:584
This represents one expression.
Definition: Expr.h:110
Represents a member of a struct/union/class.
Definition: Decl.h:3030
Represents a function declaration or definition.
Definition: Decl.h:1932
bool hasTrivialBody() const
Returns whether the function has a trivial body that does not require any specific codegen.
Definition: Decl.cpp:3155
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition: Decl.cpp:4040
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition: Decl.cpp:4028
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition: Decl.h:2302
bool isVariadic() const
Whether this function is variadic.
Definition: Decl.cpp:3077
bool isTemplateInstantiation() const
Determines if the given function was instantiated from a function template.
Definition: Decl.cpp:4092
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition: Decl.h:2395
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3678
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:3191
Declaration of a template function.
Definition: DeclTemplate.h:957
FunctionDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
Represents the results of name lookup.
Definition: Lookup.h:46
This represents a decl that may have a name.
Definition: Decl.h:249
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:315
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:129
A (possibly-)qualified type.
Definition: Type.h:941
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: Type.h:7823
field_range fields() const
Definition: Decl.h:4351
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5965
RecordDecl * getDecl() const
Definition: Type.h:5975
decl_type * getFirstDecl()
Return the first declaration of this declaration or itself if this is the only declaration.
Definition: Redeclarable.h:217
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:110
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Definition: SemaBase.cpp:60
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
void PushForceHostDevice()
Increments our count of the number of times we've seen a pragma forcing functions to be host device.
Definition: SemaCUDA.cpp:41
void checkAllowedInitializer(VarDecl *VD)
Definition: SemaCUDA.cpp:658
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:706
std::string getConfigureFuncName() const
Returns the name of the launch configuration function.
Definition: SemaCUDA.cpp:1070
bool PopForceHostDevice()
Decrements our count of the number of times we've seen a pragma forcing functions to be host device.
Definition: SemaCUDA.cpp:46
CUDAFunctionTarget IdentifyTarget(const FunctionDecl *D, bool IgnoreImplicitHDAttr=false)
Determines whether the given function is a CUDA device/host/kernel/etc.
Definition: SemaCUDA.cpp:136
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:740
ExprResult ActOnExecConfigExpr(Scope *S, SourceLocation LLLLoc, MultiExprArg ExecConfig, SourceLocation GGGLoc)
Definition: SemaCUDA.cpp:54
bool isEmptyConstructor(SourceLocation Loc, CXXConstructorDecl *CD)
Definition: SemaCUDA.cpp:512
bool isEmptyDestructor(SourceLocation Loc, CXXDestructorDecl *CD)
Definition: SemaCUDA.cpp:550
void checkTargetOverload(FunctionDecl *NewFD, const LookupResult &Previous)
Check whether NewFD is a valid overload for CUDA.
Definition: SemaCUDA.cpp:1005
CUDAFunctionTarget CurrentTarget()
Gets the CUDA target for the current context.
Definition: SemaCUDA.h:152
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:851
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:372
struct clang::SemaCUDA::CUDATargetContext CurCUDATargetCtx
CUDATargetContextKind
Defines kinds of CUDA global host/device context where a function may be called.
Definition: SemaCUDA.h:129
@ CTCK_InitGlobalVar
Unknown context.
Definition: SemaCUDA.h:131
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:819
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:882
void inheritTargetAttrs(FunctionDecl *FD, const FunctionTemplateDecl &TD)
Copies target attributes from the template TD to the function FD.
Definition: SemaCUDA.cpp:1062
static bool isImplicitHostDeviceFunction(const FunctionDecl *D)
Definition: SemaCUDA.cpp:314
void CheckLambdaCapture(CXXMethodDecl *D, const sema::Capture &Capture)
Definition: SemaCUDA.cpp:952
void MaybeAddConstantAttr(VarDecl *VD)
May add implicit CUDAConstantAttr attribute to VD, depending on VD and current compilation settings.
Definition: SemaCUDA.cpp:805
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:320
SemaCUDA(Sema &S)
Definition: SemaCUDA.cpp:31
void SetLambdaAttrs(CXXMethodDecl *Method)
Set device or host device attributes on the given lambda operator() method.
Definition: SemaCUDA.cpp:997
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:227
@ CVT_Host
Emitted on device side with a shadow variable on host side.
Definition: SemaCUDA.h:120
@ CVT_Both
Emitted on host side only.
Definition: SemaCUDA.h:121
@ CVT_Unified
Emitted on both sides with different addresses.
Definition: SemaCUDA.h:122
A RAII object to temporarily push a declaration context.
Definition: Sema.h:3023
SpecialMemberOverloadResult - The overloading result for a special member function.
Definition: Sema.h:8967
CXXMethodDecl * getMethod() const
Definition: Sema.h:8979
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:493
bool IsOverload(FunctionDecl *New, FunctionDecl *Old, bool UseMemberUsingDeclRules, bool ConsiderCudaAttrs=true)
bool IsLastErrorImmediate
Is the last error level diagnostic immediate.
Definition: Sema.h:1025
const ExpressionEvaluationContextRecord & currentEvaluationContext() const
Definition: Sema.h:6454
FunctionDecl * getCurFunctionDecl(bool AllowLambda=false) const
Returns a pointer to the innermost enclosing function, or nullptr if the current context is not insid...
Definition: Sema.cpp:1567
ASTContext & Context
Definition: Sema.h:962
ASTContext & getASTContext() const
Definition: Sema.h:560
const LangOptions & getLangOpts() const
Definition: Sema.h:553
ExprResult BuildCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, MultiExprArg ArgExprs, SourceLocation RParenLoc, Expr *ExecConfig=nullptr, bool IsExecConfig=false, bool AllowRecovery=false)
BuildCallExpr - Handle a call to Fn with the specified array of arguments.
Definition: SemaExpr.cpp:6408
FunctionEmissionStatus getEmissionStatus(const FunctionDecl *Decl, bool Final=false)
Definition: SemaDecl.cpp:20204
SourceManager & getSourceManager() const
Definition: Sema.h:558
void InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, FunctionDecl *Function, bool Recursive=false, bool DefinitionRequired=false, bool AtEndOfTU=false)
Instantiate the definition of the given function from its template.
SpecialMemberOverloadResult LookupSpecialMember(CXXRecordDecl *D, CXXSpecialMemberKind SM, bool ConstArg, bool VolatileArg, bool RValueThis, bool ConstThis, bool VolatileThis)
void MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func, bool MightBeOdrUse=true)
Mark a function referenced, and check whether it is odr-used (C++ [basic.def.odr]p2,...
Definition: SemaExpr.cpp:17862
Encodes a location in the source.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
bool isUnion() const
Definition: Decl.h:3767
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1882
bool isReferenceType() const
Definition: Type.h:8021
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
Definition: Type.cpp:4994
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition: Type.h:2695
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
Definition: Type.cpp:5001
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8540
Represents a shadow declaration implicitly introduced into a scope by a (resolved) using-declaration ...
Definition: DeclCXX.h:3324
QualType getType() const
Definition: Decl.h:678
Represents a variable declaration or definition.
Definition: Decl.h:879
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition: Decl.h:1510
bool hasInit() const
Definition: Decl.cpp:2380
bool isStaticDataMember() const
Determines whether this is a static data member.
Definition: Decl.h:1231
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition: Decl.h:1174
bool isFileVarDecl() const
Returns true for file scoped variable declaration.
Definition: Decl.h:1290
const Expr * getInit() const
Definition: Decl.h:1316
ValueDecl * getVariable() const
Definition: ScopeInfo.h:675
bool isVariableCapture() const
Definition: ScopeInfo.h:650
SourceLocation getLocation() const
Retrieve the location at which this variable was captured.
Definition: ScopeInfo.h:686
bool isThisCapture() const
Definition: ScopeInfo.h:649
bool isReferenceCapture() const
Definition: ScopeInfo.h:655
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
@ GVA_StrongExternal
Definition: Linkage.h:76
CUDAFunctionTarget
Definition: Cuda.h:140
bool CudaFeatureEnabled(llvm::VersionTuple, CudaFeature)
Definition: Cuda.cpp:251
ExprResult ExprError()
Definition: Ownership.h:264
CXXSpecialMemberKind
Kinds of C++ special members.
Definition: Sema.h:403
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition: Specifiers.h:139
SemaCUDA::CUDATargetContext SavedCtx
Definition: SemaCUDA.h:145
CUDATargetContextRAII(SemaCUDA &S_, SemaCUDA::CUDATargetContextKind K, Decl *D)
Definition: SemaCUDA.cpp:118
CUDATargetContextKind Kind
Definition: SemaCUDA.h:139