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 90 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
90 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
144d9ef
does it work now..?
picnixz Jul 14, 2025
b364ad2
does it work now..?
picnixz Jul 14, 2025
0791e89
remove xgetbv support?
picnixz Jul 14, 2025
48b2cb2
remove xgetbv support?
picnixz Jul 14, 2025
34f1337
huh?
picnixz Jul 14, 2025
91effb4
only parse maxleaf
picnixz Jul 14, 2025
6dc532d
use different variables!
picnixz Jul 14, 2025
6d5dd0b
disable everything!
picnixz Jul 14, 2025
8e5b2f0
revert CI
picnixz Jul 14, 2025
3b495f6
only check maxleaf
picnixz Jul 14, 2025
8019f09
parse L1
picnixz Jul 14, 2025
820d140
parse L7
picnixz Jul 14, 2025
df85ce5
it *should* work now
picnixz Jul 14, 2025
915383e
:@
picnixz Jul 14, 2025
c6cf903
:@
picnixz Jul 14, 2025
498518f
waaaaa!
picnixz Jul 14, 2025 < 8000 /span>
3d56d93
remove support for XCR0 registers
picnixz Jul 14, 2025
08daa8a
fix SIMD-256 detection
picnixz Jul 14, 2025
afd1137
simplify `get_cpuid_info`
picnixz Jul 14, 2025
79eb72d
add CODEOWNERS
picnixz Jul 15, 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
harden detection of CPU features
  • Loading branch information
