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

Add functionality to test if the host CPU supports native SIMD instructions #107429

Merged
merged 11 commits into from Apr 29, 2024

Conversation

ldematte
Copy link
Contributor

@ldematte ldematte commented Apr 12, 2024

This commit adds the ability to check if the host CPU supports the instruction set we use in our native vector code.
This is to avoid having the process terminated (badly) because we tried to execute a non-supported instruction; better know this early, and avoid to take the native code path entirely.

The int vec_caps() function, implemented in the native code (C) of the library, returns 0 if the processor does not support (i.e. cannot run) the instructions used to implement the other functions.

On ARM, we use the NEON vector instruction set (#106133):

  • On MacOS, vec_caps() is trivial and returns 1; for M-series CPUs, we can safely assume NEON is always present.
  • On Linux systems, we use the mechanism explained in the ARM development guide: call getauxval with AT_HWCAP, and test for the HWCAP_NEON bit (4096).

I've tested getauxval on various ARM systems and emulators (including Graviton on AWS); the NEON instruction set seems to be widely available - I was not able to find a processor supporting armv8 and not supporting NEON; nevertheless, I think that adding this functionality is good for 3 reasons:

  • it is better to err on the safe side
  • we would eventually need this for x64 (where support for vector instruction sets is way more sparse)
  • we can expand it for future optimizations

The last point: since vec_caps() returns an integer, we may want to return different values (e.g. 2) to indicate some "advanced support", and dynamically bind to a different flavor of the function (e.g. dot8s_2) that is implemented with a more performant but less diffused instruction set (e.g. AVX512, armv9, etc.)

// There must be at least this number of bytes/elements available when going native
static final int DOT_STRIDE = 32;
static final int SQR_STRIDE = 16;
static final MethodHandle dot8stride$mh = downcallHandle("dot8s_stride", FunctionDescriptor.of(JAVA_INT));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ChrisHegarty GH made a bit of a mess with the diff, but essentially code from here to the end is the same, just indendeted (encapsulated in the private inner class)

@ldematte ldematte changed the title Native vec caps 2 Add functionality to test if the host CPU supports native SIMD instructions Apr 23, 2024
@ldematte ldematte added the test-arm Pull Requests that should be tested against arm agents label Apr 23, 2024
@ldematte ldematte added :Search/Search Search-related issues that do not fall into other categories >non-issue labels Apr 23, 2024
@ldematte ldematte marked this pull request as ready for review April 23, 2024 17:48
@ldematte ldematte requested a review from a team as a code owner April 23, 2024 17:48
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search (Team:Search)

@elasticsearchmachine elasticsearchmachine added the Team:Search Meta label for search team label Apr 23, 2024
@ldematte
Copy link
Contributor Author

@elasticmachine update branch

Copy link
Contributor

@ChrisHegarty ChrisHegarty left a comment

Choose a reason for hiding this comment

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

LGTM

@ChrisHegarty ChrisHegarty added the test-windows Trigger CI checks on Windows label Apr 24, 2024
@ldematte ldematte requested a review from a team April 24, 2024 09:08
@ldematte
Copy link
Contributor Author

@elasticmachine update branch

@ldematte
Copy link
Contributor Author

@elasticmachine update branch

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

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

LGTM

@ldematte
Copy link
Contributor Author

@elasticmachine update branch

@ldematte ldematte merged commit 6ef4865 into elastic:main Apr 29, 2024
23 checks passed
@ldematte ldematte deleted the native-vec-caps-2 branch April 29, 2024 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>non-issue :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team test-arm Pull Requests that should be tested against arm agents test-windows Trigger CI checks on Windows v8.15.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants