[libc-commits] [libc] [libc] Fix generated float128 header for aarch64 target. (PR #78017)

via libc-commits libc-commits at lists.llvm.org
Tue Jan 23 05:57:17 PST 2024


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/78017

>From d9c77d02a9de54ff53a30b99133c73447d69671e Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Fri, 12 Jan 2024 21:25:34 -0500
Subject: [PATCH 1/4] [libc] Fix generated float128 header for aarch64 target.

---
 libc/config/linux/api.td                     |  2 +-
 libc/include/CMakeLists.txt                  |  1 +
 libc/include/llvm-libc-types/CMakeLists.txt  |  1 +
 libc/include/llvm-libc-types/float128.h      | 30 ++++++++++++++++++++
 libc/spec/spec.td                            |  2 +-
 libc/spec/stdc.td                            |  1 +
 libc/src/__support/macros/properties/float.h |  9 ++++--
 7 files changed, 41 insertions(+), 5 deletions(-)
 create mode 100644 libc/include/llvm-libc-types/float128.h

diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 85f6b59264eb06d..8d39069a96e48d9 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -55,7 +55,7 @@ def IntTypesAPI : PublicAPI<"inttypes.h"> {
 }
 
 def MathAPI : PublicAPI<"math.h"> {
-  let Types = ["double_t", "float_t"];
+  let Types = ["double_t", "float_t", "float128"];
 }
 
 def FenvAPI: PublicAPI<"fenv.h"> {
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index cec8b86516b51f0..54dc413663e37a3 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -92,6 +92,7 @@ add_gen_header(
     .llvm-libc-macros.math_macros
     .llvm-libc-types.double_t
     .llvm-libc-types.float_t
+    .llvm-libc-types.float128
 )
 
 # TODO: This should be conditional on POSIX networking being included.
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 500900ffa0bbb05..7603eaac811417d 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -96,3 +96,4 @@ add_header(rpc_opcodes_t HDR rpc_opcodes_t.h)
 add_header(ACTION HDR ACTION.h)
 add_header(ENTRY HDR ENTRY.h)
 add_header(struct_hsearch_data HDR struct_hsearch_data.h)
+add_header(float128 HDR float128.h)
diff --git a/libc/include/llvm-libc-types/float128.h b/libc/include/llvm-libc-types/float128.h
new file mode 100644
index 000000000000000..7030384e7d314af
--- /dev/null
+++ b/libc/include/llvm-libc-types/float128.h
@@ -0,0 +1,30 @@
+//===-- Definition of float128 type ---------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __LLVM_LIBC_TYPES_FLOAT128_H__
+#define __LLVM_LIBC_TYPES_FLOAT128_H__
+
+#if defined(__clang__)
+#define LIBC_COMPILER_IS_CLANG
+#define LIBC_COMPILER_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
+#elif defined(__GNUC__)
+#define LIBC_COMPILER_IS_GCC
+#define LIBC_COMPILER_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
+#endif
+
+#if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) &&     \
+    (defined(__aarch64__) || defined(__riscv) || defined(__x86_64__))
+typedef _Float128 float128;
+#elif (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 600)) &&\
+    (defined(__x86_64__) && !defined(__Fuchsia__))
+typedef __float128 float128;
+#elif (LDBL_MANT_DIG == 113) || (__LDBL_MANT_DIG__ == 113)
+typedef long double float128;
+#endif
+
+#endif // __LLVM_LIBC_TYPES_FLOAT128_H__
diff --git a/libc/spec/spec.td b/libc/spec/spec.td
index 9b689b5eb502a9f..5b281a496432fc4 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -51,7 +51,7 @@ def LongDoubleType : NamedType<"long double">;
 def CharType : NamedType<"char">;
 
 // TODO: Add compatibility layer to use C23 type _Float128 if possible.
-def Float128Type : NamedType<"__float128">;
+def Float128Type : NamedType<"float128">;
 
 // Common types
 def VoidPtr : PtrType<VoidType>;
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 99e9b3ca65ca59b..7ea69f00faa6824 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -352,6 +352,7 @@ def StdC : StandardSpec<"stdc"> {
       [
           NamedType<"float_t">,
           NamedType<"double_t">,
+          NamedType<"float128">,
       ],
       [], // Enumerations
       [
diff --git a/libc/src/__support/macros/properties/float.h b/libc/src/__support/macros/properties/float.h
index 98ca2a5d4bc46f0..bf2bcc7ffd9d63f 100644
--- a/libc/src/__support/macros/properties/float.h
+++ b/libc/src/__support/macros/properties/float.h
@@ -19,11 +19,11 @@
 #include <float.h> // LDBL_MANT_DIG
 
 // 'long double' properties.
-#if (LDBL_MANT_DIG == 53)
+#if (LDBL_MANT_DIG == 53) || (__LDBL_MANT_DIG__ == 53)
 #define LIBC_LONG_DOUBLE_IS_FLOAT64
-#elif (LDBL_MANT_DIG == 64)
+#elif (LDBL_MANT_DIG == 64) || (__LDBL_MANT_DIG__ == 64)
 #define LIBC_LONG_DOUBLE_IS_X86_FLOAT80
-#elif (LDBL_MANT_DIG == 113)
+#elif (LDBL_MANT_DIG == 113) || (__LDBL_MANT_DIG__ == 113)
 #define LIBC_LONG_DOUBLE_IS_FLOAT128
 #endif
 
@@ -53,6 +53,9 @@ using float16 = _Float16;
 #endif
 
 // float128 support.
+// If the definition of float128 here is updated, also update
+//   include/llvm-libc-types/float128.h
+// so that the type definition in generated header `math.h` matched.
 #if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) &&     \
     (defined(LIBC_TARGET_ARCH_IS_AARCH64) ||                                   \
      defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) ||                                 \

>From befb8b9331df6a20c5335f9e4f3c10993f28e38a Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Fri, 12 Jan 2024 22:30:22 -0500
Subject: [PATCH 2/4] Fix formatting.

---
 libc/include/llvm-libc-types/float128.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/include/llvm-libc-types/float128.h b/libc/include/llvm-libc-types/float128.h
index 7030384e7d314af..ce3a69ec803455e 100644
--- a/libc/include/llvm-libc-types/float128.h
+++ b/libc/include/llvm-libc-types/float128.h
@@ -20,7 +20,8 @@
 #if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) &&     \
     (defined(__aarch64__) || defined(__riscv) || defined(__x86_64__))
 typedef _Float128 float128;
-#elif (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 600)) &&\
+#elif (defined(LIBC_COMPILER_CLANG_VER) &&                                     \
+       (LIBC_COMPILER_CLANG_VER >= 600)) &&                                    \
     (defined(__x86_64__) && !defined(__Fuchsia__))
 typedef __float128 float128;
 #elif (LDBL_MANT_DIG == 113) || (__LDBL_MANT_DIG__ == 113)

>From 3b377aa1840a03ec16ea0c3c6bd61f3b947b6650 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Mon, 22 Jan 2024 22:46:34 -0500
Subject: [PATCH 3/4] Sync and consolidate float128 type logic in one place.

---
 libc/config/linux/aarch64/entrypoints.txt     |  3 +-
 libc/include/llvm-libc-types/CMakeLists.txt   |  8 ++++-
 libc/include/llvm-libc-types/float128.h       | 32 +++++++++++++-----
 .../macros/properties/CMakeLists.txt          |  1 +
 libc/src/__support/macros/properties/float.h  | 33 +++----------------
 5 files changed, 37 insertions(+), 40 deletions(-)

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 3f66a582f5e3ee3..37197a26e16d69f 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -360,8 +360,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 if(LIBC_COMPILER_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C23 _Float128 entrypoints
-    # Temporarily disabled since float128 isn't working on the aarch64 buildbot
-    # libc.src.math.fabsf128
+    libc.src.math.fabsf128
   )
 endif()
 
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 7603eaac811417d..75c4685bc53def6 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -96,4 +96,10 @@ add_header(rpc_opcodes_t HDR rpc_opcodes_t.h)
 add_header(ACTION HDR ACTION.h)
 add_header(ENTRY HDR ENTRY.h)
 add_header(struct_hsearch_data HDR struct_hsearch_data.h)
-add_header(float128 HDR float128.h)
+add_header(
+  float128
+  HDR
+    float128.h
+  DEPENDS
+    libc.include.llvm-libc-macros.float_macros
+)
diff --git a/libc/include/llvm-libc-types/float128.h b/libc/include/llvm-libc-types/float128.h
index ce3a69ec803455e..61dfb6474691888 100644
--- a/libc/include/llvm-libc-types/float128.h
+++ b/libc/include/llvm-libc-types/float128.h
@@ -9,23 +9,37 @@
 #ifndef __LLVM_LIBC_TYPES_FLOAT128_H__
 #define __LLVM_LIBC_TYPES_FLOAT128_H__
 
+#include <llvm-libc-macros/float-macros.h> // LDBL_MANT_DIG
+
+// Define temporary compiler and its version
 #if defined(__clang__)
-#define LIBC_COMPILER_IS_CLANG
-#define LIBC_COMPILER_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
+#define __LIBC_COMPILER_IS_CLANG
+#define __LIBC_COMPILER_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
 #elif defined(__GNUC__)
-#define LIBC_COMPILER_IS_GCC
-#define LIBC_COMPILER_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
-#endif
+#define __LIBC_COMPILER_IS_GCC
+#define __LIBC_COMPILER_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
+#endif // __clang__, __GNUC__
 
-#if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) &&     \
+#if (defined(__LIBC_COMPILER_GCC_VER) && (__LIBC_COMPILER_GCC_VER >= 1301)) && \
     (defined(__aarch64__) || defined(__riscv) || defined(__x86_64__))
+#define LIBC_COMPILER_HAS_C23_FLOAT128
 typedef _Float128 float128;
-#elif (defined(LIBC_COMPILER_CLANG_VER) &&                                     \
-       (LIBC_COMPILER_CLANG_VER >= 600)) &&                                    \
+#elif (defined(__LIBC_COMPILER_CLANG_VER) &&                                   \
+       (__LIBC_COMPILER_CLANG_VER >= 600)) &&                                  \
     (defined(__x86_64__) && !defined(__Fuchsia__))
+#define LIBC_COMPILER_HAS_FLOAT128_EXTENSION
 typedef __float128 float128;
-#elif (LDBL_MANT_DIG == 113) || (__LDBL_MANT_DIG__ == 113)
+#elif (LDBL_MANT_DIG == 113)
 typedef long double float128;
 #endif
 
+// Clean up temporary macros
+#ifdef __LIBC_COMPILER_IS_CLANG
+#undef __LIBC_COMPILER_IS_CLANG
+#undef __LIBC_COMPILER_CLANG_VER
+#elif defined(__LIBC_COMPILER_IS_GCC)
+#undef __LIBC_COMPILER_IS_GCC
+#undef __LIBC_COMPILER_GCC_VER
+#endif // __LIBC_COMPILER_IS_CLANG, __LIBC_COMPILER_IS_GCC
+
 #endif // __LLVM_LIBC_TYPES_FLOAT128_H__
diff --git a/libc/src/__support/macros/properties/CMakeLists.txt b/libc/src/__support/macros/properties/CMakeLists.txt
index ee87ce68c9da37d..b309c052a590776 100644
--- a/libc/src/__support/macros/properties/CMakeLists.txt
+++ b/libc/src/__support/macros/properties/CMakeLists.txt
@@ -33,4 +33,5 @@ add_header_library(
     .compiler
     .cpu_features
     .os
+    libc.include.llvm-libc-types.float128
 )
diff --git a/libc/src/__support/macros/properties/float.h b/libc/src/__support/macros/properties/float.h
index bf2bcc7ffd9d63f..510f39237493581 100644
--- a/libc/src/__support/macros/properties/float.h
+++ b/libc/src/__support/macros/properties/float.h
@@ -11,19 +11,19 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
 #define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
 
+#include "llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG
+#include "llvm-libc-types/float128.h"      // float128
 #include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
 #include "src/__support/macros/properties/cpu_features.h"
 #include "src/__support/macros/properties/os.h"
 
-#include <float.h> // LDBL_MANT_DIG
-
 // 'long double' properties.
-#if (LDBL_MANT_DIG == 53) || (__LDBL_MANT_DIG__ == 53)
+#if (LDBL_MANT_DIG == 53)
 #define LIBC_LONG_DOUBLE_IS_FLOAT64
-#elif (LDBL_MANT_DIG == 64) || (__LDBL_MANT_DIG__ == 64)
+#elif (LDBL_MANT_DIG == 64)
 #define LIBC_LONG_DOUBLE_IS_X86_FLOAT80
-#elif (LDBL_MANT_DIG == 113) || (__LDBL_MANT_DIG__ == 113)
+#elif (LDBL_MANT_DIG == 113)
 #define LIBC_LONG_DOUBLE_IS_FLOAT128
 #endif
 
@@ -53,29 +53,6 @@ using float16 = _Float16;
 #endif
 
 // float128 support.
-// If the definition of float128 here is updated, also update
-//   include/llvm-libc-types/float128.h
-// so that the type definition in generated header `math.h` matched.
-#if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) &&     \
-    (defined(LIBC_TARGET_ARCH_IS_AARCH64) ||                                   \
-     defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) ||                                 \
-     defined(LIBC_TARGET_ARCH_IS_X86_64))
-#define LIBC_COMPILER_HAS_C23_FLOAT128
-#endif
-#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 600)) &&  \
-    (defined(LIBC_TARGET_ARCH_IS_X86_64) &&                                    \
-     defined(LIBC_TARGET_OS_IS_LINUX) && !defined(LIBC_TARGET_OS_IS_FUCHSIA))
-#define LIBC_COMPILER_HAS_FLOAT128_EXTENSION
-#endif
-
-#if defined(LIBC_COMPILER_HAS_C23_FLOAT128)
-using float128 = _Float128;
-#elif defined(LIBC_COMPILER_HAS_FLOAT128_EXTENSION)
-using float128 = __float128;
-#elif defined(LIBC_LONG_DOUBLE_IS_FLOAT128)
-using float128 = long double;
-#endif
-
 #if defined(LIBC_COMPILER_HAS_C23_FLOAT128) ||                                 \
     defined(LIBC_COMPILER_HAS_FLOAT128_EXTENSION) ||                           \
     defined(LIBC_LONG_DOUBLE_IS_FLOAT128)

