Skip to content

Commit 6ed96d3

Browse files
committed
[SYCL] Basic diagnostics for the sycl_kernel_entry_point attribute.
The `sycl_kernel_entry_point` attribute is used to declare a function that defines a pattern for an offload kernel entry point. The attribute requires a single type argument that specifies a class type that meets the requirements for a SYCL kernel name as described in section 5.2, "Naming of kernels", of the SYCL 2020 specification. A unique kernel name type is required for each function declared with the attribute. The attribute may not first appear on a declaration that follows a definition of the function. The function is required to have a `void` return type. The function must not be a non-static member function, be deleted or defaulted, be declared with the `constexpr` or `consteval` specifiers, be declared with the `[[noreturn]]` attribute, be a coroutine, or accept variadic arguments. Diagnostics are not yet provided for the following: - Use of a type as a kernel name that does not satisfy the forward declarability requirements specified in section 5.2, "Naming of kernels", of the SYCL 2020 specification. - Use of a type as a parameter of the attributed function that does not satisfy the kernel parameter requirements specified in section 4.12.4, "Rules for parameter passing to kernels", of the SYCL 2020 specification (each such function parameter constitutes a kernel parameter). - Use of language features that are not permitted in device functions as specified in section 5.4, "Language restrictions for device functions", of the SYCL 2020 specification. There are several issues noted by various FIXME comments. - The diagnostic generated for kernel name conflicts needs additional work to better detail the relevant source locations; such as the location of each declaration as well as the original source of each kernel name. - A number of the tests illustrate spurious errors being produced due to attributes that appertain to function templates being instantiated too early (during overload resolution as opposed to after an overload is selected).
1 parent 9f231a8 commit 6ed96d3

16 files changed

+784
-21
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,6 +3360,16 @@ class ASTContext : public RefCountedBase<ASTContext> {
33603360
/// this function.
33613361
void registerSYCLEntryPointFunction(FunctionDecl *FD);
33623362

3363+
/// Given a type used as a SYCL kernel name, returns a reference to the
3364+
/// metadata generated from the corresponding SYCL kernel entry point.
3365+
/// Aborts if the provided type is not a registered SYCL kernel name.
3366+
const SYCLKernelInfo &getSYCLKernelInfo(QualType T) const;
3367+
3368+
/// Returns a pointer to the metadata generated from the corresponding
3369+
/// SYCLkernel entry point if the provided type corresponds to a registered
3370+
/// SYCL kernel name. Returns a null pointer otherwise.
3371+
const SYCLKernelInfo *findSYCLKernelInfo(QualType T) const;
3372+
33633373
//===--------------------------------------------------------------------===//
33643374
// Statistics
33653375
//===--------------------------------------------------------------------===//

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ def PoundPragmaMessage : DiagGroup<"#pragma-messages">,
648648
def : DiagGroup<"redundant-decls">;
649649
def RedeclaredClassMember : DiagGroup<"redeclared-class-member">;
650650
def GNURedeclaredEnum : DiagGroup<"gnu-redeclared-enum">;
651+
def RedundantAttribute : DiagGroup<"redundant-attribute">;
651652
def RedundantMove : DiagGroup<"redundant-move">;
652653
def Register : DiagGroup<"register", [DeprecatedRegister]>;
653654
def ReturnTypeCLinkage : DiagGroup<"return-type-c-linkage">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12408,6 +12408,29 @@ def err_sycl_special_type_num_init_method : Error<
1240812408
"types with 'sycl_special_class' attribute must have one and only one '__init' "
1240912409
"method defined">;
1241012410

12411+
// SYCL kernel entry point diagnostics
12412+
def err_sycl_entry_point_invalid : Error<
12413+
"'sycl_kernel_entry_point' attribute cannot be applied to a"
12414+
" %select{non-static member|variadic|deleted|defaulted|constexpr|consteval|"
12415+
"noreturn|coroutine}0 function">;
12416+
def err_sycl_entry_point_invalid_redeclaration : Error<
12417+
"'sycl_kernel_entry_point' kernel name argument does not match prior"
12418+
" declaration%diff{: $ vs $|}0,1">;
12419+
def err_sycl_kernel_name_conflict : Error<
12420+
"'sycl_kernel_entry_point' kernel name argument conflicts with a previous"
12421+
" declaration">;
12422+
def warn_sycl_kernel_name_not_a_class_type : Warning<
12423+
"%0 is not a valid SYCL kernel name type; a class type is required">,
12424+
InGroup<DiagGroup<"nonportable-sycl">>, DefaultError;
12425+
def warn_sycl_entry_point_redundant_declaration : Warning<
12426+
"redundant 'sycl_kernel_entry_point' attribute">, InGroup<RedundantAttribute>;
12427+
def err_sycl_entry_point_after_definition : Error<
12428+
"'sycl_kernel_entry_point' attribute cannot be added to a function after the"
12429+
" function is defined">;
12430+
def err_sycl_entry_point_return_type : Error<
12431+
"'sycl_kernel_entry_point' attribute only applies to functions with a"
12432+
" 'void' return type">;
12433+
1241112434
def warn_cuda_maxclusterrank_sm_90 : Warning<
1241212435
"maxclusterrank requires sm_90 or higher, CUDA arch provided: %0, ignoring "
1241312436
"%1 attribute">, InGroup<IgnoredAttributes>;

clang/include/clang/Sema/SemaSYCL.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class SemaSYCL : public SemaBase {
6363

6464
void handleKernelAttr(Decl *D, const ParsedAttr &AL);
6565
void handleKernelEntryPointAttr(Decl *D, const ParsedAttr &AL);
66+
67+
void CheckSYCLEntryPointFunctionDecl(FunctionDecl *FD);
6668
};
6769

6870
} // namespace clang

