https://github.com/Maetveis updated 
https://github.com/llvm/llvm-project/pull/152528

From f99c79a54f3c79fab1bda94c2d0e0491cf29fb39 Mon Sep 17 00:00:00 2001
From: Gergely Meszaros <gergely.mesza...@intel.com>
Date: Thu, 7 Aug 2025 07:58:29 -0700
Subject: [PATCH 1/2] [Clang][Sema] Reject unsupported opencl address space
 attributes in SYCL and HLSL

Raise an error instead of executing unreachable when an unsupported named
address space attribute is used in SYCL or HLSL.
Arguably some of these could be accepted, but in the interest of simplicity
reject them for now instead of crashing.

Fixes: #152460
---
 clang/docs/ReleaseNotes.rst                   |  2 ++
 .../clang/Basic/DiagnosticSemaKinds.td        |  2 +-
 clang/lib/Sema/AttributeLangSupport.h         | 30 +++++++++++++++++++
 clang/lib/Sema/SemaDeclAttr.cpp               |  9 +-----
 clang/lib/Sema/SemaType.cpp                   | 13 ++++++--
 clang/test/Sema/named_addrspace_lang.c        | 25 ++++++++++++++++
 6 files changed, 70 insertions(+), 11 deletions(-)
 create mode 100644 clang/lib/Sema/AttributeLangSupport.h
 create mode 100644 clang/test/Sema/named_addrspace_lang.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0e9fcaa5fac6a..0b71ad32d6a86 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -161,6 +161,8 @@ Bug Fixes in This Version
   targets that treat ``_Float16``/``__fp16`` as native scalar types. Previously
   the warning was silently lost because the operands differed only by an 
implicit
   cast chain. (#GH149967).
+- Fix a crash when using opencl address space attributes in HLSL or SYCL 
device code.
+  (#GH152460). 
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index cf23594201143..41a9d26673e7e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4660,7 +4660,7 @@ def err_attribute_regparm_wrong_platform : Error<
 def err_attribute_regparm_invalid_number : Error<
   "'regparm' parameter must be between 0 and %0 inclusive">;
 def err_attribute_not_supported_in_lang : Error<
-  "%0 attribute is not supported in %select{C|C++|Objective-C}1">;
+  "%0 attribute is not supported in %select{C|C++|HLSL|Objective-C|SYCL when 
compiling for the device}1">;
 def err_attribute_not_supported_on_arch
     : Error<"%0 attribute is not supported on '%1'">;
 def warn_gcc_ignores_type_attr : Warning<
diff --git a/clang/lib/Sema/AttributeLangSupport.h 
b/clang/lib/Sema/AttributeLangSupport.h
new file mode 100644
index 0000000000000..601cd6729d154
--- /dev/null
+++ b/clang/lib/Sema/AttributeLangSupport.h
@@ -0,0 +1,30 @@
+//===- AttributeLangSupport.h ---------------------------------  -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the declaration of AttributeLangSupport, which is used 
in
+/// diagnostics to indicate the language in which an attribute is (not) 
supported.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_SEMA_ATTRIBUTE_LANG_SUPPORT_H
+#define LLVM_CLANG_SEMA_ATTRIBUTE_LANG_SUPPORT_H
+
+
+// NOTE: The order should match the order of the %select in 
err_attribute_not_supported_in_lang
+//       in DiagnosticSemaKinds.td
+namespace clang::AttributeLangSupport {
+enum LANG {
+  C,
+  Cpp,
+  HLSL,
+  ObjC,
+  SYCLDevice,
+};
+} // end namespace clang::AttributeLangSupport
+
+#endif // LLVM_CLANG_SEMA_ATTRIBUTE_LANG_SUPPORT_H
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index f5f18b06c2fff..145d03e4324c3 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -10,6 +10,7 @@
 //
 
//===----------------------------------------------------------------------===//
 
+#include "AttributeLangSupport.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTMutationListener.h"
@@ -73,14 +74,6 @@
 using namespace clang;
 using namespace sema;
 
-namespace AttributeLangSupport {
-  enum LANG {
-    C,
-    Cpp,
-    ObjC
-  };
-} // end namespace AttributeLangSupport
-
 static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
   // FIXME: Include the type in the argument list.
   return AL.getNumArgs() + AL.hasParsedType();
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 1289bede3f192..6f4d7a086d40c 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -10,6 +10,7 @@
 //
 
//===----------------------------------------------------------------------===//
 
+#include "AttributeLangSupport.h"
 #include "TypeLocBuilder.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
@@ -6573,8 +6574,16 @@ static void HandleAddressSpaceTypeAttribute(QualType 
&Type,
     if (S.getLangOpts().HLSL)
       ASIdx = Attr.asHLSLLangAS();
 
-    if (ASIdx == LangAS::Default)
-      llvm_unreachable("Invalid address space");
+    if (ASIdx == LangAS::Default) {
+      assert(S.getLangOpts().SYCLIsDevice || S.getLangOpts().HLSL);
+      S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
+          << Attr
+          << (S.getLangOpts().SYCLIsDevice ? AttributeLangSupport::SYCLDevice
+                                           : AttributeLangSupport::HLSL);
+      Attr.setInvalid();
+      return;
+    }
+      
 
     if (DiagnoseMultipleAddrSpaceAttributes(S, Type.getAddressSpace(), ASIdx,
                                             Attr.getLoc())) {
diff --git a/clang/test/Sema/named_addrspace_lang.c 
b/clang/test/Sema/named_addrspace_lang.c
new file mode 100644
index 0000000000000..f5a343d8d051a
--- /dev/null
+++ b/clang/test/Sema/named_addrspace_lang.c
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -x c++ -fsycl-is-host -verify %s
+// RUN: %clang_cc1 -fsyntax-only -x c++ -fsycl-is-device -verify=sycl %s
+// RUN: %clang_cc1 -fsyntax-only -x hlsl -triple 
dxil-pc-shadermodel6.3-library -verify=hlsl %s
+
+// hlsl-error@#constant{{'opencl_constant' attribute is not supported in HLSL}}
+// hlsl-error@#generic{{'opencl_generic' attribute is not supported in HLSL}}
+// hlsl-error@#global{{'opencl_global' attribute is not supported in HLSL}}
+// hlsl-error@#global_host{{'opencl_global_host' attribute is not supported in 
HLSL}}
+// hlsl-error@#global_device{{'opencl_global_device' attribute is not 
supported in HLSL}}
+// hlsl-error@#local{{'opencl_local' attribute is not supported in HLSL}}
+// hlsl-error@#private{{'opencl_private' attribute is not supported in HLSL}}
+
+// sycl-error@#constant{{'opencl_constant' attribute is not supported in SYCL 
when compiling for the device}}
+// sycl-error@#generic{{'opencl_generic' attribute is not supported in SYCL 
when compiling for the device}}
+
+int __attribute__((opencl_constant)) glob = 1; // #constant
+int __attribute__((opencl_generic)) gen; // #generic
+int __attribute__((opencl_global)) c; // #global
+int __attribute__((opencl_global_host)) h; // #global_host
+int __attribute__((opencl_global_device)) d; // #global_device
+int __attribute__((opencl_local)) l; // #local
+int __attribute__((opencl_private)) p; // #private
+
+// expected-no-diagnostics

