Skip to content

Commit 1886b1a

Browse files
[libc] add PREFER_GENERIC flag (#73744)
There are some basic vectorization features in standard architecture specifications. Such as SSE/SSE2 for x86-64, or NEON for aarch64. Even though such features are almost always available, we still need some methods to test fallback routines without any vectorization. Previous attempt in hsearch adds a DISABLE_SSE2_OPT flag that tries to compile the code with -mno-sse2 in order to test specific table scanning routines. However, it turns out that such flag may have some unwanted side effects hindering portability. This PR introduces PREFER_GENERIC as an alternative. When a target is built with PREFER_GENERIC, cmake will define a macro __LIBC_PREFER_GENERIC such that developers can selectively choose the fallback routine based on the macro.
1 parent c1511a6 commit 1886b1a

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

libc/cmake/modules/LLVMLibCFlagRules.cmake

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ endfunction(get_fq_dep_list_without_flag)
132132
# Special flags
133133
set(FMA_OPT_FLAG "FMA_OPT")
134134
set(ROUND_OPT_FLAG "ROUND_OPT")
135-
# SSE2 is the baseline for x86_64, so we add a negative flag to disable it if needed.
136-
set(DISABLE_SSE2_OPT_FLAG "DISABLE_SSE2_OPT")
135+
# This flag will define macros that gated away vectorization code such that
136+
# one can always test the fallback generic code path.
137+
set(PREFER_GENERIC_FLAG "PREFER_GENERIC")
137138

138139
# Skip FMA_OPT flag for targets that don't support fma.
139140
if(NOT((LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "FMA")) OR
@@ -145,8 +146,3 @@ endif()
145146
if(NOT(LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "SSE4_2")))
146147
set(SKIP_FLAG_EXPANSION_ROUND_OPT TRUE)
147148
endif()
148-
149-
# Skip DISABLE_SSE2_OPT flag for targets that don't support SSE2.
150-
if(NOT(LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "SSE2")))
151-
set(SKIP_FLAG_EXPANSION_DISABLE_SSE2_OPT TRUE)
152-
endif()

libc/cmake/modules/LLVMLibCObjectRules.cmake

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ function(_get_common_compile_options output_var flags)
1818
set(ADD_SSE4_2_FLAG TRUE)
1919
endif()
2020

21-
list(FIND flags ${DISABLE_SSE2_OPT_FLAG} no_sse2)
22-
if(${no_sse2} LESS 0)
23-
list(FIND flags "${DISABLE_SSE2_OPT_FLAG}__ONLY" no_sse2)
21+
list(FIND flags ${PREFER_GENERIC_FLAG} prefer_generic)
22+
if(${prefer_generic} LESS 0)
23+
list(FIND flags "${PREFER_GENERIC_FLAG}__ONLY" prefer_generic)
2424
endif()
25-
if((${no_sse2} GREATER -1) AND (LIBC_CPU_FEATURES MATCHES "SSE2"))
26-
set(DISABLE_SSE2_FLAG TRUE)
25+
if(${prefer_generic} GREATER -1)
26+
set(ADD_PREFER_GENERIC_FLAG TRUE)
2727
endif()
2828

2929
set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${ARGN})
@@ -66,17 +66,17 @@ function(_get_common_compile_options output_var flags)
6666
if(ADD_SSE4_2_FLAG)
6767
list(APPEND compile_options "-msse4.2")
6868
endif()
69-
if(DISABLE_SSE2_FLAG)
70-
list(APPEND compile_options "-mno-sse2")
69+
if(ADD_PREFER_GENERIC_FLAG)
70+
list(APPEND compile_options "-D__LIBC_PREFER_GENERIC")
7171
endif()
7272
elseif(MSVC)
7373
list(APPEND compile_options "/EHs-c-")
7474
list(APPEND compile_options "/GR-")
7575
if(ADD_FMA_FLAG)
7676
list(APPEND compile_options "/arch:AVX2")
7777
endif()
78-
if(DISABLE_SSE2_FLAG)
79-
list(APPEND compile_options "/arch:SSE")
78+
if(ADD_PREFER_GENERIC_FLAG)
79+
list(APPEND compile_options "/D__LIBC_PREFER_GENERIC")
8080
endif()
8181
endif()
8282
if (LIBC_TARGET_ARCHITECTURE_IS_GPU)

libc/src/__support/HashTable/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
# TODO: `DISABLE_SSE2_OPT` does not quite work yet.
2-
# We will investigate a better way of feature flag control.
31
add_header_library(
42
bitmask
53
HDRS
64
bitmask.h
75
FLAGS
8-
DISABLE_SSE2_OPT
6+
PREFER_GENERIC
97
DEPENDS
108
libc.src.__support.bit
119
libc.src.__support.macros.properties.cpu_features

libc/src/__support/HashTable/bitmask.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ template <class BitMask> struct IteratableBitMaskAdaptor : public BitMask {
8585
} // namespace internal
8686
} // namespace LIBC_NAMESPACE
8787

88-
#if defined(LIBC_TARGET_CPU_HAS_SSE2)
88+
#if defined(LIBC_TARGET_CPU_HAS_SSE2) && !defined(__LIBC_PREFER_GENERIC)
8989
#include "sse2/bitmask_impl.inc"
9090
#else
9191
#include "generic/bitmask_impl.inc"

0 commit comments

Comments
 (0)