Skip to content
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

WASM SIMD glitched on iPhone and crashes on iPad #312

Open
FrostKiwi opened this issue Jun 14, 2023 · 7 comments
Open

WASM SIMD glitched on iPhone and crashes on iPad #312

FrostKiwi opened this issue Jun 14, 2023 · 7 comments

Comments

@FrostKiwi
Copy link
Contributor

FrostKiwi commented Jun 14, 2023

In my emscripten + C + WebGL project with cglm doing vector math, enabling SIMD via -msimd128 produces broken floats, resulting in warping of the texture on screen, but only on an iPhone SE, iOS 16.4.1. On an iPad 9th gen, iOS 16.3.1, the WebApp straight up refuses to launch with SIMD active.
Tested this behavior is browser independent, as both Safari and Edge are affected. According to https://webassembly.org/roadmap/, SIMD should work on these browsers.

Untitled.1.mp4

Since I'm still preparing my WebApp and don't expect users, I have purposefully left the WebApp in this broken state via commit FrostKiwi/Mirrorball@a065c5a#diff-76ed074a9305c04054cdebb9e9aad2d818052b07091de1f20cad0bbac34ffb52. You can test this out live (until I revert SIMD usage in a week) by going to https://frostkiwi.github.io/frostorama/ clicking on Project image and seeing the image glitch out, as opposed to having proper projection math applied on Android or a normal PC browser. Reverted buggy SIMD again with FrostKiwi/Mirrorball@c92cf22

I have not yet tracked down the specific line responsible, but highly suspect the vector operations from this line up to line 50 https://github.com/FrostKiwi/frostorama/blob/a065c5a7d62e3f89f2aed6a0e514134bd571cafe/src/render_loop.c#L22 . There are calculations happening preparing a Fullscreen quad to raytrace and passing these floats to a WebGL shader. I suspect, that enabling SIMD makes these calculations become invalid on the iPhone.

I was looking forward to enabling SIMD, since https://github.com/recp/cglm is purposefully designed to have WASM SIMD functions. However, I will have to revert the use of SIMD, since it breaks on iOS.

Version of emscripten/emsdk
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.38 (9eff02bc816c50ab0e3b70a3bd5b72a8dc2893a2)
clang version 17.0.0 (https://github.com/llvm/llvm-project 004bf170c6cbaa049601bcf92f86a9459aec2dc2)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\<redacted>\min\home\artsimow\build\emsdk\upstream\bin

Cross-posted this in the emscripten repo, since it affects that compiler directly as well: emscripten-core/emscripten#19619

@myfreeer
Copy link
Contributor

Hello, thanks for reporting.

  1. In apple iphone, every browser that can be installed from app store uses the safari engine, so this is likely to be a browser related issue.
  2. Have you tried with macos safari (on x86 or arm64)?
  3. It should be useful to have a minimal reproduction code, making it possible to:
    1. Try with different toolchains like wasi-sdk.
    2. Analyze the bytecode with Compiler Explorer
    3. Step-by-step compare the result in ios safari with other browsers.

@FrostKiwi
Copy link
Contributor Author

2. Have you tried with macos safari (on x86 or arm64)?

No, don't have access to MacOS devices.

3. It should be useful to have a minimal reproduction code, making it possible to:

I'll see what I can do. Maybe I'll get a minimal example working over the weekend...

@FrostKiwi
Copy link
Contributor Author

Same glitched output on an iPhone Xs with iOS version 16.5

@gottfriedleibniz
Copy link

Other repositories seem to be experiencing similar issues.

A minimal reproduction here would be great (e.g., expected vs. actual matrix values). Most operations in the "Update Camera" sequence are load/fmadd/splat/store, so it shouldn't be too difficult to debug (or cross-reference with some recent WebKit changes).

@FrostKiwi
Copy link
Contributor Author

A minimal reproduction here would be great (e.g., expected vs. actual matrix values). Most operations in the "Update Camera" sequence are load/fmadd/splat/store, so it shouldn't be too difficult to debug

I setup https://github.com/FrostKiwi/iOS-simd-bug to try and create a minimal reproduction, but failed. The issue seems to be more subtle than that. All devices give the same correct output. It's not the cglm calls, but a more delicate interaction.

Just to be sure that I have not gone insane, I have disabled SIMD again on my WebApp via FrostKiwi/Mirrorball@c92cf22 and the issue is indeed gone. Maybe I'll try later again to nail down the specific line, which causes things to explode when SIMD is turned on...

@gottfriedleibniz
Copy link

gottfriedleibniz commented Jun 18, 2023

Are you compiling iOS-simd-bug w/ release or debug flags? With release the optimizer may be precomputing everything, e.g., wasm2wat gives me ~26 instructions for the 'Update Camera' section.

At least from the perspective of cglm, it would be nice to know what the cam_rotation, view and projection matrices are in a bugged/inconsistent state (and whether there is a difference when run elsewhere w/ same parameters).

It would also be nice to know if this issue reproduces on WebKitGTK ARM64.

FrostKiwi added a commit to FrostKiwi/iOS-simd-bug that referenced this issue Jun 18, 2023
@FrostKiwi
Copy link
Contributor Author

FrostKiwi commented Jun 19, 2023

release or debug flags? With release the optimizer may be precomputing

Good suggestion! I was indeed using -O3. Retried it with -O0, but the result is the same. So I'll retry next weekend to hunt down the reason. My guess was probably wrong and it may not even be the SIMD instructions generated by cglm cauing strange effects on iPhone. Might just be autovectorization. I'll go the other way next time, stripping down the webapp step-by-step instead.

Also reconfirmed one more time: SIMD enabled = iPad ExceptionThrown and iPhone strange warping, SIMD disabled = iPad fine and iPhone fine.

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

No branches or pull requests

3 participants