clang 22.0.0git
clang::SemaCUDA Class Reference

#include "clang/Sema/SemaCUDA.h"

Inheritance diagram for clang::SemaCUDA:
[legend]

Classes

struct  CUDATargetContext
 Define the current global CUDA host/device context where a function may be called. More...
struct  CUDATargetContextRAII
struct  FunctionDeclAndLoc
 A pair of a canonical FunctionDecl and a SourceLocation. More...

Public Types

enum  CUDAVariableTarget { CVT_Device , CVT_Host , CVT_Both , CVT_Unified }
enum  CUDATargetContextKind { CTCK_Unknown , CTCK_InitGlobalVar }
 Defines kinds of CUDA global host/device context where a function may be called. More...
enum  CUDAFunctionPreference {
  CFP_Never , CFP_WrongSide , CFP_HostDevice , CFP_SameSide ,
  CFP_Native
}

Public Member Functions

 SemaCUDA (Sema &S)
void PushForceHostDevice ()
 Increments our count of the number of times we've seen a pragma forcing functions to be host device.
bool PopForceHostDevice ()
 Decrements our count of the number of times we've seen a pragma forcing functions to be host device.
ExprResult ActOnExecConfigExpr (Scope *S, SourceLocation LLLLoc, MultiExprArg ExecConfig, SourceLocation GGGLoc)
SemaDiagnosticBuilder DiagIfDeviceCode (SourceLocation Loc, unsigned DiagID)
 Creates a SemaDiagnosticBuilder that emits the diagnostic if the current context is "used as device code".
SemaDiagnosticBuilder DiagIfHostCode (SourceLocation Loc, unsigned DiagID)
 Creates a SemaDiagnosticBuilder that emits the diagnostic if the current context is "used as host code".
CUDAFunctionTarget IdentifyTarget (const FunctionDecl *D, bool IgnoreImplicitHDAttr=false)
 Determines whether the given function is a CUDA device/host/kernel/etc.
CUDAFunctionTarget IdentifyTarget (const ParsedAttributesView &Attrs)
CUDAVariableTarget IdentifyTarget (const VarDecl *D)
 Determines whether the given variable is emitted on host or device side.
CUDAFunctionTarget CurrentTarget ()
 Gets the CUDA target for the current context.
CUDAFunctionPreference IdentifyPreference (const FunctionDecl *Caller, const FunctionDecl *Callee)
 Identifies relative preference of a given Caller/Callee combination, based on their host/device attributes.
bool IsAllowedCall (const FunctionDecl *Caller, const FunctionDecl *Callee)
 Determines whether Caller may invoke Callee, based on their CUDA host/device attributes.
void maybeAddHostDeviceAttrs (FunctionDecl *FD, const LookupResult &Previous)
 May add implicit CUDAHostAttr and CUDADeviceAttr attributes to FD, depending on FD and the current compilation settings.
void MaybeAddConstantAttr (VarDecl *VD)
 May add implicit CUDAConstantAttr attribute to VD, depending on VD and current compilation settings.
bool CheckCall (SourceLocation Loc, FunctionDecl *Callee)
 Check whether we're allowed to call Callee from the current context.
void CheckLambdaCapture (CXXMethodDecl *D, const sema::Capture &Capture)
void SetLambdaAttrs (CXXMethodDecl *Method)
 Set device or host device attributes on the given lambda operator() method.
void RecordImplicitHostDeviceFuncUsedByDevice (const FunctionDecl *FD)
 Record FD if it is a CUDA/HIP implicit host device function used on device side in device compilation.
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 functions with lower calling priority.
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 base/field special members.
bool isEmptyConstructor (SourceLocation Loc, CXXConstructorDecl *CD)
bool isEmptyDestructor (SourceLocation Loc, CXXDestructorDecl *CD)
void checkAllowedInitializer (VarDecl *VD)
void checkTargetOverload (FunctionDecl *NewFD, const LookupResult &Previous)
 Check whether NewFD is a valid overload for CUDA.
void inheritTargetAttrs (FunctionDecl *FD, const FunctionTemplateDecl &TD)
 Copies target attributes from the template TD to the function FD.
std::string getConfigureFuncName () const
 Returns the name of the launch configuration function.
void recordPotentialODRUsedVariable (MultiExprArg Args, OverloadCandidateSet &CandidateSet)
 Record variables that are potentially ODR-used in CUDA/HIP.
Public Member Functions inherited from clang::SemaBase
 SemaBase (Sema &S)
ASTContextgetASTContext () const
DiagnosticsEnginegetDiagnostics () const
const LangOptionsgetLangOpts () const
DeclContextgetCurContext () const
SemaDiagnosticBuilder Diag (SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
 Emit a diagnostic.
SemaDiagnosticBuilder Diag (SourceLocation Loc, const PartialDiagnostic &PD, bool DeferHint=false)
 Emit a partial diagnostic.
SemaDiagnosticBuilder DiagCompat (SourceLocation Loc, unsigned CompatDiagId, bool DeferHint=false)
 Emit a compatibility diagnostic.
PartialDiagnostic PDiag (unsigned DiagID=0)
 Build a partial diagnostic.

Static Public Member Functions

static bool isImplicitHostDeviceFunction (const FunctionDecl *D)

Public Attributes

llvm::DenseSet< FunctionDeclAndLocLocsWithCUDACallDiags
 FunctionDecls and SourceLocations for which CheckCall has emitted a (maybe deferred) "bad call" diagnostic.
llvm::DenseMap< CanonicalDeclPtr< const FunctionDecl >, FunctionDeclAndLocDeviceKnownEmittedFns
 An inverse call graph, mapping known-emitted functions to one of their known-emitted callers (plus the location of the call).
struct clang::SemaCUDA::CUDATargetContext CurCUDATargetCtx
Public Attributes inherited from clang::SemaBase
SemaSemaRef

Friends

class ASTReader
class ASTWriter

Detailed Description

Definition at line 45 of file SemaCUDA.h.

Member Enumeration Documentation

◆ CUDAFunctionPreference

Enumerator
CFP_Never 
CFP_WrongSide 
CFP_HostDevice 
CFP_SameSide 
CFP_Native 

Definition at line 160 of file SemaCUDA.h.

◆ CUDATargetContextKind

Defines kinds of CUDA global host/device context where a function may be called.

Enumerator
CTCK_Unknown 
CTCK_InitGlobalVar 

Unknown context.

Definition at line 129 of file SemaCUDA.h.

◆ CUDAVariableTarget

Enumerator
CVT_Device 
CVT_Host 

Emitted on device side with a shadow variable on host side.

CVT_Both 

Emitted on host side only.

CVT_Unified 

Emitted on both sides with different addresses.

Definition at line 118 of file SemaCUDA.h.

Constructor & Destructor Documentation

◆ SemaCUDA()

SemaCUDA::SemaCUDA ( Sema & S)

Member Function Documentation

◆ ActOnExecConfigExpr()

◆ checkAllowedInitializer()

◆ CheckCall()

bool SemaCUDA::CheckCall ( SourceLocation Loc,
FunctionDecl * Callee )

Check whether we're allowed to call Callee from the current context.

  • If the call is never allowed in a semantically-correct program (CFP_Never), emits an error and returns false.
  • If the call is allowed in semantically-correct programs, but only if it's never codegen'ed (CFP_WrongSide), creates a deferred diagnostic to be emitted if and when the caller is codegen'ed, and returns true.

    Will only create deferred diagnostics for a given SourceLocation once, so you can safely call this multiple times without generating duplicate deferred errors.

  • Otherwise, returns true without emitting any diagnostics.

Definition at line 899 of file SemaCUDA.cpp.

References CFP_Never, CFP_WrongSide, clang::CUDA, clang::ASTContext::CUDAExternalDeviceDeclODRUsedByHost, clang::Sema::Emitted, clang::SemaBase::getASTContext(), clang::FunctionDecl::getDescribedFunctionTemplate(), clang::SemaBase::getLangOpts(), clang::GVA_StrongExternal, IdentifyPreference(), IdentifyTarget(), LocsWithCUDACallDiags, and clang::SemaBase::SemaRef.

Referenced by clang::Sema::MarkFunctionReferenced().

◆ CheckLambdaCapture()

◆ checkTargetOverload()

◆ CurrentTarget()

CUDAFunctionTarget clang::SemaCUDA::CurrentTarget ( )
inline

Gets the CUDA target for the current context.

Definition at line 152 of file SemaCUDA.h.

References IdentifyTarget(), and clang::SemaBase::SemaRef.

Referenced by clang::Sema::ActOnCXXTryBlock(), clang::Sema::BuildCXXThrow(), DiagIfDeviceCode(), DiagIfHostCode(), and handleSharedAttr().

◆ DiagIfDeviceCode()

SemaBase::SemaDiagnosticBuilder SemaCUDA::DiagIfDeviceCode ( SourceLocation Loc,
unsigned DiagID )

Creates a SemaDiagnosticBuilder that emits the diagnostic if the current context is "used as device code".

  • If CurContext is a host function, does not emit any diagnostics unless EmitOnBothSides is true.
  • If CurContext is a device or global function, emits the diagnostics immediately.
  • If CurContext is a host device function and we are compiling for the device, creates a diagnostic which is emitted if and when we realize that the function will be codegen'ed.

Example usage:

// Variable-length arrays are not allowed in CUDA device code. if (DiagIfDeviceCode(Loc, diag::err_cuda_vla) << CurrentTarget()) return ExprError(); // Otherwise, continue parsing as normal.

Definition at line 836 of file SemaCUDA.cpp.

References clang::CUDA, CurrentTarget(), clang::Device, clang::Sema::Emitted, clang::SemaBase::getDiagnostics(), clang::SemaBase::getLangOpts(), clang::Global, clang::HostDevice, and clang::SemaBase::SemaRef.

Referenced by clang::Sema::ActOnCXXTryBlock(), and clang::Sema::BuildCXXThrow().

◆ DiagIfHostCode()

Sema::SemaDiagnosticBuilder SemaCUDA::DiagIfHostCode ( SourceLocation Loc,
unsigned DiagID )

Creates a SemaDiagnosticBuilder that emits the diagnostic if the current context is "used as host code".

Same as DiagIfDeviceCode, with "host" and "device" switched.

Definition at line 868 of file SemaCUDA.cpp.

References clang::CUDA, CurrentTarget(), clang::Sema::Emitted, clang::SemaBase::getDiagnostics(), clang::SemaBase::getLangOpts(), clang::Host, clang::HostDevice, and clang::SemaBase::SemaRef.

Referenced by handleSharedAttr().

◆ EraseUnwantedMatches()

void SemaCUDA::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 functions with lower calling priority.

Definition at line 318 of file SemaCUDA.cpp.

References IdentifyPreference(), and clang::Match.

Referenced by clang::Sema::FindAllocationFunctions().

◆ getConfigureFuncName()

std::string SemaCUDA::getConfigureFuncName ( ) const

Returns the name of the launch configuration function.

This is the name of the function that will be called to configure kernel call, with the parameters specified via <<<>>>.

Definition at line 1086 of file SemaCUDA.cpp.

References clang::CUDA_USES_NEW_LAUNCH, clang::CudaFeatureEnabled(), clang::SemaBase::getASTContext(), clang::SemaBase::getLangOpts(), and clang::HIP.

Referenced by ActOnExecConfigExpr(), and clang::Sema::ActOnFunctionDeclarator().

◆ IdentifyPreference()

SemaCUDA::CUDAFunctionPreference SemaCUDA::IdentifyPreference ( const FunctionDecl * Caller,
const FunctionDecl * Callee )

Identifies relative preference of a given Caller/Callee combination, based on their host/device attributes.

Parameters
Callerfunction which needs address of Callee. nullptr in case of global context.
Calleetarget function
Returns
preference value for particular Caller/Callee combination.

Definition at line 225 of file SemaCUDA.cpp.

References CFP_HostDevice, CFP_Native, CFP_Never, CFP_SameSide, CFP_WrongSide, CTCK_InitGlobalVar, CurCUDATargetCtx, clang::Device, clang::SemaBase::getLangOpts(), clang::Global, clang::Host, clang::HostDevice, IdentifyTarget(), clang::InvalidTarget, and clang::isa().

Referenced by CheckCall(), EraseUnwantedMatches(), IsAllowedCall(), clang::isBetterOverloadCandidate(), and clang::Sema::isUsualDeallocationFunction().

◆ IdentifyTarget() [1/3]

◆ IdentifyTarget() [2/3]

CUDAFunctionTarget SemaCUDA::IdentifyTarget ( const ParsedAttributesView & Attrs)

◆ IdentifyTarget() [3/3]

SemaCUDA::CUDAVariableTarget SemaCUDA::IdentifyTarget ( const VarDecl * D)

◆ inferTargetForImplicitSpecialMember()

bool SemaCUDA::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 base/field special members.

Parameters
ClassDeclthe class for which the member is being created.
CSMthe kind of special member.
MemberDeclthe special member itself.
ConstRHStrue if this is a copy operation with a const object on its RHS.
Diagnosetrue if this call should emit diagnostics.
Returns
true if there was an error inferring. The result of this call is implicit CUDA target attribute(s) attached to the member declaration.

Definition at line 371 of file SemaCUDA.cpp.

References clang::Decl::addAttr(), clang::CXXRecordDecl::bases(), clang::Device, clang::SemaBase::Diag(), clang::RecordDecl::fields(), clang::Type::getAsCXXRecordDecl(), clang::SemaBase::getASTContext(), clang::Decl::getAttr(), clang::ASTContext::getBaseElementType(), clang::DeclContext::getLexicalParent(), clang::Decl::getLocation(), clang::Sema::SpecialMemberOverloadResult::getMethod(), clang::CXXMethodDecl::getParent(), clang::Decl::hasAttr(), clang::Host, IdentifyTarget(), clang::isa(), clang::CXXRecordDecl::isAbstract(), clang::CXXMethodDecl::isVirtual(), resolveCalleeCUDATargetConflict(), clang::SemaBase::SemaRef, clang::TSK_ExplicitInstantiationDeclaration, clang::TSK_ExplicitInstantiationDefinition, and clang::CXXRecordDecl::vbases().

Referenced by DiagnoseBadTarget().

◆ inheritTargetAttrs()

void SemaCUDA::inheritTargetAttrs ( FunctionDecl * FD,
const FunctionTemplateDecl & TD )

Copies target attributes from the template TD to the function FD.

Definition at line 1078 of file SemaCUDA.cpp.

References copyAttrIfPresent(), clang::FunctionTemplateDecl::getTemplatedDecl(), and clang::SemaBase::SemaRef.

Referenced by clang::Sema::CheckFunctionTemplateSpecialization().

◆ IsAllowedCall()

bool clang::SemaCUDA::IsAllowedCall ( const FunctionDecl * Caller,
const FunctionDecl * Callee )
inline

Determines whether Caller may invoke Callee, based on their CUDA host/device attributes.

Returns false if the call is not allowed.

Note: Will return true for CFP_WrongSide calls. These may appear in semantically correct CUDA programs, but only if they're never codegen'ed.

Definition at line 186 of file SemaCUDA.h.

References CFP_Never, and IdentifyPreference().

◆ isEmptyConstructor()

◆ isEmptyDestructor()

◆ isImplicitHostDeviceFunction()

bool SemaCUDA::isImplicitHostDeviceFunction ( const FunctionDecl * D)
static

◆ MaybeAddConstantAttr()

void SemaCUDA::MaybeAddConstantAttr ( VarDecl * VD)

◆ maybeAddHostDeviceAttrs()

◆ PopForceHostDevice()

bool SemaCUDA::PopForceHostDevice ( )

Decrements our count of the number of times we've seen a pragma forcing functions to be host device.

Returns false if the count is 0 before incrementing, so you can emit an error.

Definition at line 44 of file SemaCUDA.cpp.

References clang::CUDA, and clang::SemaBase::getLangOpts().

◆ PushForceHostDevice()

void SemaCUDA::PushForceHostDevice ( )

Increments our count of the number of times we've seen a pragma forcing functions to be host device.

So long as this count is greater than zero, all functions encountered will be host device.

Definition at line 39 of file SemaCUDA.cpp.

References clang::CUDA, and clang::SemaBase::getLangOpts().

◆ RecordImplicitHostDeviceFuncUsedByDevice()

void SemaCUDA::RecordImplicitHostDeviceFuncUsedByDevice ( const FunctionDecl * FD)

Record FD if it is a CUDA/HIP implicit host device function used on device side in device compilation.

Definition at line 723 of file SemaCUDA.cpp.

References clang::ASTContext::CUDAImplicitHostDeviceFunUsedByDevice, clang::Device, clang::SemaBase::getASTContext(), clang::Global, clang::HostDevice, IdentifyTarget(), isImplicitHostDeviceFunction(), and clang::SemaBase::SemaRef.

Referenced by clang::Sema::MarkFunctionReferenced().

◆ recordPotentialODRUsedVariable()

void SemaCUDA::recordPotentialODRUsedVariable ( MultiExprArg Args,
OverloadCandidateSet & CandidateSet )

◆ SetLambdaAttrs()

void SemaCUDA::SetLambdaAttrs ( CXXMethodDecl * Method)

Set device or host device attributes on the given lambda operator() method.

CUDA lambdas by default is host device function unless it has explicit host or device attribute.

Definition at line 1014 of file SemaCUDA.cpp.

References clang::CUDA, clang::SemaBase::getASTContext(), clang::SemaBase::getLangOpts(), and clang::Method.

Referenced by clang::Sema::ActOnStartOfLambdaDefinition().

◆ ASTReader

friend class ASTReader
friend

Definition at line 284 of file SemaCUDA.h.

References ASTReader.

Referenced by ASTReader.

◆ ASTWriter

friend class ASTWriter
friend

Definition at line 285 of file SemaCUDA.h.

References ASTWriter.

Referenced by ASTWriter.

Member Data Documentation

◆ CurCUDATargetCtx

struct clang::SemaCUDA::CUDATargetContext clang::SemaCUDA::CurCUDATargetCtx

◆ DeviceKnownEmittedFns

llvm::DenseMap< CanonicalDeclPtr<const FunctionDecl>, FunctionDeclAndLoc> clang::SemaCUDA::DeviceKnownEmittedFns

An inverse call graph, mapping known-emitted functions to one of their known-emitted callers (plus the location of the call).

Functions that we can tell a priori must be emitted aren't added to this map.

Definition at line 82 of file SemaCUDA.h.

Referenced by emitCallStackNotes().

◆ LocsWithCUDACallDiags

llvm::DenseSet<FunctionDeclAndLoc> clang::SemaCUDA::LocsWithCUDACallDiags

FunctionDecls and SourceLocations for which CheckCall has emitted a (maybe deferred) "bad call" diagnostic.

We use this to avoid emitting the same deferred diag twice.

Definition at line 73 of file SemaCUDA.h.

Referenced by CheckCall().


The documentation for this class was generated from the following files: