Skip to content

Commit c21b9df

Browse files
committed
Disallow std names in kernel names
1 parent 26b38f9 commit c21b9df

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10835,7 +10835,7 @@ def err_sycl_kernel_incorrectly_named : Error<
1083510835
"kernel %select{name is missing"
1083610836
"|needs to have a globally-visible name"
1083710837
"|name is invalid. Unscoped enum requires fixed underlying type"
10838-
"|name cannot be or use std::nullptr_t"
10838+
"|name cannot be a type in the \"std\" namespace"
1083910839
"}0">;
1084010840
def err_sycl_restrict : Error<
1084110841
"SYCL kernel cannot "

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,13 @@ void SYCLIntegrationHeader::emitFwdDecl(raw_ostream &O, const Decl *D,
19141914
}
19151915
break;
19161916
}
1917+
1918+
if (NS->isStdNamespace()) {
1919+
Diag.Report(KernelLocation, diag::err_sycl_kernel_incorrectly_named)
1920+
<< /* name cannot be a type in the std namespace */ 3;
1921+
return;
1922+
}
1923+
19171924
++NamespaceCnt;
19181925
const StringRef NSInlinePrefix = NS->isInline() ? "inline " : "";
19191926
NSStr.insert(
@@ -1997,12 +2004,9 @@ void SYCLIntegrationHeader::emitForwardClassDecls(
19972004
const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
19982005

19992006
if (!RD) {
2000-
// Most non-class types such as `int` can safely skip forward declarations,
2001-
// but `std::nullptr_t` is a special case that doesn't behave well.
2002-
if (T->isNullPtrType()) {
2007+
if (T->isNullPtrType())
20032008
Diag.Report(KernelLocation, diag::err_sycl_kernel_incorrectly_named)
2004-
<< /* name cannot be or use std::nullptr_t */ 3;
2005-
}
2009+
<< /* name cannot be a type in the std namespace */ 3;
20062010

20072011
return;
20082012
}

clang/test/CodeGenSYCL/stdtypes_kernel_type.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsycl-int-header=%t.h -DNULLPTRT_CHECK -verify %s
1+
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsycl-int-header=%t.h -DCHECK_ERROR -verify %s
22
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsycl-int-header=%t.h %s
33
// RUN: FileCheck -input-file=%t.h %s
44
//
@@ -41,20 +41,29 @@ typedef long unsigned int size_t;
4141
typedef long int ptrdiff_t;
4242
typedef decltype(nullptr) nullptr_t;
4343
enum class byte : unsigned char {};
44+
class T;
45+
class U;
4446
} // namespace std
4547

48+
template <typename T> struct Templated_kernel_name;
49+
4650
template <typename name, typename Func>
4751
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
4852
kernelFunc();
4953
}
5054

5155
int main() {
52-
#ifdef NULLPTRT_CHECK
53-
// expected-error@+1 {{kernel name cannot be or use std::nullptr_t}}
54-
kernel_single_task<std::nullptr_t>([=]() {});
56+
#ifdef CHECK_ERROR
57+
kernel_single_task<std::nullptr_t>([=]() {}); // expected-error {{kernel name cannot be a type in the "std" namespace}}
58+
kernel_single_task<std::T>([=]() {}); // expected-error {{kernel name cannot be a type in the "std" namespace}}
59+
kernel_single_task<Templated_kernel_name<std::nullptr_t>>([=]() {}); // expected-error {{kernel name cannot be a type in the "std" namespace}}
60+
kernel_single_task<Templated_kernel_name<std::U>>([=]() {}); // expected-error {{kernel name cannot be a type in the "std" namespace}}
5561
#endif
62+
63+
// Although in the std namespace, these resolve to builtins such as `int` that are allowed in kernel names
5664
kernel_single_task<std::byte>([=]() {});
5765
kernel_single_task<std::size_t>([=]() {});
5866
kernel_single_task<std::ptrdiff_t>([=]() {});
67+
5968
return 0;
6069
}

0 commit comments

Comments
 (0)