-
Notifications
You must be signed in to change notification settings - Fork 729
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
[FFI/JDK21] Enable the union support in JDK21 #18291
[FFI/JDK21] Enable the union support in JDK21 #18291
Conversation
The implementation was inspired by the libffi document at https://github.com/libffi/libffi/blob/5c6e53db873767cd2266745cebc62551958f5bee/doc/libffi.texi#L543 as to how to set the union type via libffi in native
Instead of directly changing both the java code and native code to handle the union type which is technically challenging & complex (might need to introduce a new type/symbol to differentiate the union type from other types even in the upcall thunk) and difficult from the maintenance perspective in the future, we adopted a lightweight solution to simulate the union type by laying out an array of primitive types with the same alignment as the union in java, in which case the primitive sequence can be safely mapped to the given GPRs/FPRs (or overflow/reserved area which is determined by the compiler) on the underlying hardware. |
The code has been verified with the newly added union related tests and the jtreg test suite for union at https://github.com/ibmruntimes/openj9-openjdk-jdk21/blob/openj9/test/jdk/java/foreign/nested/TestNested.java on all supported platform except one issue with |
Reviewer: @tajila |
6f536b9
to
a1f4f24
Compare
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
a1f4f24
to
4937df2
Compare
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
test/functional/Java21andUp/src/org/openj9/test/jep442/downcall/UnionStruTests.java
Outdated
Show resolved
Hide resolved
4937df2
to
19ba45c
Compare
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
48f1d90
to
ed7c943
Compare
4df913f
to
00d802d
Compare
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
3302ae9
to
0e00e81
Compare
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
An earlier version of this had new test code for unions: What happened to those test additions? |
0e00e81
to
0205731
Compare
This is the first time the union specific test suites were added to verify unions, which were previously placed in So we will need to copy all these test suites to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also a misspelling of "strings" on line 202.
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
This is not determined by our implementation in java (the method name might be misleading at this point) but rather by how the underlying libffi simulate the union with the struct (the array of primitive) based on the liffi document for union, whether the union is placed onto the registers or onto the stack. Our job in java simply guarantees that libff correctly handles it against the calling convention on the given platforms. I will create test cases with your example ( which should be a 8 |
0205731
to
97e42ed
Compare
It's not up to libffi either: it must use the calling convention expected by the function being invoked. If I use gcc to compile a function receiving or returning |
With the test case as follows:
plus the java code in UnionTests
Here's the result for the union mentioned above with our implementation:
plus the debug results for each element of the union as follows:
Which means there is no problem in dealing with this union in our code. As I mentioned previously, how to handle union on registers/stack in native is irrelevant to our code but depends on the underlying libffi/compiler. That being said, our job is to simply lay out the expected primitive arrays in java to be correctly handled by libffi/compiler as to how to map to the registers/stack. |
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Show resolved
Hide resolved
97e42ed
to
5a39519
Compare
Perhaps you can find a better, more descriptive name and update the javadoc to suit? The only effect of the answer from In the end, I expect passing or returning |
jcl/src/java.base/share/classes/openj9/internal/foreign/abi/LayoutStrPreprocessor.java
Outdated
Show resolved
Hide resolved
5a39519
to
22e78a1
Compare
Yes. I just updated the method names by adding
Yes (for GPR or stack as it is) and it was verified on all supported platforms in the majority of cases including some platform specific exceptions being addressed in our code.
This was exactly my initial assumption which proved to be entirely wrong in tests. On the contrary, the truth is: it always has to be GPR/stack for union with mixed types (including the floating and non-float primitives, nested structs/unions, etc) even though
Yes, that's what we've already done in the newly added test suites for union which are sufficient to cover most scenarios with different combinations to ensure the code is verified from different perspectives (normal, nested, mixed types, etc) |
The changes aim to implement the union type in the FFI specific code in JD21 by mapping the simplified the union type (simulated via the struct) to the underlying hardware. Signed-off-by: ChengJin01 <[email protected]>
22e78a1
to
512606f
Compare
Were you intending to add back the union test cases for Java22andUp in this pull request, or defer those to later? |
We intend to add them to |
Jenkins test sanity aix,osx,win,zlinux jdk17,jdk21 |
The test failure in CRIU/JDK17 at https://openj9-jenkins.osuosl.org/job/Test_openjdk17_j9_sanity.functional_s390x_linux_Personal/387/tapTestReport/Test_openjdk17_j9_sanity.functional_s390x_linux_Personal_testList_0%2F383%2Faqa-tests%2FTKG%2Foutput_16987876625871%2FTest_openjdk17_j9_sanity.functional_s390x_linux_Personal_testList_0.tap-65/
|
The changes aim to implement the union type in the FFI specific code in JD21
by mapping the simplified the union type (simulated via the struct) to the
underlying hardware.
Signed-off-by: ChengJin01 [email protected]