[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
Tue Nov 17 10:50:29 PST 2026


================
@@ -27,8 +29,71 @@ template <>
 LIBC_ALWAYS_INLINE constexpr bool expects_bool_condition<bool>(bool value) {
   return value;
 }
+
+template <typename T> void do_not_optimize_impl(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`.
+// Kept in non-constexpr helpers because C++17 rejects inline `asm` statements
+// directly inside `constexpr` functions (`-Wc++20-extensions`). Skipped on
+// `cl.exe` (`LIBC_COMPILER_IS_MSVC`, which excludes `clang-cl`) because MSVC
+// does not support GNU extended `asm` register constraints.
+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 <typename T>
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT void do_not_optimize(T &val) {
+#if LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED
+  if (!__builtin_is_constant_evaluated())
----------------
lntue wrote:

I'm trying to make `__support/macros` folder not depending on `__support/CPP` folder, only the other way around.

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


More information about the libc-commits mailing list