[libc-commits] [libc] [libc] Add FENV_ACCESS pragma with CMake compiler feature detection (PR #200268)

Marcos Ramirez Joos via libc-commits libc-commits at lists.llvm.org
Fri May 29 09:50:31 PDT 2026


https://github.com/maarcosrmz updated https://github.com/llvm/llvm-project/pull/200268

>From db5a6638e1bb6ad8a306d046db922962c1c9e5e6 Mon Sep 17 00:00:00 2001
From: Marcos Ramirez Joos <mramirezjoos.oss at proton.me>
Date: Thu, 28 May 2026 15:43:09 +0200
Subject: [PATCH 1/4] [libc] Add #pragma STDC FENV_ACCESS ON to FEnvImpl.h
 wrappers

---
 libc/src/__support/FPUtil/FEnvImpl.h | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/libc/src/__support/FPUtil/FEnvImpl.h b/libc/src/__support/FPUtil/FEnvImpl.h
index a21f511bd72b8..208c095170c05 100644
--- a/libc/src/__support/FPUtil/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/FEnvImpl.h
@@ -37,26 +37,41 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace fputil {
 
-LIBC_INLINE int clear_except(int excepts) { return feclearexcept(excepts); }
+LIBC_INLINE int clear_except(int excepts) {
+#pragma STDC FENV_ACCESS ON
+  return feclearexcept(excepts);
+}
 
-LIBC_INLINE int test_except(int excepts) { return fetestexcept(excepts); }
+LIBC_INLINE int test_except(int excepts) {
+#pragma STDC FENV_ACCESS ON
+  return fetestexcept(excepts);
+}
 
 LIBC_INLINE int get_except() {
+#pragma STDC FENV_ACCESS ON
   fexcept_t excepts = 0;
   fegetexceptflag(&excepts, FE_ALL_EXCEPT);
   return static_cast<int>(excepts);
 }
 
 LIBC_INLINE int set_except(int excepts) {
+#pragma STDC 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) {
+#pragma STDC FENV_ACCESS ON
+  return feraiseexcept(excepts);
+}
 
-LIBC_INLINE int get_round() { return fegetround(); }
+LIBC_INLINE int get_round() {
+#pragma STDC FENV_ACCESS ON
+  return fegetround();
+}
 
 LIBC_INLINE int set_round(int rounding_mode) {
+#pragma STDC FENV_ACCESS ON
   return fesetround(rounding_mode);
 }
 
@@ -129,6 +144,7 @@ clear_except_if_required([[maybe_unused]] int excepts) {
     return 0;
   } else {
 #ifndef LIBC_MATH_HAS_NO_EXCEPT
+#pragma STDC FENV_ACCESS ON
     if (math_errhandling & MATH_ERREXCEPT)
       return clear_except(excepts);
 #endif // LIBC_MATH_HAS_NO_EXCEPT
@@ -142,6 +158,7 @@ set_except_if_required([[maybe_unused]] int excepts) {
     return 0;
   } else {
 #ifndef LIBC_MATH_HAS_NO_EXCEPT
+#pragma STDC FENV_ACCESS ON
     if (math_errhandling & MATH_ERREXCEPT)
       return set_except(excepts);
 #endif // LIBC_MATH_HAS_NO_EXCEPT
@@ -155,6 +172,7 @@ raise_except_if_required([[maybe_unused]] int excepts) {
     return 0;
   } else {
 #ifndef LIBC_MATH_HAS_NO_EXCEPT
+#pragma STDC FENV_ACCESS ON
     if (math_errhandling & MATH_ERREXCEPT)
       return raise_except(excepts);
 #endif // LIBC_MATH_HAS_NO_EXCEPT

>From 3da6f9a6737722a4c97cb86786b2868061f29eee Mon Sep 17 00:00:00 2001
From: Marcos Ramirez Joos <mramirezjoos.oss at proton.me>
Date: Thu, 28 May 2026 21:31:37 +0200
Subject: [PATCH 2/4] [libc] Add FENV_ACCESS pragma with CMake compiler
 detection

---
 .../cmake/modules/CheckCompilerFeatures.cmake |  8 +++---
 .../check_stdc_fenv_access.cpp                |  2 ++
 libc/src/__support/FPUtil/FEnvImpl.h          | 26 ++++++++++++-------
 3 files changed, 23 insertions(+), 13 deletions(-)
 create mode 100644 libc/cmake/modules/compiler_features/check_stdc_fenv_access.cpp

diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index cbaa3213f1bf3..f0f46a6d57884 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,8 @@ 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")
+    list(APPEND compile_options "-Werror=unknown-pragmas")
   elseif(${feature} MATCHES "^builtin_" OR
          ${feature} STREQUAL "float16_conversion")
     set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT})
@@ -132,6 +135,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()
@@ -152,6 +157,3 @@ 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("-Werror -Wno-fenv-access" LIBC_CC_SUPPORTS_NO_FENV_ACCESS)
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 208c095170c05..011c3ba2ea660 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
@@ -38,40 +44,40 @@ namespace LIBC_NAMESPACE_DECL {
 namespace fputil {
 
 LIBC_INLINE int clear_except(int excepts) {
-#pragma STDC FENV_ACCESS ON
+  LIBC_FENV_ACCESS_ON
   return feclearexcept(excepts);
 }
 
 LIBC_INLINE int test_except(int excepts) {
-#pragma STDC FENV_ACCESS ON
+  LIBC_FENV_ACCESS_ON
   return fetestexcept(excepts);
 }
 
 LIBC_INLINE int get_except() {
-#pragma STDC FENV_ACCESS ON
+  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) {
-#pragma STDC FENV_ACCESS ON
+  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) {
-#pragma STDC FENV_ACCESS ON
+  LIBC_FENV_ACCESS_ON
   return feraiseexcept(excepts);
 }
 
 LIBC_INLINE int get_round() {
-#pragma STDC FENV_ACCESS ON
+  LIBC_FENV_ACCESS_ON
   return fegetround();
 }
 
 LIBC_INLINE int set_round(int rounding_mode) {
-#pragma STDC FENV_ACCESS ON
+  LIBC_FENV_ACCESS_ON
   return fesetround(rounding_mode);
 }
 
@@ -144,7 +150,7 @@ clear_except_if_required([[maybe_unused]] int excepts) {
     return 0;
   } else {
 #ifndef LIBC_MATH_HAS_NO_EXCEPT
-#pragma STDC FENV_ACCESS ON
+    LIBC_FENV_ACCESS_ON
     if (math_errhandling & MATH_ERREXCEPT)
       return clear_except(excepts);
 #endif // LIBC_MATH_HAS_NO_EXCEPT
@@ -158,7 +164,7 @@ set_except_if_required([[maybe_unused]] int excepts) {
     return 0;
   } else {
 #ifndef LIBC_MATH_HAS_NO_EXCEPT
-#pragma STDC FENV_ACCESS ON
+    LIBC_FENV_ACCESS_ON
     if (math_errhandling & MATH_ERREXCEPT)
       return set_except(excepts);
 #endif // LIBC_MATH_HAS_NO_EXCEPT
@@ -172,7 +178,7 @@ raise_except_if_required([[maybe_unused]] int excepts) {
     return 0;
   } else {
 #ifndef LIBC_MATH_HAS_NO_EXCEPT
-#pragma STDC FENV_ACCESS ON
+    LIBC_FENV_ACCESS_ON
     if (math_errhandling & MATH_ERREXCEPT)
       return raise_except(excepts);
 #endif // LIBC_MATH_HAS_NO_EXCEPT

>From 888e9fc3b53d611ab87b1815756585a6ae954c38 Mon Sep 17 00:00:00 2001
From: Marcos Ramirez Joos <mramirezjoos.oss at proton.me>
Date: Fri, 29 May 2026 16:58:49 +0200
Subject: [PATCH 3/4] Changed compiler warning flag during stdc_fenv_access
 check; connected LIBC_COMPILER_HAS_STDC_FENV_ACCESS to macro def.

---
 libc/cmake/modules/CheckCompilerFeatures.cmake      | 5 ++++-
 libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 4 ++--
 libc/cmake/modules/LLVMLibCTestRules.cmake          | 4 ++--
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index f0f46a6d57884..f30ec879adb2c 100644
--- a/libc/cmake/modules/CheckCompilerFeatures.cmake
+++ b/libc/cmake/modules/CheckCompilerFeatures.cmake
@@ -68,7 +68,10 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
   if(${feature} STREQUAL "fixed_point")
     list(APPEND compile_options "-ffixed-point")
   elseif(${feature} STREQUAL "stdc_fenv_access")
-    list(APPEND compile_options "-Werror=unknown-pragmas")
+    if (MSVC)
+      list(APPEND compile_options "/Wx")
+    else
+      list(APPEND compile_options "-Wall -Werror")
   elseif(${feature} MATCHES "^builtin_" OR
          ${feature} STREQUAL "float16_conversion")
     set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT})
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index b8c4ee0801b9d..931af70f1c505 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -316,8 +316,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 9faf47f4d3297..f8393d4b4c1dc 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -67,8 +67,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)

>From b4bc754c8be74cab5a099e015cb6ca996155ec2f Mon Sep 17 00:00:00 2001
From: Marcos Ramirez Joos <mramirezjoos.oss at proton.me>
Date: Fri, 29 May 2026 17:03:13 +0200
Subject: [PATCH 4/4] Fix

---
 libc/cmake/modules/CheckCompilerFeatures.cmake | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index f7675b3dd1a4b..5eb083a27e6bf 100644
--- a/libc/cmake/modules/CheckCompilerFeatures.cmake
+++ b/libc/cmake/modules/CheckCompilerFeatures.cmake
@@ -69,9 +69,10 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
     list(APPEND compile_options "-ffixed-point")
   elseif(${feature} STREQUAL "stdc_fenv_access")
     if (MSVC)
-      list(APPEND compile_options "/Wx")
-    else
+      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})
@@ -162,4 +163,5 @@ check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)
 check_cxx_compiler_flag("-nostdlibinc" LIBC_CC_SUPPORTS_NOSTDLIBINC)
 
 # clang-all, gcc-8+
-check_cxx_compiler_flag("-Wextra-semi" LIBC_CC_SUPPORTS_EXTRA_SEMI)
\ No newline at end of file
+check_cxx_compiler_flag("-Wextra-semi" LIBC_CC_SUPPORTS_EXTRA_SEMI)
+



More information about the libc-commits mailing list