Skip to content

[builtins] Support building the 128-bit float functions on i386 #122658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zhangtianhao6
Copy link

GCC provides these functions (e.g.__subtf3) in libgcc on i386.
Since Clang supports float128, we can also enable the existing code in i386 for ABI compatible.

#121757

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

Copy link

github-actions bot commented Jan 13, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 211bcf67aadb1175af382f55403ae759177281c7 fe61e4aeae112ec6a1ed1d3d2bd0537a582eea9f --extensions c -- compiler-rt/lib/builtins/extendxftf2.c compiler-rt/lib/builtins/trunctfxf2.c compiler-rt/test/builtins/Unit/extendxftf2_test.c compiler-rt/test/builtins/Unit/trunctfxf2_test.c
View the diff from clang-format here.
diff --git a/compiler-rt/test/builtins/Unit/extendxftf2_test.c b/compiler-rt/test/builtins/Unit/extendxftf2_test.c
index e82dd67404..d5bec2b463 100644
--- a/compiler-rt/test/builtins/Unit/extendxftf2_test.c
+++ b/compiler-rt/test/builtins/Unit/extendxftf2_test.c
@@ -7,7 +7,7 @@
 #if __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__)) &&   \
     (defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__))
 
-#include "fp_test.h"
+#  include "fp_test.h"
 
 COMPILER_RT_ABI tf_float __extendxftf2(long double a);
 
diff --git a/compiler-rt/test/builtins/Unit/trunctfxf2_test.c b/compiler-rt/test/builtins/Unit/trunctfxf2_test.c
index ea88732aa7..bd195d64c4 100644
--- a/compiler-rt/test/builtins/Unit/trunctfxf2_test.c
+++ b/compiler-rt/test/builtins/Unit/trunctfxf2_test.c
@@ -7,7 +7,7 @@
 #if __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__)) &&   \
     (defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__))
 
-#include "fp_test.h"
+#  include "fp_test.h"
 
 COMPILER_RT_ABI long double __trunctfxf2(tf_float a);
 

@@ -12,7 +12,7 @@
#define QUAD_PRECISION
#include "fp_lib.h"

#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enable __i386__ in extendxftf2_test.c

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, enabled.

@@ -12,7 +12,7 @@
#define QUAD_PRECISION
#include "fp_lib.h"

#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enable i386 in trunctfxf2_test.c

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, enabled.

@zhangtianhao6 zhangtianhao6 force-pushed the i386-support-compiler-rt-fp128-builtin branch 2 times, most recently from 627acf7 to 26f0383 Compare January 13, 2025 07:20
GCC provides these functions (e.g.__subtf3) in libgcc on i386.
Since Clang supports float128, we can also enable the existing code in i386 for ABI compatible.
@zhangtianhao6 zhangtianhao6 force-pushed the i386-support-compiler-rt-fp128-builtin branch from 26f0383 to fe61e4a Compare January 13, 2025 07:29
@hstk30-hw
Copy link
Contributor

@tru Take a look, the code_formatter suggestion is weird.

@@ -12,7 +12,8 @@
#define QUAD_PRECISION
#include "fp_lib.h"

#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is concerning. Not all 32-bit environments support FP80. Concretely, Windows, FreeBSD, and Darwin definitely do support FP80 even though the hardware does. This can make it easy to get ABI breakages.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about only enable for linux

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should actually be checking HAS_80_BIT_LONG_DOUBLE, which is set correctly depending on environments that support it:

#if (defined(__i386__) || defined(__x86_64__)) && \

Suggested change
#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && \
#if defined(CRT_HAS_TF_MODE) && HAS_80_BIT_LONG_DOUBLE

@@ -12,7 +12,8 @@
#define QUAD_PRECISION
#include "fp_lib.h"

#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise

Copy link
Member

@arichardson arichardson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks this mostly looks good just a few suggestions

@@ -906,7 +907,7 @@ else ()

# For RISCV32, we must force enable int128 for compiling long
# double routines.
if(COMPILER_RT_ENABLE_SOFTWARE_INT128 OR "${arch}" STREQUAL "riscv32")
if(COMPILER_RT_ENABLE_SOFTWARE_INT128 OR "${arch}" STREQUAL "riscv32" OR "${arch}" STREQUAL "i386")
list(APPEND BUILTIN_CFLAGS_${arch} -fforce-enable-int128)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we support other compilers? If so enabling this unconditionally for i386 might break them?

@@ -12,7 +12,8 @@
#define QUAD_PRECISION
#include "fp_lib.h"

#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should actually be checking HAS_80_BIT_LONG_DOUBLE, which is set correctly depending on environments that support it:

#if (defined(__i386__) || defined(__x86_64__)) && \

Suggested change
#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && \
#if defined(CRT_HAS_TF_MODE) && HAS_80_BIT_LONG_DOUBLE

Comment on lines +15 to +16
#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && \
(defined(__x86_64__) || defined(__i386__))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && \
(defined(__x86_64__) || defined(__i386__))
#if defined(CRT_HAS_TF_MODE) && HAS_80_BIT_LONG_DOUBLE

As above

Comment on lines +7 to 8
#if __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__)) && \
(defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__)) && \
(defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__))
#if defined(CRT_HAS_TF_MODE) && HAS_80_BIT_LONG_DOUBLE

Since we include int_lib.h should be possible to simplify this

Comment on lines +31 to 32
#if __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__)) && \
(defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__)) && \
(defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__))
#if defined(CRT_HAS_TF_MODE) && HAS_80_BIT_LONG_DOUBLE

Since we include int_lib.h should be possible to simplify this

Comment on lines +7 to 8
#if __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__)) && \
(defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__)) && \
(defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__))
#if defined(CRT_HAS_TF_MODE) && HAS_80_BIT_LONG_DOUBLE

Since we include int_lib.h should be possible to simplify this

Comment on lines +31 to 32
#if __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__)) && \
(defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if __LDBL_MANT_DIG__ == 64 && (defined(__x86_64__) || defined(__i386__)) && \
(defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__))
#if defined(CRT_HAS_TF_MODE) && HAS_80_BIT_LONG_DOUBLE

Since we include int_lib.h should be possible to simplify this

@@ -1,10 +1,10 @@
// RUN: %clang_builtins %s %librt -o %t && %run %t
// RUN: %clang_builtins %s %librt -fforce-enable-int128 -o %t && %run %t
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will adding this flag unconditionally break other/older compilers? Maybe this needs to be a lit substitution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants