[UCRT] Fix GCC/Clang SIMD compilation

GCC and Clang need to mark functions that use SSE/AVX etc, either with a function attribute or a pragma around the function. strlen uses a template function that either uses SSE2 or AVX2. Previously the template was surrounded with pragmas to allow both SSE2 and AVX2, but that makes GCC assume that it can use AVX2 instructions even in the SSE2 version. To fix this the template instances are now build in individual compilation units for SSE2 and AVX, separate from the "dispatcher" function.
Now ucrtbase doesn't crash anymore on GCC build.

Another issue was the namespace around strnlen_mode, which has confused clang so much, that it forgot to instantiate the template code.
This commit is contained in:
Timo Kreuzer 2025-07-22 20:17:46 +03:00
parent e2deec8235
commit 719ea022ec
5 changed files with 92 additions and 10 deletions

View file

@ -19,22 +19,27 @@
#if defined _CRT_SIMD_SUPPORT_AVAILABLE #if defined _CRT_SIMD_SUPPORT_AVAILABLE
#if defined(__clang__) #if defined(__clang__)
#define _UCRT_ENABLE_EXTENDED_ISA \ #define _UCRT_ENABLE_SSE2 \
_Pragma("clang attribute push(__attribute__((target(\"sse2\"))), apply_to=function)")
#define _UCRT_ENABLE_AVX2 \
_Pragma("clang attribute push(__attribute__((target(\"sse2,avx,avx2\"))), apply_to=function)") _Pragma("clang attribute push(__attribute__((target(\"sse2,avx,avx2\"))), apply_to=function)")
#define _UCRT_RESTORE_DEFAULT_ISA \ #define _UCRT_RESTORE_DEFAULT_ISA \
_Pragma("clang attribute pop") _Pragma("clang attribute pop")
#elif defined(__GNUC__) #elif defined(__GNUC__)
#define _UCRT_ENABLE_EXTENDED_ISA \ #define _UCRT_ENABLE_SSE2 \
_Pragma("GCC push_options") \
_Pragma("GCC target(\"sse2\")")
#define _UCRT_ENABLE_AVX2 \
_Pragma("GCC push_options") \ _Pragma("GCC push_options") \
_Pragma("GCC target(\"avx2\")") _Pragma("GCC target(\"avx2\")")
#define _UCRT_RESTORE_DEFAULT_ISA \ #define _UCRT_RESTORE_DEFAULT_ISA \
_Pragma("GCC pop_options") _Pragma("GCC pop_options")
#else #else
#define _UCRT_ENABLE_EXTENDED_ISA #define _UCRT_ENABLE_SSE2
#define _UCRT_ENABLE_AVX2
#define _UCRT_RESTORE_DEFAULT_ISA #define _UCRT_RESTORE_DEFAULT_ISA
#endif #endif
_UCRT_ENABLE_EXTENDED_ISA
extern "C" int __isa_available; extern "C" int __isa_available;
@ -70,6 +75,7 @@ _UCRT_ENABLE_EXTENDED_ISA
}; };
_UCRT_ENABLE_SSE2
template <> template <>
struct __crt_simd_cleanup_guard<__crt_simd_isa::sse2> struct __crt_simd_cleanup_guard<__crt_simd_isa::sse2>
@ -120,7 +126,9 @@ _UCRT_ENABLE_EXTENDED_ISA
} }
}; };
_UCRT_RESTORE_DEFAULT_ISA
_UCRT_ENABLE_AVX2
template <> template <>
struct __crt_simd_cleanup_guard<__crt_simd_isa::avx2> struct __crt_simd_cleanup_guard<__crt_simd_isa::avx2>

View file

@ -57,6 +57,17 @@ list(APPEND UCRT_STRING_SOURCES
string/wmemmove_s.cpp string/wmemmove_s.cpp
) )
# Special handling for GCC and Clang
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
list(APPEND UCRT_STRING_SOURCES
string/strnlen-avx2.cpp
string/strnlen-sse2.cpp
)
set_source_files_properties(string/strnlen-sse2.cpp PROPERTIES COMPILE_OPTIONS "-msse2")
set_source_files_properties(string/strnlen-avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2")
endif()
if(${ARCH} STREQUAL "i386") if(${ARCH} STREQUAL "i386")
list(APPEND UCRT_STRING_ASM_SOURCES list(APPEND UCRT_STRING_ASM_SOURCES
string/i386/_memicmp.s string/i386/_memicmp.s

View file

@ -0,0 +1,29 @@
//
// strnlen-avx2.cpp
//
// Copyright (c) Timo Kreuzer
//
// Explicit template instantiations for AVX2 str(n)len code
//
#pragma GCC target("avx2")
#define _UCRT_BUILD_AVX2
#include "strnlen.cpp"
template
size_t __cdecl common_strnlen_simd<bounded, __crt_simd_isa::avx2, uint8_t>(
uint8_t const* const string,
size_t const maximum_count
) throw();
template
size_t __cdecl common_strnlen_simd<bounded, __crt_simd_isa::avx2, uint16_t>(
uint16_t const* const string,
size_t const maximum_count
) throw();
template
size_t __cdecl common_strnlen_simd<unbounded, __crt_simd_isa::avx2, uint16_t>(
uint16_t const* const string,
size_t const maximum_count
) throw();

View file

@ -0,0 +1,29 @@
//
// strnlen-sse2.cpp
//
// Copyright (c) Timo Kreuzer
//
// Explicit template instantiations for SSE2 str(n)len code
//
#pragma GCC target("sse2")
#define _UCRT_BUILD_SSE2
#include "strnlen.cpp"
template
size_t __cdecl common_strnlen_simd<bounded, __crt_simd_isa::sse2, uint8_t>(
uint8_t const* const string,
size_t const maximum_count
) throw();
template
size_t __cdecl common_strnlen_simd<bounded, __crt_simd_isa::sse2, uint16_t>(
uint16_t const* const string,
size_t const maximum_count
) throw();
template
size_t __cdecl common_strnlen_simd<unbounded, __crt_simd_isa::sse2, uint16_t>(
uint16_t const* const string,
size_t const maximum_count
) throw();

View file

@ -21,14 +21,14 @@
namespace //namespace // clang doesn't like this!
{ //{
enum strnlen_mode enum strnlen_mode
{ {
bounded, // strnlen mode; maximum_count is respected bounded, // strnlen mode; maximum_count is respected
unbounded, // strlen mode; maximum_count is ignored unbounded, // strlen mode; maximum_count is ignored
}; };
} //}
// This function returns true if we have reached the end of the range to be // This function returns true if we have reached the end of the range to be
// searched for a terminator. For the bounded strnlen functions, we must // searched for a terminator. For the bounded strnlen functions, we must
@ -78,16 +78,18 @@ static __forceinline size_t __cdecl common_strnlen_c(
#ifdef _CRT_SIMD_SUPPORT_AVAILABLE #ifdef _CRT_SIMD_SUPPORT_AVAILABLE
_UCRT_ENABLE_EXTENDED_ISA
template <strnlen_mode Mode, __crt_simd_isa Isa, typename Element> template <strnlen_mode Mode, __crt_simd_isa Isa, typename Element>
_Check_return_ _Check_return_
_When_(maximum_count > _String_length_(string), _Post_satisfies_(return == _String_length_(string))) _When_(maximum_count > _String_length_(string), _Post_satisfies_(return == _String_length_(string)))
_When_(maximum_count <= _String_length_(string), _Post_satisfies_(return == maximum_count)) _When_(maximum_count <= _String_length_(string), _Post_satisfies_(return == maximum_count))
static __inline size_t __cdecl common_strnlen_simd( size_t __cdecl common_strnlen_simd(
Element const* const string, Element const* const string,
size_t const maximum_count size_t const maximum_count
) throw() ) throw()
#if (defined(__GNUC__) || defined(__clang__)) && !defined(_UCRT_BUILD_SSE2) && !defined(_UCRT_BUILD_AVX2)
;
#else
{ {
using traits = __crt_simd_traits<Isa, Element>; using traits = __crt_simd_traits<Isa, Element>;
@ -170,10 +172,12 @@ _UCRT_ENABLE_EXTENDED_ISA
return static_cast<size_t>(it - string); return static_cast<size_t>(it - string);
} }
_UCRT_RESTORE_DEFAULT_ISA #endif // (defined(__GNUC__) || defined(__clang__)) && !defined(_UCRT_BUILD_SSE2) && !defined(_UCRT_BUILD_AVX2)
#endif // _CRT_SIMD_SUPPORT_AVAILABLE #endif // _CRT_SIMD_SUPPORT_AVAILABLE
#if !defined(_UCRT_BUILD_SSE2) && !defined(_UCRT_BUILD_AVX2)
template <strnlen_mode Mode, typename Element> template <strnlen_mode Mode, typename Element>
_Check_return_ _Check_return_
_When_(maximum_count > _String_length_(string), _Post_satisfies_(return == _String_length_(string))) _When_(maximum_count > _String_length_(string), _Post_satisfies_(return == _String_length_(string)))
@ -225,3 +229,4 @@ extern "C" size_t __cdecl wcslen(
} }
#endif // _M_ARM64 #endif // _M_ARM64
#endif // !defined(_UCRT_BUILD_SSE2) && !defined(_UCRT_BUILD_AVX2)