git: 8792c03886d9 - main - Apply clang fixes for crashes compiling ARM/AArch64 CRC intrinsics

From: Dimitry Andric <dim_at_FreeBSD.org>
Date: Mon, 29 May 2023 14:28:58 UTC
The branch main has been updated by dim:

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

commit 8792c03886d9e6df10fbac825819603dc059c7ea
Author:     Dimitry Andric <dim@FreeBSD.org>
AuthorDate: 2023-05-29 14:27:58 +0000
Commit:     Dimitry Andric <dim@FreeBSD.org>
CommitDate: 2023-05-29 14:28:18 +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
---
 .../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 65ab4fcced9a..3c0a5f12681d 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 0cea0a9051e5..53fe64c317d1 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 8d8972c1613a..22b7cf4d0c7e 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 b2f61cff81c9..159b00dea5bb 100644
--- a/contrib/llvm-project/clang/lib/Basic/Targets/ARM.cpp
+++ b/contrib/llvm-project/clang/lib/Basic/Targets/ARM.cpp
@@ -979,6 +979,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"