[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:47 PDT 2025


================
@@ -202,6 +202,25 @@ void SemaSYCL::handleKernelAttr(Decl *D, const ParsedAttr &AL) {
   handleSimpleAttribute<DeviceKernelAttr>(*this, D, AL);
 }
 
+void SemaSYCL::handleExternalAttr(Decl *D, const ParsedAttr &AL) {
+  auto *FD = cast<FunctionDecl>(D);
+  if (!FD->isExternallyVisible()) {
+    Diag(AL.getLoc(), diag::err_sycl_attribute_invalid_linkage);
+    return;
+  }
+  std::string FunctionName = StringRef(FD->getNameInfo().getAsString()).lower();
+  if (FunctionName.find("main") != std::string::npos) {
+    Diag(AL.getLoc(), diag::err_sycl_attribute_avoid_main);
+    return;
+  }
+  if (FD->isDeleted()) {
+    Diag(AL.getLoc(), diag::err_sycl_attribute_avoid_deleted_function);
+    return;
+  }
----------------
tahonermann wrote:

I think this should call `isDeletedAsWritten()` so that it doesn't check for functions that are explicitly defaulted but defined as deleted. See additional test comments.

Also, I think this diagnostic would be better done in `Sema::CheckFunctionDeclaration()`; probably near the call to `CheckSYCLEntryPointFunctionDecl()`. Per my other comment, perhaps it makes sense to call a `CheckSYCLExternalFunctionDecl()` function there.

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


More information about the cfe-commits mailing list