From 242fd05564b47e1bdf5622b2b4fd179ad4fd8f2d Mon Sep 17 00:00:00 2001
From: Gergely Meszaros <gergely.mesza...@intel.com>
Date: Thu, 7 Aug 2025 08:16:27 -0700
Subject: [PATCH 2/2] Apply clang-format

---
 clang/lib/Sema/AttributeLangSupport.h | 11 ++++++-----
 clang/lib/Sema/SemaType.cpp           |  1 -
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Sema/AttributeLangSupport.h 
b/clang/lib/Sema/AttributeLangSupport.h
index 601cd6729d154..2cebcd39fb572 100644
--- a/clang/lib/Sema/AttributeLangSupport.h
+++ b/clang/lib/Sema/AttributeLangSupport.h
@@ -1,4 +1,5 @@
-//===- AttributeLangSupport.h ---------------------------------  -*- C++ 
-*-===//
+//===- AttributeLangSupport.h ---------------------------------  -*- C++
+//-*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,16 +8,16 @@
 ///
 /// \file
 /// This file contains the declaration of AttributeLangSupport, which is used 
in
-/// diagnostics to indicate the language in which an attribute is (not) 
supported.
+/// diagnostics to indicate the language in which an attribute is (not)
+/// supported.
 ///
 
//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_CLANG_SEMA_ATTRIBUTE_LANG_SUPPORT_H
 #define LLVM_CLANG_SEMA_ATTRIBUTE_LANG_SUPPORT_H
 
-
-// NOTE: The order should match the order of the %select in 
err_attribute_not_supported_in_lang
-//       in DiagnosticSemaKinds.td
+// NOTE: The order should match the order of the %select in
+//       err_attribute_not_supported_in_lang in DiagnosticSemaKinds.td
 namespace clang::AttributeLangSupport {
 enum LANG {
   C,
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 6f4d7a086d40c..bb57ca9eb8169 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -6583,7 +6583,6 @@ static void HandleAddressSpaceTypeAttribute(QualType 
&Type,
       Attr.setInvalid();
       return;
     }
-      
 
     if (DiagnoseMultipleAddrSpaceAttributes(S, Type.getAddressSpace(), ASIdx,
                                             Attr.getLoc())) {

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to