Skip to content

Conversation

@ilevkivskyi
Copy link
Contributor

No description provided.

ilevkivskyi referenced this pull request in python/mypy Nov 17, 2025
Vendor optimized base64 implementation from
https://github.com/aklomp/base64.
This is based on commit 9e8ed65048ff0f703fad3deb03bf66ac7f78a4d7 (May
2025).

Enable SIMD on macOS (64-bit ARM only). Other platforms probably use a
generic version. I'll look into enabling SIMD more generally in a
follow-up PR.

A `b64encode` micro-benchmark was up to 11 times faster compared to the
stdlib `base64` module (on a MacBook Pro).
lib-rt/setup.py Outdated
if compiler.compiler_type == "unix":
cflags += ["-O3"]
if X86_64:
cflags.append("-msse4.2") # Enable SIMD (see also mypyc/build.py)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No auto detection? I'll have to patch this out for Debian, the amd64 baseline is sse2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cflags.append("-msse4.2") # Enable SIMD (see also mypyc/build.py)

There is autodetection already, -msse4.2 is not needed: https://github.com/mypyc/librt/pull/14/files#diff-25f140cc37c41f9eb31ca08bca258a6bb6f86e404efde9344762e6aaac4d5494R188-R203

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the original mypy PR python/mypy#20244 that added this, I guess it was necessary for some compilers, maybe @JukkaL can clarify.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build fails without this, since SSE4.2 features can't be used without the flag, at least on Ubuntu 24.04. In particular, the C files within arch/sse in the path may need this flag. Auto-detection is supported by the underlying base64 library, but we'd still need to compile the SSE-related files using this flag, and setuptools doesn't make it straightforward to have different C compiler flags for specific files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a fix for per-file compile flags python/mypy#20253

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ilevkivskyi @JukkaL The fix for per-file base64 compilation flags is ready for review & merging python/mypy#20253

@JukkaL
Copy link
Contributor

JukkaL commented Nov 28, 2025

The per-file compilation flag PR was merged and this PR can be updated to reflect it (Ihttps://github.com/python/mypy/pull/20253).

@ilevkivskyi
Copy link
Contributor Author

OK, I will probably re-run the sync to also include other recent changes to lib-rt.

@mr-c
Copy link
Contributor

mr-c commented Nov 28, 2025

For the "pyodide" error

emcc: error: passing any of -msse, -msse2, -msse3, -mssse3, -msse4.1, -msse4.2, -msse4, -mavx, -mavx2, -mfpu=neon flags also requires passing -msimd128 (or -mrelaxed-simd)!

I recommend using the neon64 codec + msimd128, as those are automatically mapped well to wasm's simd using the SIMD Everywhere code copy inside of emscripten.

@mr-c
Copy link
Contributor

mr-c commented Nov 28, 2025

I also recommend making wasm64 wheels as well as the existing wasm32 wheels

@ilevkivskyi
Copy link
Contributor Author

ilevkivskyi commented Nov 28, 2025

wasm64 is not supported by cibuildwheel, at least it is not the "official table" in https://cibuildwheel.pypa.io/en/stable/options/#build-skip. Also WASM wheels are really experimental as PyPI doesn't support them at all, for now we are just trying to keep things ready for when they will be better supported by PyPA infrastructure.

@mr-c
Copy link
Contributor

mr-c commented Nov 28, 2025

Here's a patch to fix the pyodide build (and use the NEON SIMD intrinsics on this route)

0001-fix-build-for-pyodide.patch

@mr-c
Copy link
Contributor

mr-c commented Nov 28, 2025

wasm64 is not supported by cibuildwheel, at least it is not the "official table" in https://cibuildwheel.pypa.io/en/stable/options/#build-skip. Also WASM wheels are really experimental as PyPI doesn't support them at all, for now we are just trying to keep things ready for when they will be better supported by PyPA infrastructure.

I see now that pyodide doesn't support wasm64 yet: https://pyodide.org/en/stable/usage/api/js-api.html#pyodide.LockfileInfo.arch

@ilevkivskyi
Copy link
Contributor Author

Here's a patch to fix the pyodide build (and use the NEON SIMD intrinsics on this route)

Could you make a PR to mypy first (if you have time)? Normally things should go there first, I (or Jukka will then sync those here).
Also it looks like now there is another error:

clang: error: unsupported option '-mavx2' for target 'wasm32-unknown-emscripten'

Not sure if your fix is affected by this.

@mr-c
Copy link
Contributor

mr-c commented Nov 28, 2025

Here's a patch to fix the pyodide build (and use the NEON SIMD intrinsics on this route)

Could you make a PR to mypy first (if you have time)? Normally things should go there first, I (or Jukka will then sync those here).

Sure, though there is no pyodide CI in the mypy repo

python/mypy#20316

Also it looks like now there is another error:

clang: error: unsupported option '-mavx2' for target 'wasm32-unknown-emscripten'

Not sure if your fix is affected by this.

My fix doesn't have that problem, no. I tested locally using pyodide build

@ilevkivskyi
Copy link
Contributor Author

Sure, though there is no pyodide CI in the mypy repo

We used to build WASM wheels for mypy, they are temporarily disabled (because mypy now depends on librt, so things get tricky without PyPI support for WASM), but as I said it is better keep things ready :-)