clang/lib/AST/ASTContext.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14463,6 +14463,19 @@ void ASTContext::registerSYCLEntryPointFunction(FunctionDecl *FD) {
1446314463
std::make_pair(KernelNameType, BuildSYCLKernelInfo(KernelNameType, FD)));
1446414464
}
1446514465

14466+
const SYCLKernelInfo &ASTContext::getSYCLKernelInfo(QualType T) const {
14467+
CanQualType KernelNameType = getCanonicalType(T);
14468+
return SYCLKernels.at(KernelNameType);
14469+
}
14470+
14471+
const SYCLKernelInfo *ASTContext::findSYCLKernelInfo(QualType T) const {
14472+
CanQualType KernelNameType = getCanonicalType(T);
14473+
auto IT = SYCLKernels.find(KernelNameType);
14474+
if (IT != SYCLKernels.end())
14475+
return &IT->second;
14476+
return nullptr;
14477+
}
14478+
1446614479
OMPTraitInfo &ASTContext::getNewOMPTraitInfo() {
1446714480
OMPTraitInfoVector.emplace_back(new OMPTraitInfo());
1446814481
return *OMPTraitInfoVector.back();

clang/lib/Sema/SemaDecl.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "clang/Sema/SemaPPC.h"
5454
#include "clang/Sema/SemaRISCV.h"
5555
#include "clang/Sema/SemaSwift.h"
56+
#include "clang/Sema/SemaSYCL.h"
5657
#include "clang/Sema/SemaWasm.h"
5758
#include "clang/Sema/Template.h"
5859
#include "llvm/ADT/STLForwardCompat.h"
@@ -3018,6 +3019,15 @@ static void checkNewAttributesAfterDef(Sema &S, Decl *New, const Decl *Old) {
30183019
// declarations after definitions.
30193020
++I;
30203021
continue;
3022+
} else if (isa<SYCLKernelEntryPointAttr>(NewAttribute)) {
3023+
// Elevate latent uses of the sycl_kernel_entry_point attribute to an
3024+
// error since the definition will have already been created without
3025+
// the semantic effects of the attribute having been applied.
3026+
S.Diag(NewAttribute->getLocation(),
3027+
diag::err_sycl_entry_point_after_definition);
3028+
S.Diag(Def->getLocation(), diag::note_previous_definition);
3029+
++I;
3030+
continue;
30213031
}
30223032

30233033
S.Diag(NewAttribute->getLocation(),
@@ -12146,7 +12156,7 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD,
1214612156
OpenMP().ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(NewFD);
1214712157

1214812158
if (LangOpts.isSYCL() && NewFD->hasAttr<SYCLKernelEntryPointAttr>())
12149-
getASTContext().registerSYCLEntryPointFunction(NewFD);
12159+
SYCL().CheckSYCLEntryPointFunctionDecl(NewFD);
1215012160

1215112161
// Semantic checking for this function declaration (in isolation).
1215212162

@@ -15978,6 +15988,24 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
1597815988
CheckCoroutineWrapper(FD);
1597915989
}
1598015990

15991+
// Diagnose invalid SYCL kernel entry point function declarations.
15992+
if (FD && !FD->isInvalidDecl() && !FD->isTemplated() &&
15993+
FD->hasAttr<SYCLKernelEntryPointAttr>()) {
15994+
if (FD->isDeleted()) {
15995+
Diag(FD->getAttr<SYCLKernelEntryPointAttr>()->getLocation(),
15996+
diag::err_sycl_entry_point_invalid)
15997+
<< /*deleted function*/2;
15998+
} else if (FD->isDefaulted()) {
15999+
Diag(FD->getAttr<SYCLKernelEntryPointAttr>()->getLocation(),
16000+
diag::err_sycl_entry_point_invalid)
16001+
<< /*defaulted function*/3;
16002+
} else if (FSI->isCoroutine()) {
16003+
Diag(FD->getAttr<SYCLKernelEntryPointAttr>()->getLocation(),
16004+
diag::err_sycl_entry_point_invalid)
16005+
<< /*coroutine*/7;
16006+
}
16007+
}
16008+
1598116009
{
1598216010
// Do not call PopExpressionEvaluationContext() if it is a lambda because
1598316011
// one is already popped when finishing the lambda in BuildLambdaExpr().

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
#include "clang/Sema/SemaSYCL.h"
1212
#include "clang/AST/Mangle.h"
13+
#include "clang/AST/SYCLKernelInfo.h"
1314
#include "clang/AST/TypeOrdering.h"
15+
#include "clang/Basic/Diagnostic.h"
1416
#include "clang/Sema/Attr.h"
1517
#include "clang/Sema/ParsedAttr.h"
1618
#include "clang/Sema/Sema.h"
@@ -206,3 +208,127 @@ void SemaSYCL::handleKernelEntryPointAttr(Decl *D, const ParsedAttr &AL) {
206208
D->addAttr(::new (SemaRef.Context)
207209
SYCLKernelEntryPointAttr(SemaRef.Context, AL, TSI));
208210
}
211+
212+
static SourceLocation SourceLocationForType(QualType QT) {
213+
SourceLocation Loc;
214+
const Type *T = QT->getUnqualifiedDesugaredType();
215+
if (const TagType *TT = dyn_cast<TagType>(T))
216+
Loc = TT->getDecl()->getLocation();
217+
else if (const ObjCInterfaceType *ObjCIT = dyn_cast<ObjCInterfaceType>(T))
218+
Loc = ObjCIT->getDecl()->getLocation();
219+
return Loc;
220+
}
221+
222+
static bool CheckSYCLKernelName(Sema &S, SourceLocation Loc,
223+
QualType KernelName) {
224+
assert(!KernelName->isDependentType());
225+
226+
if (!KernelName->isStructureOrClassType()) {
227+
// SYCL 2020 section 5.2, "Naming of kernels", only requires that the
228+
// kernel name be a C++ typename. However, the definition of "kernel name"
229+
// in the glossary states that a kernel name is a class type. Neither
230+
// section explicitly states whether the kernel name type can be
231+
// cv-qualified. For now, kernel name types are required to be class types
232+
// and that they may be cv-qualified. The following issue requests
233+
// clarification from the SYCL WG.
234+
// https://github.com/KhronosGroup/SYCL-Docs/issues/568
235+
S.Diag(Loc, diag::warn_sycl_kernel_name_not_a_class_type) << KernelName;
236+
SourceLocation DeclTypeLoc = SourceLocationForType(KernelName);
237+
if (DeclTypeLoc.isValid())
238+
S.Diag(DeclTypeLoc, diag::note_entity_declared_at)
239+
<< KernelName;
240+
return true;
241+
}
242+
243+
return false;
244+
}
245+
246+
void SemaSYCL::CheckSYCLEntryPointFunctionDecl(FunctionDecl *FD) {
247+
// Ensure that all attributes present on the declaration are consistent
248+
// and warn about any redundant ones.
249+
const SYCLKernelEntryPointAttr *SKEPAttr = nullptr;
250+
for (auto SAI = FD->specific_attr_begin<SYCLKernelEntryPointAttr>();
251+
SAI != FD->specific_attr_end<SYCLKernelEntryPointAttr>();
252+
++SAI) {
253+
if (!SKEPAttr) {
254+
SKEPAttr = *SAI;
255+
continue;
256+
}
257+
if (!getASTContext().hasSameType(SAI->getKernelName(),
258+
SKEPAttr->getKernelName())) {
259+
Diag(SAI->getLocation(), diag::err_sycl_entry_point_invalid_redeclaration)
260+
<< SAI->getKernelName() << SKEPAttr->getKernelName();
261+
Diag(SKEPAttr->getLocation(), diag::note_previous_attribute);
262+
} else {
263+
Diag(SAI->getLocation(),
264+
diag::warn_sycl_entry_point_redundant_declaration);
265+
Diag(SKEPAttr->getLocation(), diag::note_previous_attribute);
266+
}
267+
}
268+
assert(SKEPAttr && "Missing sycl_kernel_entry_point attribute");
269+
270+
// Ensure the kernel name type is valid.
271+
if (!SKEPAttr->getKernelName()->isDependentType()) {
272+
CheckSYCLKernelName(SemaRef, SKEPAttr->getLocation(),
273+
SKEPAttr->getKernelName());
274+
}
275+
276+
// Ensure that an attribute present on the previous declaration
277+
// matches the one on this declaration.
278+
FunctionDecl *PrevFD = FD->getPreviousDecl();
279+
if (PrevFD && !PrevFD->isInvalidDecl()) {
280+
const auto *PrevSKEPAttr = PrevFD->getAttr<SYCLKernelEntryPointAttr>();
281+
if (PrevSKEPAttr) {
282+
if (!getASTContext().hasSameType(SKEPAttr->getKernelName(),
283+
PrevSKEPAttr->getKernelName())) {
284+
Diag(SKEPAttr->getLocation(),
285+
diag::err_sycl_entry_point_invalid_redeclaration)
286+
<< SKEPAttr->getKernelName() << PrevSKEPAttr->getKernelName();
287+
Diag(PrevSKEPAttr->getLocation(), diag::note_previous_decl)
288+
<< PrevFD;;
289+
}
290+
}
291+
}
292+
293+
if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
294+
if (!MD->isStatic()) {
295+
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
296+
<< /*non-static member function*/0;
297+
}
298+
}
299+
if (FD->isVariadic()) {
300+
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
301+
<< /*variadic function*/1;
302+
}
303+
if (FD->isConsteval()) {
304+
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
305+
<< /*consteval function*/5;
306+
} else if (FD->isConstexpr()) {
307+
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
308+
<< /*constexpr function*/4;
309+
}
310+
if (FD->isNoReturn()) {
311+
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
312+
<< /*noreturn function*/6;
313+
}
314+
315+
if (!FD->getReturnType()->isVoidType()) {
316+
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_return_type);
317+
}
318+
319+
if (!FD->isInvalidDecl() && !FD->isTemplated()) {
320+
const SYCLKernelInfo *SKI =
321+
getASTContext().findSYCLKernelInfo(SKEPAttr->getKernelName());
322+
if (SKI) {
323+
if (!declaresSameEntity(FD, SKI->getKernelEntryPointDecl())) {
324+
// FIXME: This diagnostic should include the origin of the kernel
325+
// FIXME: names; not just the locations of the conflicting declarations.
326+
Diag(FD->getLocation(), diag::err_sycl_kernel_name_conflict);
327+
Diag(SKI->getKernelEntryPointDecl()->getLocation(),
328+
diag::note_previous_declaration);
329+
}
330+
} else {
331+
getASTContext().registerSYCLEntryPointFunction(FD);
332+
}
333+
}
334+
}

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,8 @@ NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
15501550
IdResolver.AddDecl(Param);
15511551
}
15521552

