Skip to content

Instantly share code, notes, and snippets.

@navyxliu
Created January 30, 2023 23:04
Show Gist options
  • Save navyxliu/cb32d2990089d097705edc716a4c3c50 to your computer and use it in GitHub Desktop.
Save navyxliu/cb32d2990089d097705edc716a4c3c50 to your computer and use it in GitHub Desktop.
Example3_3.java
// java -ea -Xms16m -Xmx16M -XX:+UnlockExperimentalVMOptions -XX:-UseOnStackReplacement -XX:+PrintEscapeAnalysis -XX:+PrintEliminateAllocations -XX:+UseEpsilonGC -XX:-UseTLAB -XX:+DoPartialEscapeAnalysis -XX:CompileCommand=compileonly,Example3_3::foo -XX:CompileCommand=dontinline,Example3_3::blackhole -XX:CompileCommand=quiet -Xbatch Example3_3
class Example3_3 {
// use volatile to force c2 to load it again
volatile int value;
Example3_3(int value) {
this.value = value;
}
void blackhole(int field) {
assert field == 42 :" wrong answer";
}
public static void foo(int value) {
var x = new Example3_3(value);
x.blackhole(x.value);
return;
}
public static void main(String[] args) {
long iterations = 0;
try {
while (true) {
foo(42);
iterations++;
}
} finally {
System.err.println("Epsilon Test: " + iterations);
}
}
}
@navyxliu
Copy link
Author

There are two key factors in this example

  1. Example3_3::value is volatile, so c2 will use MemBarAcquire for the argument of blackhole(int)
  2. Example3_3::blackhole is not inlined. it triggers the materialization of x at function call.

@navyxliu
Copy link
Author

I have a patch to handle this, but I need more time to polish it.
put aside it because it's not a blocker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment