clang 19.0.0git
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
clang::SemaCUDA Class Reference

#include "clang/Sema/SemaCUDA.h"

Inheritance diagram for clang::SemaCUDA:
Inheritance graph
[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.
 
- Public Member Functions inherited from clang::SemaBase
 SemaBase (Sema &S)
 
ASTContextgetASTContext () const
 
DiagnosticsEnginegetDiagnostics () const
 
const LangOptionsgetLangOpts () 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.
 
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 35 of file SemaCUDA.h.

Member Enumeration Documentation

◆ CUDAFunctionPreference

Enumerator
CFP_Never 
CFP_WrongSide 
CFP_HostDevice 
CFP_SameSide 
CFP_Native 

Definition at line 150 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 119 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 108 of file SemaCUDA.h.

Constructor & Destructor Documentation

◆ SemaCUDA()

SemaCUDA::SemaCUDA ( Sema S)

Definition at line 31 of file SemaCUDA.cpp.

Member Function Documentation

◆ ActOnExecConfigExpr()

ExprResult SemaCUDA::ActOnExecConfigExpr ( Scope S,
SourceLocation  LLLLoc,
MultiExprArg  ExecConfig,
SourceLocation  GGGLoc 
)

◆ checkAllowedInitializer()

void SemaCUDA::checkAllowedInitializer ( VarDecl VD)

◆ 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 882 of file SemaCUDA.cpp.

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

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

◆ CheckLambdaCapture()

void SemaCUDA::CheckLambdaCapture ( CXXMethodDecl D,
const sema::Capture Capture 
)

◆ checkTargetOverload()

void SemaCUDA::checkTargetOverload ( FunctionDecl NewFD,
const LookupResult Previous 
)

◆ CurrentTarget()

CUDAFunctionTarget clang::SemaCUDA::CurrentTarget ( )
inline

Gets the CUDA target for the current context.

Definition at line 142 of file SemaCUDA.h.

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

Referenced by 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 819 of file SemaCUDA.cpp.

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

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

◆ 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 851 of file SemaCUDA.cpp.

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

Referenced by clang::SemaBase::Diag(), and 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 320 of file SemaCUDA.cpp.

References IdentifyPreference().

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 1070 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 227 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, clang::SemaCUDA::CUDATargetContext::Kind, and clang::SemaCUDA::CUDATargetContext::Target.

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

◆ IdentifyTarget() [1/3]

CUDAFunctionTarget SemaCUDA::IdentifyTarget ( const FunctionDecl D,
bool  IgnoreImplicitHDAttr = false 
)

◆ 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 372 of file SemaCUDA.cpp.

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

Referenced by clang::Sema::DeclareImplicitCopyAssignment(), clang::Sema::DeclareImplicitCopyConstructor(), clang::Sema::DeclareImplicitDefaultConstructor(), clang::Sema::DeclareImplicitDestructor(), clang::Sema::DeclareImplicitMoveAssignment(), clang::Sema::DeclareImplicitMoveConstructor(), DiagnoseBadTarget(), and clang::Sema::ShouldDeleteSpecialMember().

◆ inheritTargetAttrs()

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

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

Definition at line 1062 of file SemaCUDA.cpp.

References 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 176 of file SemaCUDA.h.

References CFP_Never, and IdentifyPreference().

◆ isEmptyConstructor()

bool SemaCUDA::isEmptyConstructor ( SourceLocation  Loc,
CXXConstructorDecl CD 
)

◆ isEmptyDestructor()

bool SemaCUDA::isEmptyDestructor ( SourceLocation  Loc,
CXXDestructorDecl CD 
)

◆ isImplicitHostDeviceFunction()

bool SemaCUDA::isImplicitHostDeviceFunction ( const FunctionDecl D)
static

◆ MaybeAddConstantAttr()

void SemaCUDA::MaybeAddConstantAttr ( VarDecl VD)

◆ maybeAddHostDeviceAttrs()

void SemaCUDA::maybeAddHostDeviceAttrs ( FunctionDecl FD,
const LookupResult Previous 
)

◆ 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 46 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 41 of file SemaCUDA.cpp.

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

◆ RecordImplicitHostDeviceFuncUsedByDevice()

void SemaCUDA::RecordImplicitHostDeviceFuncUsedByDevice ( const FunctionDecl FD)

◆ 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 997 of file SemaCUDA.cpp.

References clang::Decl::addAttr(), clang::CUDA, clang::SemaBase::getASTContext(), clang::SemaBase::getLangOpts(), and clang::Decl::hasAttr().

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

Friends And Related Function Documentation

◆ ASTReader

friend class ASTReader
friend

Definition at line 270 of file SemaCUDA.h.

◆ ASTWriter

friend class ASTWriter
friend

Definition at line 271 of file SemaCUDA.h.

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 72 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 63 of file SemaCUDA.h.

Referenced by CheckCall().


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