From 6d9dac2d228dff80cb413ae8dd2b6508f8d286ae Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Wed, 5 Feb 2025 01:52:10 +0000 Subject: [PATCH 1/3] [libc] Make LIBC_MATH_NO_ERRNO imply `-fno-math-errno` This partially reverts #124200. Rather than using a CMake option to control whether to enable `-fno-math-errno`, use LIBC_MATH_NO_ERRNO configuration option. While there might be other cases when we want to set `-fno-math-errno`, having LIBC_MATH_NO_ERRNO imply it should be always safe and represents a reasonable starting point. --- libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 10 +++------- libc/cmake/modules/LLVMLibCTestRules.cmake | 5 ----- libc/config/baremetal/config.json | 2 +- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index 0facb0b9be0c1..f5f638bef5c1d 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -28,13 +28,6 @@ function(_get_compile_options_from_flags output_var) elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV64) list(APPEND compile_options "-D__LIBC_RISCV_USE_FMA") endif() - # For clang, we will build the math functions with `-fno-math-errno` so that - # __builtin_fma* will generate the fused-mutliply-add instructions. We - # don't put the control flag to the public config yet, and see if it makes - # sense to just enable this flag by default. - if(LIBC_ADD_FNO_MATH_ERRNO) - list(APPEND compile_options "-fno-math-errno") - endif() endif() if(ADD_ROUND_OPT_FLAG) if(LIBC_TARGET_ARCHITECTURE_IS_X86_64) @@ -71,6 +64,9 @@ function(_get_compile_options_from_flags output_var) "SHELL:-Xclang -target-feature -Xclang +fullfp16") endif() endif() + if(LIBC_CONF_MATH_OPTIMIZATIONS MATCHES "LIBC_MATH_NO_ERRNO") + list(APPEND compile_options "-fno-math-errno") + endif() elseif(MSVC) if(ADD_FMA_FLAG) list(APPEND compile_options "/arch:AVX2") diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake index 10bb9c9487d63..96fa6c3a707e4 100644 --- a/libc/cmake/modules/LLVMLibCTestRules.cmake +++ b/libc/cmake/modules/LLVMLibCTestRules.cmake @@ -1,11 +1,6 @@ function(_get_common_test_compile_options output_var c_test flags) _get_compile_options_from_flags(compile_flags ${flags}) - # Remove -fno-math-errno if it was added. - if(LIBC_ADD_FNO_MATH_ERRNO) - list(REMOVE_ITEM compile_options "-fno-math-errno") - endif() - set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${LIBC_TEST_COMPILE_OPTIONS_DEFAULT} diff --git a/libc/config/baremetal/config.json b/libc/config/baremetal/config.json index 08c581d1c6822..d29fb64e370dd 100644 --- a/libc/config/baremetal/config.json +++ b/libc/config/baremetal/config.json @@ -28,7 +28,7 @@ }, "math": { "LIBC_CONF_MATH_OPTIMIZATIONS": { - "value": "(LIBC_MATH_SKIP_ACCURATE_PASS | LIBC_MATH_SMALL_TABLES)" + "value": "(LIBC_MATH_SKIP_ACCURATE_PASS | LIBC_MATH_SMALL_TABLES | LIBC_MATH_NO_ERRNO)" } } } From 92a0fd5c869aff07407e734213824ea9f097ae4c Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Wed, 5 Feb 2025 08:01:06 +0000 Subject: [PATCH 2/3] Move the check --- libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index f5f638bef5c1d..1ab5dbeb5cae0 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -64,9 +64,6 @@ function(_get_compile_options_from_flags output_var) "SHELL:-Xclang -target-feature -Xclang +fullfp16") endif() endif() - if(LIBC_CONF_MATH_OPTIMIZATIONS MATCHES "LIBC_MATH_NO_ERRNO") - list(APPEND compile_options "-fno-math-errno") - endif() elseif(MSVC) if(ADD_FMA_FLAG) list(APPEND compile_options "/arch:AVX2") @@ -100,6 +97,9 @@ function(_get_compile_options_from_config output_var) if(LIBC_CONF_MATH_OPTIMIZATIONS) list(APPEND config_options "-DLIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}") + if(LIBC_CONF_MATH_OPTIMIZATIONS MATCHES "LIBC_MATH_NO_ERRNO") + list(APPEND config_options "-fno-math-errno") + endif() endif() set(${output_var} ${config_options} PARENT_SCOPE) From 18520d417809b46b1332373c46d03653689a2f48 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Wed, 15 Oct 2025 10:30:33 -0700 Subject: [PATCH 3/3] Readd the null check --- libc/cmake/modules/LLVMLibCTestRules.cmake | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake index 2a607b69b3d2c..933b81b9f1d46 100644 --- a/libc/cmake/modules/LLVMLibCTestRules.cmake +++ b/libc/cmake/modules/LLVMLibCTestRules.cmake @@ -3,6 +3,11 @@ function(_get_common_test_compile_options output_var c_test flags) _get_compile_options_from_config(config_flags) _get_compile_options_from_arch(arch_flags) + # Death test executor is only available in Linux for now. + if(NOT ${LIBC_TARGET_OS} STREQUAL "linux") + list(REMOVE_ITEM config_flags "-DLIBC_ADD_NULL_CHECKS") + endif() + set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${LIBC_TEST_COMPILE_OPTIONS_DEFAULT}