Page MenuHomePhabricator

[libc++] Rewrite <cmath>
AbandonedPublicDraft

Authored by philnik on Sep 24 2022, 5:14 AM.

Details

Reviewers
ldionne
Mordante
var-const
huixie90
Group Reviewers
Restricted Project
Summary

The <cmath> implementation has a few things that I wanted to change. I originally planned to do them one at a time, but noticed that the changes essentially amount to a rewrite of the header, so I did that instead.

Specifically the changes are:

  • Replacing _LIBCPP_INLINE_VISIBILITY with _LIBCPP_HIDE_FROM_ABI
  • Moving the enable_ifs from the return type to a defaulted template parameter
  • Removing the libcpp prefix on the argument names
  • Granularizing the header
  • Moving the functions into the versioned namespace
  • Using builtins for the math functions

The libcpp prefix has been added for compatibility with newlib in D5080, but I think we can remove the workaround. We use __x elsewhere and newlib doesn't use __x in it's implementation since 2015 for GCC 2.97 and later. I think even older newlibs shouldn't be affected, since we don't rely on the macros from the libc with this patch.

The main functional change here is switching to builtins for all functions. This has multiple benefits:

  • We can provide the full overload set of all functions without having to include the libc math.h
  • We can support <cmath> during constant evaluation, even on platforms which don't have the functions implemented (once P0533R9 is implemented)
  • Platforms with an incomplete implementation of math.h can use libc++ (there will be a link-time error if the functions are used instead of a compile-time error when trying to build libc++)

The main disadvantage of this patch is that it adds a double overload for all functions, but I think this is outweighed by the benefits, especially since the overloads are all just forwarding to __builtin_<function name>.

Diff Detail

Unit TestsFailed

