[libc-commits] [libc] [libc] fix unit tests in fullbuild (PR #78864)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Sat Jan 20 20:03:02 PST 2024


https://github.com/SchrodingerZhu updated https://github.com/llvm/llvm-project/pull/78864

>From d98e27a370c73f5d9e4de3efc1f2402f5305271e Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Sat, 20 Jan 2024 16:22:49 -0500
Subject: [PATCH 1/4] [libc] fix unit tests in fullbuild

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 24f543f6e4c132..9b7e9180275414 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -187,7 +187,7 @@ function(create_libc_unittest fq_target_name)
   if(LLVM_LIBC_FULL_BUILD)
     target_compile_options(
       ${fq_build_target_name}
-      PRIVATE -ffreestanding
+      PRIVATE -ffreestanding -fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables
     )
   endif()
   if(LIBC_UNITTEST_COMPILE_OPTIONS)

>From bb1897f1ffc44cd719a3cf528773e7f6789e098b Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Sat, 20 Jan 2024 16:33:43 -0500
Subject: [PATCH 2/4] fix: add freestanding flags for MPFR

---
 libc/utils/MPFRWrapper/CMakeLists.txt | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/libc/utils/MPFRWrapper/CMakeLists.txt b/libc/utils/MPFRWrapper/CMakeLists.txt
index d9fa0e31df0e50..0f935da183198c 100644
--- a/libc/utils/MPFRWrapper/CMakeLists.txt
+++ b/libc/utils/MPFRWrapper/CMakeLists.txt
@@ -1,4 +1,13 @@
 if(LIBC_TESTS_CAN_USE_MPFR)
+  if (LLVM_LIBC_FULL_BUILD)
+    add_compile_options(
+      -ffreestanding
+      -fno-exceptions
+      -fno-rtti
+      -fno-unwind-tables
+      -fno-asynchronous-unwind-tables
+    )
+  endif()
   add_library(libcMPFRWrapper
     MPFRUtils.cpp
     MPFRUtils.h

>From d89d06bfad6057b7e5ae364a3370d638ae4be703 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Sat, 20 Jan 2024 22:59:08 -0500
Subject: [PATCH 3/4] fix: add in missing stdlibs

---
 libc/utils/MPFRWrapper/CMakeLists.txt | 20 +++++++-------------
 libc/utils/MPFRWrapper/MPFRUtils.cpp  |  7 +++----
 2 files changed, 10 insertions(+), 17 deletions(-)

diff --git a/libc/utils/MPFRWrapper/CMakeLists.txt b/libc/utils/MPFRWrapper/CMakeLists.txt
index 0f935da183198c..18acf5b7c75327 100644
--- a/libc/utils/MPFRWrapper/CMakeLists.txt
+++ b/libc/utils/MPFRWrapper/CMakeLists.txt
@@ -1,21 +1,15 @@
 if(LIBC_TESTS_CAN_USE_MPFR)
-  if (LLVM_LIBC_FULL_BUILD)
-    add_compile_options(
-      -ffreestanding
-      -fno-exceptions
-      -fno-rtti
-      -fno-unwind-tables
-      -fno-asynchronous-unwind-tables
-    )
-  endif()
   add_library(libcMPFRWrapper
     MPFRUtils.cpp
     MPFRUtils.h
     mpfr_inc.h
   )
-  add_compile_options(
-    -O3
-  )
+  target_compile_options(libcMPFRWrapper PRIVATE -O3)
+  if (LLVM_LIBC_FULL_BUILD)
+    # Target using this library will need to link against C++ and unwind libraries.
+    # Since we are using MPFR anyway, we directly specifies the GNU toolchain.
+    target_link_libraries(libcMPFRWrapper PUBLIC -lstdc++ -lgcc_s)
+  endif()
   add_dependencies(
     libcMPFRWrapper 
     libc.src.__support.CPP.string_view 
@@ -28,7 +22,7 @@ if(LIBC_TESTS_CAN_USE_MPFR)
     target_include_directories(libcMPFRWrapper PUBLIC ${LLVM_LIBC_MPFR_INSTALL_PATH}/include)
     target_link_directories(libcMPFRWrapper PUBLIC ${LLVM_LIBC_MPFR_INSTALL_PATH}/lib)
   endif()
-  target_link_libraries(libcMPFRWrapper LibcFPTestHelpers.unit LibcTest.unit mpfr gmp)
+  target_link_libraries(libcMPFRWrapper PUBLIC LibcFPTestHelpers.unit LibcTest.unit mpfr gmp)
 elseif(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
   message(WARNING "Math tests using MPFR will be skipped.")
 endif()
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index b6ca525db6cf74..9b14c0d452d91f 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -14,9 +14,8 @@
 #include "src/__support/FPUtil/fpbits_str.h"
 #include "test/UnitTest/FPMatcher.h"
 
-#include <cmath>
 #include <fenv.h>
-#include <memory>
+#include <math.h>
 #include <stdint.h>
 
 #include "mpfr_inc.h"
@@ -474,8 +473,8 @@ class MPFRNumber {
     // If the control reaches here, it means that this number and input are
     // of the same sign but different exponent. In such a case, ULP error is
     // calculated as sum of two parts.
-    thisAsT = std::abs(thisAsT);
-    input = std::abs(input);
+    thisAsT = ::abs(thisAsT);
+    input = ::abs(input);
     T min = thisAsT > input ? input : thisAsT;
     T max = thisAsT > input ? thisAsT : input;
     int minExponent = FPBits<T>(min).get_exponent();

>From 4b38010baa23ed71e9fd3711b931eff3e47c5b40 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Sat, 20 Jan 2024 23:02:13 -0500
Subject: [PATCH 4/4] undo extra changes

---
 libc/utils/MPFRWrapper/MPFRUtils.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 9b14c0d452d91f..b6ca525db6cf74 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -14,8 +14,9 @@
 #include "src/__support/FPUtil/fpbits_str.h"
 #include "test/UnitTest/FPMatcher.h"
 
+#include <cmath>
 #include <fenv.h>
-#include <math.h>
+#include <memory>
 #include <stdint.h>
 
 #include "mpfr_inc.h"
@@ -473,8 +474,8 @@ class MPFRNumber {
     // If the control reaches here, it means that this number and input are
     // of the same sign but different exponent. In such a case, ULP error is
     // calculated as sum of two parts.
-    thisAsT = ::abs(thisAsT);
-    input = ::abs(input);
+    thisAsT = std::abs(thisAsT);
+    input = std::abs(input);
     T min = thisAsT > input ? input : thisAsT;
     T max = thisAsT > input ? thisAsT : input;
     int minExponent = FPBits<T>(min).get_exponent();



More information about the libc-commits mailing list