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

8293117: Add atomic bitset functions #13711

Closed
wants to merge 3 commits into from

Conversation

kimbarrett
Copy link

@kimbarrett kimbarrett commented Apr 28, 2023

Please review this change that adds and/or/xor bitops to Atomic.

Some offline discussion reached consensus on the following API (with T being a
template parameter that must be an integral type):
T fetch_then_and(volatile T* dest, T bits, atomic_memory_order order)
T fetch_then_or(volatile T* dest, T bits, atomic_memory_order order)
T fetch_then_xor(volatile T* dest, T bits, atomic_memory_order order)
T and_then_fetch(volatile T* dest, T bits, atomic_memory_order order)
T or_then_fetch(volatile T* dest, T bits, atomic_memory_order order)
T xor_then_fetch(volatile T* dest, T bits, atomic_memory_order order)
with order defaulting to memory_order_conservative.

I'm hoping there won't be more bike-shedding on the names.

This naming convention differs from what exists for add/sub, where we have
fetch_and_add, add_and_fetch, and the like. fetch_and_and and and_and_fetch
just looked too weird. For consistency we should probably rename the add/sub
operations to use "then".

A default implementation is provided, using a CAS loop for all operations.
That implementation can be overridden on a per-platform basis. Currently
there aren't any platform-specific implementations; such will be added by
followup RFEs. We'll want to override for most (if not all) platforms, since
the CAS loop implementation is generally less than optimal (in some cases
rather comically so).

For some platforms a CAS loop is necessary to obtain either the old or the new
value, but have a better implementation available if neither value is needed.
However, we can't easily make use of such. We would need to either add
functions returning void to the API or change the API to return expression
templates so we can detect how the result is used. However, we may be able to
use compiler intrinsics for some platforms, which may be able to specialize
for the usage context. If that turns out to be a problem then we can expand
the API accordingly later.

Testing:
mach5 tier1, including new gtests for the new functionality.


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

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 13711

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 28, 2023

👋 Welcome back kbarrett! 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 openjdk bot added the rfr Pull request is ready for review label Apr 28, 2023
@openjdk
Copy link

openjdk bot commented Apr 28, 2023

@kimbarrett The following label will be automatically applied to this pull request:

  • hotspot-runtime

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.

@openjdk openjdk bot added the hotspot-runtime hotspot-runtime-dev@openjdk.org label Apr 28, 2023
@mlbridge
Copy link

mlbridge bot commented Apr 28, 2023

Webrevs

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

This look okay, a couple of comments.

do {
old_value = fetched_value;
new_value = operation(old_value);
fetched_value = Atomic::cmpxchg(dest, old_value, new_value, order);
Copy link
Member

Choose a reason for hiding this comment

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

At some point in future, it would be nice to replace the retry-loops like these with weak CASes. It could be done in platform-specific code, though, but it would seem awkward to specialize for all LL/SC-retry-loop platforms just to get access to weak CAS.

Copy link
Author

Choose a reason for hiding this comment

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

Yes. I haven't checked, but I would guess the majority of our CAS uses are loops that would benefit from
using a weak CAS on platforms where that's available.

Comment on lines +757 to +775
// Implement bitop_then_fetch operations by calling fetch_then_bitop and
// applying the operation to the result and the bits argument.
class Atomic::PostfetchBitopsUsingPrefetch {
public:
template<typename T>
T and_then_fetch(T volatile* dest, T bits, atomic_memory_order order) const {
return bits & Atomic::fetch_then_and(dest, bits, order);
}

template<typename T>
T or_then_fetch(T volatile* dest, T bits, atomic_memory_order order) const {
return bits | Atomic::fetch_then_or(dest, bits, order);
}

template<typename T>
T xor_then_fetch(T volatile* dest, T bits, atomic_memory_order order) const {
return bits ^ Atomic::fetch_then_xor(dest, bits, order);
}
};
Copy link
Member

Choose a reason for hiding this comment

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

Is this used somewhere? Looks like all current helpers use CASes instead of this "prefetch" code.

Copy link
Author

Choose a reason for hiding this comment

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

Not used yet. It's used by some of the platform-specific implementations I have queued up.

Comment on lines 285 to 287
TEST(AtomicBitopsTest, int64) {
AtomicBitopsTestSupport<int64_t>()();
}
Copy link
Member

Choose a reason for hiding this comment

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

These 64-bit tests would fail on 32-bit systems, are they not? They are outside the requirements for the contract to only support sizeof(int) and sizeof(void*). Probably just #ifdef _LP64 these?

Copy link
Author

Choose a reason for hiding this comment

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

Well-spotted. I forgot to conditionalize for _LP64 as is done for other tests in this file.

Copy link
Contributor

@coleenp coleenp left a comment

Choose a reason for hiding this comment

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

Thank you for doing this. I'll file a follow up to convert some duplicated code to use these after this is pushed.

@openjdk
Copy link

openjdk bot commented Apr 28, 2023

@kimbarrett 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:

8293117: Add atomic bitset functions

Reviewed-by: shade, coleenp, dholmes

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 no new commits pushed to the master branch. If another commit should be pushed before you perform the /integrate command, your PR will be automatically rebased. If you prefer to avoid any potential automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Apr 28, 2023
@coleenp
Copy link
Contributor

coleenp commented May 1, 2023

@kimbarrett do you have GHA configured? This says 3 checks have passed but this sort of change might have problems compiling for other platforms.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Seems quite reasonable.

I think for the naming it wouldn't be a terrible idea to consider using uppercase for the operation name: AND/OR/XOR/ADD/SUB. That would make the operation name stand out more clearly. Just a thought.

Thanks.

@coleenp
Copy link
Contributor

coleenp commented May 2, 2023

The names aren't my favorite but capitalizing them would be worse, imo.

@kimbarrett
Copy link
Author

@kimbarrett do you have GHA configured? This says 3 checks have passed but this sort of change might have problems compiling for other platforms.

Manually triggered GHA testing - completed successfully.

@kimbarrett
Copy link
Author

Thanks for reviews @coleenp , @shipilev , and @dholmes-ora .

@kimbarrett
Copy link
Author

/integrate

@openjdk
Copy link

openjdk bot commented May 2, 2023

Going to push as commit 8a70664.

@openjdk openjdk bot added the integrated Pull request has been integrated label May 2, 2023
@openjdk openjdk bot closed this May 2, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 2, 2023
@openjdk
Copy link

openjdk bot commented May 2, 2023

@kimbarrett Pushed as commit 8a70664.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@kimbarrett kimbarrett deleted the default-atomic-bitops branch May 2, 2023 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-runtime hotspot-runtime-dev@openjdk.org integrated Pull request has been integrated
4 participants