TimeTest
9,520 mslibcxx CI AIX (32-bit) > ibm-libc++-shared-cfg-in.std/localization/locale_categories/category_numeric/locale_num_get/facet_num_get_members::get_long_double.pass.cpp
Script: -- : 'COMPILED WITH'; /opt/clang/clang-15-e939bf67e34037970192fa90ad22ab7628f5de33/bin/clang++ /scratch/powerllvm/cpap8009/llvm-project/libcxx-ci/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_long_double.pass.cpp --target=powerpc-ibm-aix -nostdinc++ -D__LIBC_NO_CPP_MATH_OVERLOADS__ -I /scratch/powerllvm/cpap8009/llvm-project/libcxx-ci/build/aix/include/c++/v1 -I /scratch/powerllvm/cpap8009/llvm-project/libcxx-ci/libcxx/test/support -pthread -std=c++2b -Werror -Wall -Wctad-maybe-unsupported -Wextra -Wshadow -Wundef -Wno-unused-command-line-argument -Wno-attributes -Wno-pessimizing-move -Wno-c++11-extensions -Wno-noexcept-type -Wno-atomic-alignment -Wno-user-defined-literals -Wno-tautological-compare -Wsign-compare -Wunused-variable -Wunused-parameter -Wunreachable-code -Wno-unused-local-typedef -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_ENABLE_EXPERIMENTAL -D_LIBCPP_DISABLE_AVAILABILITY -fcoroutines-ts -Werror=thread-safety -Wuser-defined-warnings -lc++experimental -nostdlib++ -L /scratch/powerllvm/cpap8009/llvm-project/libcxx-ci/build/aix/lib -lc++ -lc++abi -latomic -Wl,-bbigtoc -o /scratch/powerllvm/cpap8009/llvm-project/libcxx-ci/build/aix/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/Output/get_long_double.pass.cpp.dir/t.tmp.exe
6,960 mslibcxx CI AIX (64-bit) > ibm-libc++-shared-cfg-in.std/localization/locale_categories/category_numeric/locale_num_get/facet_num_get_members::get_long_double.pass.cpp
Script: -- : 'COMPILED WITH'; /opt/clang/clang-15-e939bf67e34037970192fa90ad22ab7628f5de33/bin/clang++ /scratch/powerllvm/cpap8007/llvm-project/libcxx-ci/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_long_double.pass.cpp --target=powerpc64-ibm-aix -nostdinc++ -D__LIBC_NO_CPP_MATH_OVERLOADS__ -I /scratch/powerllvm/cpap8007/llvm-project/libcxx-ci/build/aix/include/c++/v1 -I /scratch/powerllvm/cpap8007/llvm-project/libcxx-ci/libcxx/test/support -pthread -std=c++2b -Werror -Wall -Wctad-maybe-unsupported -Wextra -Wshadow -Wundef -Wno-unused-command-line-argument -Wno-attributes -Wno-pessimizing-move -Wno-c++11-extensions -Wno-noexcept-type -Wno-atomic-alignment -Wno-user-defined-literals -Wno-tautological-compare -Wsign-compare -Wunused-variable -Wunused-parameter -Wunreachable-code -Wno-unused-local-typedef -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_ENABLE_EXPERIMENTAL -D_LIBCPP_DISABLE_AVAILABILITY -fcoroutines-ts -Werror=thread-safety -Wuser-defined-warnings -lc++experimental -nostdlib++ -L /scratch/powerllvm/cpap8007/llvm-project/libcxx-ci/build/aix/lib -lc++ -lc++abi -latomic -Wl,-bbigtoc -o /scratch/powerllvm/cpap8007/llvm-project/libcxx-ci/build/aix/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/Output/get_long_double.pass.cpp.dir/t.tmp.exe
12,960 mslibcxx CI MinGW (DLL, i686) > llvm-libc++-mingw-cfg-in.libcxx::clang_tidy.sh.cpp
Script: -- : 'RUN: at line 15'; clang-tidy C:\ws\w2\llvm-project\libcxx-ci\libcxx\test\libcxx\clang_tidy.sh.cpp --warnings-as-errors=* -header-filter=.* --config-file=C:\ws\w2\llvm-project\libcxx-ci\libcxx\test\libcxx/../../.clang-tidy -- -Wweak-vtables -nostdinc++ -I C:/ws/w2/llvm-project/libcxx-ci/build/mingw-dll-i686/include/c++/v1 -I C:/ws/w2/llvm-project/libcxx-ci/build/mingw-dll-i686/include/c++/v1 -I C:/ws/w2/llvm-project/libcxx-ci/libcxx/test/support -std=c++2b -Werror -Wall -Wctad-maybe-unsupported -Wextra -Wshadow -Wundef -Wno-unused-command-line-argument -Wno-attributes -Wno-pessimizing-move -Wno-c++11-extensions -Wno-noexcept-type -Wno-atomic-alignment -Wno-user-defined-literals -Wno-tautological-compare -Wsign-compare -Wunused-variable -Wunused-parameter -Wunreachable-code -Wno-unused-local-typedef -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_ENABLE_EXPERIMENTAL -D_LIBCPP_DISABLE_AVAILABILITY -fcoroutines-ts -Werror=thread-safety -Wuser-defined-warnings -DTEST_WINDOWS_DLL -fno-modules
16,309 mslibcxx CI MinGW (DLL, x86_64) > llvm-libc++-mingw-cfg-in.libcxx::clang_tidy.sh.cpp
Script: -- : 'RUN: at line 15'; clang-tidy C:\ws\w32-1\llvm-project\libcxx-ci\libcxx\test\libcxx\clang_tidy.sh.cpp --warnings-as-errors=* -header-filter=.* --config-file=C:\ws\w32-1\llvm-project\libcxx-ci\libcxx\test\libcxx/../../.clang-tidy -- -Wweak-vtables -nostdinc++ -I C:/ws/w32-1/llvm-project/libcxx-ci/build/mingw-dll/include/c++/v1 -I C:/ws/w32-1/llvm-project/libcxx-ci/build/mingw-dll/include/c++/v1 -I C:/ws/w32-1/llvm-project/libcxx-ci/libcxx/test/support -std=c++2b -Werror -Wall -Wctad-maybe-unsupported -Wextra -Wshadow -Wundef -Wno-unused-command-line-argument -Wno-attributes -Wno-pessimizing-move -Wno-c++11-extensions -Wno-noexcept-type -Wno-atomic-alignment -Wno-user-defined-literals -Wno-tautological-compare -Wsign-compare -Wunused-variable -Wunused-parameter -Wunreachable-code -Wno-unused-local-typedef -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_ENABLE_EXPERIMENTAL -D_LIBCPP_DISABLE_AVAILABILITY -fcoroutines-ts -Werror=thread-safety -Wuser-defined-warnings -DTEST_WINDOWS_DLL -fno-modules
13,930 mslibcxx CI MinGW (Static, x86_64) > llvm-libc++-mingw-cfg-in.libcxx::clang_tidy.sh.cpp
Script: -- : 'RUN: at line 15'; clang-tidy C:\ws\w5\llvm-project\libcxx-ci\libcxx\test\libcxx\clang_tidy.sh.cpp --warnings-as-errors=* -header-filter=.* --config-file=C:\ws\w5\llvm-project\libcxx-ci\libcxx\test\libcxx/../../.clang-tidy -- -Wweak-vtables -nostdinc++ -I C:/ws/w5/llvm-project/libcxx-ci/build/mingw-static/include/c++/v1 -I C:/ws/w5/llvm-project/libcxx-ci/build/mingw-static/include/c++/v1 -I C:/ws/w5/llvm-project/libcxx-ci/libcxx/test/support -std=c++2b -Werror -Wall -Wctad-maybe-unsupported -Wextra -Wshadow -Wundef -Wno-unused-command-line-argument -Wno-attributes -Wno-pessimizing-move -Wno-c++11-extensions -Wno-noexcept-type -Wno-atomic-alignment -Wno-user-defined-literals -Wno-tautological-compare -Wsign-compare -Wunused-variable -Wunused-parameter -Wunreachable-code -Wno-unused-local-typedef -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_ENABLE_EXPERIMENTAL -D_LIBCPP_DISABLE_AVAILABILITY -fcoroutines-ts -Werror=thread-safety -Wuser-defined-warnings -fno-modules

