8000 gh-125022: add support for simple SIMD features detection by picnixz · Pull Request #125011 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-125022: add support for simple SIMD features detection #125011

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

Draft
wants to merge 70 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
9a6ccb5
support simple SIMD detection
picnixz Oct 5, 2024
f4e4f99
add _Py prefix
picnixz Oct 5, 2024
5006686
Use `_py` prefix
picnixz Oct 5, 2024
3c0b4f1
make the interface friendlier for future adjustments
picnixz Oct 6, 2024
01ed21a
Allow `cpu_simd_flags` to be merged.
picnixz Oct 6, 2024
969a619
update comments
picnixz Oct 6, 2024
5a5acc2
fix typo
picnixz Oct 6, 2024
ac1b165
fix configure script
picnixz Oct 6, 2024
6f304f2
fix bit detection
picnixz Oct 6, 2024
f3bd027
Harden detection of AVX instructions.
picnixz Oct 7, 2024
16b2aed
do not guard the parsing of `os_xsave`
picnixz Oct 7, 2024
5018fa9
Remove old comment.
picnixz Oct 7, 2024
e758065
Update cpuinfo.c comments
picnixz Oct 7, 2024
731be81
Update pycore_cpuinfo.h comments
picnixz Oct 7, 2024
7947715
fix lint
picnixz Oct 7, 2024
7a17cbb
I really shouldn't use a Web UI
picnixz Oct 7, 2024
76f67b1
Fix _xgetbv() on Windows builds.
picnixz Oct 7, 2024
0b49a50
fix comment
picnixz Oct 8, 2024
9fd6152
harden detection of CPU features
picnixz Oct 8, 2024
97a0fc5
update configure
picnixz Oct 11, 2024
f7da530
Merge remote-tracking branch 'upstream/main' into core/simd-helpers-1…
picnixz Oct 11, 2024
5f2884d
update comments
picnixz Oct 13, 2024
7c3b74e
update Makefile
picnixz Oct 13, 2024
130d099
address Erlend's review
picnixz Oct 14, 2024
cd575f0
lint & comment fixups
picnixz Oct 14, 2024
2b597a4
Update docs
picnixz Oct 27, 2024
78be530
Fix typo
picnixz Oct 27, 2024
fd47f0e
Merge branch 'main' into core/simd-helpers
picnixz Dec 17, 2024
cbb7b53
re-export functions for extension modules
picnixz Dec 17, 2024
21d8ca8
rename os_xsave to osxsave for future automatism
picnixz Dec 17, 2024
1f9dbb4
remember `maxleaf` and make detection more readable
picnixz Dec 18, 2024
553aa7c
use enumeration for flags
picnixz Dec 18, 2024
39d2ba4
fix warnings
picnixz Dec 21, 2024
602bb9c
Merge branch 'main' into core/simd-helpers
picnixz Dec 21, 2024
d6a3523
remove un-necessary comment and newline continuation
picnixz Dec 22, 2024
ff4212e
Merge branch 'main' into core/simd-helpers
picnixz Feb 16, 2025
3cb79f6
regen configure
picnixz Feb 17, 2025
e0a578c
clinic now supports empty comment lines in Python blocks
picnixz Feb 17, 2025
6fdbbdf
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz Mar 11, 2025
c265851
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz Mar 29, 2025
c12f9c7
move cpuinfo enumerations to real invokable Python scripts
picnixz Mar 29, 2025
a6c443f
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz Apr 5, 2025
bd3589f
add comments
picnixz Apr 5, 2025
d213b67
update C comments
picnixz Apr 8, 2025
4109d90
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz Apr 8, 2025
19b7d86
TMP: usage proof-of-concept
picnixz Apr 8, 2025
1732b6b
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz Apr 22, 2025
d59d06d
improve configure.ac
picnixz Apr 22, 2025
bc2c1e5
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz Apr 25, 2025
4a92103
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz Jun 2, 2025
26ed6fd
Merge branch 'main' into feat/core/simd-125022
picnixz Jun 30, 2025
d00da3e
post-merge
picnixz Jun 30, 2025
8b7ecfb
use `_Py` prefix to prevent public namespace pollution
picnixz Jun 30, 2025
3c31ba3
let the compiler decide on the inlineness
picnixz Jun 30, 2025
143d57e
drop CPUID_REG alias
picnixz Jun 30, 2025
ee2a83c
simplify `_Py_cpuid_check_features`
picnixz Jun 30, 2025
e6d4583
amend docs for `_Py_cpuid_disable_features`
picnixz Jun 30, 2025
838f928
use macros to support larger flag ranges
picnixz Jun 30, 2025
62c9a40
handle -Wpedantic
picnixz Jun 30, 2025
a22aa95
reorganize files
picnixz Jul 8, 2025
87039dc
suppress compilation warnings
picnixz Jul 8, 2025
8a4b120
add linting
picnixz Jul 8, 2025
8603e14
typo
picnixz Jul 8, 2025
d6213a5
typo
picnixz Jul 8, 2025
79d5b34
log more!
picnixz Jul 13, 2025
f69d74a
skip CI
picnixz Jul 13, 2025
cb9065d
more printf
picnixz Jul 13, 2025
88df3b7
faster ci
picnixz Jul 13, 2025
db64ba5
!!
picnixz Jul 13, 2025
04012b4
!!
picnixz Jul 13, 2025
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
Prev Previous commit
Next Next commit
use _Py prefix to prevent public namespace pollution
  • Loading branch information
picnixz committed Jun 30, 2025
commit 8b7ecfb856508896e90fd9142968ac6cc2389dda
22 changes: 11 additions & 11 deletions Include/internal/pycore_cpuinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ extern "C" {
#include "pycore_cpuinfo_cpuid_features.h"
#include "pycore_cpuinfo_xsave_features.h"

typedef struct py_cpuid_features {
typedef struct _Py_cpuid_features_s {
uint32_t maxleaf;
/*
* Macro to declare a member flag of 'py_cpuid_features' as a uint8_t.
* Macro to declare a member flag of '_Py_cpuid_features' as a uint8_t.
* Whenever this macro is used, do not forget to update the number of
* fields and the bitsize of the 'ready' member (see structure end).
*/
Expand Down Expand Up @@ -116,7 +116,7 @@ typedef struct py_cpuid_features {
// number of fields (40) and adjust the bitsize of 'ready'
// so that the size of this structure is a multiple of 8.
uint8_t ready; // set if the structure is ready for usage
} py_cpuid_features;
} _Py_cpuid_features;

/*
* Explicitly initialize all members to zero to guarantee that
Expand All @@ -128,7 +128,7 @@ typedef struct py_cpuid_features {
* Note: This function does not set any exception and thus never fails.
*/
PyAPI_FUNC(void)
_Py_cpuid_disable_features(py_cpuid_features *flags);
_Py_cpuid_disable_features(_Py_cpuid_features *flags);

/*
* Check whether the structure is ready and flags are inter-compatible,
Expand All @@ -140,7 +140,7 @@ _Py_cpuid_disable_features(py_cpuid_features *flags);
* Note: This function does not set any exception and thus never fails.
*/
PyAPI_FUNC(int)
_Py_cpuid_check_features(const py_cpuid_features *flags);
_Py_cpuid_check_features(const _Py_cpuid_features *flags);

/*
* Return 1 if all expected flags are set in 'actual', 0 otherwise.
Expand All @@ -150,8 +150,8 @@ _Py_cpuid_check_features(const py_cpuid_features *flags);
* Note: This function does not set any exception and thus never fails.
*/
PyAPI_FUNC(int)
_Py_cpuid_has_features(const py_cpuid_features *actual,
const py_cpuid_features *expect);
_Py_cpuid_has_features(const _Py_cpuid_features *actual,
const _Py_cpuid_features *expect);

/*
* Return 1 if 'actual' and 'expect' are identical, 0 otherwise.
Expand All @@ -161,16 +161,16 @@ _Py_cpuid_has_features(const py_cpuid_features *actual,
* Note: This function does not set any exception and thus never fails.
*/
PyAPI_FUNC(int)
_Py_cpuid_match_features(const py_cpuid_features *actual,
const py_cpuid_features *expect);
_Py_cpuid_match_features(const _Py_cpuid_features *actual,
const _Py_cpuid_features *expect);

/*
* Detect the available features on this machine, storing the result in 'flags'.
* Detect the available host features, storing the result in 'flags'.
*
* Note: This function does not set any exception and thus never fails.
*/
PyAPI_FUNC(void)
_Py_cpuid_detect_features(py_cpuid_features *flags);
_Py_cpuid_detect_features(_Py_cpuid_features *flags);

#ifdef __cplusplus
}
Expand Down
103 changes: 47 additions & 56 deletions Include/internal/pycore_cpuinfo_cpuid_features.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
/**
* @author Bénédikt Tran
* @seealso Tools/cpuinfo/cpuid_features_gen.py
* @seealso @file Tools/cpuinfo/cpuid_features_gen.py
*
* The enumeration describes masks to apply on CPUID output registers.
*
* Member names are Py_CPUID_MASK_<REGISTER>_L<LEAF>[S<SUBLEAF>]_<FEATURE>,
* Member names are _Py_CPUID_MASK_<REGISTER>_L<LEAF>[S<SUBLEAF>]_<FEATURE>,
* where <> (resp. []) denotes a required (resp. optional) group and:
*
* - REGISTER is EAX, EBX, ECX or EDX,
Expand Down Expand Up @@ -35,68 +35,59 @@ extern "C" {

#include "Python.h"

// fmt: off
/*[python input]
import importlib
import os
import sys

ROOT = os.getcwd()
TOOL = os.path.join(ROOT, "Tools/cpuinfo/cpuid_features_gen.py")
TOOL = os.path.realpath(TOOL)

if not os.path.exists(TOOL):
raise FileNotFoundError(TOOL)

sys.path.insert(0, os.path.dirname(os.path.dirname(TOOL)))
module = importlib.import_module("cpuinfo.cpuid_features_gen")
print(module.generate_cpuid_features_enum("py_cpuid_feature_mask"))
import os, sys
sys.path.insert(0, os.path.realpath(os.path.join(os.getcwd(), "Tools")))
from cpuinfo.cpuid_features_gen import generate_cpuid_features_enum
print(generate_cpuid_features_enum("_Py_cpuid_feature_mask"))
[python start generated code]*/
typedef enum py_cpuid_feature_mask {
// fmt: off
/** Enumeration for CPUID features */
enum _Py_cpuid_feature_mask_e {
/* CPUID (LEAF=1, SUBLEAF=0) [ECX] */
Py_CPUID_MASK_ECX_L1_SSE3 = 0x00000001, // bit = 0
Py_CPUID_MASK_ECX_L1_PCLMULQDQ = 0x00000002, // bit = 1
Py_CPUID_MASK_ECX_L1_SSSE3 = 0x00000200, // bit = 9
Py_CPUID_MASK_ECX_L1_FMA = 0x00001000, // bit = 12
Py_CPUID_MASK_ECX_L1_SSE4_1 = 0x00080000, // bit = 19
Py_CPUID_MASK_ECX_L1_SSE4_2 = 0x00100000, // bit = 20
Py_CPUID_MASK_ECX_L1_POPCNT = 0x00800000, // bit = 23
Py_CPUID_MASK_ECX_L1_XSAVE = 0x04000000, // bit = 26
Py_CPUID_MASK_ECX_L1_OSXSAVE = 0x08000000, // bit = 27
Py_CPUID_MASK_ECX_L1_AVX = 0x10000000, // bit = 28
_Py_CPUID_MASK_ECX_L1_SSE3 = 0x00000001, // bit = 0
_Py_CPUID_MASK_ECX_L1_PCLMULQDQ = 0x00000002, // bit = 1
_Py_CPUID_MASK_ECX_L1_SSSE3 = 0x00000200, // bit = 9
_Py_CPUID_MASK_ECX_L1_FMA = 0x00001000, // bit = 12
_Py_CPUID_MASK_ECX_L1_SSE4_1 = 0x00080000, // bit = 19
_Py_CPUID_MASK_ECX_L1_SSE4_2 = 0x00100000, // bit = 20
_Py_CPUID_MASK_ECX_L1_POPCNT = 0x00800000, // bit = 23
_Py_CPUID_MASK_ECX_L1_XSAVE = 0x04000000, // bit = 26
_Py_CPUID_MASK_ECX_L1_OSXSAVE = 0x08000000, // bit = 27
_Py_CPUID_MASK_ECX_L1_AVX = 0x10000000, // bit = 28
/* CPUID (LEAF=1, SUBLEAF=0) [EDX] */
Py_CPUID_MASK_EDX_L1_CMOV = 0x00008000, // bit = 15
Py_CPUID_MASK_EDX_L1_SSE = 0x02000000, // bit = 25
Py_CPUID_MASK_EDX_L1_SSE2 = 0x04000000, // bit = 26
_Py_CPUID_MASK_EDX_L1_CMOV = 0x00008000, // bit = 15
_Py_CPUID_MASK_EDX_L1_SSE = 0x02000000, // bit = 25
_Py_CPUID_MASK_EDX_L1_SSE2 = 0x04000000, // bit = 26
/* CPUID (LEAF=7, SUBLEAF=0) [EBX] */
Py_CPUID_MASK_EBX_L7_AVX2 = 0x00000020, // bit = 5
Py_CPUID_MASK_EBX_L7_AVX512_F = 0x00010000, // bit = 16
Py_CPUID_MASK_EBX_L7_AVX512_DQ = 0x00020000, // bit = 17
Py_CPUID_MASK_EBX_L7_AVX512_IFMA = 0x00200000, // bit = 21
Py_CPUID_MASK_EBX_L7_AVX512_PF = 0x04000000, // bit = 26
Py_CPUID_MASK_EBX_L7_AVX512_ER = 0x08000000, // bit = 27
Py_CPUID_MASK_EBX_L7_AVX512_CD = 0x10000000, // bit = 28
Py_CPUID_MASK_EBX_L7_AVX512_BW = 0x40000000, // bit = 30
Py_CPUID_MASK_EBX_L7_AVX512_VL = 0x80000000, // bit = 31
_Py_CPUID_MASK_EBX_L7_AVX2 = 0x00000020, // bit = 5
_Py_CPUID_MASK_EBX_L7_AVX512_F = 0x00010000, // bit = 16
_Py_CPUID_MASK_EBX_L7_AVX512_DQ = 0x00020000, // bit = 17
_Py_CPUID_MASK_EBX_L7_AVX512_IFMA = 0x00200000, // bit = 21
_Py_CPUID_MASK_EBX_L7_AVX512_PF = 0x04000000, // bit = 26
_Py_CPUID_MASK_EBX_L7_AVX512_ER = 0x08000000, // bit = 27
_Py_CPUID_MASK_EBX_L7_AVX512_CD = 0x10000000, // bit = 28
_Py_CPUID_MASK_EBX_L7_AVX512_BW = 0x40000000, // bit = 30
_Py_CPUID_MASK_EBX_L7_AVX512_VL = 0x80000000, // bit = 31
/* CPUID (LEAF=7, SUBLEAF=0) [ECX] */
Py_CPUID_MASK_ECX_L7_AVX512_VBMI = 0x00000002, // bit = 1
Py_CPUID_MASK_ECX_L7_AVX512_VBMI2 = 0x00000040, // bit = 6
Py_CPUID_MASK_ECX_L7_AVX512_VNNI = 0x00000800, // bit = 11
Py_CPUID_MASK_ECX_L7_AVX512_BITALG = 0x00001000, // bit = 12
Py_CPUID_MASK_ECX_L7_AVX512_VPOPCNTDQ = 0x00004000, // bit = 14
_Py_CPUID_MASK_ECX_L7_AVX512_VBMI = 0x00000002, // bit = 1
_Py_CPUID_MASK_ECX_L7_AVX512_VBMI2 = 0x00000040, // bit = 6
_Py_CPUID_MASK_ECX_L7_AVX512_VNNI = 0x00000800, // bit = 11
_Py_CPUID_MASK_ECX_L7_AVX512_BITALG = 0x00001000, // bit = 12
_Py_CPUID_MASK_ECX_L7_AVX512_VPOPCNTDQ = 0x00004000, // bit = 14
/* CPUID (LEAF=7, SUBLEAF=0) [EDX] */
Py_CPUID_MASK_EDX_L7_AVX512_4VNNIW = 0x00000004, // bit = 2
Py_CPUID_MASK_EDX_L7_AVX512_4FMAPS = 0x00000008, // bit = 3
Py_CPUID_MASK_EDX_L7_AVX512_VP2INTERSECT = 0x00000100, // bit = 8
_Py_CPUID_MASK_EDX_L7_AVX512_4VNNIW = 0x00000004, // bit = 2
_Py_CPUID_MASK_EDX_L7_AVX512_4FMAPS = 0x00000008, // bit = 3
_Py_CPUID_MASK_EDX_L7_AVX512_VP2INTERSECT = 0x00000100, // bit = 8
/* CPUID (LEAF=7, SUBLEAF=1) [EAX] */
Py_CPUID_MASK_EAX_L7S1_AVX_VNNI = 0x00000010, // bit = 4
Py_CPUID_MASK_EAX_L7S1_AVX_IFMA = 0x00800000, // bit = 23
_Py_CPUID_MASK_EAX_L7S1_AVX_VNNI = 0x00000010, // bit = 4
_Py_CPUID_MASK_EAX_L7S1_AVX_IFMA = 0x00800000, // bit = 23
/* CPUID (LEAF=7, SUBLEAF=1) [EDX] */
Py_CPUID_MASK_EDX_L7S1_AVX_VNNI_INT8 = 0x00000010, // bit = 4
Py_CPUID_MASK_EDX_L7S1_AVX_NE_CONVERT = 0x00000020, // bit = 5
Py_CPUID_MASK_EDX_L7S1_AVX_VNNI_INT16 = 0x00000400, // bit = 10
} py_cpuid_feature_mask;
/*[python end generated code: output=c4460242e465fa91 input=61d2b5f1bc368b94]*/
_Py_CPUID_MASK_EDX_L7S1_AVX_VNNI_INT8 = 0x00000010, // bit = 4
_Py_CPUID_MASK_EDX_L7S1_AVX_NE_CONVERT = 0x00000020, // bit = 5
_Py_CPUID_MASK_EDX_L7S1_AVX_VNNI_INT16 = 0x00000400, // bit = 10
};
// fmt: on
/*[python end generated code: output=8e58b0997d69bbf8 input=fce00935f64021f9]*/

#endif // !Py_INTERNAL_CPUINFO_CPUID_FEATURES_H
41 changes: 17 additions & 24 deletions Include/internal/pycore_cpuinfo_xsave_features.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
/**
* @author Bénédikt Tran
* @seealso Tools/cpuinfo/xsave_features_gen.py
* @seealso @file Tools/cpuinfo/xsave_features_gen.py
*
* XSAVE state components (XCR0 control register).
*
* See https://en.wikipedia.org/wiki/Control_register#XCR0_and_XSS.
*/

#ifndef Py_INTERNAL_CPUINFO_XSAVE_FEATURES_H
#define Py_INTERNAL_CPUINFO_XSAVE_FEATURES_H

Expand All @@ -21,29 +22,21 @@ extern "C" {

// fmt: off
/*[python input]
import importlib
import os
import sys

ROOT = os.getcwd()
TOOL = os.path.join(ROOT, "Tools/cpuinfo/xsave_features_gen.py")
TOOL = os.path.realpath(TOOL)

if not os.path.exists(TOOL):
raise FileNotFoundError(TOOL)

sys.path.insert(0, os.path.dirname(os.path.dirname(TOOL)))
module = importlib.import_module("cpuinfo.xsave_features_gen")
print(module.generate_xsave_features_enum("py_xsave_feature_mask"))
import os, sys
sys.path.insert(0, os.path.realpath(os.path.join(os.getcwd(), "Tools")))
from cpuinfo.xsave_features_gen import generate_xsave_features_enum
print(generate_xsave_features_enum("_Py_xsave_feature_mask"))
[python start generated code]*/
typedef enum py_xsave_feature_mask {
Py_XSAVE_MASK_XCR0_SSE = 0x00000002, // bit = 1
Py_XSAVE_MASK_XCR0_AVX = 0x00000004, // bit = 2
Py_XSAVE_MASK_XCR0_AVX512_OPMASK = 0x00000020, // bit = 5
Py_XSAVE_MASK_XCR0_AVX512_ZMM_HI256 = 0x00000040, // bit = 6
Py_XSAVE_MASK_XCR0_AVX512_HI16_ZMM = 0x00000080, // bit = 7
} py_xsave_feature_mask;
/*[python end generated code: output=9a476ed0abbc617b input=41f35058299c0118]*/
// fmt: off
/** Enumeration for XSAVE components */
enum _Py_xsave_feature_mask_e {
_Py_XSAVE_MASK_XCR0_SSE = 0x00000002, // bit = 1
_Py_XSAVE_MASK_XCR0_AVX = 0x00000004, // bit = 2
_Py_XSAVE_MASK_XCR0_AVX512_OPMASK = 0x00000020, // bit = 5
_Py_XSAVE_MASK_XCR0_AVX512_ZMM_HI256 = 0x00000040, // bit = 6
_Py_XSAVE_MASK_XCR0_AVX512_HI16_ZMM = 0x00000080, // bit = 7
};
// fmt: on
/*[python end generated code: output=35ea9a165938f8ef input=336793a305515376]*/

#endif // !Py_INTERNAL_CPUINFO_XSAVE_FEATURES_H
4 changes: 2 additions & 2 deletions Modules/blake2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "Python.h"
#include "hashlib.h"
#include "pycore_cpuinfo.h" // py_cpuid_features
#include "pycore_cpuinfo.h" // _Py_cpuid_features
#include "pycore_strhex.h" // _Py_strhex()
#include "pycore_typeobject.h"
#include "pycore_moduleobject.h"
Expand Down Expand Up @@ -111,7 +111,7 @@ _blake2_free(void *module)
static void
blake2module_init_cpu_features(Blake2State *state)
{
py_cpuid_features flags;
_Py_cpuid_features flags;
_Py_cpuid_detect_features(&flags);
#if _Py_HACL_CAN_COMPILE_VEC128
state->can_run_simd128 = flags.sse && flags.sse2 && flags.sse3
Expand Down
4 changes: 2 additions & 2 deletions Modules/hmacmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#endif

#include "Python.h"
#include "pycore_cpuinfo.h" // py_cpuid_features
#include "pycore_cpuinfo.h" // _Py_cpuid_features
#include "pycore_hashtable.h"
#include "pycore_strhex.h" // _Py_strhex()

Expand Down Expand Up @@ -1553,7 +1553,7 @@ hmacmodule_init_globals(PyObject *module, hmacmodule_state *state)
static void
hmacmodule_init_cpu_features(hmacmodule_state *state)
{
py_cpuid_features flags;
_Py_cpuid_features flags;
_Py_cpuid_detect_features(&flags);
#if _Py_HACL_CAN_COMPILE_VEC128
state->can_run_simd128 = flags.sse && flags.sse2 && flags.sse3
Expand Down
Loading
0