@ilevkivskyi
Copy link
Contributor Author

Hm, @mr-c now there are some weird compile errors in WASM build:

  In file included from base64/arch/neon64/codec.c:59:
  base64/arch/neon64/enc_reshuffle.c:22:15: error: call to undeclared function 'vsliq_n_u8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     22 |         out.val[1] = vsliq_n_u8(out.val[1], in.val[0], 4);
        |                      ^
  base64/arch/neon64/enc_reshuffle.c:22:13: error: assigning to 'simde_uint8x16_t' (vector of 16 'uint8_t' values) from incompatible type 'int'
     22 |         out.val[1] = vsliq_n_u8(out.val[1], in.val[0], 4);
        |                    ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  base64/arch/neon64/enc_reshuffle.c:23:13: error: assigning to 'simde_uint8x16_t' (vector of 16 'uint8_t' values) from incompatible type 'int'
     23 |         out.val[2] = vsliq_n_u8(out.val[2], in.val[1], 2);
        |                    ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3 errors generated.

@ilevkivskyi
Copy link
Contributor Author

Interestingly, the errors only appear in Python 3.12 WASM build, Python 3.13 works fine.

@mr-c
Copy link
Contributor

mr-c commented Nov 30, 2025

Interestingly, the errors only appear in Python 3.12 WASM build, Python 3.13 works fine.

Ah, I was testing with Python 3.13. I will look into this soon

@mr-c
Copy link
Contributor

mr-c commented Dec 1, 2025

@ilevkivskyi Fix for Pyodide with Python 3.12 is at python/mypy#20342

ilevkivskyi pushed a commit to python/mypy that referenced this pull request Dec 1, 2025
Fixes the issue reported at
mypyc/librt#14 (comment)

For Python 3.12, [the pyodide_2024_0 ABI requires emscripten
3.1.58](https://pyodide.org/en/stable/development/abi.html#pyodide-2024-0),
which uses an older version of SIMDe that doesn't include all the NEON
64-bit intrinsics we need.

So we use the 32-bit NEON intrinsics implementation of base64 for
Pyodide on Python 3.12, and use the 64-bit NEON intrinsics
implementations of base64 for Pyodide on Python 3.13.

[Pyodide for Python 3.13, the pyodide_2025_0 ABI, uses emscripten
4.0.9](https://pyodide.org/en/stable/development/abi.html#pyodide-2025-0-under-development)

Emscripten versdion 3.1.63 appears to be the first version with the
updated SIMDe versions:


emscripten-core/emscripten@6223c43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants