[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

@ -21,14 +21,14 @@
namespace
{
//namespace // clang doesn't like this!
//{
enum strnlen_mode
{
bounded, // strnlen mode; maximum_count is respected
unbounded, // strlen mode; maximum_count is ignored
};
}
//}
// 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
@ -78,16 +78,18 @@ static __forceinline size_t __cdecl common_strnlen_c(
#ifdef _CRT_SIMD_SUPPORT_AVAILABLE
_UCRT_ENABLE_EXTENDED_ISA
template <strnlen_mode Mode, __crt_simd_isa Isa, typename Element>
_Check_return_
_When_(maximum_count > _String_length_(string), _Post_satisfies_(return == _String_length_(string)))
_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,
size_t const maximum_count
) throw()
#if (defined(__GNUC__) || defined(__clang__)) && !defined(_UCRT_BUILD_SSE2) && !defined(_UCRT_BUILD_AVX2)
;
#else
{
using traits = __crt_simd_traits<Isa, Element>;
@ -170,10 +172,12 @@ _UCRT_ENABLE_EXTENDED_ISA
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
#if !defined(_UCRT_BUILD_SSE2) && !defined(_UCRT_BUILD_AVX2)
template <strnlen_mode Mode, typename Element>
_Check_return_
_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 // !defined(_UCRT_BUILD_SSE2) && !defined(_UCRT_BUILD_AVX2)