picnixz committed Oct 11, 2024
commit 9fd6152c0cf1b54ad737d2ea1460413e96278da3
162 changes: 94 additions & 68 deletions Include/internal/pycore_cpuinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,112 +11,138 @@ extern "C" {

#include <stdint.h> // uint8_t

/* Macro indicating that the member is a CPUID bit. */
#define _Py_SIMD_FEAT uint8_t
/* Macro indicating that the member is a XCR0 bit. */
#define _Py_SIMD_XCR0_BIT uint8_t

typedef struct py_simd_features {
/* Streaming SIMD Extensions */
_Py_SIMD_FEAT sse: 1;
_Py_SIMD_FEAT sse2: 1;
_Py_SIMD_FEAT sse3: 1;
_Py_SIMD_FEAT ssse3: 1; // Supplemental SSE3 instructions
_Py_SIMD_FEAT sse41: 1; // SSE4.1
_Py_SIMD_FEAT sse42: 1; // SSE4.2

/* Advanced Vector Extensions */
_Py_SIMD_FEAT avx: 1;
_Py_SIMD_FEAT avx_ifma: 1;
_Py_SIMD_FEAT avx_ne_convert: 1;

_Py_SIMD_FEAT avx_vnni: 1;
_Py_SIMD_FEAT avx_vnni_int8: 1;
_Py_SIMD_FEAT avx_vnni_int16: 1;

/* Advanced Vector Extensions 2. */
_Py_SIMD_FEAT avx2: 1;

/* Declare a member of 'py_cpuid_features' storing a CPUID bit. */
#define _Py_CPUID_DECL_FEAT(X) uint8_t X:1
/* Declare a member of 'py_cpuid_features' storing a XCR0 bit. */
#define _Py_CPUID_DECL_XCR0(X) uint8_t X:1

typedef struct py_cpuid_features {
// --- Streaming SIMD Extensions ------------------------------------------
_Py_CPUID_DECL_FEAT(sse);
_Py_CPUID_DECL_FEAT(sse2);
_Py_CPUID_DECL_FEAT(sse3);
_Py_CPUID_DECL_FEAT(ssse3); // Supplemental SSE3 instructions
_Py_CPUID_DECL_FEAT(sse41); // SSE4.1
_Py_CPUID_DECL_FEAT(sse42); // SSE4.2

// --- Advanced Vector Extensions -----------------------------------------
_Py_CPUID_DECL_FEAT(avx);
_Py_CPUID_DECL_FEAT(avx_ifma);
_Py_CPUID_DECL_FEAT(avx_ne_convert);

_Py_CPUID_DECL_FEAT(avx_vnni);
_Py_CPUID_DECL_FEAT(avx_vnni_int8);
_Py_CPUID_DECL_FEAT(avx_vnni_int16);

// --- Advanced Vector Extensions 2 ---------------------------------------
_Py_CPUID_DECL_FEAT(avx2);

// --- Advanced Vector Extensions (512-bit) -------------------------------
/*
*
* AVX-512 instruction set are grouped by the processor generation
* that implements them (see https://en.wikipedia.org/wiki/AVX-512).
*
* We do not include GFNI, VPCLMULQDQ and VAES instructions since
* they are not exactly AVX-512 per se, nor do we include BF16 or
* FP16 since they operate on bfloat16 and binary16 (half-float).
*
* See https://en.wikipedia.org/wiki/AVX-512#Instruction_set for
* the meaning of each suffix (e.g., 'f' stands for 'Foundation').
*/
_Py_SIMD_FEAT avx512_f: 1;
_Py_SIMD_FEAT avx512_cd: 1;

_Py_SIMD_FEAT avx512_er: 1;
_Py_SIMD_FEAT avx512_pf: 1;
_Py_CPUID_DECL_FEAT(avx512_f);
_Py_CPUID_DECL_FEAT(avx512_cd);

_Py_SIMD_FEAT avx512_4fmaps: 1;
_Py_SIMD_FEAT avx512_4vnniw: 1;
_Py_CPUID_DECL_FEAT(avx512_er);
_Py_CPUID_DECL_FEAT(avx512_pf);

_Py_SIMD_FEAT avx512_vpopcntdq: 1;
_Py_CPUID_DECL_FEAT(avx512_4fmaps);
_Py_CPUID_DECL_FEAT(avx512_4vnniw);

_Py_SIMD_FEAT avx512_vl: 1;
_Py_SIMD_FEAT avx512_dq: 1;
_Py_SIMD_FEAT avx512_bw: 1;
_Py_CPUID_DECL_FEAT(avx512_vpopcntdq);

_Py_SIMD_FEAT avx512_ifma: 1;
_Py_CPUID_DECL_FEAT(avx512_vl);
_Py_CPUID_DECL_FEAT(avx512_dq);
_Py_CPUID_DECL_FEAT(avx512_bw);

_Py_SIMD_FEAT avx512_vbmi: 1;
_Py_CPUID_DECL_FEAT(avx512_ifma);
_Py_CPUID_DECL_FEAT(avx512_vbmi);

_Py_SIMD_FEAT avx512_vnni: 1;
_Py_CPUID_DECL_FEAT(avx512_vnni);

_Py_SIMD_FEAT avx512_vbmi2: 1;
_Py_SIMD_FEAT avx512_bitalg: 1;
_Py_CPUID_DECL_FEAT(avx512_vbmi2);
_Py_CPUID_DECL_FEAT(avx512_bitalg);

_Py_SIMD_FEAT avx512_vp2intersect: 1;
_Py_CPUID_DECL_FEAT(avx512_vp2intersect);

_Py_SIMD_FEAT os_xsave: 1; // XSAVE is supported
// --- Instructions -------------------------------------------------------
_Py_CPUID_DECL_FEAT(cmov);
_Py_CPUID_DECL_FEAT(fma);
_Py_CPUID_DECL_FEAT(popcnt);
_Py_CPUID_DECL_FEAT(pclmulqdq);

/* XCR0 register bits */
_Py_SIMD_XCR0_BIT xcr0_sse: 1;
_Py_CPUID_DECL_FEAT(xsave); // XSAVE/XRSTOR/XSETBV/XGETBV
_Py_CPUID_DECL_FEAT(os_xsave); // XSAVE is enabled by the OS

// --- XCR0 register bits -------------------------------------------------
_Py_CPUID_DECL_XCR0(xcr0_sse);
// On some Intel CPUs, it is possible for the CPU to support AVX2
// instructions even though the underlying OS does not know about
// AVX. In particular, only (SSE) XMM registers will be saved and
// restored on context-switch, but not (AVX) YMM registers.
_Py_SIMD_XCR0_BIT xcr0_avx: 1;
_Py_SIMD_XCR0_BIT xcr0_avx512_opmask: 1;
_Py_SIMD_XCR0_BIT xcr0_avx512_zmm_hi256: 1;
_Py_SIMD_XCR0_BIT xcr0_avx512_hi16_zmm: 1;

// We want the structure to be aligned correctly, namely
// its size in bits must be a multiple of 8.
//
_Py_CPUID_DECL_XCR0(xcr0_avx);
_Py_CPUID_DECL_XCR0(xcr0_avx512_opmask);
_Py_CPUID_DECL_XCR0(xcr0_avx512_zmm_hi256);
_Py_CPUID_DECL_XCR0(xcr0_avx512_hi16_zmm);

// Whenever a field is added or removed above, update the
// number of fields (35) and adjust the bitsize of 'done'.
uint8_t done: 5; // set if the structure was filled
} py_simd_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;

/*
* Explicitly initialize all members to zero to guarantee that
* we never have an un-initialized attribute at runtime which
* could lead to an illegal instruction error.
*
* This does not mark 'flags' as being ready yet.
*/
extern void
_Py_disable_simd_features(py_simd_features *flags);
_Py_cpuid_disable_features(py_cpuid_features *flags);

/*
* Apply a bitwise-OR on all flags in 'out' using those in 'src',
* unconditionally updating 'out' (i.e. 'out->done' is ignored).
* Check whether the structure is ready and flags are inter-compatible,
* returning 1 on success and 0 otherwise.
*
* This also sets 'out->done' to 1 at the end.
* The caller should disable all CPUID detected features if the check
* fails to avoid encountering runtime illegal instruction errors.
*/
extern int
_Py_cpuid_check_features(const py_cpuid_features *flags);

/*
* Return 1 if all expected flags are set in 'actual', 0 otherwise.
*
* Note that the caller is responsible to ensure that the flags set to 1
* must not lead to illegal instruction errors if the corresponding SIMD
* instruction(s) are used.
* If 'actual' or 'expect' are not ready yet, this also returns 0.
*/
extern void
_Py_update_simd_features(py_simd_features *out, const py_simd_features *src);
extern int
_Py_cpuid_has_features(const py_cpuid_features *actual,
const py_cpuid_features *expect);


/*
* Return 1 if 'actual' and 'expect' are identical, 0 otherwise.
*
* If 'actual' or 'expect' are not ready yet, this also returns 0.
*/
extern int
_Py_cpuid_match_features(const py_cpuid_features *actual,
const py_cpuid_features *expect);

/* Detect the available SIMD features on this machine. */
/* Detect the available features on this machine. */
extern void
_Py_detect_simd_features(py_simd_features *flags);
_Py_cpuid_detect_features(py_cpuid_features *flags);

#ifdef __cplusplus
}
Expand Down
Loading
0