[libc] gcc errors related to complex floats
$ gcc --version | head -n1
gcc (Debian 14.2.0-3+build3) 14.2.0
$ cmake ../runtimes -G Ninja -DLLVM_ENABLE_RUNTIMES="libc" -DCMAKE_BUILD_TYPE=Release -DLLVM_LIBC_FULL_BUILD=ON
$ ninja libc-unit-tests
...
/android0/llvm-project/libc/src/complex/generic/cimagf128.cpp:16:42: error: ‘cfloat128’ was not declared in this scope; did you mean ‘float128’?
16 | LLVM_LIBC_FUNCTION(float128, cimagf128, (cfloat128 x)) {
| ^~~~~~~~~
/android0/llvm-project/libc/src/__support/common.h:49:64: note: in definition of macro ‘LLVM_LIBC_FUNCTION_IMPL’
49 | #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) type name arglist
| ^~~~~~~
/android0/llvm-project/libc/src/complex/generic/cimagf128.cpp:16:1: note: in expansion of macro ‘LLVM_LIBC_FUNCTION’
16 | LLVM_LIBC_FUNCTION(float128, cimagf128, (cfloat128 x)) {
| ^~~~~~~~~~~~~~~~~~
...
is our compiler detection for complex float support broken?
@llvm/issue-subscribers-libc
Author: Nick Desaulniers (nickdesaulniers)
mmh....Well,
cfloat128should be defined forgcc 14.2with the current definition. Refer: https://godbolt.org/z/aG3xYT3dM- If for some weird reason it is not defined then the CFP128 tests should not be run. Refer: this and this.
@nickdesaulniers , are you maybe aware of some other part of the codebase that should have been updated ? I can't think of any.
Just out of curiosity, i dug deeper into this.
Source Of Error?
The source of error seems to be in the header: libc/include/llvm-libc-types/cfloat128.h
That's the header where the type cfloat128 seems to to define by conditional macros
#if defined(__STDC_IEC_60559_COMPLEX__) && !defined(__clang__)
#if !defined(__cplusplus)
#define LIBC_TYPES_HAS_CFLOAT128
typedef _Complex _Float128 cfloat128;
#elif defined(__GNUC__) && __GNUC__ >= 13
#define LIBC_TYPES_HAS_CFLOAT128
typedef _Complex _Float128 cfloat128;
#endif
...
When i tried to check whether the macros did their job, i noticed that the GNUC block didn't seem to work even though i tried to compile them with:
❯ g++ --version | head -n1
g++ (GCC) 14.2.1 20240912 (Red Hat 14.2.1-3)
But when i tried to isolate the __GNUC__ macro it worked!, so it tried to isolate the parents and Gotcha! __STDC_IEC_60559_COMPLEX__ was the culprit.
Why was it erroneous?
The problem seems to revolve around how that macro is put in place,
__STDC_IEC_60559_COMPLEX__ seems to be one of many macros defined by the compiler (GCC) by implicitly including the stdc-predefs.h header.
but this behaviour seems to be disrupted by certain flags which includes -ffreestanding (ref) and i saw a plethora of them in cmake files.
-ffreestanding # To avoid compiler warnings about calling the main function.
Possible Solutions
- Change
__STDC_IEC_60559_COMPLEX__to__STDC_IEC_559_COMPLEX__ - Add
stdc-predefs.hheader manually - Remove
-ffreestanding(not feasible?)
please correct me if i'm off somewhere. thanks!
@StarOne01 thanks for the catch! I did not have enough time yesterday to dig into this. However, a simpler solution exists. Since the compiler was not complaining about float128 and the macros used for checking both cfloat128 and float128 are the same, I figured Something must be going off with our compiler support. And surely, it turned out that I was misled earlier by a comment in float128.h. You see the comment said that gcc defined __float128 using __SIZEOF_FLOAT128__ and since gcc defined the complex variant of F128 only with _Float128, I figured that checking __SIZEOF_FLOAT128__ for gcc would be redundant. But as it turns out, gcc actually defines both _Float128 and __float128 with that macro which means that _Complex _Float128 was missing for this gcc variant and thus the reason for the missing cfloat128.
That's a clever move, thanks for the explanation @Sh0g0-1758 !
Remove -ffreestanding (not feasible?)
I plan to remove -ffreestanding in https://github.com/llvm/llvm-project/issues/118392. That it controls the definition of __STDC_IEC_60559_COMPLEX__ is surprising!