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

Optimize struct RMW ops in OptimizeInstructions #7225

Merged
merged 2 commits into from
Jan 17, 2025
Merged

Conversation

tlively
Copy link
Member

@tlively tlively commented Jan 17, 2025

When the RMW operation can be proven not to change the accessed value,
optimize it to a simple atomic get instead. This is valid because a
write that does not change an in-memory value does not synchronize with
any subsequent reads of that value, since those reads can be considered
to be reading from the previous write.

Also optimize RMW operations on unshared structs to their non-atomic
equivalent operations. This can increase code size, but can also enable
follow-on optimizations of the simpler operations and can be less
expensive at runtime.

When the RMW operation can be proven not to change the accessed value,
optimize it to a simple atomic get instead. This is valid because a
write that does not change an in-memory value does not synchronize with
any subsequent reads of that value, since those reads can be considered
to be reading from the previous write.

Also optimize RMW operations on unshared structs to their non-atomic
equivalent operations. This can increase code size, but can also enable
follow-on optimizations of the simpler operations and can be less
expensive at runtime.
@tlively tlively requested a review from kripken January 17, 2025 01:59
@tlively
Copy link
Member Author

tlively commented Jan 17, 2025

@conrad-watt, could I ask you to double check that these optimizations are valid? In particular, can you confirm that it is valid to optimize atomic RMW operations that do not write new values to atomic gets?


Builder builder(*getModule());

// Even when the access to shared memory, we can optimize out the modify and
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Even when the access to shared memory, we can optimize out the modify and
// Even when we access shared memory, we can optimize out the modify and

// operation into several non-atomic operations is safe because no other
// thread can observe an intermediate state in the unshared memory. This
// initially increases code size, but the more basic operations may be
// more optimizable than the original RMW.
Copy link
Member

Choose a reason for hiding this comment

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

How likely are we to succeed? I am somewhat worried that generally the size increase here will stick around, and might also be slower.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure! We can experiment with it once we have real test cases. I figure it will be easier to experimentally disable this optimization than it would be to write the optimization just for an experiment, though. I could imagine in the long run we would want to do this only when optimizing for speed over size.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, sounds good. Perhaps add a TODO for that?

// operation into several non-atomic operations is safe because no other
// thread can observe an intermediate state in the unshared memory. This
// initially increases code size, but the more basic operations may be
// more optimizable than the original RMW.
Copy link
Member

Choose a reason for hiding this comment

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

Ok, sounds good. Perhaps add a TODO for that?

@tlively tlively enabled auto-merge (squash) January 17, 2025 18:37
@tlively tlively merged commit 8623f73 into main Jan 17, 2025
13 checks passed
@tlively tlively deleted the optimize-struct-rmw branch January 17, 2025 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants