[clang] [clang][SYCL] Add sycl_external attribute and restrict emitting device code (PR #140282)

Tom Honermann via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 24 10:31:48 PDT 2025


================
@@ -0,0 +1,54 @@
+// RUN: %clang_cc1 -fsycl-is-device -fsyntax-only -verify %s
+
+// Semantic tests for sycl_external attribute
+
+[[clang::sycl_external]] // expected-error {{'sycl_external' can only be applied to functions with external linkage}}
+static void func1() {}
+
+namespace {
+  [[clang::sycl_external]] // expected-error {{'sycl_external' can only be applied to functions with external linkage}}
+  void func2() {}
+
+  struct UnnX {};
+}
+
+[[clang::sycl_external]] // expected-error {{'sycl_external' can only be applied to functions with external linkage}}
+  void func4(UnnX) {}
+
+// FIXME: The first declaration of a function is required to have the attribute.
+// The attribute may be optionally present on subsequent declarations
+int foo(int c);
+
+[[clang::sycl_external]] void foo();
+
+class C {
+  [[clang::sycl_external]] void member();
+};
+
+[[clang::sycl_external]] int main() // expected-error {{'sycl_external' cannot be applied to main function}}
+{
+    return 0;
+}
+
+class D {
+  [[clang::sycl_external]] void del() = delete; // expected-error {{'sycl_external' cannot be applied to explicitly deleted functions}}
+};
+
+class A {
+  [[clang::sycl_external]]
+  A() {}
+
+  [[clang::sycl_external]] void func3() {}
+};
+
+class B {
+public:
+  [[clang::sycl_external]] virtual void foo() {}
+
+  [[clang::sycl_external]] virtual void bar() = 0;
+};
+
+[[clang::sycl_external]] int *func0() { return nullptr; }
+
+[[clang::sycl_external]] void func2(int *) {}
+
----------------
tahonermann wrote:

Let's add tests for functions declared `constexpr` and `consteval` as well. `consteval` requires C++20, so the `RUN` line will have to be updated accordingly. I suggest running the test for both C++17 and C++20 with the portion that exercises C++20 features appropriately #ifdef'd.

I wonder if we should reject use of the attribute with functions declared `consteval` since no symbols are emitted for such functions. I'm on the fence since the attribute doesn't do any harm there. Perhaps a warning about a useless attribute use would be appropriate?

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


More information about the cfe-commits mailing list