[compiler-rt] ce6d40f - [compiler-rt] [builtins] Detect _Float16 support at compile time

Dimitry Andric via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 29 11:59:17 PDT 2022


Author: Dimitry Andric
Date: 2022-07-29T20:58:52+02:00
New Revision: ce6d40f5c23923a807388c58b82b4c343eced0ce

URL: https://github.com/llvm/llvm-project/commit/ce6d40f5c23923a807388c58b82b4c343eced0ce
DIFF: https://github.com/llvm/llvm-project/commit/ce6d40f5c23923a807388c58b82b4c343eced0ce.diff

LOG: [compiler-rt] [builtins] Detect _Float16 support at compile time

Instead of detecting `_Float16` support at CMake configuration time,
detect it at compile time by checking for the predefined (by the
compiler) macro `__FLT16_MAX__` instead.

This solves the issue where compiler-rt is built simultaneously for both
x86_64 and i386 targets, and the CMake configuration uses x86_64
compilation to detect `_Float16` support, while it may not be supported
by the i386 target (if it does not have SSE2).

While here, rename `COMPILERT_RT_HAS_FLOAT16` to `CRT_HAS_FLOAT16`, to
conform more to the naming style used in `int_lib.h` and `int_types.h`.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D130718

Added: 
    

Modified: 
    compiler-rt/cmake/builtin-config-ix.cmake
    compiler-rt/lib/builtins/CMakeLists.txt
    compiler-rt/lib/builtins/extendhftf2.c
    compiler-rt/lib/builtins/fp_extend.h
    compiler-rt/lib/builtins/fp_trunc.h
    compiler-rt/lib/builtins/int_types.h
    compiler-rt/lib/builtins/trunctfhf2.c
    compiler-rt/test/builtins/CMakeLists.txt
    compiler-rt/test/builtins/Unit/extendhftf2_test.c
    compiler-rt/test/builtins/Unit/fp_test.h
    compiler-rt/test/builtins/Unit/trunctfhf2_test.c

Removed: 
    


################################################################################
diff  --git a/compiler-rt/cmake/builtin-config-ix.cmake b/compiler-rt/cmake/builtin-config-ix.cmake
index 9cbadf33e2574..ce7e0a7f400e8 100644
--- a/compiler-rt/cmake/builtin-config-ix.cmake
+++ b/compiler-rt/cmake/builtin-config-ix.cmake
@@ -22,14 +22,6 @@ int foo(int x, int y) {
 }
 ")
 
-builtin_check_c_compiler_source(COMPILER_RT_HAS_FLOAT16
-"
-_Float16 foo(_Float16 x) {
- return x;
-}
-"
-)
-
 builtin_check_c_compiler_source(COMPILER_RT_HAS_ASM_LSE
 "
 asm(\".arch armv8-a+lse\");

diff  --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 0b91b4ce79933..8efc6d13aa085 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -695,8 +695,6 @@ else ()
     append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full BUILTIN_CFLAGS)
   endif()
 
-  append_list_if(COMPILER_RT_HAS_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS)
-
   append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 BUILTIN_CFLAGS)
 
   # These flags would normally be added to CMAKE_C_FLAGS by the llvm

diff  --git a/compiler-rt/lib/builtins/extendhftf2.c b/compiler-rt/lib/builtins/extendhftf2.c
index aefe9737d34f9..196f8c9abb3ba 100644
--- a/compiler-rt/lib/builtins/extendhftf2.c
+++ b/compiler-rt/lib/builtins/extendhftf2.c
@@ -11,7 +11,7 @@
 #include "fp_lib.h"
 
 #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) &&                     \
-    defined(COMPILER_RT_HAS_FLOAT16)
+    defined(CRT_HAS_FLOAT16)
 #define SRC_HALF
 #define DST_QUAD
 #include "fp_extend_impl.inc"

diff  --git a/compiler-rt/lib/builtins/fp_extend.h b/compiler-rt/lib/builtins/fp_extend.h
index eee4722bf90e6..7ba89f7f0a15d 100644
--- a/compiler-rt/lib/builtins/fp_extend.h
+++ b/compiler-rt/lib/builtins/fp_extend.h
@@ -40,7 +40,7 @@ static __inline int src_rep_t_clz(src_rep_t a) {
 }
 
 #elif defined SRC_HALF
-#ifdef COMPILER_RT_HAS_FLOAT16
+#ifdef CRT_HAS_FLOAT16
 typedef _Float16 src_t;
 #else
 typedef uint16_t src_t;

diff  --git a/compiler-rt/lib/builtins/fp_trunc.h b/compiler-rt/lib/builtins/fp_trunc.h
index 7a54564a3520a..b92acd8070f53 100644
--- a/compiler-rt/lib/builtins/fp_trunc.h
+++ b/compiler-rt/lib/builtins/fp_trunc.h
@@ -50,7 +50,7 @@ typedef uint32_t dst_rep_t;
 static const int dstSigBits = 23;
 
 #elif defined DST_HALF
-#ifdef COMPILER_RT_HAS_FLOAT16
+#ifdef CRT_HAS_FLOAT16
 typedef _Float16 dst_t;
 #else
 typedef uint16_t dst_t;

diff  --git a/compiler-rt/lib/builtins/int_types.h b/compiler-rt/lib/builtins/int_types.h
index 9ee5a327b28a4..8bbdab5c2ecc1 100644
--- a/compiler-rt/lib/builtins/int_types.h
+++ b/compiler-rt/lib/builtins/int_types.h
@@ -165,6 +165,10 @@ typedef struct {
 #define HAS_80_BIT_LONG_DOUBLE 0
 #endif
 
+#ifdef __FLT16_MAX__
+#define CRT_HAS_FLOAT16
+#endif
+
 #if CRT_HAS_FLOATING_POINT
 typedef union {
   uqwords u;

diff  --git a/compiler-rt/lib/builtins/trunctfhf2.c b/compiler-rt/lib/builtins/trunctfhf2.c
index e3a2309d954bc..b66b4351f0c1e 100644
--- a/compiler-rt/lib/builtins/trunctfhf2.c
+++ b/compiler-rt/lib/builtins/trunctfhf2.c
@@ -11,7 +11,7 @@
 #include "fp_lib.h"
 
 #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) &&                     \
-    defined(COMPILER_RT_HAS_FLOAT16)
+    defined(CRT_HAS_FLOAT16)
 #define SRC_QUAD
 #define DST_HALF
 #include "fp_trunc_impl.inc"

diff  --git a/compiler-rt/test/builtins/CMakeLists.txt b/compiler-rt/test/builtins/CMakeLists.txt
index ceacbd8335804..2a69f59812140 100644
--- a/compiler-rt/test/builtins/CMakeLists.txt
+++ b/compiler-rt/test/builtins/CMakeLists.txt
@@ -44,19 +44,6 @@ foreach(arch ${BUILTIN_TEST_ARCH})
     string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
   endif()
 
-  if(APPLE)
-    # TODO: Support the new ABI on Apple platforms.
-    if (${arch} MATCHES "arm|aarch64|arm64" AND COMPILER_RT_HAS_FLOAT16)
-      list(APPEND BUILTINS_TEST_TARGET_CFLAGS -DCOMPILER_RT_HAS_FLOAT16)
-      string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
-    endif()
-  else()
-    if (${arch} MATCHES "arm|aarch64|arm64|i?86|x86_64|AMD64|riscv32|riscv64" AND COMPILER_RT_HAS_FLOAT16)
-      list(APPEND BUILTINS_TEST_TARGET_CFLAGS -DCOMPILER_RT_HAS_FLOAT16)
-      string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
-    endif()
-  endif()
-
   if(COMPILER_RT_ENABLE_CET)
     if(NOT arch MATCHES "i?86|x86_64|AMD64")
       message(SEND_ERROR "${arch} does not support CET")

diff  --git a/compiler-rt/test/builtins/Unit/extendhftf2_test.c b/compiler-rt/test/builtins/Unit/extendhftf2_test.c
index 7d3ea3049e8a1..43a5d44a38ec7 100644
--- a/compiler-rt/test/builtins/Unit/extendhftf2_test.c
+++ b/compiler-rt/test/builtins/Unit/extendhftf2_test.c
@@ -4,7 +4,7 @@
 #include "int_lib.h"
 #include <stdio.h>
 
-#if __LDBL_MANT_DIG__ == 113 && defined(COMPILER_RT_HAS_FLOAT16)
+#if __LDBL_MANT_DIG__ == 113 && defined(CRT_HAS_FLOAT16)
 
 #include "fp_test.h"
 
@@ -28,7 +28,7 @@ char assumption_1[sizeof(TYPE_FP16) * CHAR_BIT == 16] = {0};
 #endif
 
 int main() {
-#if __LDBL_MANT_DIG__ == 113 && defined(COMPILER_RT_HAS_FLOAT16)
+#if __LDBL_MANT_DIG__ == 113 && defined(CRT_HAS_FLOAT16)
   // qNaN
   if (test__extendhftf2(makeQNaN16(),
                         UINT64_C(0x7fff800000000000),

diff  --git a/compiler-rt/test/builtins/Unit/fp_test.h b/compiler-rt/test/builtins/Unit/fp_test.h
index e54dfc108e718..6fc80ac88011e 100644
--- a/compiler-rt/test/builtins/Unit/fp_test.h
+++ b/compiler-rt/test/builtins/Unit/fp_test.h
@@ -1,10 +1,11 @@
-#include <stdlib.h>
+#include "int_lib.h"
 #include <limits.h>
-#include <string.h>
 #include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
 
-#ifdef COMPILER_RT_HAS_FLOAT16
-#define TYPE_FP16 _Float16
+#ifdef CRT_HAS_FLOAT16
+#  define TYPE_FP16 _Float16
 #else
 #define TYPE_FP16 uint16_t
 #endif
@@ -15,10 +16,10 @@ enum EXPECTED_RESULT {
 
 static inline TYPE_FP16 fromRep16(uint16_t x)
 {
-#ifdef COMPILER_RT_HAS_FLOAT16
-    TYPE_FP16 ret;
-    memcpy(&ret, &x, sizeof(ret));
-    return ret;
+#ifdef CRT_HAS_FLOAT16
+  TYPE_FP16 ret;
+  memcpy(&ret, &x, sizeof(ret));
+  return ret;
 #else
     return x;
 #endif
@@ -50,7 +51,7 @@ static inline long double fromRep128(uint64_t hi, uint64_t lo)
 
 static inline uint16_t toRep16(TYPE_FP16 x)
 {
-#ifdef COMPILER_RT_HAS_FLOAT16
+#ifdef CRT_HAS_FLOAT16
     uint16_t ret;
     memcpy(&ret, &x, sizeof(ret));
     return ret;

diff  --git a/compiler-rt/test/builtins/Unit/trunctfhf2_test.c b/compiler-rt/test/builtins/Unit/trunctfhf2_test.c
index 9c8c4afd1b835..e4489d2741d1d 100644
--- a/compiler-rt/test/builtins/Unit/trunctfhf2_test.c
+++ b/compiler-rt/test/builtins/Unit/trunctfhf2_test.c
@@ -4,7 +4,7 @@
 #include "int_lib.h"
 #include <stdio.h>
 
-#if __LDBL_MANT_DIG__ == 113 && defined(COMPILER_RT_HAS_FLOAT16)
+#if __LDBL_MANT_DIG__ == 113 && defined(CRT_HAS_FLOAT16)
 
 #include "fp_test.h"
 
@@ -27,7 +27,7 @@ char assumption_1[sizeof(TYPE_FP16) * CHAR_BIT == 16] = {0};
 #endif
 
 int main() {
-#if __LDBL_MANT_DIG__ == 113 && defined(COMPILER_RT_HAS_FLOAT16)
+#if __LDBL_MANT_DIG__ == 113 && defined(CRT_HAS_FLOAT16)
   // qNaN
   if (test__trunctfhf2(makeQNaN128(),
                        UINT16_C(0x7e00)))


        


More information about the llvm-commits mailing list