Skip to content

Add support for unaligned accesses on arm64 - #13807

Merged
gasche merged 1 commit into
ocaml:trunkfrom
matthewelse:push-qoxskxtmtlzr
Feb 19, 2025
Merged

gasche merged 1 commit into
ocaml:trunkfrom
matthewelse:push-qoxskxtmtlzr

Conversation

@matthewelse

Copy link
Copy Markdown
Contributor

Resolves #13631

Per the discussion in #13631, it seems like not supporting aligned accesses on arm64 has been unnecessarily conservative. This PR makes the trivial change to flip allow_unaligned_access on arm64. As it turns out, this only affects a handful of code-paths in cmm_helpers.ml, but makes a noticeable difference to performance in code that processes bigstrings, amongst other things.

Testing:
I've been using this as a local build from time to time on my M2 Mac over the last few months, and I've run the test suite locally. I've also documented the behaviour of other compilers (clang and GCC) in the corresponding issue. I've also manually audited the handful of use-cases of allow_unaligned_access, and nothing looks suspicious to my untrained eye.

@matthewelse
matthewelse force-pushed the push-qoxskxtmtlzr branch 2 times, most recently from d80ab5c to 11a65b8 Compare February 17, 2025 22:00

@xavierleroy xavierleroy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I ran the "precheck" CI test, which includes two ARM64 configurations (macOS on an M1 and Linux on a rPi 4). Everything works fine, as expected. I'm in favor of merging this PR and see what happens.

As discussed in ocaml#13631, the decision not to support unaligned accesses
on arm64 was made before any hardware was available, and in practice all
ARMv8 CPUs support unaligned accesses.
@matthewelse

Copy link
Copy Markdown
Contributor Author

Force pushed to update Changes, thanks for running the extra tests!

@OlivierNicole

Copy link
Copy Markdown
Contributor

Also in favor of merging, from the discussion cited I expect it will be fine, the caveat regarding atomic accesses on unaligned addresses should not apply to OCaml atomics that are word-aligned anyway.

@gasche
gasche merged commit 25265d7 into ocaml:trunk Feb 19, 2025
@gasche

gasche commented Feb 19, 2025

Copy link
Copy Markdown
Member

Well, let's merge then :-)

@tmcgilchrist

Copy link
Copy Markdown
Contributor

but makes a noticeable difference to performance in code that processes bigstrings, amongst other things.

@matthewelse Do you have some performance numbers to share on this change? Or a sample application that could be added to sandmark? It would be good to get a sense of what improvement can be expected.

@matthewelse

Copy link
Copy Markdown
Contributor Author

@tmcgilchrist I've been implementing string searching algorithms in OCaml, and as part of that I was experimenting with an implementation of memchr in OCaml -- usual caveats around microbenchmarks, but without this change (unscientific testing, there might be other changes between the two OPAM switches that I've forgotten about) searching through a 10,000 byte bigstring goes from about 2us to about 0.7us with this change

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.

Support unaligned accesses on arm64

5 participants