Notes on debugging HotSpot's JIT compilation (2023)
jornvernee.github.io
jornvernee.github.io
public class TestJIT {
public static void main(String[] args) {
for (int i = 0; i < 20_000; i++) {
payload();
}
}
public static int payload(int a, int b) {
return a + b;
}
}
shouldn't be optimized into a 'no-op'. The end-effect is the same.You would change behavior of this program
If I put my leg on my desktop tower then I may feel enjoyably warm or if I put some chocolate on my laptop then it may start melting
Also fans will be louder
Computers arent purely abstract, they exist in real world and are affected by it, so lets do not try to pretend otherwise
This goes not only for Java compiler, but many other languages as well.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=8537
>When optimizing code for "dead store removal" the optimizing compiler may remove code necessary for security.
You can say that about any code at all, so no optimising would ever be possible. The program running faster is a side effect after all.
In this case it doesn't happen because to see that the entire operation is dead requires the compiler to inline payload into main, but he says he disabled inlining for that method specifically so it wouldn't happen. Recall that the goal is to see the assembly for a block of code in isolation, not demo what the JVM can do when given free reign.
- If payload is not inlined, the loop can't be optimized away. The fact of iteration itself may be a desirable side-effect (spin-wait/pause) a stricter compiler can't make an assumption about, unlike GCC or Clang
- If payload is inlined, it should be a no-op. If it's not, and its result consumed by an opaque "sink" method, there may be limitations.
On interproc analysis - don't forget you can dynamically load code and access payload through reflection too. This limits certain optimizations that are otherwise legal in AOT compilation. .NET has similar restrictions and corresponding differences when publishing binaries with JIT vs AOT - the former gets to enjoy DynamicPGO (HotSpot kind of optimizations), the latter gets to enjoy frozen world (with exact devirtualization, faster reflection, auto-sealing, etc. but overall not as good as DynamicPGO with guarded devirt, branch reordering, etc.).