Event Timeline

philnik created this revision.Sep 24 2022, 5:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 24 2022, 5:14 AM
philnik updated this revision to Diff 462661.Sep 24 2022, 5:26 AM
  • Generate files
philnik updated this revision to Diff 462665.Sep 24 2022, 6:16 AM
  • try to fix CI
philnik retitled this revision from [libc++] Assume that builtins are available for cmake functions to [libc++] Assume that builtins are available for cmath functions.Sep 27 2022, 1:34 AM
philnik updated this revision to Diff 464947.Oct 4 2022, 4:07 AM

Rewrite the rest of math.h

philnik updated this revision to Diff 464974.Oct 4 2022, 5:25 AM
  • Try to fix CI
philnik retitled this revision from [libc++] Assume that builtins are available for cmath functions to [libc++] Rewrite <cmath>.Oct 4 2022, 6:30 AM
philnik edited the summary of this revision. (Show Details)
philnik updated this revision to Diff 465022.Oct 4 2022, 8:27 AM
  • Try to fix CI
philnik updated this revision to Diff 465065.Oct 4 2022, 10:09 AM
  • Fix tests
philnik updated this revision to Diff 465723.Oct 6 2022, 7:22 AM
  • Try to fix CI
ldionne added inline comments.Oct 6 2022, 8:51 AM
libcxx/include/__cmath/angular_functions.h
1

This change needs to be split into multiple patches that can be reviewed and validated on their own, especially since cmath and math.h interact with the C library, which makes them even more tricky. You already outlined in your commit message all the different things that this patch does, and I think each of them can be a separate patch. I know this is more work, but I think it's the only way to land this.

philnik abandoned this revision.Tue, Nov 29, 5:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptTue, Nov 29, 5:37 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript