Skip to content

[libc] add PREFER_GENERIC flag #73744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions libc/cmake/modules/LLVMLibCFlagRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ endfunction(get_fq_dep_list_without_flag)
# Special flags
set(FMA_OPT_FLAG "FMA_OPT")
set(ROUND_OPT_FLAG "ROUND_OPT")
# SSE2 is the baseline for x86_64, so we add a negative flag to disable it if needed.
set(DISABLE_SSE2_OPT_FLAG "DISABLE_SSE2_OPT")
# This flag will define macros that gated away vectorization code such that
# one can always test the fallback generic code path.
set(PREFER_GENERIC_FLAG "PREFER_GENERIC")

# Skip FMA_OPT flag for targets that don't support fma.
if(NOT((LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "FMA")) OR
Expand All @@ -145,8 +146,3 @@ endif()
if(NOT(LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "SSE4_2")))
set(SKIP_FLAG_EXPANSION_ROUND_OPT TRUE)
endif()

# Skip DISABLE_SSE2_OPT flag for targets that don't support SSE2.
if(NOT(LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "SSE2")))
set(SKIP_FLAG_EXPANSION_DISABLE_SSE2_OPT TRUE)
endif()
18 changes: 9 additions & 9 deletions libc/cmake/modules/LLVMLibCObjectRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ function(_get_common_compile_options output_var flags)
set(ADD_SSE4_2_FLAG TRUE)
endif()

list(FIND flags ${DISABLE_SSE2_OPT_FLAG} no_sse2)
if(${no_sse2} LESS 0)
list(FIND flags "${DISABLE_SSE2_OPT_FLAG}__ONLY" no_sse2)
list(FIND flags ${PREFER_GENERIC_FLAG} prefer_generic)
if(${prefer_generic} LESS 0)
list(FIND flags "${PREFER_GENERIC_FLAG}__ONLY" prefer_generic)
endif()
if((${no_sse2} GREATER -1) AND (LIBC_CPU_FEATURES MATCHES "SSE2"))
set(DISABLE_SSE2_FLAG TRUE)
if(${prefer_generic} GREATER -1)
set(ADD_PREFER_GENERIC_FLAG TRUE)
endif()

set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${ARGN})
Expand Down Expand Up @@ -66,17 +66,17 @@ function(_get_common_compile_options output_var flags)
if(ADD_SSE4_2_FLAG)
list(APPEND compile_options "-msse4.2")
endif()
if(DISABLE_SSE2_FLAG)
list(APPEND compile_options "-mno-sse2")
if(ADD_PREFER_GENERIC_FLAG)
list(APPEND compile_options "-D__LIBC_PREFER_GENERIC")
endif()
elseif(MSVC)
list(APPEND compile_options "/EHs-c-")
list(APPEND compile_options "/GR-")
if(ADD_FMA_FLAG)
list(APPEND compile_options "/arch:AVX2")
endif()
if(DISABLE_SSE2_FLAG)
list(APPEND compile_options "/arch:SSE")
if(ADD_PREFER_GENERIC_FLAG)
list(APPEND compile_options "/D__LIBC_PREFER_GENERIC")
endif()
endif()
if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
Expand Down
4 changes: 1 addition & 3 deletions libc/src/__support/HashTable/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# TODO: `DISABLE_SSE2_OPT` does not quite work yet.
# We will investigate a better way of feature flag control.
add_header_library(
bitmask
HDRS
bitmask.h
FLAGS
DISABLE_SSE2_OPT
PREFER_GENERIC
DEPENDS
libc.src.__support.bit
libc.src.__support.macros.properties.cpu_features
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/HashTable/bitmask.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ template <class BitMask> struct IteratableBitMaskAdaptor : public BitMask {
} // namespace internal
} // namespace LIBC_NAMESPACE

#if defined(LIBC_TARGET_CPU_HAS_SSE2)
#if defined(LIBC_TARGET_CPU_HAS_SSE2) && !defined(__LIBC_PREFER_GENERIC)
#include "sse2/bitmask_impl.inc"
#else
#include "generic/bitmask_impl.inc"
Expand Down