Skip to content

Commit 5e8004d

Browse files
committed
Emit diagnostic when kernel names use nullptr_t
1 parent 46c54c6 commit 5e8004d

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10675,7 +10675,8 @@ def err_sycl_attribute_address_space_invalid : Error<
1067510675
"address space is outside the valid range of values">;
1067610676
def err_sycl_kernel_incorrectly_named : Error<
1067710677
"kernel %select{name is missing"
10678-
"|needs to have a globally-visible name}0">;
10678+
"|needs to have a globally-visible name"
10679+
"|name cannot be or use std::nullptr_t}0">;
1067910680
def err_sycl_restrict : Error<
1068010681
"SYCL kernel cannot "
1068110682
"%select{use a non-const global variable"

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,8 +1744,16 @@ void SYCLIntegrationHeader::emitForwardClassDecls(
17441744
;
17451745
const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
17461746

1747-
if (!RD)
1747+
if (!RD) {
1748+
// Most non-class types such as `int` can safely skip forward declarations,
1749+
// but `std::nullptr_t` is a special case that doesn't behave well.
1750+
if (T->isNullPtrType()) {
1751+
Diag.Report(KernelLocation, diag::err_sycl_kernel_incorrectly_named)
1752+
<< /* name cannot be or use std::nullptr_t */ 2;
1753+
}
1754+
17481755
return;
1756+
}
17491757

17501758
// see if this is a template specialization ...
17511759
if (const auto *TSD = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
@@ -1823,10 +1831,8 @@ void SYCLIntegrationHeader::emit(raw_ostream &O) {
18231831
O << "// This is auto-generated SYCL integration header.\n";
18241832
O << "\n";
18251833

1826-
O << "#include <cstddef>\n";
18271834
O << "#include <CL/sycl/detail/defines.hpp>\n";
18281835
O << "#include <CL/sycl/detail/kernel_desc.hpp>\n";
1829-
O << "using nullptr_t = std::nullptr_t;\n";
18301836

18311837
O << "\n";
18321838

clang/test/CodeGenSYCL/stdtypes_kernel_type.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1+
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsycl-int-header=%t.h -DNULLPTRT_CHECK -verify %s
12
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsycl-int-header=%t.h %s
23
// RUN: FileCheck -input-file=%t.h %s
34
//
4-
// CHECK: #include <cstddef>
5-
// CHECK-NEXT: #include <CL/sycl/detail/defines.hpp>
5+
// CHECK: #include <CL/sycl/detail/defines.hpp>
66
// CHECK-NEXT: #include <CL/sycl/detail/kernel_desc.hpp>
7-
// CHECK-NEXT: using nullptr_t = std::nullptr_t;
87
//
98
// CHECK: static constexpr
109
// CHECK-NEXT: const char* const kernel_names[] = {
11-
// CHECK-NEXT: "_ZTSDn"
1210
// CHECK-NEXT: "_ZTSSt4byte"
1311
// CHECK-NEXT: "_ZTSm",
1412
// CHECK-NEXT: "_ZTSl"
1513
// CHECK-NEXT: };
1614
//
1715
// CHECK: static constexpr
1816
// CHECK-NEXT: const kernel_param_desc_t kernel_signatures[] = {
19-
// CHECK-NEXT: //--- _ZTSDn
20-
// CHECK-EMPTY:
2117
// CHECK-NEXT: //--- _ZTSSt4byte
2218
// CHECK-EMPTY:
2319
// CHECK-NEXT: //--- _ZTSm
@@ -28,13 +24,11 @@
2824
//
2925
// CHECK: static constexpr
3026
// CHECK-NEXT: const unsigned kernel_signature_start[] = {
31-
// CHECK-NEXT: 0, // _ZTSDn
32-
// CHECK-NEXT: 1, // _ZTSSt4byte
33-
// CHECK-NEXT: 2, // _ZTSm
34-
// CHECK-NEXT: 3 // _ZTSl
27+
// CHECK-NEXT: 0, // _ZTSSt4byte
28+
// CHECK-NEXT: 1, // _ZTSm
29+
// CHECK-NEXT: 2 // _ZTSl
3530
// CHECK-NEXT: };
3631

37-
// CHECK: template <> struct KernelInfo<nullptr_t> {
3832
// CHECK: template <> struct KernelInfo<::std::byte> {
3933
// CHECK: template <> struct KernelInfo<unsigned long> {
4034
// CHECK: template <> struct KernelInfo<long> {
@@ -55,7 +49,10 @@ __attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
5549
}
5650

5751
int main() {
58-
kernel_single_task<std::nullptr_t>([]() { usage(); });
52+
#ifdef NULLPTRT_CHECK
53+
// expected-error@+1 {{kernel name cannot be or use std::nullptr_t}}
54+
kernel_single_task<std::nullptr_t>([=]() {});
55+
#endif
5956
kernel_single_task<std::byte>([=]() {});
6057
kernel_single_task<std::size_t>([=]() {});
6158
kernel_single_task<std::ptrdiff_t>([=]() {});

0 commit comments

Comments
 (0)