-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8331734: Atomic MemorySegment VarHandle operations fails for element layouts #19124
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
8331734: Atomic MemorySegment VarHandle operations fails for element layouts #19124
Conversation
|
👋 Welcome back mcimadamore! A progress list of the required criteria for merging this PR into |
|
@mcimadamore This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 37 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
|
@mcimadamore The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
minborg
left a comment
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.
LGTM. As mentioned in the PR notes, additional optimizations could be made and this could be the objective of a future PR.
|
Overall, I'm not too sure about the alignment check on the root layout performed by a var handle pointing to a group layout member. We've got there honestly - e.g. make sure the accessed segment/offset is really compatible with root layout. But, if so, then why also not checking the segment size against the root layout size? E.g. say I have a layout for: var POINT_LAYOUT = structLayout(JAVA_DOUBLE.withName("x"), JAVA_DOUBLE.withName("y"))And I derive a var handle for VarHandle x_handle = POINT_LAYOUT.varHandle(PathElement.groupLayout("x"));Say that I have a 8-byte segment, and I use that with the MemorySegment halfPoint = Arena.ofAuto().allocate(8);
double x = (double)x_handle.get(halfPoint, 0);Should this work? Currently it does, and one might claim that it does so "by accident" - e.g. it just so happen that the provided segment/offset combo was big enough to access There are of course two possible interpretations here, and both are valid:
The former favors performance, the latter favors safety, as it ensures that the initial segment/offset combo do indeed describe a segment whose characteristics are compatible with those of the selected layout. This is also related to This means that the root layout checks performed by the obtained method handle will refer to the alignment (and size) of the current layout. For instance, if I write: Assuming we picked the safer semantics described above, the resulting var handle will check that the initial segment/offset combo will point to a segment whose alignment (and size) are compatible with those of |
An example of this approach is attempted here: Again, no significant performance regression is observed - the root layout checks dominate the value layout checks, and it seems like C2 is able to spot that. |
|
I've decided to integrate this for now, as the fix in this PR restores correctness of the implementation. |
|
/integrate |
|
Going to push as commit 1c5f150.
Your commit was automatically rebased without conflicts. |
|
@mcimadamore Pushed as commit 1c5f150. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
This PR fixes an issue that has crept into the FFM API implementation.
From very early stages, the FFM API used to disable the alignment check on nested layout elements, in favor of an alignment check against the memory segment base address. The rationale was that the JIT had issue with eliminating redundant alignment checks, and accessing nested elements could never result in alignment issues, since the alignment of the container is provably bigger than that of the contained element. This means that, when creating a var handle for a nested layout element, we set the nested layout alignment to 1 (unaligned), derive its var handle, and then decorate the var handle with an alignment check for the container.
At some point in 22, we tweaked the API to throw
UnsupportedOperationExceptionwhen using an access mode incompatible with the alignment constraint of the accessed layout. That is, a volatile read on anintis only possible if the access occurs at an address that is at least 4-byte aligned. Otherwise anUOEis thrown.Unfortunately this change broke the aforementioned optimization: creating a var handle for an unaligned layout works, but the resulting layout will not support any of the atomic access modes.
Since this optimization is not really required anymore (proper C2 support to hoist/eliminate alignment checks has been added since then), it is better to disable this implementation quirk, and leave optimizations to C2.
(If we really really wanted to optimize things a bit, we could remove the container alignment check in the case the accessed value is the first layout element nested in the container, but this PR doesn't go that far).
I've run relevant benchmarks before/after and found no differences. In part this is because
arrayElementVarHandleis unaffected. But, even after tweaking theLoopOverNonConstantbenchmark to add explicit tests for the code path affected, no significant difference was found, sign that C2 is indeed able to spot (and remove) the redundant alignment check. Note: if we know thataligned_to_N(base)holds, then it's easy to prove thataligned_to_M(base + offset + index * scale)holds, whenN >= Mand withoffsetandscaleknown (the latter a power of two).Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/19124/head:pull/19124$ git checkout pull/19124Update a local copy of the PR:
$ git checkout pull/19124$ git pull https://git.openjdk.org/jdk.git pull/19124/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 19124View PR using the GUI difftool:
$ git pr show -t 19124Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/19124.diff
Webrev
Link to Webrev Comment