Skip to content

[libc] Create a separate proxy header for math-function-macros.h #98430

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

Merged
merged 12 commits into from
Jul 28, 2024

Conversation

changkhothuychung
Copy link
Contributor

Fix #98393

@llvmbot llvmbot added the libc label Jul 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 11, 2024

@llvm/pr-subscribers-libc

Author: Nhat Nguyen (changkhothuychung)

Changes

Fix #98393


Full diff: https://github.com/llvm/llvm-project/pull/98430.diff

3 Files Affected:

  • (modified) libc/hdr/CMakeLists.txt (+7)
  • (added) libc/hdr/math_function_macros.h (+14)
  • (modified) libc/hdr/math_macros.h (-2)
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 7549342514304..4c47f38a1ffe8 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -29,6 +29,13 @@ add_proxy_header_library(
     math_macros.h
   FULL_BUILD_DEPENDS
     libc.include.llvm-libc-macros.math_macros
+)
+
+add_proxy_header_library(
+  math_function_macros
+  HDRS
+    math_function_macros.h
+  FULL_BUILD_DEPENDS
     libc.include.math
 )
 
diff --git a/libc/hdr/math_function_macros.h b/libc/hdr/math_function_macros.h
new file mode 100644
index 0000000000000..c2de468af61b0
--- /dev/null
+++ b/libc/hdr/math_function_macros.h
@@ -0,0 +1,14 @@
+//===-- Definition of macros from math.h ----------------------------------===//
+//
+// 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_HDR_MATH_FUNCTION_MACROS_H
+#define LLVM_LIBC_HDR_MATH_FUNCTION_MACROS_H
+
+#include <math.h>
+
+#endif // LLVM_LIBC_HDR_MATH_MACROS_H
diff --git a/libc/hdr/math_macros.h b/libc/hdr/math_macros.h
index d13c5ff7647ad..135ffd498b9a0 100644
--- a/libc/hdr/math_macros.h
+++ b/libc/hdr/math_macros.h
@@ -15,8 +15,6 @@
 
 #else // Overlay mode
 
-#include <math.h>
-
 // Some older math.h header does not have FP_INT_* constants yet.
 #ifndef FP_INT_UPWARD
 #define FP_INT_UPWARD 0

@lntue lntue self-requested a review July 11, 2024 16:42
@@ -21,7 +21,6 @@
#if defined(__GNUC__) && !defined(__clang__)
#define _GLIBCXX_INCLUDE_NEXT_C_HEADERS
#endif
#include <math.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

<math.h> should be included in the overlay mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can you explain me why it is needed in overlay mode ?

Copy link
Contributor

@lntue lntue Jul 12, 2024

Choose a reason for hiding this comment

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

If you look at other proxy headers in libc/hdr/ folder, the structure is that if it is in full build mode (detected with #ifdef LIBC_FULL_BUILD, then we include our own headers in libc/include/llvm-libc-*. OTOH, if it is in overlay mode, we will just use the corresponding system header. And in this case, the corresponding system header is <math.h>.

Copy link
Contributor

Choose a reason for hiding this comment

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

The main purpose of proxy headers in libc/hdr is to provide a clear separation between our own public headers and the system headers, make sure that they won't be mixed and mismatched in different build modes.

@lntue
Copy link
Contributor

lntue commented Jul 12, 2024

When building the current PR's head in full build mode, some of the tests that rely on math function macros failed to build. You should update those tests to use isnan and isinf directly from FPBits class instead.

To reproduce:

$ cmake ../llvm -GNinja -DLLVM_ENABLE_PROJECTS="llvm;clang;compiler-rt;libc" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLLVM_LIBC_FULL_BUILD=ON -DLLVM_LIBC_INCLUDE_SCUDO=ON

$ ninja libc

$ ninja check-libc

Some of the failures:

In file included from /usr/local/google/home/lntue/experiment/llvm-project/libc/test/src/math/fminf_test.cpp:9:
/usr/local/google/home/lntue/experiment/llvm-project/libc/test/src/math/FMinTest.h:68:11: error: use of undeclared identifier 'isnan'
      if (isnan(x) || isinf(x))
          ^
/usr/local/google/home/lntue/experiment/llvm-project/libc/test/src/math/fminf_test.cpp:13:1: note: in instantiation of member function 'FMinTest<float>::testRange' requested here
LIST_FMIN_TESTS(float, LIBC_NAMESPACE::fminf)
^
/usr/local/google/home/lntue/experiment/llvm-project/libc/test/src/math/FMinTest.h:90:37: note: expanded from macro 'LIST_FMIN_TESTS'
  TEST_F(LlvmLibcFMinTest, Range) { testRange(&func); }
                                    ^
/usr/local/google/home/lntue/experiment/llvm-project/libc/test/src/math/FMinTest.h:70:11: error: use of undeclared identifier 'isnan'
      if (isnan(y) || isinf(y))
          ^

@changkhothuychung
Copy link
Contributor Author

FPBits

I dont see isnan and isinf in FPBits class - https://github.com/llvm/llvm-project/blob/main/libc/src/__support/FPUtil/FPBits.h

Which function are you referring to?

@lntue
Copy link
Contributor

lntue commented Jul 23, 2024

@changkhothuychung
Copy link
Contributor Author

@lntue some of the tests derive the type directly, thus I can't use the functions is_nan() and is_inf() from FPBits, do u have any recommendations to handle this? For example this test - https://github.com/llvm/llvm-project/blob/main/libc/test/src/math/atan2f_test.cpp#L80-L81

@lntue
Copy link
Contributor

lntue commented Jul 28, 2024

@lntue some of the tests derive the type directly, thus I can't use the functions is_nan() and is_inf() from FPBits, do u have any recommendations to handle this? For example this test - https://github.com/llvm/llvm-project/blob/main/libc/test/src/math/atan2f_test.cpp#L80-L81

For something like that, you can feed the floating point variables back to FPBits class without issues, like:

   if (FPBits(x).is_inf_or_nan()) ...

@changkhothuychung
Copy link
Contributor Author

https://github.com/llvm/llvm-project/blob/main/libc/test/src/math/atan2f_test.cpp#L80-L81

thanks, and how about this case ? https://github.com/llvm/llvm-project/blob/main/libc/test/src/math/atan2f_test.cpp#L85-L87

Should I create a FPBits from result and perform is_nan and is_inf check for it?

@lntue
Copy link
Contributor

lntue commented Jul 28, 2024

https://github.com/llvm/llvm-project/blob/main/libc/test/src/math/atan2f_test.cpp#L80-L81

thanks, and how about this case ? https://github.com/llvm/llvm-project/blob/main/libc/test/src/math/atan2f_test.cpp#L85-L87

Should I create a FPBits from result and perform is_nan and is_inf check for it?

Yes, that would be the most straightforward way to do it. And for FPBits, is_inf_or_nan() is the same as is_inf() || is_nan()

@changkhothuychung
Copy link
Contributor Author

@lntue do you have any idea why there are some erros with undeclared identifier? I see the file FPBits.h is included.

@lntue
Copy link
Contributor

lntue commented Jul 28, 2024

@lntue do you have any idea why there are some erros with undeclared identifier? I see the file FPBits.h is included.

The failure in explogxf_test is because the fully qualified name for FPBits template is LIBC_NAMESPACE::fputil::FPBits<T>, which is not defined / aliased in that test.

@changkhothuychung
Copy link
Contributor Author

@lntue CI looks good, can you help me confirm the full build mode? my laptop (mac m1) has issues with building libc on full build mode.

@@ -34,7 +36,7 @@ TEST_F(LlvmLibcExplogfTest, ExpInFloatRange) {
return static_cast<float>(result.mh * r);
};
auto f_check = [](float x) -> bool {
return !((isnan(x) || isinf(x) || x < -70 || x > 70 ||
Copy link
Contributor

Choose a reason for hiding this comment

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

this will also need to be fully qualified. Maybe you can have using FPBits = LIBC_NAMESPACE::fputil::FPBits<float> at the beginning of the file, and the all subsequence usage can just be FPBits

@changkhothuychung changkhothuychung requested a review from lntue July 28, 2024 04:20
Copy link
Contributor

@lntue lntue left a comment

Choose a reason for hiding this comment

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

Thanks for fixing the issue!

@lntue lntue merged commit f8f5b17 into llvm:main Jul 28, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc] Create a separate proxy header for math-function-macros.h
3 participants