>From 0babc3ea6b507a7b0abe73439bf42b077b111958 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Tue, 23 Jan 2024 08:56:32 -0500
Subject: [PATCH 4/4] Add missing dependency and fix __copied_hdr__ target
 issues.

---
 libc/cmake/modules/LLVMLibCLibraryRules.cmake    | 12 +++++++++++-
 libc/include/llvm-libc-macros/float-macros.h     | 16 ++++++++++++++++
 libc/include/llvm-libc-types/float128.h          |  2 +-
 .../__support/macros/properties/CMakeLists.txt   |  1 +
 libc/src/__support/macros/properties/float.h     |  4 ++--
 5 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCLibraryRules.cmake b/libc/cmake/modules/LLVMLibCLibraryRules.cmake
index e6cfd79e65a085a..adb3bdeea2cb3bc 100644
--- a/libc/cmake/modules/LLVMLibCLibraryRules.cmake
+++ b/libc/cmake/modules/LLVMLibCLibraryRules.cmake
@@ -173,7 +173,17 @@ function(create_header_library fq_target_name)
   target_sources(${fq_target_name} INTERFACE ${ADD_HEADER_HDRS})
   if(ADD_HEADER_DEPENDS)
     add_dependencies(${fq_target_name} ${ADD_HEADER_DEPENDS})
