[lworld] RFR: 8367785: [lworld] Compiler adherence to new memory model rules for strict instance fields

Tobias Hartmann thartmann at openjdk.org
Mon Oct 6 07:39:14 UTC 2025


On Sat, 4 Oct 2025 04:38:09 GMT, Chen Liang <liach at openjdk.org> wrote:

>> The new memory model rules for strict instance fields (see [JDK-8366372](https://bugs.openjdk.org/browse/JDK-8366372)) require a memory barrier at the beginning of `java.lang.Object::<init>`. The JITs will try to omit it if the receiver type is statically known (potentially via CHA + a dependency) and the class does not contain any strict fields.
>> 
>> Potentially, there's some more tricks that we can play. Most importantly, I think we can omit barriers for final fields at the end of the constructors if the field is also strict. I filed [JDK-8369166](https://bugs.openjdk.org/browse/JDK-8369166) for this.
>> 
>> I added a test that already caught [JDK-8369044](https://bugs.openjdk.org/browse/JDK-8369044) and will fail immediately without the JIT changes (on AArch64).
>> 
>> Thanks,
>> Tobias
>
> src/hotspot/share/c1/c1_GraphBuilder.cpp line 4304:
> 
>> 4302:   // Check if we need a membar at the beginning of the java.lang.Object
>> 4303:   // constructor to satisfy the memory model for strict fields.
>> 4304:   if (EnableValhalla && method()->intrinsic_id() == vmIntrinsics::_Object_init) {
> 
> Suggestion:
> 
>   if (EnableValhalla && callee->intrinsic_id() == vmIntrinsics::_Object_init) {
> 
> Fits better in the context of this function.

I would rather leave the code as is because we enter the callee scope just above. Also, code further below also uses `method()`.

> src/hotspot/share/c1/c1_LIRGenerator.cpp line 3019:
> 
>> 3017:   // Check if we need a membar at the beginning of the java.lang.Object
>> 3018:   // constructor to satisfy the memory model for strict fields.
>> 3019:   if (EnableValhalla && method()->intrinsic_id() == vmIntrinsics::_Object_init) {
> 
> Should we perform checks to try skip this fence here?

What checks are you suggesting? `java.lang.Object::<init>` is root of the compilation here, so we can't omit the barrier. C1 would not emit a barrier at the end of the constructor, like the interpreter does. C1 would only do that if these conditions are met:
https://github.com/openjdk/valhalla/blob/274d56076260a490ed52df29373216617ddf73ef/src/hotspot/share/c1/c1_GraphBuilder.cpp#L1645-L1652

-------------

PR Review Comment: https://git.openjdk.org/valhalla/pull/1656#discussion_r2405058821
PR Review Comment: https://git.openjdk.org/valhalla/pull/1656#discussion_r2405003535


More information about the valhalla-dev mailing list