git: dbbac83e39fa - stable/12 - Apply clang fixes for crashes compiling ARM/AArch64 CRC intrinsics

From: Dimitry Andric <dim_at_FreeBSD.org>
Date: Thu, 01 Jun 2023 19:17:02 UTC
The branch stable/12 has been updated by dim:

URL: https://cgit.FreeBSD.org/src/commit/?id=dbbac83e39faca07c6081cd549128c253861cdac

commit dbbac83e39faca07c6081cd549128c253861cdac
Author:     Dimitry Andric <dim@FreeBSD.org>
AuthorDate: 2023-05-29 14:27:58 +0000
Commit:     Dimitry Andric <dim@FreeBSD.org>
CommitDate: 2023-06-01 19:16:11 +0000

    Apply clang fixes for crashes compiling ARM/AArch64 CRC intrinsics
    
    Merge commit 069ecd0c6e2c from llvm-project (by Fangrui Song):
    
      [ARM] Check target feature support for __builtin_arm_crc*
    
      `__builtin_arm_crc*` requires the target feature crc which is available on armv8
      and above. Calling the fuctions for armv7 leads to a SelectionDAG crash.
    
      ```
      % clang -c --target=armv7-unknown-linux-gnueabi -c a.c
      fatal error: error in backend: Cannot select: intrinsic %llvm.arm.crc32b
      PLEASE submit a bug report to ...
      ```
    
      Add `TARGET_BUILTIN` and define required features for these builtins to
      report an error in `CodeGenFunction::checkTargetFeatures`. The problem is quite widespread.
      I will add `TARGET_BUILTIN` for more builtins later.
    
      Fix https://github.com/llvm/llvm-project/issues/57802
    
      Differential Revision: https://reviews.llvm.org/D134127
    
    Merge commit b2d7a0dcf1ff from llvm-project (by Fangrui Song):
    
      [AArch64] Check target feature support for __builtin_arm_crc*
    
      This is the AArch64 counterpart of D134127.
      Daniel Kiss will change more `BUILTIN` to `TARGET_BUILTIN`.
    
      Fix #57802
    
    Note that programs attempting to use ARM/AArch64 CRC intrinsics, when
    they are not supported by the targeted CPU, will still receive a regular
    compilation error (instead of a fatal backend error) similar to:
    
      7zCrc.c:4:10: error: '__builtin_arm_crc32b' needs target feature crc
        return __builtin_arm_crc32b(a, b);
               ^
    
    Reported by:    Alastair Hogge <agh@riseup.net>
    PR:             271624
    MFC after:      3 days
    
    (cherry picked from commit 8792c03886d9e6df10fbac825819603dc059c7ea)
---
 .../clang/include/clang/Basic/BuiltinsAArch64.def    | 20 ++++++++++++--------
 .../clang/include/clang/Basic/BuiltinsARM.def        | 20 ++++++++++++--------
 .../llvm-project/clang/lib/Basic/Targets/AArch64.cpp |  2 ++
 contrib/llvm-project/clang/lib/Basic/Targets/ARM.cpp |  2 ++
 4 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/contrib/llvm-project/clang/include/clang/Basic/BuiltinsAArch64.def b/contrib/llvm-project/clang/include/clang/Basic/BuiltinsAArch64.def
index 634bcaed20a6..244280a68d75 100644
--- a/contrib/llvm-project/clang/include/clang/Basic/BuiltinsAArch64.def
+++ b/contrib/llvm-project/clang/include/clang/Basic/BuiltinsAArch64.def
@@ -17,6 +17,10 @@
 #   define LANGBUILTIN(ID, TYPE, ATTRS, BUILTIN_LANG) BUILTIN(ID, TYPE, ATTRS)
 #endif
 
+#if defined(BUILTIN) && !defined(TARGET_BUILTIN)
+#  define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) BUILTIN(ID, TYPE, ATTRS)
+#endif
+
 #if defined(BUILTIN) && !defined(TARGET_HEADER_BUILTIN)
 #  define TARGET_HEADER_BUILTIN(ID, TYPE, ATTRS, HEADER, LANG, FEATURE) BUILTIN(ID, TYPE, ATTRS)
 #endif
@@ -45,14 +49,14 @@ BUILTIN(__builtin_arm_sev, "v", "")
 BUILTIN(__builtin_arm_sevl, "v", "")
 
 // CRC32
-BUILTIN(__builtin_arm_crc32b, "UiUiUc", "nc")
-BUILTIN(__builtin_arm_crc32cb, "UiUiUc", "nc")
-BUILTIN(__builtin_arm_crc32h, "UiUiUs", "nc")
-BUILTIN(__builtin_arm_crc32ch, "UiUiUs", "nc")
-BUILTIN(__builtin_arm_crc32w, "UiUiUi", "nc")
-BUILTIN(__builtin_arm_crc32cw, "UiUiUi", "nc")
-BUILTIN(__builtin_arm_crc32d, "UiUiWUi", "nc")
-BUILTIN(__builtin_arm_crc32cd, "UiUiWUi", "nc")
+TARGET_BUILTIN(__builtin_arm_crc32b, "UiUiUc", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32cb, "UiUiUc", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32h, "UiUiUs", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32ch, "UiUiUs", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32w, "UiUiUi", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32cw, "UiUiUi", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32d, "UiUiWUi", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32cd, "UiUiWUi", "nc", "crc")
 
 // Memory Tagging Extensions (MTE)
 BUILTIN(__builtin_arm_irg, "v*v*Ui", "t")
diff --git a/contrib/llvm-project/clang/include/clang/Basic/BuiltinsARM.def b/contrib/llvm-project/clang/include/clang/Basic/BuiltinsARM.def
index be20c24aa28a..0e98f8680063 100644
--- a/contrib/llvm-project/clang/include/clang/Basic/BuiltinsARM.def
+++ b/contrib/llvm-project/clang/include/clang/Basic/BuiltinsARM.def
@@ -17,6 +17,10 @@
 #   define LANGBUILTIN(ID, TYPE, ATTRS, BUILTIN_LANG) BUILTIN(ID, TYPE, ATTRS)
 #endif
 
+#if defined(BUILTIN) && !defined(TARGET_BUILTIN)
+#  define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) BUILTIN(ID, TYPE, ATTRS)
+#endif
+
 #if defined(BUILTIN) && !defined(TARGET_HEADER_BUILTIN)
 #  define TARGET_HEADER_BUILTIN(ID, TYPE, ATTRS, HEADER, LANG, FEATURE) BUILTIN(ID, TYPE, ATTRS)
 #endif
@@ -157,14 +161,14 @@ BUILTIN(__builtin_arm_mrrc, "LLUiUIiUIiUIi", "")
 BUILTIN(__builtin_arm_mrrc2, "LLUiUIiUIiUIi", "")
 
 // CRC32
-BUILTIN(__builtin_arm_crc32b, "UiUiUc", "nc")
-BUILTIN(__builtin_arm_crc32cb, "UiUiUc", "nc")
-BUILTIN(__builtin_arm_crc32h, "UiUiUs", "nc")
-BUILTIN(__builtin_arm_crc32ch, "UiUiUs", "nc")
-BUILTIN(__builtin_arm_crc32w, "UiUiUi", "nc")
-BUILTIN(__builtin_arm_crc32cw, "UiUiUi", "nc")
-BUILTIN(__builtin_arm_crc32d, "UiUiLLUi", "nc")
-BUILTIN(__builtin_arm_crc32cd, "UiUiLLUi", "nc")
+TARGET_BUILTIN(__builtin_arm_crc32b, "UiUiUc", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32cb, "UiUiUc", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32h, "UiUiUs", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32ch, "UiUiUs", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32w, "UiUiUi", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32cw, "UiUiUi", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32d, "UiUiLLUi", "nc", "crc")
+TARGET_BUILTIN(__builtin_arm_crc32cd, "UiUiLLUi", "nc", "crc")
 
 // ARMv8-M Security Extensions a.k.a CMSE
 BUILTIN(__builtin_arm_cmse_TT, "Uiv*", "n")
diff --git a/contrib/llvm-project/clang/lib/Basic/Targets/AArch64.cpp b/contrib/llvm-project/clang/lib/Basic/Targets/AArch64.cpp
index e163ebfa2348..c4ab1bc961af 100644
--- a/contrib/llvm-project/clang/lib/Basic/Targets/AArch64.cpp
+++ b/contrib/llvm-project/clang/lib/Basic/Targets/AArch64.cpp
@@ -35,6 +35,8 @@ const Builtin::Info AArch64TargetInfo::BuiltinInfo[] = {
    {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
 #define LANGBUILTIN(ID, TYPE, ATTRS, LANG)                                     \
   {#ID, TYPE, ATTRS, nullptr, LANG, nullptr},
+#define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE)                               \
+  {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
 #define TARGET_HEADER_BUILTIN(ID, TYPE, ATTRS, HEADER, LANGS, FEATURE)         \
   {#ID, TYPE, ATTRS, HEADER, LANGS, FEATURE},
 #include "clang/Basic/BuiltinsAArch64.def"
diff --git a/contrib/llvm-project/clang/lib/Basic/Targets/ARM.cpp b/contrib/llvm-project/clang/lib/Basic/Targets/ARM.cpp
index 0e4048f8d5ff..36d3b34a897a 100644
--- a/contrib/llvm-project/clang/lib/Basic/Targets/ARM.cpp
+++ b/contrib/llvm-project/clang/lib/Basic/Targets/ARM.cpp
@@ -895,6 +895,8 @@ const Builtin::Info ARMTargetInfo::BuiltinInfo[] = {
   {#ID, TYPE, ATTRS, nullptr, LANG, nullptr},
 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER)                                    \
   {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
+#define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE)                               \
+  {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
 #define TARGET_HEADER_BUILTIN(ID, TYPE, ATTRS, HEADER, LANGS, FEATURE)         \
   {#ID, TYPE, ATTRS, HEADER, LANGS, FEATURE},
 #include "clang/Basic/BuiltinsARM.def"