1553+
ProcessDeclAttributes(S, Param, D);
1554+
15531555
// C++0x [temp.param]p9:
15541556
// A default template-argument may be specified for any kind of
15551557
// template-parameter that is not a template parameter pack.

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,8 +1134,19 @@ void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
11341134
// the presence of a sycl_kernel_entry_point attribute, register it so that
11351135
// associated metadata is recreated.
11361136
if (FD->hasAttr<SYCLKernelEntryPointAttr>()) {
1137+
const auto *SKEPAttr = FD->getAttr<SYCLKernelEntryPointAttr>();
11371138
ASTContext &C = Reader.getContext();
1138-
C.registerSYCLEntryPointFunction(FD);
1139+
const SYCLKernelInfo *SKI =
1140+
C.findSYCLKernelInfo(SKEPAttr->getKernelName());
1141+
if (SKI) {
1142+
if (!declaresSameEntity(FD, SKI->getKernelEntryPointDecl())) {
1143+
Reader.Diag(FD->getLocation(), diag::err_sycl_kernel_name_conflict);
1144+
Reader.Diag(SKI->getKernelEntryPointDecl()->getLocation(),
1145+
diag::note_previous_declaration);
1146+
}
1147+
} else {
1148+
C.registerSYCLEntryPointFunction(FD);
1149+
}
11391150
}
11401151
}
11411152

