# Issue
A few test failed intermittently with -Xcomp on Mac due to an unschedulable 
graph in C2:
- valhalla/valuetypes/WeakReferenceTest.java
- valhalla/valuetypes/ProxyTest.java
- valhalla/valuetypes/ObjectNewInstance.java
- valhalla/valuetypes/ObjectMethodsViaCondy.java

# Causes

The origin of the issue seems to be coming from strength reduction 
(`process_late_inline_calls_no_inline`) where we replace virtual and MH calls 
with direct calls. 
https://github.com/dafedafe/valhalla/blob/75e2dd95df5d847d7d6e35a23016d22705681cf4/src/hotspot/share/opto/compile.cpp#L3072
If the return type of the methods are not loaded, we add a call to runtime's 
`store_inline_type_fields_to_buf` right after the actual method call, to save 
the scalarized return into a oop. This happens first for the caller at parse 
time and then for the callee when strength-reducing the virtual call to a 
direct one. The return projections of the inline fields of the call are added 
to `store_inline_type_fields_to_buf` and its oop projection is then added as 
input to the other `store_inline_type_fields_to_buf` which fundamentally leaves 
the graph around it in an awkward state.

If this happens in combination with loop unswitching it can lead to a graph not 
being schedulable, which is what happens in the failure of this issue, where we 
have:
* a virtual call with a following `store_inline_type_fields_to_buf` (1) in a 
loop.
* the loop undergoes unswitching, which creates 2 copies of the body (including 
a copy of the virtual call). All outputs of the new virtual call are phi-d with 
the one of the other path as input of the `store_inline_type_fields_to_buf`.
* the new copy of the virtual call is later replaced with a direct call: the 
creation of the new direct call adds a `store_inline_type_fields_to_buf` (2) 
right after the direct call. All the inline type return values of the call 
being inlined are removed, so the phis now only have one input and are removed 
by a later GVN pass.
<img width="600" alt="Screenshot 2025-11-26 at 10 33 51" 
src="https://github.com/user-attachments/assets/35e947c2-350e-414f-9d44-72559d480a88";
 />

* this creates an issue later during GCM since the 
`store_inline_type_fields_to_buf` (1) call is logically not dominated by any of 
the 2 sides of the unswitched loop and lands in a separate dominance path of 
the arguments whose phis have been removed (which are still dominated by the 
original virtual call).

# Solution

The issue happens only when strength-reducing to a direct call and the return 
type is not loaded. So, there seems to be 2 conceivable fixes: either we avoid 
strength-reduction if the type is not loaded or we avoid creating the second 
`store_inline_type_fields_to_buf` and rewire the output projections of the 
strength-reduced call to the output of the projections of the original virtual 
call.
By choosing the first solution we potentially skip a useful optimization (in 
particular if the return value is always null, which  won't trigger the 
deoptimization). So, the rewiring solution seems to be the best option in this 
case.

# Testing
* repeated testing with failing tests (same conditions)
* replay of failing compilations
* Tier 1-3

Unfortunately creating a regression test that would consistently trigger the 
specific issue with unloaded return type and strength-reduction to direct call 
has proven to be unviable.

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

Commit messages:
 - JDK-8369045: remove new line
 - Revert "JDK-8369045: add bool for rt_loaded"
 - JDK-8369045: add comment
 - JDK-8369045: add bool for rt_loaded
 - JDK-8369045:  replace result projections instead of no strength reduction
 - JDK-8369045: [lworld] valhalla/valuetypes/WeakReferenceTest.java has an 
unschedulable graph

Changes: https://git.openjdk.org/valhalla/pull/1768/files
  Webrev: https://webrevs.openjdk.org/?repo=valhalla&pr=1768&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8369045
  Stats: 49 lines in 3 files changed: 28 ins; 3 del; 18 mod
  Patch: https://git.openjdk.org/valhalla/pull/1768.diff
  Fetch: git fetch https://git.openjdk.org/valhalla.git pull/1768/head:pull/1768

PR: https://git.openjdk.org/valhalla/pull/1768

Reply via email to