RFR: 8360654: AArch64: Remove redundant dmb from C1 compareAndSet [v5]

Ruben duke at openjdk.org
Wed Nov 19 16:56:57 UTC 2025


On Wed, 19 Nov 2025 15:37:01 GMT, Andrew Haley <aph at openjdk.org> wrote:

>> As far as I can tell, this is currently used for codegen of [getAndAddAcquire](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/invoke/VarHandle.html#getAndAddAcquire(java.lang.Object...)).
>> 
>> The method itself doesn't emit a trailing barrier, however a `membar_acquire` is inserted by `C2AccessFence` - https://github.com/openjdk/jdk/blob/0bff5f3dbe69ab2a59db771af1020b04c0132954/src/hotspot/share/gc/shared/c2/barrierSetC2.cpp#L324-L332
>> This barrier doesn't appear to be elided.
>> It seems, in this case the `cmpxchg_barrier` should be used and the `membar_acquire` should be elided. Achieving that would require extra changes - on the C2 side.
>> If this conclusion is correct, should the extra modification be in scope of this PR?
>
>> This barrier doesn't appear to be elided.
> 
> Please post your test case so I understand what is happening.

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;

public class Test {
  int value;

  static final VarHandle handle;
  static {
    try {
      handle = MethodHandles.lookup().findVarHandle(Test.class, "value", int.class);
    } catch(Exception e) {
      throw new RuntimeException(e);
    }
  }

  static void test(Test instance) {
    handle.getAndAddAcquire(instance, 1);
  }

  public static void main(String[] args) {
    Test instance = new Test();
    for (int i = 0; i < 5000000; i++) {
      test(instance);
    }
    System.out.println(instance.value);
  }
}


I'm running it as follows:

java -XX:-TieredCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:+UseLSE -XX:CompileCommand=compileonly,Test::test Test


The output includes:

050
050 +   cmpxchgw R29 = [R10], R11, R13  # (int, weak) if [R10] == R11 then [R10] <-- R13csetw R29, EQ   # R29 <-- (EQ ? 1 : 0)
064
064 +   membar_acquire
        dmb ishld

and

  0x0000ef1d383bee54:   casl    w8, w13, [x10]
  0x0000ef1d383bee58:   cmp     w8, w11
 ;; 0x1F1F1F1F1F1F1F1F
  0x0000ef1d383bee5c:   mov     x8, #0x1f1f1f1f1f1f1f1f         // #2242545357980376863
 ;; } cmpxchg
  0x0000ef1d383bee60:   cset    w29, eq  // eq = none
 ;; membar_acquire
  0x0000ef1d383bee64:   dmb     ishld

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

PR Review Comment: https://git.openjdk.org/jdk/pull/26000#discussion_r2542797500


More information about the hotspot-compiler-dev mailing list