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

8353786: Migrate Vector API math library support to FFM API #24462

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

iwanowww
Copy link
Contributor

@iwanowww iwanowww commented Apr 4, 2025

Migrate Vector API math library (SVML and SLEEF) linkage from native code (in JVM) to Java FFM API.

Since FFM API doesn't support vector calling conventions yet, migration affects only symbol lookup for now. But it still enables significant simplifications on JVM side.

The patch consists of the following parts:

  • on-demand symbol lookup in Java code replaces eager lookup from native code during JVM startup;
  • 2 new VM intrinsics for vector calls (support unary and binary shapes) (code separated from unary/binary vector operations);
  • new internal interface to query supported CPU ISA extensions (jdk.incubator.vector.CPUFeatures) used for CPU dispatching.

java.lang.foreign API is used to perform symbol lookup in vector math library, then the address is cached and fed into corresponding JVM intrinsic, so C2 can turn it into a direct vector call in generated code.

Once java.lang.foreign supports vectors & vector calling conventions, VM intrinsics can go away.

Performance is on par with original implementation (tested with microbenchmarks on linux-x64 and macosx-aarch64).

Testing: hs-tier1 - hs-tier6, microbenchmarks (on linux-x64 and macosx-aarch64)

Thanks!


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8353786: Migrate Vector API math library support to FFM API (Enhancement - P3)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/24462/head:pull/24462
$ git checkout pull/24462

Update a local copy of the PR:
$ git checkout pull/24462
$ git pull https://git.openjdk.org/jdk.git pull/24462/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24462

View PR using the GUI difftool:
$ git pr show -t 24462

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/24462.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 4, 2025

👋 Welcome back vlivanov! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Apr 4, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Apr 4, 2025

@iwanowww
The core-libs label was successfully added.

The hotspot-compiler label was successfully added.

@liach
Copy link
Member

liach commented Apr 4, 2025

Moving vector API library selection to Java code looks like a right step to me.

@iwanowww iwanowww force-pushed the vector.math.01.java branch from 03c5a8c to fc27aee Compare April 5, 2025 01:30
@iwanowww iwanowww changed the title XXXXXXX: ??? 8353786: Migrate Vector API math library support to FFM API Apr 5, 2025
@iwanowww
Copy link
Contributor Author

iwanowww commented Apr 5, 2025

/cc hotspot

@openjdk
Copy link

openjdk bot commented Apr 5, 2025

@iwanowww
The hotspot label was successfully added.

@iwanowww iwanowww marked this pull request as ready for review April 5, 2025 02:38
@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 5, 2025
@mlbridge
Copy link

mlbridge bot commented Apr 5, 2025

Webrevs

Entry<T> constructEntry(Operator op, int opc, VectorSpecies<E> vspecies, IntFunction<T> implSupplier) {
if (LIBRARY.isSupported(op, vspecies)) {
String symbol = LIBRARY.symbolName(op, vspecies);
MemorySegment addr = LOOKUP.find(symbol)
Copy link
Contributor

Choose a reason for hiding this comment

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

It is better to use LOOKUP.findOrThrow() because it does not require lambda creation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, changed as you suggested. I introduced a try-catch block instead.

Copy link
Contributor

@vnkozlov vnkozlov left a comment

Choose a reason for hiding this comment

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

Few questions?

return features.contains(feature.toLowerCase());
}

public static class X64 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we create src/jdk.incubator.vector/cpu/ for CPU specific information? As separate refactoring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To clarify: are you suggesting to move platform-specific classes into a separate package or platform-specific location?

It does make sense to separate platform-specific parts into their own classes once amount of code grows over some limit. For now it doesn't look too attractive since amount of code is very small.

}

/**
* Naming convention in SVML vector math library.
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this library has code for all AVX configurations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, there are 4 configurations (-XX:UseAVX=[0..3]) in total covered by SVML library.

@iwanowww
Copy link
Contributor Author

iwanowww commented Apr 7, 2025

In addition to addressing review feedback, there are 2 updates:

  • SVML: I overlooked that 64-bit vectors are covered by original implementation; fixed now;
  • JVM: features_string to cpu_info_string renaming uniformly across all platforms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants