问题描述
我们知道,一些 JIT 允许对对象初始化重新排序,例如,
可以分解为以下步骤:
JIT 编译器可能会重新排序如下:
即step2和step3可以被JIT编译器重新排序.尽管这在理论上有效重新排序,但我无法在 x86 平台下使用 Hotspot(jdk1.7) 重现它.
那么,Hotspot JIT 编译器是否有任何可以重现的指令重新排序?
<小时>更新:我做了 其中节点的输入是节点操作的输入.每个节点根据其输入和操作定义一个值,并且该值在所有输出边上都可用.很明显,编译器看不到指针和整数存储节点之间的任何区别,因此唯一限制它的就是内存屏障.结果,为了减少寄存器压力,目标代码大小或其他编译器决定以这种奇怪(从用户的角度)顺序在基本块中调度指令.您可以使用以下选项(在 fastdebug build 中可用)在 Hotspot 中使用指令调度:-XX:+StressLCM
和 -XX:+StressGCM
.
As we know, some JIT allows reordering for object initialization, for example,
can be decomposed into below steps:
JIT compiler may reorder it as below:
namely, step2 and step3 can be reordered by JIT compiler. Even though this is theoretically valid reordering, I was unable to reproduce it with Hotspot(jdk1.7) under x86 platform.
So, Is there any instruction reordering done by the Hotspot JIT comipler that can be reproduced?
Update: I did the test on my machine(Linux x86_64,JDK 1.8.0_40, i5-3210M ) using below command:
and I can see the tool reported something like:
[1] 5 ACCEPTABLE The object is published, at least 1 field is visible.
That meant an observer thread saw an uninitialized instance of MyObject.
However,I did NOT see assembly code generated like @Ivan's:
There seems to be no compiler reordering here.
Update2: @Ivan corrected me. I used wrong JIT command to capture the assembly code.After fixing this error, I can grap below assembly code:
Apparently, the compiler did the reordering which caused an unsafe publication.
You can reproduce any compiler reordering. The right question is - which tool to use for this. In order to see compiler reordering - you have to follow down to assembly level with JITWatch(as it uses HotSpot's assembly log output) or JMH with LinuxPerfAsmProfiler.
Let's consider the following benchmark based on JMH:
Please note that array access is unordered. On my machine for method with global variable sum
assembler output is:
but for method with local variable sum
access pattern was changed:
You can play with c1 compiler optimizations c1_RangeCheckElimination
Update:
It is extremely hard to see only compiler reorderings from user's point of view, because you have to run bilions of samples to catch the racy behavior. Also it is important to separate compiler and hardware issues, for instance, weakly-ordered hardware like POWER can change behavior. Let's start from the right tool: jcstress - an experimental harness and a suite of tests to aid the research in the correctness of concurrency support in the JVM, class libraries, and hardware. Here is a reproducer where the instruction scheduler may decide to emit a few field stores, then publish the reference, then emit the rest of the field stores(also you can read about safe publications and instruction scheduling here). In some cases on my machine with Linux x86_64, JDK 1.8.0_60, i5-4300M compiler generates the following code:
but sometimes:
Update 2:
Regarding to the question about performance benefits. In our case, this optimization(reordering) does not bring meaningful performance benefit it's just a side effect of the compiler's implementation. HotSpot uses sea of nodes
graph to model data and control flow(you can read about graph-based intermediate representation here). The following picture shows the IR graph for our example(-XX:+PrintIdeal -XX:PrintIdealGraphLevel=1 -XX:PrintIdealGraphFile=graph.xml
options + ideal graph visualizer):
where inputs to a node are inputs to the node's operation. Each node defines a value based on it's inputs and operation, and that value is available on all output edges. It is obvious that compiler does not see any difference between pointer and integer store nodes so the only thing that limits it - is memory barrier. As a result in order to reduce register pressure, target code size or something else compiler decides to schedule instructions within the basic block in this strange(from user's point of view) order. You can play with instruction scheduling in Hotspot by using the following options(available in fastdebug build): -XX:+StressLCM
and -XX:+StressGCM
.
这篇关于Hotspot JIT 编译器是否有任何可以重现的指令重新排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!