[libc-commits] [libc] [llvm] [libc][math] Add do not optimize macros and guard some double-double functions. (PR #243203)

via libc-commits libc-commits at lists.llvm.org
Mon Nov 16 11:58:06 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: lntue

<details>
<summary>Changes</summary>

Guard some double-double computations against `-(a * b) -> a * (-b)` optimizations from compilers.

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


9 Files Affected:

- (modified) libc/src/__support/FPUtil/CMakeLists.txt (+1) 
- (modified) libc/src/__support/FPUtil/double_double.h (+6) 
- (modified) libc/src/__support/macros/CMakeLists.txt (+4) 
- (modified) libc/src/__support/macros/optimization.h (+73) 
- (modified) libc/test/src/__support/FPUtil/CMakeLists.txt (+12) 
- (added) libc/test/src/__support/FPUtil/double_double_test.cpp (+172) 
- (modified) utils/bazel/llvm-project-overlay/libc/src/__support/FPUtil/BUILD.bazel (+1) 
- (modified) utils/bazel/llvm-project-overlay/libc/src/__support/macros/BUILD.bazel (+2) 
- (modified) utils/bazel/llvm-project-overlay/libc/test/src/__support/FPUtil/BUILD.bazel (+11) 


``````````diff
diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index f2d07ab222bab3..fc0a01670338a9 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -192,6 +192,7 @@ add_header_library(
   DEPENDS
     libc.src.__support.common
     libc.src.__support.number_pair
+    libc.src.__support.macros.optimization
     libc.src.__support.macros.properties.cpu_features
     .multiply_add
 )
diff --git a/libc/src/__support/FPUtil/double_double.h b/libc/src/__support/FPUtil/double_double.h
index 45ce8a10acdac4..a0ca626771263c 100644
--- a/libc/src/__support/FPUtil/double_double.h
+++ b/libc/src/__support/FPUtil/double_double.h
@@ -12,6 +12,7 @@
 #include "multiply_add.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_DO_NOT_OPTIMIZE
 #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
 #include "src/__support/number_pair.h"
 
@@ -173,6 +174,10 @@ LIBC_INLINE LIBC_CONSTEXPR NumberPair<T> exact_mult(T a, T b) {
 
   if constexpr (TargetHasFmaInstruction<T>::VALUE) {
     r.hi = a * b;
+    // Prevent `-r.hi` below from being rewritten into `a * (-b)` when `a * b`
+    // is duplicated (e.g. by SLP vectorization), which breaks directed
+    // rounding modes (`RU(a * (-b)) != -RU(a * b)`).
+    LIBC_DO_NOT_OPTIMIZE_ROUNDING(r.hi);
     r.lo = fputil::multiply_add(a, b, -r.hi);
   } else {
     // Dekker's Product.
@@ -231,6 +236,7 @@ LIBC_INLINE NumberPair<T> div(const NumberPair<T> &a, const NumberPair<T> &b) {
   NumberPair<T> r;
   T q = T(1) / b.hi;
   r.hi = a.hi * q;
+  LIBC_DO_NOT_OPTIMIZE_ROUNDING(r.hi);
 
 #ifdef LIBC_TARGET_CPU_HAS_FMA
   T e_hi = fputil::multiply_add(b.hi, -r.hi, a.hi);
diff --git a/libc/src/__support/macros/CMakeLists.txt b/libc/src/__support/macros/CMakeLists.txt
index c600b453af510f..8c254448e0dbd7 100644
--- a/libc/src/__support/macros/CMakeLists.txt
+++ b/libc/src/__support/macros/CMakeLists.txt
@@ -20,7 +20,11 @@ add_header_library(
   HDRS
     optimization.h
   DEPENDS
+    .attributes
+    .config
+    libc.src.__support.macros.properties.architectures
     libc.src.__support.macros.properties.compiler
+    libc.src.__support.macros.properties.cpu_features
 )
 
 add_header_library(
diff --git a/libc/src/__support/macros/optimization.h b/libc/src/__support/macros/optimization.h
index 55151bc132e2d6..ebf974e8e21d4a 100644
--- a/libc/src/__support/macros/optimization.h
+++ b/libc/src/__support/macros/optimization.h
@@ -12,7 +12,9 @@
 
 #include "src/__support/macros/attributes.h" // LIBC_INLINE
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h" // LIBC_COMPILER_IS_CLANG
+#include "src/__support/macros/properties/cpu_features.h"
 
 // We use a template to implement likely/unlikely to make sure that we don't
 // accidentally pass an integer.
@@ -27,8 +29,76 @@ template <>
 LIBC_ALWAYS_INLINE constexpr bool expects_bool_condition<bool>(bool value) {
   return value;
 }
+
+template <typename T>
+LIBC_ALWAYS_INLINE constexpr void do_not_optimize(T &) = delete;
+
+// Zero-cost SSA value barrier for float and double: keeps `val` in its native
+// register class without spilling to memory or moving across GPR/FPR banks,
+// while preventing the optimizer from looking through or duplicating `val`.
+LIBC_ALWAYS_INLINE void do_not_optimize_impl(LIBC_UNUSED float &val) {
+#if !defined(LIBC_COMPILER_IS_MSVC)
+#if defined(LIBC_TARGET_CPU_HAS_ARM_FPU_FLOAT)
+  asm("" : "+w"(val));
+#elif defined(LIBC_TARGET_ARCH_IS_X86) && defined(LIBC_TARGET_CPU_HAS_SSE2)
+  asm("" : "+x"(val));
+#elif defined(LIBC_TARGET_ARCH_IS_X86)
+  asm("" : "+t"(val));
+#elif defined(LIBC_TARGET_CPU_HAS_RISCV_FPU_FLOAT)
+  asm("" : "+f"(val));
+#elif defined(LIBC_TARGET_ARCH_IS_AMDGPU)
+  asm("" : "+v"(val));
+#elif defined(LIBC_TARGET_ARCH_IS_NVPTX)
+  asm("" : "+f"(val));
+#elif defined(LIBC_TARGET_ARCH_IS_ARM) ||                                      \
+    defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) ||                                  \
+    defined(LIBC_TARGET_ARCH_IS_WASM) || defined(LIBC_TARGET_ARCH_IS_HEXAGON)
+  asm("" : "+r"(val));
+#endif
+#endif // !LIBC_COMPILER_IS_MSVC
+}
+
+LIBC_ALWAYS_INLINE void do_not_optimize_impl(LIBC_UNUSED double &val) {
+#if !defined(LIBC_COMPILER_IS_MSVC)
+#if defined(LIBC_TARGET_CPU_HAS_ARM_FPU_DOUBLE)
+  asm("" : "+w"(val));
+#elif defined(LIBC_TARGET_ARCH_IS_X86) && defined(LIBC_TARGET_CPU_HAS_SSE2)
+  asm("" : "+x"(val));
+#elif defined(LIBC_TARGET_ARCH_IS_X86)
+  asm("" : "+t"(val));
+#elif defined(LIBC_TARGET_CPU_HAS_RISCV_FPU_DOUBLE)
+  asm("" : "+f"(val));
+#elif defined(LIBC_TARGET_ARCH_IS_AMDGPU)
+  asm("" : "+v"(val));
+#elif defined(LIBC_TARGET_ARCH_IS_NVPTX)
+  asm("" : "+d"(val));
+#elif defined(LIBC_TARGET_ARCH_IS_RISCV64) || defined(LIBC_TARGET_ARCH_IS_WASM)
+  asm("" : "+r"(val));
+#endif
+#endif // !LIBC_COMPILER_IS_MSVC
+}
+
+template <>
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT void do_not_optimize(float &val) {
+#if LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED
+  if (!__builtin_is_constant_evaluated())
+#endif
+    do_not_optimize_impl(val);
+}
+
+template <>
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT void do_not_optimize(double &val) {
+#if LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED
+  if (!__builtin_is_constant_evaluated())
+#endif
+    do_not_optimize_impl(val);
+}
+
 } // namespace details
 } // namespace LIBC_NAMESPACE_DECL
+
+#define LIBC_DO_NOT_OPTIMIZE(var) LIBC_NAMESPACE::details::do_not_optimize(var)
+
 #define LIBC_LIKELY(x)                                                         \
   (__builtin_expect(LIBC_NAMESPACE::details::expects_bool_condition((x)), true))
 #define LIBC_UNLIKELY(x)                                                       \
@@ -95,6 +165,9 @@ LIBC_ALWAYS_INLINE constexpr bool expects_bool_condition<bool>(bool value) {
 
 #if ((LIBC_MATH) & LIBC_MATH_ASSUME_ROUND_NEAREST_ONLY)
 #define LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+#define LIBC_DO_NOT_OPTIMIZE_ROUNDING(var) ((void)(var))
+#else
+#define LIBC_DO_NOT_OPTIMIZE_ROUNDING(var) LIBC_DO_NOT_OPTIMIZE(var)
 #endif
 
 // Helper macros to compare math implementation namespace tokens in #if
diff --git a/libc/test/src/__support/FPUtil/CMakeLists.txt b/libc/test/src/__support/FPUtil/CMakeLists.txt
index b888d187166d7f..964a37cecc9a3d 100644
--- a/libc/test/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/test/src/__support/FPUtil/CMakeLists.txt
@@ -73,6 +73,18 @@ add_fp_unittest(
     libc.src.__support.FPUtil.rounding_mode
 )
 
+add_fp_unittest(
+  double_double_test
+  SUITE
+    libc-fputil-tests
+  SRCS
+    double_double_test.cpp
+  DEPENDS
+    libc.src.__support.FPUtil.double_double
+    libc.src.__support.macros.config
+    libc.src.__support.macros.optimization
+)
+
 add_fp_unittest(
   float128_test
   SUITE
diff --git a/libc/test/src/__support/FPUtil/double_double_test.cpp b/libc/test/src/__support/FPUtil/double_double_test.cpp
new file mode 100644
index 00000000000000..2395568fca4040
--- /dev/null
+++ b/libc/test/src/__support/FPUtil/double_double_test.cpp
@@ -0,0 +1,172 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for LIBC_DO_NOT_OPTIMIZE and double-double utilities.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/RoundingModeUtils.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::fputil::DoubleDouble;
+using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode;
+using LIBC_NAMESPACE::fputil::testing::RoundingMode;
+
+namespace {
+
+#if LIBC_HAS_CONSTANT_EVALUATION
+constexpr float constexpr_barrier_check_f(float x) {
+  LIBC_DO_NOT_OPTIMIZE(x);
+  LIBC_DO_NOT_OPTIMIZE_ROUNDING(x);
+  return x + 1.0f;
+}
+constexpr double constexpr_barrier_check_d(double x) {
+  LIBC_DO_NOT_OPTIMIZE(x);
+  LIBC_DO_NOT_OPTIMIZE_ROUNDING(x);
+  return x + 1.0;
+}
+static_assert(constexpr_barrier_check_f(2.5f) == 3.5f);
+static_assert(constexpr_barrier_check_d(2.5) == 3.5);
+#endif
+
+} // namespace
+
+class LlvmLibcDoubleDoubleTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+};
+
+TEST_F(LlvmLibcDoubleDoubleTest, DoNotOptimizeFloatAndDouble) {
+  float f = 1.5f;
+  LIBC_DO_NOT_OPTIMIZE(f);
+  LIBC_DO_NOT_OPTIMIZE_ROUNDING(f);
+  EXPECT_FP_EQ(f, 1.5f);
+
+  double d = -2.25;
+  LIBC_DO_NOT_OPTIMIZE(d);
+  LIBC_DO_NOT_OPTIMIZE_ROUNDING(d);
+  EXPECT_FP_EQ(d, -2.25);
+}
+
+#if !defined(LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY) &&                       \
+    !defined(LIBC_COMPILER_IS_MSVC)
+TEST_F(LlvmLibcDoubleDoubleTest, DoNotOptimizeBlocksConstantFolding) {
+  {
+    ForceRoundingMode r(RoundingMode::Upward);
+    if (r.success) {
+      float one_f = 1.0f;
+      LIBC_DO_NOT_OPTIMIZE(one_f);
+      EXPECT_FP_EQ(one_f + 0x1.0p-50f, 0x1.000002p0f);
+
+      double one_d = 1.0;
+      LIBC_DO_NOT_OPTIMIZE_ROUNDING(one_d);
+      EXPECT_FP_EQ(one_d + 0x1.0p-100, 0x1.0000000000001p0);
+    }
+  }
+  {
+    ForceRoundingMode r(RoundingMode::Downward);
+    if (r.success) {
+      float neg_one_f = -1.0f;
+      LIBC_DO_NOT_OPTIMIZE(neg_one_f);
+      EXPECT_FP_EQ(neg_one_f - 0x1.0p-50f, -0x1.000002p0f);
+
+      double neg_one_d = -1.0;
+      LIBC_DO_NOT_OPTIMIZE_ROUNDING(neg_one_d);
+      EXPECT_FP_EQ(neg_one_d - 0x1.0p-100, -0x1.0000000000001p0);
+    }
+  }
+}
+#endif
+
+TEST_F(LlvmLibcDoubleDoubleTest, ExactMultDirectedRounding) {
+  if constexpr (LIBC_NAMESPACE::fputil::TargetHasFmaInstruction<
+                    double>::VALUE) {
+    // Exact product: (1 + 2^-52)^2 = (1 + 2^-51) + 2^-104
+    //                              = (1 + 3*2^-52) + (-2^-52 + 2^-104).
+    auto check_square = [](RoundingMode mode, double expected_hi,
+                           double expected_lo) {
+      ForceRoundingMode r(mode);
+      if (!r.success)
+        return;
+      double a = 1.0 + 0x1.0p-52;
+      LIBC_DO_NOT_OPTIMIZE(a);
+      DoubleDouble prod = LIBC_NAMESPACE::fputil::exact_mult(a, a);
+      EXPECT_FP_EQ(prod.hi, expected_hi);
+      EXPECT_FP_EQ(prod.lo, expected_lo);
+    };
+
+    check_square(RoundingMode::Nearest, 1.0 + 0x1.0p-51, 0x1.0p-104);
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+    check_square(RoundingMode::Upward, 1.0 + 0x1.8p-51,
+                 -0x1.0p-52 + 0x1.0p-104);
+    check_square(RoundingMode::Downward, 1.0 + 0x1.0p-51, 0x1.0p-104);
+    check_square(RoundingMode::TowardZero, 1.0 + 0x1.0p-51, 0x1.0p-104);
+#endif
+
+    // Four-way exact_mult + exact_add<false> cancellation pattern (as in
+    // sincos, b/586440175):
+    //   p0 = (1 + 2^-52) * (1 + 2^-52)   =  1 + 2^-51 + 2^-104
+    //   p1 = (1 + 2^-51) * -(1 + 2^-51)  = -1 - 2^-50 - 2^-102
+    //   p0 + p1 = -(2^-51 + 1.5 * 2^-103) (exact midpoint in ulp(2^-51))
+    //   p2 = (1 + 2^-50) * (1 + 2^-50)   =  1 + 2^-49 + 2^-100
+    //   p3 = (1 + 2^-49) * -(1 + 2^-49)  = -1 - 2^-48 - 2^-98
+    //   p2 + p3 = -(2^-49 + 3 * 2^-100)  (exact binary64:
+    //   -0x1.0000000000006p-49)
+    auto check_four_way = [](RoundingMode mode, double expected_s0,
+                             double expected_s1) {
+      ForceRoundingMode r(mode);
+      if (!r.success)
+        return;
+
+      double a0 = 1.0 + 0x1.0p-52;
+      double b0 = 1.0 + 0x1.0p-52;
+      double a1 = 1.0 + 0x1.0p-51;
+      double b1 = -(1.0 + 0x1.0p-51);
+      double a2 = 1.0 + 0x1.0p-50;
+      double b2 = 1.0 + 0x1.0p-50;
+      double a3 = 1.0 + 0x1.0p-49;
+      double b3 = -(1.0 + 0x1.0p-49);
+      LIBC_DO_NOT_OPTIMIZE(a0);
+      LIBC_DO_NOT_OPTIMIZE(b0);
+      LIBC_DO_NOT_OPTIMIZE(a1);
+      LIBC_DO_NOT_OPTIMIZE(b1);
+      LIBC_DO_NOT_OPTIMIZE(a2);
+      LIBC_DO_NOT_OPTIMIZE(b2);
+      LIBC_DO_NOT_OPTIMIZE(a3);
+      LIBC_DO_NOT_OPTIMIZE(b3);
+
+      DoubleDouble p0 = LIBC_NAMESPACE::fputil::exact_mult(a0, b0);
+      DoubleDouble p1 = LIBC_NAMESPACE::fputil::exact_mult(a1, b1);
+      DoubleDouble p2 = LIBC_NAMESPACE::fputil::exact_mult(a2, b2);
+      DoubleDouble p3 = LIBC_NAMESPACE::fputil::exact_mult(a3, b3);
+
+      DoubleDouble s0 = LIBC_NAMESPACE::fputil::exact_add<false>(p0.hi, p1.hi);
+      DoubleDouble s1 = LIBC_NAMESPACE::fputil::exact_add<false>(p2.hi, p3.hi);
+      s0.lo += p0.lo + p1.lo;
+      s1.lo += p2.lo + p3.lo;
+
+      EXPECT_FP_EQ(s0.hi + s0.lo, expected_s0);
+      EXPECT_FP_EQ(s1.hi + s1.lo, expected_s1);
+    };
+
+    check_four_way(RoundingMode::Nearest, -0x1.0000000000002p-51,
+                   -0x1.0000000000006p-49);
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+    check_four_way(RoundingMode::Upward, -0x1.0000000000001p-51,
+                   -0x1.0000000000006p-49);
+    check_four_way(RoundingMode::Downward, -0x1.0000000000002p-51,
+                   -0x1.0000000000006p-49);
+    check_four_way(RoundingMode::TowardZero, -0x1.0000000000001p-51,
+                   -0x1.0000000000006p-49);
+#endif
+  }
+}
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/FPUtil/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/FPUtil/BUILD.bazel
index 5bc72a3e9d798d..45d4b344c6ad4d 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/FPUtil/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/FPUtil/BUILD.bazel
@@ -434,6 +434,7 @@ libc_support_library(
         "//libc/src/__support:common",
         "//libc/src/__support:number_pair",
         "//libc/src/__support/macros:config",
+        "//libc/src/__support/macros:optimization",
         "//libc/src/__support/macros/properties:cpu_features",
     ],
 )
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/macros/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/macros/BUILD.bazel
index a084dae65fb0bb..598800555a680d 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/macros/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/macros/BUILD.bazel
@@ -58,7 +58,9 @@ libc_support_library(
     deps = [
         ":attributes",
         ":config",
+        "//libc/src/__support/macros/properties:architectures",
         "//libc/src/__support/macros/properties:compiler",
+        "//libc/src/__support/macros/properties:cpu_features",
     ],
 )
 
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/__support/FPUtil/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/__support/FPUtil/BUILD.bazel
index 60eff7e02a18ce..022de7e2dc951d 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/__support/FPUtil/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/__support/FPUtil/BUILD.bazel
@@ -10,6 +10,17 @@ package(default_visibility = ["//visibility:public"])
 
 licenses(["notice"])
 
+libc_test(
+    name = "double_double_test",
+    srcs = ["double_double_test.cpp"],
+    deps = [
+        "//libc/src/__support/FPUtil:double_double",
+        "//libc/src/__support/macros:config",
+        "//libc/src/__support/macros:optimization",
+        "//libc/test/UnitTest:fp_test_helpers",
+    ],
+)
+
 libc_test(
     name = "fpbits_test",
     srcs = ["fpbits_test.cpp"],

``````````

</details>


https://github.com/llvm/llvm-project/pull/243203


More information about the libc-commits mailing list