clang/test/ASTSYCL/ast-dump-sycl-kernel-entry-point.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,29 @@ void skep5<KN<5,2>>(long) {
107107
// CHECK: | |-TemplateArgument type 'long'
108108
// CHECK: | `-SYCLKernelEntryPointAttr {{.*}} KN<5, 2>
109109

110+
// FIXME: C++23 [temp.expl.spec]p12 states:
111+
// FIXME: ... Similarly, attributes appearing in the declaration of a template
112+
// FIXME: have no effect on an explicit specialization of that template.
113+
// FIXME: Clang currently instantiates a function template specialization from
114+
// FIXME: the function template declaration and links it as a previous
115+
// FIXME: declaration of an explicit specialization. The instantiated
116+
// FIXME: declaration includes attributes instantiated from the function
117+
// FIXME: template declaration. When the instantiated declaration and the
118+
// FIXME: explicit specialization both specify a sycl_kernel_entry_point
119+
// FIXME: attribute with different kernel name types, a spurious diagnostic
120+
// FIXME: is issued. The following test case is incorrectly diagnosed as
121+
// FIXME: having conflicting kernel name types (KN<5,3> vs the incorrectly
122+
// FIXME: inherited KN<5,-1>).
123+
#if 0
110124
template<>
111125
[[clang::sycl_kernel_entry_point(KN<5,3>)]]
112126
void skep5<KN<5,-1>>(long long) {
113127
}
114-
// CHECK: |-FunctionDecl {{.*}} prev {{.*}} skep5 'void (long long)' explicit_specialization
115-
// CHECK-NEXT: | |-TemplateArgument type 'KN<5, -1>'
116-
// CHECK: | |-TemplateArgument type 'long long'
117-
// CHECK: | `-SYCLKernelEntryPointAttr {{.*}} KN<5, 3>
128+
// FIXME-CHECK: |-FunctionDecl {{.*}} prev {{.*}} skep5 'void (long long)' explicit_specialization
129+
// FIXME-CHECK-NEXT: | |-TemplateArgument type 'KN<5, -1>'
130+
// FIXME-CHECK: | |-TemplateArgument type 'long long'
131+
// FIXME-CHECK: | `-SYCLKernelEntryPointAttr {{.*}} KN<5, 3>
132+
#endif
118133

119134
template void skep5<KN<5,4>>(int);
120135
// Checks are located with the primary template declaration above.

0 commit comments

Comments
 (0)