Emit acquire
fence before loading final
field
#3699
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In typelevel/cats-effect#3899 (comment) @durban brought to my attention that the current implementation of
final
Field Semantics when loading afinal
field is not correct.Specifically, we must issue an
aquire
fence prior to loading thefinal
field. This prevents the load of the field from being re-ordered before the load of the object reference (e.g. due to a compiler optimization that attempts an early, speculative load). Such a re-ordering could cause a pre-initialized value for the field to be observed.Unfortunately I'm still not confident that we will have a correct implementation of
final
Field Semantics after this change. However, I do believe that this change brings us closer to that.On the other end of the spectrum, I am hopeful that we might eventually be able to remove this fence entirely. Ideally, the dependency of the load of the
final
field on the load of the object reference would enforce the ordering. I don't have sufficient imagination to see how the compiler could generate speculative code that violates this ordering. If we can be confident that neither LLVM nor supported hardware will perform such a re-ordering, then it could be safe to remove this fence.