Skip to content

[libc] Make FPUtils' rounding_mode.h functions constexpr. #149167

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 1 commit into from
Jul 23, 2025

Conversation

lntue
Copy link
Contributor

@lntue lntue commented Jul 16, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jul 16, 2025

@llvm/pr-subscribers-libc

Author: None (lntue)

Changes

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

2 Files Affected:

  • (modified) libc/src/__support/FPUtil/CMakeLists.txt (+1)
  • (modified) libc/src/__support/FPUtil/rounding_mode.h (+44-23)
diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index cc941f23135a6..764cb33a83c0e 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -16,6 +16,7 @@ add_header_library(
     rounding_mode.h
   DEPENDS
     libc.hdr.fenv_macros
+    libc.src.__support.CPP.type_traits
     libc.src.__support.macros.attributes
     libc.src.__support.macros.properties.architectures
     libc.src.__support.macros.sanitizer
diff --git a/libc/src/__support/FPUtil/rounding_mode.h b/libc/src/__support/FPUtil/rounding_mode.h
index bc66d09b94160..4ee0a0b0490fc 100644
--- a/libc/src/__support/FPUtil/rounding_mode.h
+++ b/libc/src/__support/FPUtil/rounding_mode.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_ROUNDING_MODE_H
 
 #include "hdr/fenv_macros.h"
+#include "src/__support/CPP/type_traits.h"   // is_constant_evaluated
 #include "src/__support/macros/attributes.h" // LIBC_INLINE
 #include "src/__support/macros/config.h"
 
@@ -20,18 +21,26 @@ namespace fputil {
 // Using the following observation:
 //   1.0f + 2^-25 = 1.0f        for FE_TONEAREST, FE_DOWNWARD, FE_TOWARDZERO
 //                = 0x1.000002f for FE_UPWARD.
-LIBC_INLINE bool fenv_is_round_up() {
-  volatile float x = 0x1.0p-25f;
-  return (1.0f + x != 1.0f);
+LIBC_INLINE static constexpr bool fenv_is_round_up() {
+  if (cpp::is_constant_evaluated()) {
+    return false;
+  } else {
+    volatile float x = 0x1.0p-25f;
+    return (1.0f + x != 1.0f);
+  }
 }
 
 // Quick free-standing test whether fegetround() == FE_DOWNWARD.
 // Using the following observation:
 //   -1.0f - 2^-25 = -1.0f        for FE_TONEAREST, FE_UPWARD, FE_TOWARDZERO
 //                 = -0x1.000002f for FE_DOWNWARD.
-LIBC_INLINE bool fenv_is_round_down() {
-  volatile float x = 0x1.0p-25f;
-  return (-1.0f - x != -1.0f);
+LIBC_INLINE static constexpr bool fenv_is_round_down() {
+  if (cpp::is_constant_evaluated()) {
+    return false;
+  } else {
+    volatile float x = 0x1.0p-25f;
+    return (-1.0f - x != -1.0f);
+  }
 }
 
 // Quick free-standing test whether fegetround() == FE_TONEAREST.
@@ -40,10 +49,14 @@ LIBC_INLINE bool fenv_is_round_down() {
 //                = 0x1.100002p0f  for FE_UPWARD,
 //   1.5f - 2^-24 = 1.5f           for FE_TONEAREST, FE_UPWARD
 //                = 0x1.0ffffep-1f for FE_DOWNWARD, FE_TOWARDZERO
-LIBC_INLINE bool fenv_is_round_to_nearest() {
-  static volatile float x = 0x1.0p-24f;
-  float y = x;
-  return (1.5f + y == 1.5f - y);
+LIBC_INLINE static constexpr bool fenv_is_round_to_nearest() {
+  if (cpp::is_constant_evaluated()) {
+    return true;
+  } else {
+    volatile float x = 0x1.0p-24f;
+    float y = 1.5f + x;
+    return (y == 1.5f - x);
+  }
 }
 
 // Quick free-standing test whether fegetround() == FE_TOWARDZERO.
@@ -56,23 +69,31 @@ LIBC_INLINE bool fenv_is_round_to_nearest() {
 // (0x1.000002p0f + 2^-24) + (-1.0f - 2^-24) = 2^-23 for FE_TOWARDZERO
 //                                           = 2^-22 for FE_TONEAREST, FE_UPWARD
 //                                           = 0 for FE_DOWNWARD
-LIBC_INLINE bool fenv_is_round_to_zero() {
-  static volatile float x = 0x1.0p-24f;
-  float y = x;
-  return ((0x1.000002p0f + y) + (-1.0f - y) == 0x1.0p-23f);
+LIBC_INLINE static constexpr bool fenv_is_round_to_zero() {
+  if (cpp::is_constant_evaluated()) {
+    return false;
+  } else {
+    volatile float x = 0x1.0p-24f;
+    volatile float y = 0x1.000002p0f + x;
+    return (y + (-1.0f - x) == 0x1.0p-23f);
+  }
 }
 
 // Quick free standing get rounding mode based on the above observations.
-LIBC_INLINE int quick_get_round() {
-  static volatile float x = 0x1.0p-24f;
-  float y = x;
-  float z = (0x1.000002p0f + y) + (-1.0f - y);
+LIBC_INLINE static constexpr int quick_get_round() {
+  if (cpp::is_constant_evaluated()) {
+    return FE_TONEAREST;
+  } else {
+    volatile float x = 0x1.0p-24f;
+    volatile float y = 0x1.000002p0f + x;
+    float z = y + (-1.0f - x);
 
-  if (z == 0.0f)
-    return FE_DOWNWARD;
-  if (z == 0x1.0p-23f)
-    return FE_TOWARDZERO;
-  return (2.0f + y == 2.0f) ? FE_TONEAREST : FE_UPWARD;
+    if (z == 0.0f)
+      return FE_DOWNWARD;
+    if (z == 0x1.0p-23f)
+      return FE_TOWARDZERO;
+    return (2.0f + x == 2.0f) ? FE_TONEAREST : FE_UPWARD;
+  }
 }
 
 } // namespace fputil

Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

Copy link
Member

@overmighty overmighty left a comment

Choose a reason for hiding this comment

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

LGTM besides a nit.

Comment on lines +25 to +27
if (cpp::is_constant_evaluated()) {
return false;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: if if constexpr is not needed then the else after return should be removed as per the LLVM Coding Standards. Same for the other functions changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the codegen semantic of is_constant_evaluated is similar to if constexpr / if consteval, so the else clause will make sure that the non-constexpr branch is not seen in the constexpr context.

@lntue lntue merged commit e0a48bb into llvm:main Jul 23, 2025
14 of 19 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 23, 2025

LLVM Buildbot has detected a new failure on builder openmp-offload-amdgpu-runtime-2 running on rocm-worker-hw-02 while building libc at step 5 "compile-openmp".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/10/builds/10025

Here is the relevant piece of the build log for the reference
Step 5 (compile-openmp) failure: build (failure)
...
6.277 [858/24/3815] Building CXX object lib/Target/X86/AsmParser/CMakeFiles/LLVMX86AsmParser.dir/X86AsmParser.cpp.o
6.285 [858/23/3816] Building CXX object tools/llvm-exegesis/lib/X86/CMakeFiles/obj.LLVMExegesisX86.dir/Target.cpp.o
6.303 [858/22/3817] Linking CXX shared library lib/libLLVMX86Info.so.22.0git
6.306 [857/22/3818] Building CXX object tools/clang/lib/Driver/CMakeFiles/obj.clangDriver.dir/ToolChains/HIPUtility.cpp.o
6.312 [857/21/3819] Creating library symlink lib/libLLVMX86Info.so
6.369 [855/22/3820] Linking CXX shared library lib/libLLVMX86Disassembler.so.22.0git
6.378 [854/22/3821] Creating library symlink lib/libLLVMX86Disassembler.so
6.409 [854/21/3822] Linking CXX shared library lib/libLLVMX86Desc.so.22.0git
6.418 [853/21/3823] Creating library symlink lib/libLLVMX86Desc.so
7.124 [853/20/3824] Building CXX object tools/llvm-gpu-loader/CMakeFiles/llvm-gpu-loader.dir/amdhsa.cpp.o
FAILED: tools/llvm-gpu-loader/CMakeFiles/llvm-gpu-loader.dir/amdhsa.cpp.o 
ccache /usr/bin/c++ -DAMDHSA_SUPPORT -DEXPERIMENTAL_KEY_INSTRUCTIONS -DGTEST_HAS_RTTI=0 -DLIBC_NAMESPACE=__llvm_libc_common_utils -DLLVM_BUILD_STATIC -D_DEBUG -D_GLIBCXX_ASSERTIONS -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.build/tools/llvm-gpu-loader -I/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/tools/llvm-gpu-loader -I/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.build/include -I/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/include -I/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc -isystem /opt/rocm/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG  -fno-exceptions -funwind-tables -fno-rtti -UNDEBUG -std=c++17 -MD -MT tools/llvm-gpu-loader/CMakeFiles/llvm-gpu-loader.dir/amdhsa.cpp.o -MF tools/llvm-gpu-loader/CMakeFiles/llvm-gpu-loader.dir/amdhsa.cpp.o.d -o tools/llvm-gpu-loader/CMakeFiles/llvm-gpu-loader.dir/amdhsa.cpp.o -c /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/tools/llvm-gpu-loader/amdhsa.cpp
In file included from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/stdio/printf_core/int_converter.h:19,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/stdio/printf_core/strerror_converter.h:15,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/stdio/printf_core/converter.h:15,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/RPC/rpc_server.h:43,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/shared/rpc_server.h:13,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/tools/llvm-gpu-loader/server.h:19,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/tools/llvm-gpu-loader/amdhsa.cpp:17:
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/stdio/printf_core/writer.h:55:30: warning: ‘maybe_unused’ attribute ignored [-Wattributes]
   55 |   [[maybe_unused]] WriteMode write_mode_;
      |                              ^~~~~~~~~~~
In file included from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/stdio/printf_core/float_dec_converter.h:14,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/stdio/printf_core/converter_atlas.h:32,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/stdio/printf_core/converter.h:21,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/RPC/rpc_server.h:43,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/shared/rpc_server.h:13,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/tools/llvm-gpu-loader/server.h:19,
                 from /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/tools/llvm-gpu-loader/amdhsa.cpp:17:
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/FPUtil/rounding_mode.h: In function ‘constexpr bool __llvm_libc_common_utils::fputil::fenv_is_round_up()’:
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/FPUtil/rounding_mode.h:29:20: error: lvalue-to-rvalue conversion of a volatile lvalue ‘x’ with type ‘volatile float’
   29 |     return (1.0f + x != 1.0f);
      |                    ^
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/FPUtil/rounding_mode.h: In function ‘constexpr bool __llvm_libc_common_utils::fputil::fenv_is_round_down()’:
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/FPUtil/rounding_mode.h:42:21: error: lvalue-to-rvalue conversion of a volatile lvalue ‘x’ with type ‘volatile float’
   42 |     return (-1.0f - x != -1.0f);
      |                     ^
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/FPUtil/rounding_mode.h: In function ‘constexpr bool __llvm_libc_common_utils::fputil::fenv_is_round_to_nearest()’:
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/FPUtil/rounding_mode.h:57:22: error: lvalue-to-rvalue conversion of a volatile lvalue ‘x’ with type ‘volatile float’
   57 |     float y = 1.5f + x;
      |                      ^
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/FPUtil/rounding_mode.h: In function ‘constexpr bool __llvm_libc_common_utils::fputil::fenv_is_round_to_zero()’:
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/FPUtil/rounding_mode.h:77:40: error: lvalue-to-rvalue conversion of a volatile lvalue ‘x’ with type ‘volatile float’
   77 |     volatile float y = 0x1.000002p0f + x;
      |                                        ^
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/FPUtil/rounding_mode.h: In function ‘constexpr int __llvm_libc_common_utils::fputil::quick_get_round()’:
/home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/cmake/Modules/../../libc/src/__support/FPUtil/rounding_mode.h:88:40: error: lvalue-to-rvalue conversion of a volatile lvalue ‘x’ with type ‘volatile float’
   88 |     volatile float y = 0x1.000002p0f + x;
      |                                        ^

@lntue lntue deleted the rounding_mode branch July 23, 2025 15:05
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 23, 2025

LLVM Buildbot has detected a new failure on builder premerge-monolithic-linux running on premerge-linux-1 while building libc at step 7 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/153/builds/39088

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-all) failure: test (failure)
...
PASS: lld :: COFF/entry-inference32.test (98846 of 101834)
PASS: lld :: COFF/exclude-symbols-embedded.s (98847 of 101834)
PASS: lld :: COFF/export-arm64.yaml (98848 of 101834)
PASS: lld :: COFF/exclude-symbols.s (98849 of 101834)
PASS: lld :: COFF/export-exe.test (98850 of 101834)
PASS: cfi-standalone-lld-thinlto-x86_64 :: simple-fail.cpp (98851 of 101834)
PASS: lld :: COFF/export-stdcall.s (98852 of 101834)
PASS: cfi-devirt-lld-thinlto-x86_64 :: cross-dso/icall/dlopen.cpp (98853 of 101834)
PASS: lit :: googletest-timeout.py (98854 of 101834)
TIMEOUT: MLIR :: Examples/standalone/test.toy (98855 of 101834)
******************** TEST 'MLIR :: Examples/standalone/test.toy' FAILED ********************
Exit Code: 1
Timeout: Reached timeout of 60 seconds

Command Output (stdout):
--
# RUN: at line 1
"/etc/cmake/bin/cmake" "/build/buildbot/premerge-monolithic-linux/llvm-project/mlir/examples/standalone" -G "Ninja"  -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_C_COMPILER=/usr/bin/clang  -DLLVM_ENABLE_LIBCXX=OFF -DMLIR_DIR=/build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir  -DLLVM_USE_LINKER=lld  -DPython3_EXECUTABLE="/usr/bin/python3.10"
# executed command: /etc/cmake/bin/cmake /build/buildbot/premerge-monolithic-linux/llvm-project/mlir/examples/standalone -G Ninja -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_C_COMPILER=/usr/bin/clang -DLLVM_ENABLE_LIBCXX=OFF -DMLIR_DIR=/build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir -DLLVM_USE_LINKER=lld -DPython3_EXECUTABLE=/usr/bin/python3.10
# .---command stdout------------
# | -- The CXX compiler identification is Clang 16.0.6
# | -- The C compiler identification is Clang 16.0.6
# | -- Detecting CXX compiler ABI info
# | -- Detecting CXX compiler ABI info - done
# | -- Check for working CXX compiler: /usr/bin/clang++ - skipped
# | -- Detecting CXX compile features
# | -- Detecting CXX compile features - done
# | -- Detecting C compiler ABI info
# | -- Detecting C compiler ABI info - done
# | -- Check for working C compiler: /usr/bin/clang - skipped
# | -- Detecting C compile features
# | -- Detecting C compile features - done
# | -- Looking for histedit.h
# | -- Looking for histedit.h - found
# | -- Found LibEdit: /usr/include (found version "2.11") 
# | -- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11") 
# | -- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.13") 
# | -- Using MLIRConfig.cmake in: /build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir
# | -- Using LLVMConfig.cmake in: /build/buildbot/premerge-monolithic-linux/build/lib/cmake/llvm
# | -- Linker detection: unknown
# | -- Performing Test LLVM_LIBSTDCXX_MIN
# | -- Performing Test LLVM_LIBSTDCXX_MIN - Success
# | -- Performing Test LLVM_LIBSTDCXX_SOFT_ERROR
# | -- Performing Test LLVM_LIBSTDCXX_SOFT_ERROR - Success
# | -- Performing Test CXX_SUPPORTS_CUSTOM_LINKER
# | -- Performing Test CXX_SUPPORTS_CUSTOM_LINKER - Success
# | -- Performing Test C_SUPPORTS_FPIC
# | -- Performing Test C_SUPPORTS_FPIC - Success
# | -- Performing Test CXX_SUPPORTS_FPIC

@Kewen12
Copy link
Contributor

Kewen12 commented Jul 23, 2025

Hi there, this PR caused build error in our bot. Could you please take a look? Thanks!

bot: https://lab.llvm.org/buildbot/#/builders/10/builds/10025

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.

5 participants