gh-135755: rename undocumented HACL_CAN_COMPILE_SIMD{128,256} macros (#135847)

Rename undocumented `HACL_CAN_COMPILE_SIMD{128,256}` macros
to `_Py_HACL_CAN_COMPILE_VEC{128,256}`. These macros are private.
This commit is contained in:
Bénédikt Tran 2025-06-27 17:12:21 +02:00 committed by GitHub
parent 065194c1a9
commit 1e975aee28
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 62 additions and 55 deletions

View file

@ -43,25 +43,25 @@
// SIMD256 can't be compiled on macOS ARM64, and performance of SIMD128 isn't // SIMD256 can't be compiled on macOS ARM64, and performance of SIMD128 isn't
// great; but when compiling a universal2 binary, autoconf will set // great; but when compiling a universal2 binary, autoconf will set
// HACL_CAN_COMPILE_SIMD128 and HACL_CAN_COMPILE_SIMD256 because they *can* be // _Py_HACL_CAN_COMPILE_VEC{128,256} because they *can* be compiled on x86_64.
// compiled on x86_64. If we're on macOS ARM64, disable these preprocessor // If we're on macOS ARM64, we however disable these preprocessor symbols.
// symbols.
#if defined(__APPLE__) && defined(__arm64__) #if defined(__APPLE__) && defined(__arm64__)
# undef HACL_CAN_COMPILE_SIMD128 # undef _Py_HACL_CAN_COMPILE_VEC128
# undef HACL_CAN_COMPILE_SIMD256 # undef _Py_HACL_CAN_COMPILE_VEC256
#endif #endif
// Small mismatch between the variable names Python defines as part of configure // HACL* expects HACL_CAN_COMPILE_VEC* macros to be set in order to enable
// at the ones HACL* expects to be set in order to enable those headers. // the corresponding SIMD instructions so we need to "forward" the values
#define HACL_CAN_COMPILE_VEC128 HACL_CAN_COMPILE_SIMD128 // we just deduced above.
#define HACL_CAN_COMPILE_VEC256 HACL_CAN_COMPILE_SIMD256 #define HACL_CAN_COMPILE_VEC128 _Py_HACL_CAN_COMPILE_VEC128
#define HACL_CAN_COMPILE_VEC256 _Py_HACL_CAN_COMPILE_VEC256
#include "_hacl/Hacl_Hash_Blake2s.h" #include "_hacl/Hacl_Hash_Blake2s.h"
#include "_hacl/Hacl_Hash_Blake2b.h" #include "_hacl/Hacl_Hash_Blake2b.h"
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
#include "_hacl/Hacl_Hash_Blake2s_Simd128.h" #include "_hacl/Hacl_Hash_Blake2s_Simd128.h"
#endif #endif
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
#include "_hacl/Hacl_Hash_Blake2b_Simd256.h" #include "_hacl/Hacl_Hash_Blake2b_Simd256.h"
#endif #endif
@ -88,7 +88,7 @@ blake2_get_state(PyObject *module)
return (Blake2State *)state; return (Blake2State *)state;
} }
#if defined(HACL_CAN_COMPILE_SIMD128) || defined(HACL_CAN_COMPILE_SIMD256) #if defined(_Py_HACL_CAN_COMPILE_VEC128) || defined(_Py_HACL_CAN_COMPILE_VEC256)
static inline Blake2State * static inline Blake2State *
blake2_get_state_from_type(PyTypeObject *module) blake2_get_state_from_type(PyTypeObject *module)
{ {
@ -181,7 +181,7 @@ blake2module_init_cpu_features(Blake2State *state)
#undef ECX_SSE3 #undef ECX_SSE3
#undef EBX_AVX2 #undef EBX_AVX2
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
// TODO(picnixz): use py_cpuid_features (gh-125022) to improve detection // TODO(picnixz): use py_cpuid_features (gh-125022) to improve detection
state->can_run_simd128 = sse && sse2 && sse3 && sse41 && sse42 && cmov; state->can_run_simd128 = sse && sse2 && sse3 && sse41 && sse42 && cmov;
#else #else
@ -191,7 +191,7 @@ blake2module_init_cpu_features(Blake2State *state)
state->can_run_simd128 = false; state->can_run_simd128 = false;
#endif #endif
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
// TODO(picnixz): use py_cpuid_features (gh-125022) to improve detection // TODO(picnixz): use py_cpuid_features (gh-125022) to improve detection
state->can_run_simd256 = state->can_run_simd128 && avx && avx2; state->can_run_simd256 = state->can_run_simd128 && avx && avx2;
#else #else
@ -332,18 +332,18 @@ is_blake2s(blake2_impl impl)
static inline blake2_impl static inline blake2_impl
type_to_impl(PyTypeObject *type) type_to_impl(PyTypeObject *type)
{ {
#if defined(HACL_CAN_COMPILE_SIMD128) || defined(HACL_CAN_COMPILE_SIMD256) #if defined(_Py_HACL_CAN_COMPILE_VEC128) || defined(_Py_HACL_CAN_COMPILE_VEC256)
Blake2State *st = blake2_get_state_from_type(type); Blake2State *st = blake2_get_state_from_type(type);
#endif #endif
if (!strcmp(type->tp_name, blake2b_type_spec.name)) { if (!strcmp(type->tp_name, blake2b_type_spec.name)) {
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
return st->can_run_simd256 ? Blake2b_256 : Blake2b; return st->can_run_simd256 ? Blake2b_256 : Blake2b;
#else #else
return Blake2b; return Blake2b;
#endif #endif
} }
else if (!strcmp(type->tp_name, blake2s_type_spec.name)) { else if (!strcmp(type->tp_name, blake2s_type_spec.name)) {
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
return st->can_run_simd128 ? Blake2s_128 : Blake2s; return st->can_run_simd128 ? Blake2s_128 : Blake2s;
#else #else
return Blake2s; return Blake2s;
@ -357,10 +357,10 @@ typedef struct {
union { union {
Hacl_Hash_Blake2s_state_t *blake2s_state; Hacl_Hash_Blake2s_state_t *blake2s_state;
Hacl_Hash_Blake2b_state_t *blake2b_state; Hacl_Hash_Blake2b_state_t *blake2b_state;
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
Hacl_Hash_Blake2s_Simd128_state_t *blake2s_128_state; Hacl_Hash_Blake2s_Simd128_state_t *blake2s_128_state;
#endif #endif
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
Hacl_Hash_Blake2b_Simd256_state_t *blake2b_256_state; Hacl_Hash_Blake2b_Simd256_state_t *blake2b_256_state;
#endif #endif
}; };
@ -429,13 +429,13 @@ blake2_update_unlocked(Blake2Object *self, uint8_t *buf, Py_ssize_t len)
switch (self->impl) { switch (self->impl) {
// blake2b_256_state and blake2s_128_state must be if'd since // blake2b_256_state and blake2s_128_state must be if'd since
// otherwise this results in an unresolved symbol at link-time. // otherwise this results in an unresolved symbol at link-time.
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
case Blake2b_256: case Blake2b_256:
HACL_UPDATE(Hacl_Hash_Blake2b_Simd256_update, HACL_UPDATE(Hacl_Hash_Blake2b_Simd256_update,
self->blake2b_256_state, buf, len); self->blake2b_256_state, buf, len);
return; return;
#endif #endif
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
case Blake2s_128: case Blake2s_128:
HACL_UPDATE(Hacl_Hash_Blake2s_Simd128_update, HACL_UPDATE(Hacl_Hash_Blake2s_Simd128_update,
self->blake2s_128_state, buf, len); self->blake2s_128_state, buf, len);
@ -555,12 +555,12 @@ py_blake2_new(PyTypeObject *type, PyObject *data, int digest_size,
// Ensure that the states are NULL-initialized in case of an error. // Ensure that the states are NULL-initialized in case of an error.
// See: py_blake2_clear() for more details. // See: py_blake2_clear() for more details.
switch (self->impl) { switch (self->impl) {
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
case Blake2b_256: case Blake2b_256:
self->blake2b_256_state = NULL; self->blake2b_256_state = NULL;
break; break;
#endif #endif
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
case Blake2s_128: case Blake2s_128:
self->blake2s_128_state = NULL; self->blake2s_128_state = NULL;
break; break;
@ -623,12 +623,12 @@ py_blake2_new(PyTypeObject *type, PyObject *data, int digest_size,
} while (0) } while (0)
switch (self->impl) { switch (self->impl) {
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
case Blake2b_256: case Blake2b_256:
BLAKE2_MALLOC(Blake2b_Simd256, self->blake2b_256_state); BLAKE2_MALLOC(Blake2b_Simd256, self->blake2b_256_state);
break; break;
#endif #endif
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
case Blake2s_128: case Blake2s_128:
BLAKE2_MALLOC(Blake2s_Simd128, self->blake2s_128_state); BLAKE2_MALLOC(Blake2s_Simd128, self->blake2s_128_state);
break; break;
@ -756,12 +756,12 @@ blake2_blake2b_copy_unlocked(Blake2Object *self, Blake2Object *cpy)
} while (0) } while (0)
switch (self->impl) { switch (self->impl) {
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
case Blake2b_256: case Blake2b_256:
BLAKE2_COPY(Blake2b_Simd256, blake2b_256_state); BLAKE2_COPY(Blake2b_Simd256, blake2b_256_state);
break; break;
#endif #endif
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
case Blake2s_128: case Blake2s_128:
BLAKE2_COPY(Blake2s_Simd128, blake2s_128_state); BLAKE2_COPY(Blake2s_Simd128, blake2s_128_state);
break; break;
@ -840,12 +840,12 @@ static uint8_t
blake2_blake2b_compute_digest(Blake2Object *self, uint8_t *digest) blake2_blake2b_compute_digest(Blake2Object *self, uint8_t *digest)
{ {
switch (self->impl) { switch (self->impl) {
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
case Blake2b_256: case Blake2b_256:
return Hacl_Hash_Blake2b_Simd256_digest( return Hacl_Hash_Blake2b_Simd256_digest(
self->blake2b_256_state, digest); self->blake2b_256_state, digest);
#endif #endif
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
case Blake2s_128: case Blake2s_128:
return Hacl_Hash_Blake2s_Simd128_digest( return Hacl_Hash_Blake2s_Simd128_digest(
self->blake2s_128_state, digest); self->blake2s_128_state, digest);
@ -923,11 +923,11 @@ static Hacl_Hash_Blake2b_index
hacl_get_blake2_info(Blake2Object *self) hacl_get_blake2_info(Blake2Object *self)
{ {
switch (self->impl) { switch (self->impl) {
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
case Blake2b_256: case Blake2b_256:
return Hacl_Hash_Blake2b_Simd256_info(self->blake2b_256_state); return Hacl_Hash_Blake2b_Simd256_info(self->blake2b_256_state);
#endif #endif
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
case Blake2s_128: case Blake2s_128:
return Hacl_Hash_Blake2s_Simd128_info(self->blake2s_128_state); return Hacl_Hash_Blake2s_Simd128_info(self->blake2s_128_state);
#endif #endif
@ -975,12 +975,12 @@ py_blake2_clear(PyObject *op)
} while (0) } while (0)
switch (self->impl) { switch (self->impl) {
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
case Blake2b_256: case Blake2b_256:
BLAKE2_FREE(Blake2b_Simd256, self->blake2b_256_state); BLAKE2_FREE(Blake2b_Simd256, self->blake2b_256_state);
break; break;
#endif #endif
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
case Blake2s_128: case Blake2s_128:
BLAKE2_FREE(Blake2s_Simd128, self->blake2s_128_state); BLAKE2_FREE(Blake2s_Simd128, self->blake2s_128_state);
break; break;

View file

@ -31,14 +31,15 @@
#endif #endif
#if defined(__APPLE__) && defined(__arm64__) #if defined(__APPLE__) && defined(__arm64__)
# undef HACL_CAN_COMPILE_SIMD128 # undef _Py_HACL_CAN_COMPILE_VEC128
# undef HACL_CAN_COMPILE_SIMD256 # undef _Py_HACL_CAN_COMPILE_VEC256
#endif #endif
// Small mismatch between the variable names Python defines as part of configure // HACL* expects HACL_CAN_COMPILE_VEC* macros to be set in order to enable
// at the ones HACL* expects to be set in order to enable those headers. // the corresponding SIMD instructions so we need to "forward" the values
#define HACL_CAN_COMPILE_VEC128 HACL_CAN_COMPILE_SIMD128 // we just deduced above.
#define HACL_CAN_COMPILE_VEC256 HACL_CAN_COMPILE_SIMD256 #define HACL_CAN_COMPILE_VEC128 _Py_HACL_CAN_COMPILE_VEC128
#define HACL_CAN_COMPILE_VEC256 _Py_HACL_CAN_COMPILE_VEC256
#include "_hacl/Hacl_HMAC.h" #include "_hacl/Hacl_HMAC.h"
#include "_hacl/Hacl_Streaming_HMAC.h" // Hacl_Agile_Hash_* identifiers #include "_hacl/Hacl_Streaming_HMAC.h" // Hacl_Agile_Hash_* identifiers
@ -361,7 +362,7 @@ narrow_hmac_hash_kind(hmacmodule_state *state, HMAC_Hash_Kind kind)
{ {
switch (kind) { switch (kind) {
case Py_hmac_kind_hmac_blake2s_32: { case Py_hmac_kind_hmac_blake2s_32: {
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
if (state->can_run_simd128) { if (state->can_run_simd128) {
return Py_hmac_kind_hmac_vectorized_blake2s_32; return Py_hmac_kind_hmac_vectorized_blake2s_32;
} }
@ -369,7 +370,7 @@ narrow_hmac_hash_kind(hmacmodule_state *state, HMAC_Hash_Kind kind)
return kind; return kind;
} }
case Py_hmac_kind_hmac_blake2b_32: { case Py_hmac_kind_hmac_blake2b_32: {
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
if (state->can_run_simd256) { if (state->can_run_simd256) {
return Py_hmac_kind_hmac_vectorized_blake2b_32; return Py_hmac_kind_hmac_vectorized_blake2b_32;
} }
@ -1601,7 +1602,7 @@ hmacmodule_init_cpu_features(hmacmodule_state *state)
#undef ECX_SSE3 #undef ECX_SSE3
#undef EBX_AVX2 #undef EBX_AVX2
#if HACL_CAN_COMPILE_SIMD128 #if _Py_HACL_CAN_COMPILE_VEC128
// TODO(picnixz): use py_cpuid_features (gh-125022) to improve detection // TODO(picnixz): use py_cpuid_features (gh-125022) to improve detection
state->can_run_simd128 = sse && sse2 && sse3 && sse41 && sse42 && cmov; state->can_run_simd128 = sse && sse2 && sse3 && sse41 && sse42 && cmov;
#else #else
@ -1611,7 +1612,7 @@ hmacmodule_init_cpu_features(hmacmodule_state *state)
state->can_run_simd128 = false; state->can_run_simd128 = false;
#endif #endif
#if HACL_CAN_COMPILE_SIMD256 #if _Py_HACL_CAN_COMPILE_VEC256
// TODO(picnixz): use py_cpuid_features (gh-125022) to improve detection // TODO(picnixz): use py_cpuid_features (gh-125022) to improve detection
state->can_run_simd256 = state->can_run_simd128 && avx && avx2; state->can_run_simd256 = state->can_run_simd128 && avx && avx2;
#else #else

View file

@ -419,8 +419,12 @@
<ClCompile Include="..\Modules\_abc.c" /> <ClCompile Include="..\Modules\_abc.c" />
<ClCompile Include="..\Modules\_bisectmodule.c" /> <ClCompile Include="..\Modules\_bisectmodule.c" />
<ClCompile Include="..\Modules\blake2module.c"> <ClCompile Include="..\Modules\blake2module.c">
<PreprocessorDefinitions Condition="'$(Platform)' == 'x64'">HACL_CAN_COMPILE_SIMD128;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions Condition="'$(Platform)' == 'x64'">
<PreprocessorDefinitions Condition="'$(Platform)' == 'x64'">HACL_CAN_COMPILE_SIMD256;%(PreprocessorDefinitions)</PreprocessorDefinitions> _Py_HACL_CAN_COMPILE_VEC128;%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Platform)' == 'x64'">
_Py_HACL_CAN_COMPILE_VEC256;%(PreprocessorDefinitions)
</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<ClCompile Include="..\Modules\_codecsmodule.c" /> <ClCompile Include="..\Modules\_codecsmodule.c" />
<ClCompile Include="..\Modules\_collectionsmodule.c" /> <ClCompile Include="..\Modules\_collectionsmodule.c" />

4
configure generated vendored
View file

@ -32633,7 +32633,7 @@ then :
LIBHACL_SIMD128_FLAGS="-msse -msse2 -msse3 -msse4.1 -msse4.2" LIBHACL_SIMD128_FLAGS="-msse -msse2 -msse3 -msse4.1 -msse4.2"
printf "%s\n" "#define HACL_CAN_COMPILE_SIMD128 1" >>confdefs.h printf "%s\n" "#define _Py_HACL_CAN_COMPILE_VEC128 1" >>confdefs.h
# macOS universal2 builds *support* the -msse etc flags because they're # macOS universal2 builds *support* the -msse etc flags because they're
@ -32709,7 +32709,7 @@ then :
LIBHACL_SIMD256_FLAGS="-mavx2" LIBHACL_SIMD256_FLAGS="-mavx2"
printf "%s\n" "#define HACL_CAN_COMPILE_SIMD256 1" >>confdefs.h printf "%s\n" "#define _Py_HACL_CAN_COMPILE_VEC256 1" >>confdefs.h
# macOS universal2 builds *support* the -mavx2 compiler flag because it's # macOS universal2 builds *support* the -mavx2 compiler flag because it's

View file

@ -8026,7 +8026,8 @@ then
AX_CHECK_COMPILE_FLAG([-msse -msse2 -msse3 -msse4.1 -msse4.2],[ AX_CHECK_COMPILE_FLAG([-msse -msse2 -msse3 -msse4.1 -msse4.2],[
[LIBHACL_SIMD128_FLAGS="-msse -msse2 -msse3 -msse4.1 -msse4.2"] [LIBHACL_SIMD128_FLAGS="-msse -msse2 -msse3 -msse4.1 -msse4.2"]
AC_DEFINE([HACL_CAN_COMPILE_SIMD128], [1], [HACL* library can compile SIMD128 implementations]) AC_DEFINE([_Py_HACL_CAN_COMPILE_VEC128], [1], [
HACL* library can compile SIMD128 implementations])
# macOS universal2 builds *support* the -msse etc flags because they're # macOS universal2 builds *support* the -msse etc flags because they're
# available on x86_64. However, performance of the HACL SIMD128 implementation # available on x86_64. However, performance of the HACL SIMD128 implementation
@ -8057,7 +8058,8 @@ if test "$ac_sys_system" != "Linux-android" -a "$ac_sys_system" != "WASI" || \
then then
AX_CHECK_COMPILE_FLAG([-mavx2],[ AX_CHECK_COMPILE_FLAG([-mavx2],[
[LIBHACL_SIMD256_FLAGS="-mavx2"] [LIBHACL_SIMD256_FLAGS="-mavx2"]
AC_DEFINE([HACL_CAN_COMPILE_SIMD256], [1], [HACL* library can compile SIMD256 implementations]) AC_DEFINE([_Py_HACL_CAN_COMPILE_VEC256], [1], [
HACL* library can compile SIMD256 implementations])
# macOS universal2 builds *support* the -mavx2 compiler flag because it's # macOS universal2 builds *support* the -mavx2 compiler flag because it's
# available on x86_64; but the HACL SIMD256 build then fails because the # available on x86_64; but the HACL SIMD256 build then fails because the

View file

@ -50,12 +50,6 @@
/* Define if getpgrp() must be called as getpgrp(0). */ /* Define if getpgrp() must be called as getpgrp(0). */
#undef GETPGRP_HAVE_ARG #undef GETPGRP_HAVE_ARG
/* HACL* library can compile SIMD128 implementations */
#undef HACL_CAN_COMPILE_SIMD128
/* HACL* library can compile SIMD256 implementations */
#undef HACL_CAN_COMPILE_SIMD256
/* Define if you have the 'accept' function. */ /* Define if you have the 'accept' function. */
#undef HAVE_ACCEPT #undef HAVE_ACCEPT
@ -2026,6 +2020,12 @@
/* Defined if _Complex C type can be used with libffi. */ /* Defined if _Complex C type can be used with libffi. */
#undef _Py_FFI_SUPPORT_C_COMPLEX #undef _Py_FFI_SUPPORT_C_COMPLEX
/* HACL* library can compile SIMD128 implementations */
#undef _Py_HACL_CAN_COMPILE_VEC128
/* HACL* library can compile SIMD256 implementations */
#undef _Py_HACL_CAN_COMPILE_VEC256
/* Define to force use of thread-safe errno, h_errno, and other functions */ /* Define to force use of thread-safe errno, h_errno, and other functions */
#undef _REENTRANT #undef _REENTRANT