[libc-commits] [libc] [libc] Add FENV_ACCESS pragma with CMake compiler feature detection (PR #200268)
via libc-commits
libc-commits at lists.llvm.org
Fri May 29 08:33:29 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Marcos Ramirez Joos (maarcosrmz)
<details>
<summary>Changes</summary>
Related to https://github.com/llvm/llvm-project/pull/199009
Added compiler feature detection for _STDC FENV_ACCESS_ pragma. It is used to conditionally add function-scoped `#pragma STDC FENV_ACCESS ON` to `libc/src/__support/FPUtil/FEnvAccess.h`, whenever functions from the `<fenv.h>` header are called and the target supports the pragma.
---
Full diff: https://github.com/llvm/llvm-project/pull/200268.diff
5 Files Affected:
- (modified) libc/cmake/modules/CheckCompilerFeatures.cmake (+10-3)
- (modified) libc/cmake/modules/LLVMLibCCompileOptionRules.cmake (+2-2)
- (modified) libc/cmake/modules/LLVMLibCTestRules.cmake (+2-2)
- (added) libc/cmake/modules/compiler_features/check_stdc_fenv_access.cpp (+2)
- (modified) libc/src/__support/FPUtil/FEnvImpl.h (+28-4)
``````````diff
diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index 325b5efe3f805..2949d5d4cf6e2 100644
--- a/libc/cmake/modules/CheckCompilerFeatures.cmake
+++ b/libc/cmake/modules/CheckCompilerFeatures.cmake
@@ -17,6 +17,7 @@ set(
"cfloat16"
"cfloat128"
"ext_vector_type"
+ "stdc_fenv_access"
)
# Making sure ALL_COMPILER_FEATURES is sorted.
@@ -66,6 +67,12 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
set(link_options "")
if(${feature} STREQUAL "fixed_point")
list(APPEND compile_options "-ffixed-point")
+ elseif(${feature} STREQUAL "stdc_fenv_access")
+ if (MSVC)
+ list(APPEND compile_options "/Wx")
+ else()
+ list(APPEND compile_options "-Wall -Werror")
+ endif()
elseif(${feature} MATCHES "^builtin_" OR
${feature} STREQUAL "float16_conversion")
set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT})
@@ -132,6 +139,8 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
set(LIBC_COMPILER_HAS_BUILTIN_ROUNDEVEN TRUE)
elseif(${feature} STREQUAL "ext_vector_type")
set(LIBC_COMPILER_HAS_EXT_VECTOR_TYPE TRUE)
+ elseif(${feature} STREQUAL "stdc_fenv_access")
+ set(LIBC_COMPILER_HAS_STDC_FENV_ACCESS TRUE)
endif()
endif()
endforeach()
@@ -153,8 +162,6 @@ check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)
# clang-3.0+
check_cxx_compiler_flag("-nostdlibinc" LIBC_CC_SUPPORTS_NOSTDLIBINC)
-# clang-23+, post llvm.org/pr187860
-check_cxx_compiler_flag("-Wfenv-access" LIBC_CC_SUPPORTS_NO_FENV_ACCESS)
-
# clang-all, gcc-8+
check_cxx_compiler_flag("-Wextra-semi" LIBC_CC_SUPPORTS_EXTRA_SEMI)
+
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index c65fc4db605c9..44b239b3949c9 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -323,8 +323,8 @@ function(_get_common_compile_options output_var flags)
list(APPEND compile_options "-Wshadow")
endif()
- if(LIBC_CC_SUPPORTS_NO_FENV_ACCESS)
- list(APPEND compile_options "-Wno-fenv-access")
+ if(LIBC_COMPILER_HAS_STDC_FENV_ACCESS)
+ list(APPEND compile_options "-DLIBC_COMPILER_HAS_STDC_FENV_ACCESS")
endif()
elseif(MSVC)
list(APPEND compile_options "/EHs-c-")
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 89db3f0af50e0..57a0ad5f95936 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -72,8 +72,8 @@ function(_get_common_test_compile_options output_var c_test flags)
list(APPEND compile_options "-Wthread-safety")
endif()
- if(LIBC_CC_SUPPORTS_NO_FENV_ACCESS)
- list(APPEND compile_options "-Wno-fenv-access")
+ if(LIBC_COMPILER_HAS_STDC_FENV_ACCESS)
+ list(APPEND compile_options "-DLIBC_COMPILER_HAS_STDC_FENV_ACCESS")
endif()
endif()
set(${output_var} ${compile_options} PARENT_SCOPE)
diff --git a/libc/cmake/modules/compiler_features/check_stdc_fenv_access.cpp b/libc/cmake/modules/compiler_features/check_stdc_fenv_access.cpp
new file mode 100644
index 0000000000000..d409da80c52cd
--- /dev/null
+++ b/libc/cmake/modules/compiler_features/check_stdc_fenv_access.cpp
@@ -0,0 +1,2 @@
+#pragma STDC FENV_ACCESS ON
+extern "C" void _start() {}
diff --git a/libc/src/__support/FPUtil/FEnvImpl.h b/libc/src/__support/FPUtil/FEnvImpl.h
index 89188d20297e6..806ccb2fb1667 100644
--- a/libc/src/__support/FPUtil/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/FEnvImpl.h
@@ -20,6 +20,12 @@
#include "src/__support/macros/properties/architectures.h"
#include "src/__support/macros/properties/compiler.h"
+#ifdef LIBC_COMPILER_HAS_STDC_FENV_ACCESS
+#define LIBC_FENV_ACCESS_ON _Pragma("STDC FENV_ACCESS ON")
+#else
+#define LIBC_FENV_ACCESS_ON
+#endif
+
// In full build mode we are the system fenv in libc.
#if defined(LIBC_FULL_BUILD)
#undef LIBC_MATH_USE_SYSTEM_FENV
@@ -37,26 +43,41 @@
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
-LIBC_INLINE int clear_except(int excepts) { return feclearexcept(excepts); }
+LIBC_INLINE int clear_except(int excepts) {
+ LIBC_FENV_ACCESS_ON
+ return feclearexcept(excepts);
+}
-LIBC_INLINE int test_except(int excepts) { return fetestexcept(excepts); }
+LIBC_INLINE int test_except(int excepts) {
+ LIBC_FENV_ACCESS_ON
+ return fetestexcept(excepts);
+}
LIBC_INLINE int get_except() {
+ LIBC_FENV_ACCESS_ON
fexcept_t excepts = 0;
fegetexceptflag(&excepts, FE_ALL_EXCEPT);
return static_cast<int>(excepts);
}
LIBC_INLINE int set_except(int excepts) {
+ LIBC_FENV_ACCESS_ON
fexcept_t exc = static_cast<fexcept_t>(excepts);
return fesetexceptflag(&exc, FE_ALL_EXCEPT);
}
-LIBC_INLINE int raise_except(int excepts) { return feraiseexcept(excepts); }
+LIBC_INLINE int raise_except(int excepts) {
+ LIBC_FENV_ACCESS_ON
+ return feraiseexcept(excepts);
+}
-LIBC_INLINE int get_round() { return fegetround(); }
+LIBC_INLINE int get_round() {
+ LIBC_FENV_ACCESS_ON
+ return fegetround();
+}
LIBC_INLINE int set_round(int rounding_mode) {
+ LIBC_FENV_ACCESS_ON
return fesetround(rounding_mode);
}
@@ -129,6 +150,7 @@ clear_except_if_required([[maybe_unused]] int excepts) {
return 0;
} else {
#ifndef LIBC_MATH_HAS_NO_EXCEPT
+ LIBC_FENV_ACCESS_ON
if (math_errhandling & MATH_ERREXCEPT)
return clear_except(excepts);
#endif // LIBC_MATH_HAS_NO_EXCEPT
@@ -142,6 +164,7 @@ set_except_if_required([[maybe_unused]] int excepts) {
return 0;
} else {
#ifndef LIBC_MATH_HAS_NO_EXCEPT
+ LIBC_FENV_ACCESS_ON
if (math_errhandling & MATH_ERREXCEPT)
return set_except(excepts);
#endif // LIBC_MATH_HAS_NO_EXCEPT
@@ -155,6 +178,7 @@ raise_except_if_required([[maybe_unused]] int excepts) {
return 0;
} else {
#ifndef LIBC_MATH_HAS_NO_EXCEPT
+ LIBC_FENV_ACCESS_ON
if (math_errhandling & MATH_ERREXCEPT)
return raise_except(excepts);
#endif // LIBC_MATH_HAS_NO_EXCEPT
``````````
</details>
https://github.com/llvm/llvm-project/pull/200268
More information about the libc-commits
mailing list