-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
picnixz
wants to merge
90
commits into
python:main
Choose a base branch
from
picnixz:core/simd-helpers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,051
−257
Draft
Changes from 1 commit
Commits
Show all changes
90 commits
Select commit
Hold shift + click to select a range
9a6ccb5
support simple SIMD detection
8000
picnixz f4e4f99
add _Py prefix
picnixz 5006686
Use `_py` prefix
picnixz 3c0b4f1
make the interface friendlier for future adjustments
picnixz 01ed21a
Allow `cpu_simd_flags` to be merged.
picnixz 969a619
update comments
picnixz 5a5acc2
fix typo
picnixz ac1b165
fix configure script
picnixz 6f304f2
fix bit detection
picnixz f3bd027
Harden detection of AVX instructions.
picnixz 16b2aed
do not guard the parsing of `os_xsave`
picnixz 5018fa9
Remove old comment.
picnixz e758065
Update cpuinfo.c comments
picnixz 731be81
Update pycore_cpuinfo.h comments
picnixz 7947715
fix lint
picnixz 7a17cbb
I really shouldn't use a Web UI
picnixz 76f67b1
Fix _xgetbv() on Windows builds.
picnixz 0b49a50
fix comment
picnixz 9fd6152
harden detection of CPU features
picnixz 97a0fc5
update configure
picnixz f7da530
Merge remote-tracking branch 'upstream/main' into core/simd-helpers-1…
picnixz 5f2884d
update comments
picnixz 7c3b74e
update Makefile
picnixz 130d099
address Erlend's review
picnixz cd575f0
lint & comment fixups
picnixz 2b597a4
Update docs
picnixz 78be530
Fix typo
picnixz fd47f0e
Merge branch 'main' into core/simd-helpers
picnixz cbb7b53
re-export functions for extension modules
picnixz 21d8ca8
rename os_xsave to osxsave for future automatism
picnixz 1f9dbb4
remember `maxleaf` and make detection more readable
picnixz 553aa7c
use enumeration for flags
picnixz 39d2ba4
fix warnings
picnixz 602bb9c
Merge branch 'main' into core/simd-helpers
picnixz d6a3523
remove un-necessary comment and newline continuation
picnixz ff4212e
Merge branch 'main' into core/simd-helpers
picnixz 3cb79f6
regen configure
picnixz e0a578c
clinic now supports empty comment lines in Python blocks
picnixz 6fdbbdf
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz c265851
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz c12f9c7
move cpuinfo enumerations to real invokable Python scripts
picnixz a6c443f
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz bd3589f
add comments
picnixz d213b67
update C comments
picnixz 4109d90
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz 19b7d86
TMP: usage proof-of-concept
picnixz 1732b6b
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz d59d06d
improve configure.ac
picnixz bc2c1e5
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz 4a92103
Merge remote-tracking branch 'upstream/main' into feat/core/simd-125022
picnixz 26ed6fd
Merge branch 'main' into feat/core/simd-125022
picnixz d00da3e
post-merge
picnixz 8b7ecfb
use `_Py` prefix to prevent public namespace pollution
picnixz 3c31ba3
let the compiler decide on the inlineness
picnixz 143d57e
drop CPUID_REG alias
picnixz ee2a83c
simplify `_Py_cpuid_check_features`
picnixz e6d4583
amend docs for `_Py_cpuid_disable_features`
picnixz 838f928
use macros to support larger flag ranges
picnixz 62c9a40
handle -Wpedantic
picnixz a22aa95
reorganize files
picnixz 87039dc
suppress compilation warnings
picnixz 8a4b120
add linting
picnixz 8603e14
typo
picnixz d6213a5
typo
picnixz 79d5b34
log more!
picnixz f69d74a
skip CI
picnixz cb9065d
more printf
picnixz 88df3b7
faster ci
picnixz db64ba5
!!
picnixz 04012b4
!!
picnixz 144d9ef
does it work now..?
picnixz b364ad2
does it work now..?
picnixz 0791e89
remove xgetbv support?
picnixz 48b2cb2
remove xgetbv support?
picnixz 34f1337
huh?
picnixz 91effb4
only parse maxleaf
picnixz 6dc532d
use different variables!
picnixz 6d5dd0b
disable everything!
picnixz 8e5b2f0
revert CI
picnixz 3b495f6
only check maxleaf
picnixz 8019f09
parse L1
picnixz 820d140
parse L7
picnixz df85ce5
it *should* work now
picnixz 915383e
:@
picnixz c6cf903
:@
picnixz 498518f
waaaaa!
picnixz 3d56d93
remove support for XCR0 registers
picnixz 08daa8a
fix SIMD-256 detection
picnixz afd1137
simplify `get_cpuid_info`
picnixz 79eb72d
add CODEOWNERS
picnixz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
support simple SIMD detection
- Loading branch information
commit 9a6ccb54c1052aa7fd2cd8d486e703c8da848d9d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef Py_INTERNAL_CPUINFO_H | ||
#define Py_INTERNAL_CPUINFO_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef Py_BUILD_CORE | ||
# error "this header requires Py_BUILD_CORE define" | ||
#endif | ||
|
||
#include <stdbool.h> | ||
|
||
typedef struct { | ||
bool sse, sse2, sse3, sse41, sse42, avx, avx2, avx512vbmi; | ||
bool done; | ||
} cpu_simd_flags; | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
extern void | ||
detect_cpu_simd_features(cpu_simd_flags *flags); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* !Py_INTERNAL_CPUINFO_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Naive CPU SIMD features detection. | ||
* | ||
* See Modules/black2module.c. | ||
*/ | ||
|
||
#include "Python.h" | ||
#include "pycore_cpuinfo.h" | ||
|
||
#include <stdbool.h> | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#if defined(__x86_64__) F438 && defined(__GNUC__) | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <cpuid.h> | ||
#elif defined(_M_X64) | ||
#include <intrin.h> | ||
#endif | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// AVX2 cannot be compiled on macOS ARM64 (yet it can be compiled on x86_64). | ||
// However, since autoconf incorrectly assumes so when compiling a universal2 | ||
// binary, we disable all AVX-related instructions. | ||
#if defined(__APPLE__) && defined(__arm64__) | ||
# undef CAN_COMPILE_SIMD_AVX_INSTRUCTIONS | ||
# undef CAN_COMPILE_SIMD_AVX2_INSTRUCTIONS | ||
# undef CAN_COMPILE_SIMD_AVX512_VBMI_INSTRUCTIONS | ||
#endif | ||
|
||
#define EDX1_SSE (1 << 25) // sse, EDX, page 1, bit 25 | ||
#define EDX1_SSE2 (1 << 26) // sse2, EDX, page 1, bit 26 | ||
#define ECX1_SSE3 (1 << 9) // sse3, ECX, page 1, bit 0 | ||
#define ECX1_SSE4_1 (1 << 19) // sse4.1, ECX, page 1, bit 19 | ||
#define ECX1_SSE4_2 (1 << 20) // sse4.2, ECX, page 1, bit 20 | ||
#define ECX1_AVX (1 << 28) // avx, ECX, page 1, bit 28 | ||
#define EBX7_AVX2 (1 << 5) // avx2, EBX, page 7, bit 5 | ||
#define ECX7_AVX512_VBMI (1 << 1) // avx512-vbmi, ECX, page 7, bit 1 | ||
|
||
void | ||
detect_cpu_simd_features(cpu_simd_flags *flags) | ||
{ | ||
if (flags->done) { | ||
return; | ||
} | ||
|
||
int eax1 = 0, ebx1 = 0, ecx1 = 0, edx1 = 0; | ||
int eax7 = 0, ebx7 = 0, ecx7 = 0, edx7 = 0; | ||
#if defined(__x86_64__) && defined(__GNUC__) | ||
__cpuid_count(1, 0, eax1, ebx1, ecx1, edx1); | ||
__cpuid_count(7, 0, eax7, ebx7, ecx7, edx7); | ||
#elif defined(_M_X64) | ||
int info1[4] = {0}; | ||
__cpuidex(info1, 1, 0); | ||
eax1 = info1[0]; | ||
ebx1 = info1[1]; | ||
ecx1 = info1[2]; | ||
edx1 = info1[3]; | ||
|
||
int info7[4] = {0}; | ||
__cpuidex(info7, 7, 0); | ||
eax7 = info7[0]; | ||
ebx7 = info7[1]; | ||
ecx7 = info7[2]; | ||
edx7 = info7[3]; | ||
#else | ||
// use (void) expressions to avoid warnings | ||
(void) eax1; (void) ebx1; (void) ecx1; (void) edx1; | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(void) eax7; (void) ebx7; (void) ecx7; (void) edx7; | ||
#endif | ||
|
||
#ifdef CAN_COMPILE_SIMD_SSE_INSTRUCTIONS | ||
flags->sse = (edx1 & EDX1_SSE) != 0; | ||
#else | ||
flags->sse = false; | ||
#endif | ||
#ifdef CAN_COMPILE_SIMD_SSE2_INSTRUCTIONS | ||
flags->sse2 = (edx1 & EDX1_SSE2) != 0; | ||
#else | ||
flags->sse2 = false; | ||
#endif | ||
#ifdef CAN_COMPILE_SIMD_SSE3_INSTRUCTIONS | ||
flags->sse3 = (ecx1 & ECX1_SSE3) != 0; | ||
#else | ||
#endif | ||
flags->sse3 = false; | ||
#ifdef CAN_COMPILE_SIMD_SSE4_1_INSTRUCTIONS | ||
flags->sse41 = (ecx1 & ECX1_SSE4_1) != 0; | ||
#else | ||
flags->sse41 = false; | ||
#endif | ||
#ifdef CAN_COMPILE_SIMD_SSE4_2_INSTRUCTIONS | ||
flags->sse42 = (ecx1 & ECX1_SSE4_2) != 0; | ||
#else | ||
flags->sse42 = false; | ||
#endif | ||
#ifdef CAN_COMPILE_SIMD_AVX_INSTRUCTIONS | ||
flags->avx = (ecx1 & ECX1_AVX) != 0; | ||
#else | ||
flags->avx = false; | ||
#endif | ||
#ifdef CAN_COMPILE_SIMD_AVX2_INSTRUCTIONS | ||
flags->avx2 = (ebx7 & EBX7_AVX2) != 0; | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#else | ||
flags->avx2 = false; | ||
#endif | ||
#ifdef CAN_COMPILE_SIMD_AVX512_VBMI_INSTRUCTIONS | ||
flags->avx512vbmi = (ecx7 & ECX7_AVX512_VBMI) != 0; | ||
#else | ||
flags->avx512vbmi = false; | ||
#endif | ||
|
||
flags->done = true; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.