From 693404d8c0b342f01824e34f633003f3ea8bf8d8 Mon Sep 17 00:00:00 2001 From: "Michael R. Crusoe" Date: Fri, 28 Nov 2025 19:12:16 +0100 Subject: [PATCH 1/2] [mypyc] librt base64: support pyodide via the NEON intrinsics --- mypyc/build_setup.py | 48 ++++++++++++++---------- mypyc/lib-rt/base64/arch/neon64/codec.c | 2 +- mypyc/lib-rt/base64/config.h | 2 +- mypyc/lib-rt/setup.py | 49 +++++++++++++++---------- 4 files changed, 60 insertions(+), 41 deletions(-) diff --git a/mypyc/build_setup.py b/mypyc/build_setup.py index a3e7a669abee..3f75c7fe8a35 100644 --- a/mypyc/build_setup.py +++ b/mypyc/build_setup.py @@ -1,5 +1,6 @@ import platform import sys +import os try: # Import setuptools so that it monkey-patch overrides distutils @@ -30,32 +31,41 @@ ccompiler.CCompiler.__spawn = ccompiler.CCompiler.spawn # type: ignore[attr-defined] X86_64 = platform.machine() in ("x86_64", "AMD64", "amd64") +PYODIDE = "PYODIDE" in os.environ 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 + if PYODIDE: + new_cmd = list(cmd) for argument in reversed(new_cmd): - # Check if the matching argument contains a source filename. if not str(argument).endswith(".c"): continue + if "base64/arch/" in str(argument): + new_cmd.extend(["-msimd128"]) + else: + 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 + 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) diff --git a/mypyc/lib-rt/base64/arch/neon64/codec.c b/mypyc/lib-rt/base64/arch/neon64/codec.c index 70dc463de94f..72f136f67b8a 100644 --- a/mypyc/lib-rt/base64/arch/neon64/codec.c +++ b/mypyc/lib-rt/base64/arch/neon64/codec.c @@ -12,7 +12,7 @@ #include // Only enable inline assembly on supported compilers. -#if defined(__GNUC__) || defined(__clang__) +#if !defined(__wasm__) && (defined(__GNUC__) || defined(__clang__)) #define BASE64_NEON64_USE_ASM #endif diff --git a/mypyc/lib-rt/base64/config.h b/mypyc/lib-rt/base64/config.h index 467a722c2f11..8517c7b87d8c 100644 --- a/mypyc/lib-rt/base64/config.h +++ b/mypyc/lib-rt/base64/config.h @@ -13,7 +13,7 @@ #define BASE64_WITH_NEON32 0 #define HAVE_NEON32 BASE64_WITH_NEON32 -#if defined(__APPLE__) && defined(__aarch64__) +#if (defined(__APPLE__) && defined(__aarch64__)) || (defined(__wasm__) && defined(__wasm_simd128__)) #define BASE64_WITH_NEON64 1 #else #define BASE64_WITH_NEON64 0 diff --git a/mypyc/lib-rt/setup.py b/mypyc/lib-rt/setup.py index 5f71d332229c..1030ce496ccd 100644 --- a/mypyc/lib-rt/setup.py +++ b/mypyc/lib-rt/setup.py @@ -42,32 +42,41 @@ ccompiler.CCompiler.__spawn = ccompiler.CCompiler.spawn # type: ignore[attr-defined] X86_64 = platform.machine() in ("x86_64", "AMD64", "amd64") +PYODIDE = "PYODIDE" in os.environ 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 + if PYODIDE: + new_cmd = list(cmd) 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 + if "base64/arch/" in str(argument): + new_cmd.extend(["-msimd128"]) + else: + 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) From d7aa6eeb64659a6ebbaa675ec21823b7a6a6e654 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Nov 2025 18:14:46 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypyc/build_setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypyc/build_setup.py b/mypyc/build_setup.py index 3f75c7fe8a35..328518e220d6 100644 --- a/mypyc/build_setup.py +++ b/mypyc/build_setup.py @@ -1,6 +1,6 @@ +import os import platform import sys -import os try: # Import setuptools so that it monkey-patch overrides distutils