-    target_link_libraries(${fq_target_name} INTERFACE ${ADD_HEADER_DEPENDS})
+
+    # `*.__copied_hdr__` is created only to copy the header files to the target
+    # location, not to be linked against.
+    set(link_lib "")
+    foreach(dep ${ADD_HEADER_DEPENDS})
+      if (NOT dep MATCHES "__copied_hdr__")
+        list(APPEND link_lib ${dep})
+      endif()
+    endforeach()
+
+    target_link_libraries(${fq_target_name} INTERFACE ${link_lib})
   endif()
   if(ADD_HEADER_COMPILE_OPTIONS)
     target_compile_options(${fq_target_name} INTERFACE ${ADD_HEADER_COMPILE_OPTIONS})
diff --git a/libc/include/llvm-libc-macros/float-macros.h b/libc/include/llvm-libc-macros/float-macros.h
index 8e4b91b9a0f557d..79aa0fd62aee8ef 100644
--- a/libc/include/llvm-libc-macros/float-macros.h
+++ b/libc/include/llvm-libc-macros/float-macros.h
@@ -9,8 +9,24 @@
 #ifndef __LLVM_LIBC_MACROS_FLOAT_MACROS_H
 #define __LLVM_LIBC_MACROS_FLOAT_MACROS_H
 
