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/7] [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/7] 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())) {

From 5fbbf99968d6c5e53e3d411106c9f07b26f9a69c Mon Sep 17 00:00:00 2001
From: Gergely Meszaros <gergely.mesza...@intel.com>
Date: Thu, 7 Aug 2025 08:20:31 -0700
Subject: [PATCH 3/7] Fix file copyright header

---
 clang/lib/Sema/AttributeLangSupport.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/lib/Sema/AttributeLangSupport.h 
b/clang/lib/Sema/AttributeLangSupport.h
index 2cebcd39fb572..24bef9ee70943 100644
--- a/clang/lib/Sema/AttributeLangSupport.h
+++ b/clang/lib/Sema/AttributeLangSupport.h
@@ -1,5 +1,4 @@
-//===- 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.

From a247f8220387d27c1f778190a9dc4b5bd9aa2abe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20Gergely?= <gergely.mesza...@intel.com>
Date: Fri, 8 Aug 2025 14:24:32 +0200
Subject: [PATCH 4/7] Update file header

Co-authored-by: Alexey Bader <alexey.ba...@intel.com>
---
 clang/lib/Sema/AttributeLangSupport.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/lib/Sema/AttributeLangSupport.h 
b/clang/lib/Sema/AttributeLangSupport.h
index 24bef9ee70943..91a3511c2c794 100644
--- a/clang/lib/Sema/AttributeLangSupport.h
+++ b/clang/lib/Sema/AttributeLangSupport.h
@@ -3,6 +3,7 @@
 // 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

From bc24ace7531c096a10e2c2919e0039588e508f18 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20Gergely?= <gergely.mesza...@intel.com>
Date: Fri, 8 Aug 2025 14:25:30 +0200
Subject: [PATCH 5/7] Add assert invariant message

Co-authored-by: Alexey Bader <alexey.ba...@intel.com>
---
 clang/lib/Sema/SemaType.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index bb57ca9eb8169..d745ddb1a00a0 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -6575,7 +6575,7 @@ static void HandleAddressSpaceTypeAttribute(QualType 
&Type,
       ASIdx = Attr.asHLSLLangAS();
 
     if (ASIdx == LangAS::Default) {
-      assert(S.getLangOpts().SYCLIsDevice || S.getLangOpts().HLSL);
+      assert((S.getLangOpts().SYCLIsDevice || S.getLangOpts().HLSL) && 
"Unexpected language mode");
       S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
           << Attr
           << (S.getLangOpts().SYCLIsDevice ? AttributeLangSupport::SYCLDevice

From 458354ec755d19fa5c183c2f1aeab7cda71e9979 Mon Sep 17 00:00:00 2001
From: Gergely Meszaros <gergely.mesza...@intel.com>
Date: Fri, 8 Aug 2025 05:39:30 -0700
Subject: [PATCH 6/7] Fix clang format

---
 clang/lib/Sema/SemaType.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index d745ddb1a00a0..7a8a6fd4aa891 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -6575,7 +6575,8 @@ static void HandleAddressSpaceTypeAttribute(QualType 
&Type,
       ASIdx = Attr.asHLSLLangAS();
 
     if (ASIdx == LangAS::Default) {
-      assert((S.getLangOpts().SYCLIsDevice || S.getLangOpts().HLSL) && 
"Unexpected language mode");
+      assert((S.getLangOpts().SYCLIsDevice || S.getLangOpts().HLSL) &&
+             "Unexpected language mode");
       S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
           << Attr
           << (S.getLangOpts().SYCLIsDevice ? AttributeLangSupport::SYCLDevice

From fa25abfae6a484a89dc520c97569bd65b20ff9ba Mon Sep 17 00:00:00 2001
From: Gergely Meszaros <gergely.mesza...@intel.com>
Date: Tue, 19 Aug 2025 02:33:03 -0700
Subject: [PATCH 7/7] Reject in SYCL host compilation mode too

instead of just in SYCL device mode.
---
 clang/docs/ReleaseNotes.rst                      |  2 +-
 clang/include/clang/Basic/DiagnosticSemaKinds.td |  2 +-
 clang/lib/Sema/AttributeLangSupport.h            |  2 +-
 clang/lib/Sema/SemaType.cpp                      | 10 +++++-----
 clang/test/Sema/named_addrspace_lang.c           |  6 +++---
 5 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0b71ad32d6a86..c9fe340335b9b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -161,7 +161,7 @@ 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.
+- Fix a crash when using opencl address space attributes in HLSL or SYCL.
   (#GH152460). 
 
 Bug Fixes to Compiler Builtins
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 41a9d26673e7e..4766206a6451d 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++|HLSL|Objective-C|SYCL when 
compiling for the device}1">;
+  "%0 attribute is not supported in %select{C|C++|HLSL|Objective-C|SYCL}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
index 91a3511c2c794..efabf8fd7c9b0 100644
--- a/clang/lib/Sema/AttributeLangSupport.h
+++ b/clang/lib/Sema/AttributeLangSupport.h
@@ -24,7 +24,7 @@ enum LANG {
   Cpp,
   HLSL,
   ObjC,
-  SYCLDevice,
+  SYCL,
 };
 } // end namespace clang::AttributeLangSupport
 
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 7a8a6fd4aa891..aab5b7b4e32af 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -6569,18 +6569,18 @@ static void HandleAddressSpaceTypeAttribute(QualType 
&Type,
       Attr.setInvalid();
   } else {
     // The keyword-based type attributes imply which address space to use.
-    ASIdx = S.getLangOpts().SYCLIsDevice ? Attr.asSYCLLangAS()
-                                         : Attr.asOpenCLLangAS();
+    ASIdx =
+        S.getLangOpts().isSYCL() ? Attr.asSYCLLangAS() : Attr.asOpenCLLangAS();
     if (S.getLangOpts().HLSL)
       ASIdx = Attr.asHLSLLangAS();
 
     if (ASIdx == LangAS::Default) {
-      assert((S.getLangOpts().SYCLIsDevice || S.getLangOpts().HLSL) &&
+      assert((S.getLangOpts().isSYCL() || S.getLangOpts().HLSL) &&
              "Unexpected language mode");
       S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
           << Attr
-          << (S.getLangOpts().SYCLIsDevice ? AttributeLangSupport::SYCLDevice
-                                           : AttributeLangSupport::HLSL);
+          << (S.getLangOpts().isSYCL() ? AttributeLangSupport::SYCL
+                                       : AttributeLangSupport::HLSL);
       Attr.setInvalid();
       return;
     }
diff --git a/clang/test/Sema/named_addrspace_lang.c 
b/clang/test/Sema/named_addrspace_lang.c
index f5a343d8d051a..fdb81d119c4cf 100644
--- a/clang/test/Sema/named_addrspace_lang.c
+++ b/clang/test/Sema/named_addrspace_lang.c
@@ -1,5 +1,5 @@
 // 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-host -verify=sycl %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
 
@@ -11,8 +11,8 @@
 // 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}}
+// sycl-error@#constant{{'opencl_constant' attribute is not supported in SYCL}}
+// sycl-error@#generic{{'opencl_generic' attribute is not supported in SYCL}}
 
 int __attribute__((opencl_constant)) glob = 1; // #constant
 int __attribute__((opencl_generic)) gen; // #generic

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

Reply via email to