[libc-commits] [libc] 756e515 - [libc] Simplify fma handling for riscv (#149673)

via libc-commits libc-commits at lists.llvm.org
Sat Jul 19 15:10:57 PDT 2025


Author: Mikhail R. Gadelha
Date: 2025-07-19T19:10:52-03:00
New Revision: 756e515ce317a282fd5adf08b54600337346c6b0

URL: https://github.com/llvm/llvm-project/commit/756e515ce317a282fd5adf08b54600337346c6b0
DIFF: https://github.com/llvm/llvm-project/commit/756e515ce317a282fd5adf08b54600337346c6b0.diff

LOG: [libc] Simplify fma handling for riscv (#149673)

This PR simplifies how we enable the different fma configs for riscv:

1. Removes __LIBC_RISCV_USE_FMA define
2. Checks if __riscv_flen is defined to set LIBC_TARGET_CPU_HAS_FMA

As a bonus, we enable *FMA_OPT tests for rv32, so any rv32 hardware that
doesn't implement the f/d extensions is also covered by the tests.

Added: 
    

Modified: 
    libc/cmake/modules/LLVMLibCArchitectures.cmake
    libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
    libc/cmake/modules/LLVMLibCFlagRules.cmake
    libc/src/__support/macros/properties/cpu_features.h

Removed: 
    


################################################################################
diff  --git a/libc/cmake/modules/LLVMLibCArchitectures.cmake b/libc/cmake/modules/LLVMLibCArchitectures.cmake
index c94a407d974df..d4103f8a5a23f 100644
--- a/libc/cmake/modules/LLVMLibCArchitectures.cmake
+++ b/libc/cmake/modules/LLVMLibCArchitectures.cmake
@@ -153,9 +153,11 @@ elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "x86_64")
 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "i386")
   set(LIBC_TARGET_ARCHITECTURE_IS_X86 TRUE)
 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "riscv64")
+  set(LIBC_TARGET_ARCHITECTURE_IS_ANY_RISCV TRUE)
   set(LIBC_TARGET_ARCHITECTURE_IS_RISCV64 TRUE)
   set(LIBC_TARGET_ARCHITECTURE "riscv")
 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "riscv32")
+  set(LIBC_TARGET_ARCHITECTURE_IS_ANY_RISCV TRUE)
   set(LIBC_TARGET_ARCHITECTURE_IS_RISCV32 TRUE)
   set(LIBC_TARGET_ARCHITECTURE "riscv")
 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "amdgpu")

diff  --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 82d06e2b9eb55..2478fde64d430 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -13,7 +13,7 @@ endif()
 function(_get_compile_options_from_flags output_var)
   set(compile_options "")
 
-  if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64 OR(LIBC_CPU_FEATURES MATCHES "FMA"))
+  if(LIBC_CPU_FEATURES MATCHES "FMA")
     check_flag(ADD_FMA_FLAG ${FMA_OPT_FLAG} ${ARGN})
   endif()
   check_flag(ADD_ROUND_OPT_FLAG ${ROUND_OPT_FLAG} ${ARGN})
@@ -25,8 +25,6 @@ function(_get_compile_options_from_flags output_var)
       if(LIBC_TARGET_ARCHITECTURE_IS_X86_64)
         list(APPEND compile_options "-mavx2")
         list(APPEND compile_options "-mfma")
-      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

diff  --git a/libc/cmake/modules/LLVMLibCFlagRules.cmake b/libc/cmake/modules/LLVMLibCFlagRules.cmake
index 7d5e73c2f1214..4bbd21ab569dc 100644
--- a/libc/cmake/modules/LLVMLibCFlagRules.cmake
+++ b/libc/cmake/modules/LLVMLibCFlagRules.cmake
@@ -270,7 +270,7 @@ set(MISC_MATH_BASIC_OPS_OPT_FLAG "MISC_MATH_BASIC_OPS_OPT")
 # Skip FMA_OPT flag for targets that don't support fma.
 if(NOT DEFINED SKIP_FLAG_EXPANSION_FMA_OPT)
   if(NOT((LIBC_TARGET_ARCHITECTURE_IS_X86_64 AND (LIBC_CPU_FEATURES MATCHES "FMA")) OR
-        LIBC_TARGET_ARCHITECTURE_IS_RISCV64))
+        LIBC_TARGET_ARCHITECTURE_IS_ANY_RISCV))
     set(SKIP_FLAG_EXPANSION_FMA_OPT TRUE)
   endif()
 endif()

diff  --git a/libc/src/__support/macros/properties/cpu_features.h b/libc/src/__support/macros/properties/cpu_features.h
index cdb2df97b2b9a..fde30eadfd83b 100644
--- a/libc/src/__support/macros/properties/cpu_features.h
+++ b/libc/src/__support/macros/properties/cpu_features.h
@@ -81,7 +81,7 @@
 #endif
 
 #if defined(__ARM_FEATURE_FMA) || (defined(__AVX2__) && defined(__FMA__)) ||   \
-    defined(__NVPTX__) || defined(__AMDGPU__) || defined(__LIBC_RISCV_USE_FMA)
+    defined(__NVPTX__) || defined(__AMDGPU__) || defined(__riscv_flen)
 #define LIBC_TARGET_CPU_HAS_FMA
 // Provide a more fine-grained control of FMA instruction for ARM targets.
 #if defined(LIBC_TARGET_CPU_HAS_FPU_HALF)


        


More information about the libc-commits mailing list