Skip to content
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

Found one more delta to unbreak build for z/os #82789

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions compiler-rt/lib/builtins/fp_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,10 @@ static __inline fp_t __compiler_rt_fmax(fp_t x, fp_t y) {
#endif
}

#elif defined(QUAD_PRECISION) && defined(CRT_HAS_TF_MODE)
#elif defined(QUAD_PRECISION)
#if defined(CRT_HAS_TF_MODE) && defined(CRT_HAS_IEEE_TF)
Copy link
Member

Choose a reason for hiding this comment

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

The functions below work without int128, so this makes sense to me, but it would be good to check that this doesn't break 32-bit sparc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rorth can you confirm this works with 32-bit sparc (replacing your change in #101662 with this one). Thanks

Copy link
Collaborator

Choose a reason for hiding this comment

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

While this does work with gcc-14, it breaks when using clang-20 instead: 26 builtins files fail with the likes of

FAILED: projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-sparc.dir/extendhftf2.c.o 
/vol/llvm/src/llvm-project/dist/compiler-rt/lib/builtins/fp_lib.h:411:2: error: Unsupported TF mode type
  411 | #error Unsupported TF mode type
      |  ^

long double on SPARC is a royal mess, unfortunately: while the SPARC psABI (both 32 and 64-bit) requires long double to be 128 bit (although no current hardware does support that) and Solaris follows the spec, Linux/sparc64 chose to ignore that, keeping long double as 64 bit. While gcc gets this right, clang never did.

The following shows the values of the relevant macros:

       	      	  	clang-20		gcc-14

  QUAD_PRECISION	defined			defined
  CRT_HAS_TF_MODE	undef			undef
  			CRT_HAS_128BIT && CRT_HAS_F128
  CRT_HAS_IEEE_TF	undef			defined
  			same as next + __LDBL_MANT_DIG__ == 113
  CRT_LDBL_128BIT	undef	       		defined
  			__LDBL_MANT_DIG__ == 113 || (__FLT_RADIX__ == 16 && __LDBL_MANT_DIG__ == 28)

  CRT_HAS_F128		undef			defined
  __LDBL_MANT_DIG__	53			113
  __FLT_RADIX__		2			2

Due to all this, dealing with long double on SPARC is fragile as hell...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given what you have said, I would suggest the real fix for SPARC is to change the list of source files for SPARC so you exclude all of the source files related to QUAD_PRECISSION. This looks like you just need to skip adding GENERIC_TF_SOURCES to the list of source files for SPARC.

Copy link
Member

Choose a reason for hiding this comment

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

So Clang and GCC disagree on the Linux ABI here? It sounds like clang should be following GCC and using 128-bit long double since that will be used for all existing Linux code?

If there is not 128-bit floating point type on sparc (at least with clang), the it sounds to me like it should not be building the tf files at all?

It looks like GCC uses IEEE 128-bit long double, so maybe we can just hoist the ifdefs above the fp_lib.h include?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rorth Ping. Have you looked at this? I would like to get the z/os builds working again.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So Clang and GCC disagree on the Linux ABI here? It sounds like clang should be following GCC and using 128-bit long double since that will be used for all existing Linux code?

No: Solaris/sparc follows the ELF SPARC psABI which dictates that long double be 128-bit. While Linux/sparc, generally following the psABI, it chose to ignore that particular part of the spec and went for 64-bit long double instead. clang should indeed match the psABI/GCC/Solaris libc, but fixing this is way beyond my abilities, and LLVM SPARC maintenance these days is limited.

If there is not 128-bit floating point type on sparc (at least with clang), the it sounds to me like it should not be building the tf files at all?

There is on Solaris, but it's soft-float only.

It looks like GCC uses IEEE 128-bit long double, so maybe we can just hoist the ifdefs above the fp_lib.h include?

I've meanwhile found that the builtins situation is even messier than I thought: until LLVM 17, libclang_rt.builtins-sparc.a did contain __divtc3 and__multc3. Sometime before LLVM 18, those definitions got lost (and apparently there are no checks that the builtins interface remains stable). So instead of cementing that regression, the definitions should be restored, not removed for good. I've started looking into when the removal happened, but unfortunately so many intermediate revisions don't even build that this is very hard to do ;-(

What I believe should happen first is identify the revision that caused this breakage, than look into fixing that to continue working on SPARC together with whatever it was meant to achieve.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the update.

Also look at the compiler side. Can the compiler generate calls to __divtc3 and __multc3 or any of the TF functions? If the compiler can't generate a call to these TF functions then it is ok to remove them from the archive even though LLVM 17 had them in it. I'd also look at what code would be in the __divtc3 in LLVM 17 if you can't provide that logfb function. How did that file compile in the first place?

When making your patch can you start with this change so we are building on top of the same code. And when you are ready ping me and I will try your change on z/OS.

// The generic implementation only works for ieee754 floating point. For other
// floating point types, continue to rely on the libm implementation for now.
#if defined(CRT_HAS_IEEE_TF)
static __inline tf_float __compiler_rt_logbtf(tf_float x) {
return __compiler_rt_logbX(x);
}
Expand Down
Loading