-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[mypyc] librt base64: use existing SIMD CPU dispatch by customizing build flags #20253
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
Changes from all commits
379cd1e
44e1ebc
d0eda17
3bc82e6
cb0c4ed
2cf7bff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import platform | ||
| import sys | ||
|
|
||
| try: | ||
| # Import setuptools so that it monkey-patch overrides distutils | ||
| import setuptools # noqa: F401 | ||
| except ImportError: | ||
| pass | ||
|
|
||
| if sys.version_info >= (3, 12): | ||
| # From setuptools' monkeypatch | ||
| from distutils import ccompiler # type: ignore[import-not-found] | ||
| else: | ||
| from distutils import ccompiler | ||
|
|
||
| EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT = { | ||
| "unix": { | ||
| "base64/arch/ssse3": ["-mssse3"], | ||
| "base64/arch/sse41": ["-msse4.1"], | ||
| "base64/arch/sse42": ["-msse4.2"], | ||
| "base64/arch/avx2": ["-mavx2"], | ||
| "base64/arch/avx": ["-mavx"], | ||
| }, | ||
| "msvc": { | ||
| "base64/arch/sse42": ["/arch:SSE4.2"], | ||
| "base64/arch/avx2": ["/arch:AVX2"], | ||
| "base64/arch/avx": ["/arch:AVX"], | ||
| }, | ||
| } | ||
|
|
||
| ccompiler.CCompiler.__spawn = ccompiler.CCompiler.spawn # type: ignore[attr-defined] | ||
| X86_64 = platform.machine() in ("x86_64", "AMD64", "amd64") | ||
|
|
||
|
|
||
| def spawn(self, cmd, **kwargs) -> None: # type: ignore[no-untyped-def] | ||
| compiler_type: str = self.compiler_type | ||
| extra_options = EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT[compiler_type] | ||
| new_cmd = list(cmd) | ||
| if X86_64 and extra_options is not None: | ||
| # filenames are closer to the end of command line | ||
| for argument in reversed(new_cmd): | ||
| # Check if the matching argument contains a source filename. | ||
| if not str(argument).endswith(".c"): | ||
| continue | ||
|
|
||
| for path in extra_options.keys(): | ||
| if path in str(argument): | ||
| if compiler_type == "bcpp": | ||
| compiler = new_cmd.pop() | ||
| # Borland accepts a source file name at the end, | ||
| # insert the options before it | ||
| new_cmd.extend(extra_options[path]) | ||
| new_cmd.append(compiler) | ||
| else: | ||
| new_cmd.extend(extra_options[path]) | ||
|
|
||
| # path component is found, no need to search any further | ||
| break | ||
| self.__spawn(new_cmd, **kwargs) | ||
|
|
||
|
|
||
| ccompiler.CCompiler.spawn = spawn # type: ignore[method-assign] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,9 +25,55 @@ | |
| "pythonsupport.c", | ||
| ] | ||
|
|
||
| EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT = { | ||
| "unix": { | ||
| "base64/arch/ssse3": ["-mssse3"], | ||
| "base64/arch/sse41": ["-msse4.1"], | ||
| "base64/arch/sse42": ["-msse4.2"], | ||
| "base64/arch/avx2": ["-mavx2"], | ||
| "base64/arch/avx": ["-mavx"], | ||
| }, | ||
| "msvc": { | ||
| "base64/arch/sse42": ["/arch:SSE4.2"], | ||
| "base64/arch/avx2": ["/arch:AVX2"], | ||
| "base64/arch/avx": ["/arch:AVX"], | ||
| }, | ||
| } | ||
|
|
||
| ccompiler.CCompiler.__spawn = ccompiler.CCompiler.spawn # type: ignore[attr-defined] | ||
| X86_64 = platform.machine() in ("x86_64", "AMD64", "amd64") | ||
|
|
||
|
|
||
| def spawn(self, cmd, **kwargs) -> None: # type: ignore[no-untyped-def] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks similar if not the same, any particular reason why not have it in a shared location, or is this because of the fact these are
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the later; the existing code also has duplication issues as already noted
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that there is more duplicated we can try to share more of the code, but it can happen outside this PR. |
||
| compiler_type: str = self.compiler_type | ||
| extra_options = EXTRA_FLAGS_PER_COMPILER_TYPE_PER_PATH_COMPONENT[compiler_type] | ||
| new_cmd = list(cmd) | ||
| if X86_64 and extra_options is not None: | ||
| # filenames are closer to the end of command line | ||
| for argument in reversed(new_cmd): | ||
| # Check if the matching argument contains a source filename. | ||
| if not str(argument).endswith(".c"): | ||
| continue | ||
|
|
||
| for path in extra_options.keys(): | ||
| if path in str(argument): | ||
| if compiler_type == "bcpp": | ||
| compiler = new_cmd.pop() | ||
| # Borland accepts a source file name at the end, | ||
| # insert the options before it | ||
| new_cmd.extend(extra_options[path]) | ||
| new_cmd.append(compiler) | ||
| else: | ||
| new_cmd.extend(extra_options[path]) | ||
|
|
||
| # path component is found, no need to search any further | ||
| break | ||
| self.__spawn(new_cmd, **kwargs) | ||
|
|
||
|
|
||
| ccompiler.CCompiler.spawn = spawn # type: ignore[method-assign] | ||
|
|
||
|
|
||
| class BuildExtGtest(build_ext): | ||
| def get_library_names(self) -> list[str]: | ||
| return ["gtest"] | ||
|
|
@@ -80,14 +126,10 @@ def run(self) -> None: | |
| compiler = ccompiler.new_compiler() | ||
| sysconfig.customize_compiler(compiler) | ||
| cflags: list[str] = [] | ||
| if compiler.compiler_type == "unix": | ||
| if compiler.compiler_type == "unix": # type: ignore[attr-defined] | ||
| cflags += ["-O3"] | ||
| if X86_64: | ||
| cflags.append("-msse4.2") # Enable SIMD (see also mypyc/build.py) | ||
| elif compiler.compiler_type == "msvc": | ||
| elif compiler.compiler_type == "msvc": # type: ignore[attr-defined] | ||
| cflags += ["/O2"] | ||
| if X86_64: | ||
| cflags.append("/arch:SSE4.2") # Enable SIMD (see also mypyc/build.py) | ||
|
|
||
| setup( | ||
| ext_modules=[ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason why not to annotate this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tried annotating this before, but the signature varies too much between setuptools/distutils versions and Python versions.