+#if __has_include_next(<float.h>)
+
+// Suppress `#include_next is a language extension` warnings.
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wgnu-include-next"
+#else // gcc
+#pragma GCC system_header
+#endif // __clang__, __GNUC__
+
 #include_next <float.h>
 
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif // __clang__
+
+#endif // __has_include_next(<float.h>)
+
 #ifndef FLT_RADIX
 #define FLT_RADIX __FLT_RADIX__
 #endif // FLT_RADIX
diff --git a/libc/include/llvm-libc-types/float128.h b/libc/include/llvm-libc-types/float128.h
index 61dfb6474691888..f592fc43b17c43e 100644
--- a/libc/include/llvm-libc-types/float128.h
+++ b/libc/include/llvm-libc-types/float128.h
@@ -9,7 +9,7 @@
 #ifndef __LLVM_LIBC_TYPES_FLOAT128_H__
 #define __LLVM_LIBC_TYPES_FLOAT128_H__
 
-#include <llvm-libc-macros/float-macros.h> // LDBL_MANT_DIG
+#include <include/llvm-libc-macros/float-macros.h> // LDBL_MANT_DIG
 
 // Define temporary compiler and its version
 #if defined(__clang__)
diff --git a/libc/src/__support/macros/properties/CMakeLists.txt b/libc/src/__support/macros/properties/CMakeLists.txt
index b309c052a590776..3c492ab55a90cb5 100644
--- a/libc/src/__support/macros/properties/CMakeLists.txt
+++ b/libc/src/__support/macros/properties/CMakeLists.txt
@@ -33,5 +33,6 @@ add_header_library(
     .compiler
     .cpu_features
     .os
+    libc.include.llvm-libc-macros.float_macros
     libc.include.llvm-libc-types.float128
 )
diff --git a/libc/src/__support/macros/properties/float.h b/libc/src/__support/macros/properties/float.h
index 510f39237493581..08a1ab726cbde46 100644
--- a/libc/src/__support/macros/properties/float.h
+++ b/libc/src/__support/macros/properties/float.h
@@ -11,8 +11,8 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
 #define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
 
-#include "llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG
-#include "llvm-libc-types/float128.h"      // float128
+#include "include/llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG
+#include "include/llvm-libc-types/float128.h"      // float128
 #include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
 #include "src/__support/macros/properties/cpu_features.h"



More information about the libc-commits mailing list