[clang] [clang][SYCL] Add additional Sema rules for SYCL kernel parameters (PR #208571)

Tom Honermann via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 31 10:57:29 PDT 2026


================
@@ -213,3 +188,163 @@ void test() {
 }
 
 } // namespace badref7
+
+
+#include <stdatomic.h>
+// Check for atomic parameters and subobjects.
+namespace atomic1 {
+// Kernel entry point template definition.
+template<typename KNT, typename T>
+[[clang::sycl_kernel_entry_point(KNT)]]
+void kernel_single_task(T t) {} // expected-note-re {{within parameter 't' of type '(lambda at {{.*}})' declared here}}
+                                // expected-note-re at -1 {{within parameter 't' of type '(lambda at {{.*}})' declared here}}
+                                // expected-note-re at -2 {{within parameter 't' of type '(lambda at {{.*}})' declared here}}
+                                // expected-note-re at -3 {{within parameter 't' of type '(lambda at {{.*}})' declared here}}
+                                // expected-note at -4 {{within parameter 't' of type 'atomic1::Kernel' declared here}}
+
+struct Sa { 
+  int a;
+  _Atomic int b; // expected-error {{'_Atomic(int)' cannot be used as the type of a kernel parameter}}
+                 // expected-note at -3 {{within field of type 'Sa' declared here}}
+                 // expected-error at -2 {{'_Atomic(int)' cannot be used as the type of a kernel parameter}}
+                 // expected-note at -5 {{within field of type 'Sa' declared here}}
+                 // expected-error at -4 {{'_Atomic(int)' cannot be used as the type of a kernel parameter}}
+                 // expected-note at -7 {{within field of type 'Sa' declared here}}
+};
+
+class Kernel {
+  Sa data{1, 2}; // expected-note at -1 {{within field of type 'Kernel' declared here}}
+public:
+  void operator()() { }
+};
+
+void test() {
+  _Atomic int a = 0;
+  _Atomic(int) b = 2;
+  Sa s{1, 2};
+  Sa arr[] = {s, s};
+  kernel_single_task<class KN<15>>([=]{ (void)a; });
+  // expected-error at -1 {{'_Atomic(int)' cannot be used as the type of a kernel parameter}}
+  // expected-note-re at -2 {{in instantiation of function template specialization 'atomic1::kernel_single_task<KN<{{[0-9]+}}>, {{.*}}>' requested here}}
+  // expected-note at -3 {{within capture 'a' of lambda expression here}}
+  kernel_single_task<class KN<16>>([=]{ (void)b; });
+  // expected-error at -1 {{'_Atomic(int)' cannot be used as the type of a kernel parameter}}
+  // expected-note-re at -2 {{in instantiation of function template specialization 'atomic1::kernel_single_task<KN<{{[0-9]+}}>, {{.*}}>' requested here}}
+  // expected-note at -3 {{within capture 'b' of lambda expression here}}
+  kernel_single_task<class KN<17>>([=]{ (void)s; });
+  // expected-note-re at -1 {{in instantiation of function template specialization 'atomic1::kernel_single_task<KN<{{[0-9]+}}>, {{.*}}>' requested here}}
+  // expected-note at -2 {{within capture 's' of lambda expression here}}
+  kernel_single_task<class KN<18>>([=]{ (void)arr; });
+  // expected-note-re at -1 {{in instantiation of function template specialization 'atomic1::kernel_single_task<KN<{{[0-9]+}}>, {{.*}}>' requested here}}
+  // expected-note at -2 {{within capture 'arr' of lambda expression here}}
+  kernel_single_task<class KN<19>>(Kernel{});
+  // expected-note-re at -1 {{in instantiation of function template specialization 'atomic1::kernel_single_task<KN<{{[0-9]+}}>, {{.*}}>' requested here}}
+}
+
+} // namespace atomic1
+
+// Check for flexible array members -- would not be copyable to device
+namespace fam1 {
+// Kernel entry point template definition.
+template<typename KNT, typename T>
+[[clang::sycl_kernel_entry_point(KNT)]]
+void kernel_single_task(T t) {} // expected-note {{within parameter 't' of type 'fam1::Kernel' declared here}}
+
+struct FAM { 
+  int a;
+  int b[];
+};
+
+class Kernel { // expected-note {{within field of type 'Kernel' declared here}}
+  FAM fam; // expected-error {{'FAM' contains a flexible array member and cannot be used as a SYCL kernel parameter}}
----------------
tahonermann wrote:

Actually, I don't think that (extra) AST recursion is needed. The subobject visitation will naturally recurse through class types that are a FAM because they contain a FAM. I think we just need to guard emit of the diagnostic both on `isStructureTypeWithFlexibleArrayMember()` being true and on the last non-static data member being an array with unknown bound (not an incomplete array type as I stated in my last comment; my mistake).

https://github.com/llvm/llvm-project/pull/208571


More information about the cfe-commits mailing list