Skip to content

Commit cb5ddb4

Browse files
[SYCL] Improve diagnostics for invalid SYCL kernel names. (#2730)
This patch modifies the error diagnostic to print the fully instantiated kernel name to easily identify problematic types in kernel name. A note diagnostic with the problematic type was also added. Signed-off-by: Elizabeth Andrews <[email protected]>
1 parent fb34adc commit cb5ddb4

File tree

6 files changed

+96
-39
lines changed

6 files changed

+96
-39
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11066,12 +11066,14 @@ def err_ext_int_max_size : Error<"%select{signed|unsigned}0 _ExtInt of bit "
1106611066
def err_esimd_glob_cant_init : Error<
1106711067
"SYCL explicit SIMD does not permit private global variable to have an initializer">;
1106811068

11069-
def err_sycl_kernel_incorrectly_named : Error<
11070-
"kernel %select{name is missing"
11071-
"|needs to have a globally-visible name"
11072-
"|name is invalid. Unscoped enum requires fixed underlying type"
11073-
"|name cannot be a type in the \"std\" namespace"
11069+
def err_sycl_kernel_incorrectly_named : Error<"%0 is an invalid kernel name type">;
11070+
def note_invalid_type_in_sycl_kernel : Note<
11071+
"%select{%1 should be globally-visible"
11072+
"|unscoped enum %1 requires fixed underlying type"
11073+
"|type %1 cannot be in the \"std\" namespace"
11074+
"|kernel name is missing"
1107411075
"}0">;
11076+
1107511077
def err_sycl_kernel_not_function_object
1107611078
: Error<"kernel parameter must be a lambda or function object">;
1107711079
def err_sycl_restrict : Error<

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,6 +2833,7 @@ class SYCLKernelNameTypeVisitor
28332833
public ConstTemplateArgumentVisitor<SYCLKernelNameTypeVisitor> {
28342834
Sema &S;
28352835
SourceLocation KernelInvocationFuncLoc;
2836+
QualType KernelNameType;
28362837
using InnerTypeVisitor = TypeVisitor<SYCLKernelNameTypeVisitor>;
28372838
using InnerTemplArgVisitor =
28382839
ConstTemplateArgumentVisitor<SYCLKernelNameTypeVisitor>;
@@ -2844,8 +2845,10 @@ class SYCLKernelNameTypeVisitor
28442845
}
28452846

28462847
public:
2847-
SYCLKernelNameTypeVisitor(Sema &S, SourceLocation KernelInvocationFuncLoc)
2848-
: S(S), KernelInvocationFuncLoc(KernelInvocationFuncLoc) {}
2848+
SYCLKernelNameTypeVisitor(Sema &S, SourceLocation KernelInvocationFuncLoc,
2849+
QualType KernelNameType)
2850+
: S(S), KernelInvocationFuncLoc(KernelInvocationFuncLoc),
2851+
KernelNameType(KernelNameType) {}
28492852

28502853
bool isValid() { return !IsInvalid; }
28512854

@@ -2856,7 +2859,9 @@ class SYCLKernelNameTypeVisitor
28562859
if (!RD) {
28572860
if (T->isNullPtrType()) {
28582861
S.Diag(KernelInvocationFuncLoc, diag::err_sycl_kernel_incorrectly_named)
2859-
<< /* kernel name cannot be a type in the std namespace */ 3;
2862+
<< KernelNameType;
2863+
S.Diag(KernelInvocationFuncLoc, diag::note_invalid_type_in_sycl_kernel)
2864+
<< /* kernel name cannot be a type in the std namespace */ 2 << T;
28602865
IsInvalid = true;
28612866
}
28622867
return;
@@ -2881,9 +2886,10 @@ class SYCLKernelNameTypeVisitor
28812886
const EnumDecl *ED = T->getDecl();
28822887
if (!ED->isScoped() && !ED->isFixed()) {
28832888
S.Diag(KernelInvocationFuncLoc, diag::err_sycl_kernel_incorrectly_named)
2884-
<< /* Unscoped enum requires fixed underlying type */ 2;
2885-
S.Diag(ED->getSourceRange().getBegin(), diag::note_entity_declared_at)
2886-
<< ED;
2889+
<< KernelNameType;
2890+
S.Diag(KernelInvocationFuncLoc, diag::note_invalid_type_in_sycl_kernel)
2891+
<< /* Unscoped enum requires fixed underlying type */ 1
2892+
<< QualType(ED->getTypeForDecl(), 0);
28872893
IsInvalid = true;
28882894
}
28892895
}
@@ -2900,7 +2906,10 @@ class SYCLKernelNameTypeVisitor
29002906
auto *NameSpace = dyn_cast_or_null<NamespaceDecl>(DeclCtx);
29012907
if (NameSpace && NameSpace->isStdNamespace()) {
29022908
S.Diag(KernelInvocationFuncLoc, diag::err_sycl_kernel_incorrectly_named)
2903-
<< /* kernel name cannot be a type in the std namespace */ 3;
2909+
<< KernelNameType;
2910+
S.Diag(KernelInvocationFuncLoc, diag::note_invalid_type_in_sycl_kernel)
2911+
<< /* kernel name cannot be a type in the std namespace */ 2
2912+
<< QualType(Tag->getTypeForDecl(), 0);
29042913
IsInvalid = true;
29052914
return;
29062915
}
@@ -2909,19 +2918,27 @@ class SYCLKernelNameTypeVisitor
29092918
if (KernelNameIsMissing) {
29102919
S.Diag(KernelInvocationFuncLoc,
29112920
diag::err_sycl_kernel_incorrectly_named)
2912-
<< /* kernel name is missing */ 0;
2921+
<< KernelNameType;
2922+
S.Diag(KernelInvocationFuncLoc,
2923+
diag::note_invalid_type_in_sycl_kernel)
2924+
<< /* kernel name is missing */ 3;
29132925
IsInvalid = true;
29142926
return;
29152927
}
29162928
if (Tag->isCompleteDefinition()) {
29172929
S.Diag(KernelInvocationFuncLoc,
29182930
diag::err_sycl_kernel_incorrectly_named)
2919-
<< /* kernel name is not globally-visible */ 1;
2931+
<< KernelNameType;
2932+
S.Diag(KernelInvocationFuncLoc,
2933+
diag::note_invalid_type_in_sycl_kernel)
2934+
<< /* kernel name is not globally-visible */ 0
2935+
<< QualType(Tag->getTypeForDecl(), 0);
29202936
IsInvalid = true;
2921-
} else
2937+
} else {
29222938
S.Diag(KernelInvocationFuncLoc, diag::warn_sycl_implicit_decl);
2923-
S.Diag(Tag->getSourceRange().getBegin(), diag::note_previous_decl)
2924-
<< Tag->getName();
2939+
S.Diag(Tag->getSourceRange().getBegin(), diag::note_previous_decl)
2940+
<< Tag->getName();
2941+
}
29252942
}
29262943
}
29272944
}
@@ -2998,7 +3015,8 @@ void Sema::CheckSYCLKernelCall(FunctionDecl *KernelFunc, SourceRange CallLoc,
29983015
SyclKernelArgsSizeChecker ArgsSizeChecker(*this, Args[0]->getExprLoc());
29993016

30003017
KernelObjVisitor Visitor{*this};
3001-
SYCLKernelNameTypeVisitor KernelNameTypeVisitor(*this, Args[0]->getExprLoc());
3018+
SYCLKernelNameTypeVisitor KernelNameTypeVisitor(*this, Args[0]->getExprLoc(),
3019+
KernelNameType);
30023020

30033021
DiagnosingSYCLKernel = true;
30043022

clang/test/SemaSYCL/implicit_kernel_type.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ int main() {
2525
queue q;
2626

2727
#if defined(WARN)
28-
// expected-error@Inputs/sycl.hpp:220 {{kernel needs to have a globally-visible name}}
29-
// expected-note@+7 {{InvalidKernelName1 declared here}}
28+
// expected-error@Inputs/sycl.hpp:220 {{'InvalidKernelName1' is an invalid kernel name type}}
29+
// expected-note@Inputs/sycl.hpp:220 {{'InvalidKernelName1' should be globally-visible}}
3030
// expected-note@+8 {{in instantiation of function template specialization}}
3131
#elif defined(ERROR)
32-
// expected-error@Inputs/sycl.hpp:220 {{kernel needs to have a globally-visible name}}
33-
// expected-note@+3 {{InvalidKernelName1 declared here}}
32+
// expected-error@Inputs/sycl.hpp:220 {{'InvalidKernelName1' is an invalid kernel name type}}
33+
// expected-note@Inputs/sycl.hpp:220 {{'InvalidKernelName1' should be globally-visible}}
3434
// expected-note@+4 {{in instantiation of function template specialization}}
3535
#endif
3636
class InvalidKernelName1 {};

clang/test/SemaSYCL/kernelname-enum.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsycl-int-header=%t.h -fsyntax-only -Wno-sycl-2017-compat -verify %s
22

3-
// expected-error@Inputs/sycl.hpp:220 2{{kernel name is invalid. Unscoped enum requires fixed underlying type}}
43
#include "Inputs/sycl.hpp"
54

65
enum unscoped_enum_int : int {
76
val_1,
87
val_2
98
};
109

11-
// expected-note@+1 2 {{'unscoped_enum_no_type_set' declared here}}
1210
enum unscoped_enum_no_type_set {
1311
val_3,
1412
val_4
@@ -69,11 +67,15 @@ int main() {
6967
});
7068

7169
q.submit([&](cl::sycl::handler &cgh) {
70+
// expected-error@Inputs/sycl.hpp:220 {{'dummy_functor_2<val_3>' is an invalid kernel name type}}
71+
// expected-note@Inputs/sycl.hpp:220 {{unscoped enum 'unscoped_enum_no_type_set' requires fixed underlying type}}
7272
// expected-note@+1{{in instantiation of function template specialization}}
7373
cgh.single_task(f2);
7474
});
7575

7676
q.submit([&](cl::sycl::handler &cgh) {
77+
// expected-error@Inputs/sycl.hpp:220 {{'templated_functor<dummy_functor_2>' is an invalid kernel name type}}
78+
// expected-note@Inputs/sycl.hpp:220 {{unscoped enum 'unscoped_enum_no_type_set' requires fixed underlying type}}
7779
// expected-note@+1{{in instantiation of function template specialization}}
7880
cgh.single_task(f5);
7981
});

clang/test/SemaSYCL/stdtypes_kernel_type.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,55 @@ typedef long int ptrdiff_t;
88
typedef decltype(nullptr) nullptr_t;
99
class T;
1010
class U;
11+
class Foo;
1112
} // namespace std
1213

1314
template <typename T>
1415
struct Templated_kernel_name;
1516

17+
template <typename T>
18+
struct Templated_kernel_name2;
19+
1620
template <typename T, typename... Args> class TemplParamPack;
1721

1822
using namespace cl::sycl;
1923
queue q;
2024

2125
int main() {
2226
#ifdef CHECK_ERROR
23-
// expected-error@Inputs/sycl.hpp:220 5 {{kernel name cannot be a type in the "std" namespace}}
2427
q.submit([&](handler &h) {
28+
// expected-error@Inputs/sycl.hpp:220 {{'nullptr_t' is an invalid kernel name type}}
29+
// expected-note@Inputs/sycl.hpp:220 {{type 'nullptr_t' cannot be in the "std" namespace}}
2530
// expected-note@+1{{in instantiation of function template specialization}}
2631
h.single_task<std::nullptr_t>([=] {});
2732
});
2833
q.submit([&](handler &h) {
34+
// expected-error@Inputs/sycl.hpp:220 {{'std::T' is an invalid kernel name type}}
35+
// expected-note@Inputs/sycl.hpp:220 {{type 'std::T' cannot be in the "std" namespace}}
2936
// expected-note@+1{{in instantiation of function template specialization}}
3037
h.single_task<std::T>([=] {});
3138
});
3239
q.submit([&](handler &h) {
40+
// expected-error@Inputs/sycl.hpp:220 {{'Templated_kernel_name<nullptr_t>' is an invalid kernel name type}}
41+
// expected-note@Inputs/sycl.hpp:220 {{type 'nullptr_t' cannot be in the "std" namespace}}
3342
// expected-note@+1{{in instantiation of function template specialization}}
3443
h.single_task<Templated_kernel_name<std::nullptr_t>>([=] {});
3544
});
3645
q.submit([&](handler &h) {
46+
// expected-error@Inputs/sycl.hpp:220 {{'Templated_kernel_name<std::U>' is an invalid kernel name type}}
47+
// expected-note@Inputs/sycl.hpp:220 {{type 'std::U' cannot be in the "std" namespace}}
3748
// expected-note@+1{{in instantiation of function template specialization}}
3849
h.single_task<Templated_kernel_name<std::U>>([=] {});
3950
});
4051
q.submit([&](handler &cgh) {
52+
// expected-error@Inputs/sycl.hpp:220 {{'Templated_kernel_name2<Templated_kernel_name<std::Foo>>' is an invalid kernel name type}}
53+
// expected-note@Inputs/sycl.hpp:220{{type 'std::Foo' cannot be in the "std" namespace}}
54+
// expected-note@+1{{in instantiation of function template specialization}}
55+
cgh.single_task<Templated_kernel_name2<Templated_kernel_name<std::Foo>>>([]() {});
56+
});
57+
q.submit([&](handler &cgh) {
58+
// expected-error@Inputs/sycl.hpp:220 {{'TemplParamPack<int, float, nullptr_t, double>' is an invalid kernel name type}}
59+
// expected-note@Inputs/sycl.hpp:220 {{type 'nullptr_t' cannot be in the "std" namespace}}
4160
// expected-note@+1{{in instantiation of function template specialization}}
4261
cgh.single_task<TemplParamPack<int, float, std::nullptr_t, double>>([]() {});
4362
});

clang/test/SemaSYCL/unnamed-kernel.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ typedef struct {
1616
} max_align_t;
1717
} // namespace std
1818

19+
template <typename T>
20+
struct Templated_kernel_name;
21+
22+
template <typename T>
23+
struct Templated_kernel_name2;
24+
1925
struct MyWrapper {
2026
private:
2127
class InvalidKernelName0 {};
@@ -27,8 +33,8 @@ struct MyWrapper {
2733
void test() {
2834
cl::sycl::queue q;
2935
#ifndef __SYCL_UNNAMED_LAMBDA__
30-
// expected-error@Inputs/sycl.hpp:220 {{kernel needs to have a globally-visible name}}
31-
// expected-note@+3 {{InvalidKernelName1 declared here}}
36+
// expected-error@Inputs/sycl.hpp:220 {{'InvalidKernelName1' is an invalid kernel name type}}
37+
// expected-note@Inputs/sycl.hpp:220 {{'InvalidKernelName1' should be globally-visible}}
3238
// expected-note@+4{{in instantiation of function template specialization}}
3339
#endif
3440
class InvalidKernelName1 {};
@@ -37,8 +43,8 @@ struct MyWrapper {
3743
});
3844

3945
#ifndef __SYCL_UNNAMED_LAMBDA__
40-
// expected-error@Inputs/sycl.hpp:220 {{kernel needs to have a globally-visible name}}
41-
// expected-note@+3 {{InvalidKernelName2 declared here}}
46+
// expected-error@Inputs/sycl.hpp:220 {{'namespace1::KernelName<InvalidKernelName2>' is an invalid kernel name type}}
47+
// expected-note@Inputs/sycl.hpp:220 {{'InvalidKernelName2' should be globally-visible}}
4248
// expected-note@+4{{in instantiation of function template specialization}}
4349
#endif
4450
class InvalidKernelName2 {};
@@ -47,17 +53,17 @@ struct MyWrapper {
4753
});
4854

4955
#ifndef __SYCL_UNNAMED_LAMBDA__
50-
// expected-error@Inputs/sycl.hpp:220 {{kernel needs to have a globally-visible name}}
51-
// expected-note@21 {{InvalidKernelName0 declared here}}
56+
// expected-error@Inputs/sycl.hpp:220 {{'MyWrapper::InvalidKernelName0' is an invalid kernel name type}}
57+
// expected-note@Inputs/sycl.hpp:220 {{'MyWrapper::InvalidKernelName0' should be globally-visible}}
5258
// expected-note@+3{{in instantiation of function template specialization}}
5359
#endif
5460
q.submit([&](cl::sycl::handler &h) {
5561
h.single_task<InvalidKernelName0>([] {});
5662
});
5763

5864
#ifndef __SYCL_UNNAMED_LAMBDA__
59-
// expected-error@Inputs/sycl.hpp:220 {{kernel needs to have a globally-visible name}}
60-
// expected-note@22 {{InvalidKernelName3 declared here}}
65+
// expected-error@Inputs/sycl.hpp:220 {{'namespace1::KernelName<MyWrapper::InvalidKernelName3>' is an invalid kernel name type}}
66+
// expected-note@Inputs/sycl.hpp:220 {{'MyWrapper::InvalidKernelName3' should be globally-visible}}
6167
// expected-note@+3{{in instantiation of function template specialization}}
6268
#endif
6369
q.submit([&](cl::sycl::handler &h) {
@@ -70,7 +76,8 @@ struct MyWrapper {
7076
});
7177

7278
#ifndef __SYCL_UNNAMED_LAMBDA__
73-
// expected-error@Inputs/sycl.hpp:220 {{kernel name cannot be a type in the "std" namespace}}
79+
// expected-error@Inputs/sycl.hpp:220 {{'std::max_align_t' is an invalid kernel name type}}
80+
// expected-note@Inputs/sycl.hpp:220 {{type 'std::max_align_t' cannot be in the "std" namespace}}
7481
// expected-note@+3{{in instantiation of function template specialization}}
7582
#endif
7683
q.submit([&](cl::sycl::handler &h) {
@@ -79,8 +86,8 @@ struct MyWrapper {
7986

8087
using InvalidAlias = InvalidKernelName4;
8188
#ifndef __SYCL_UNNAMED_LAMBDA__
82-
// expected-error@Inputs/sycl.hpp:220 {{kernel needs to have a globally-visible name}}
83-
// expected-note@23 {{InvalidKernelName4 declared here}}
89+
// expected-error@Inputs/sycl.hpp:220 {{'MyWrapper::InvalidKernelName4' is an invalid kernel name type}}
90+
// expected-note@Inputs/sycl.hpp:220 {{'MyWrapper::InvalidKernelName4' should be globally-visible}}
8491
// expected-note@+3{{in instantiation of function template specialization}}
8592
#endif
8693
q.submit([&](cl::sycl::handler &h) {
@@ -89,20 +96,29 @@ struct MyWrapper {
8996

9097
using InvalidAlias1 = InvalidKernelName5;
9198
#ifndef __SYCL_UNNAMED_LAMBDA__
92-
// expected-error@Inputs/sycl.hpp:220 {{kernel needs to have a globally-visible name}}
93-
// expected-note@24 {{InvalidKernelName5 declared here}}
99+
// expected-error@Inputs/sycl.hpp:220 {{'namespace1::KernelName<MyWrapper::InvalidKernelName5>' is an invalid kernel name type}}
100+
// expected-note@Inputs/sycl.hpp:220 {{'MyWrapper::InvalidKernelName5' should be globally-visible}}
94101
// expected-note@+3{{in instantiation of function template specialization}}
95102
#endif
96103
q.submit([&](cl::sycl::handler &h) {
97104
h.single_task<namespace1::KernelName<InvalidAlias1>>([] {});
98105
});
106+
#ifndef __SYCL_UNNAMED_LAMBDA__
107+
// expected-error@Inputs/sycl.hpp:220 {{'Templated_kernel_name2<Templated_kernel_name<InvalidKernelName1>>' is an invalid kernel name type}}
108+
// expected-note@Inputs/sycl.hpp:220 {{'InvalidKernelName1' should be globally-visible}}
109+
// expected-note@+3{{in instantiation of function template specialization}}
110+
#endif
111+
q.submit([&](cl::sycl::handler &h) {
112+
h.single_task<Templated_kernel_name2<Templated_kernel_name<InvalidKernelName1>>>([] {});
113+
});
99114
}
100115
};
101116

102117
int main() {
103118
cl::sycl::queue q;
104119
#ifndef __SYCL_UNNAMED_LAMBDA__
105-
// expected-error@Inputs/sycl.hpp:220 {{kernel name is missing}}
120+
// expected-error-re@Inputs/sycl.hpp:220 {{'(lambda at {{.*}}unnamed-kernel.cpp{{.*}}' is an invalid kernel name type}}
121+
// expected-note@Inputs/sycl.hpp:220 {{kernel name is missing}}
106122
// expected-note@+2{{in instantiation of function template specialization}}
107123
#endif
108124
q.submit([&](cl::sycl::handler &h) { h.single_task([] {}); });

0 commit comments

Comments
 (0)