On Thu, 5 Nov 2026 15:17:04 GMT, Quan Anh Mai <[email protected]> wrote:
> ## Problem
>
> When C2 parses a variable, it tries to be smart about the type of that
> variable. If a method parameter declares that it is of type `A`, and `A` is
> an abstract class that only has a single concrete subclass `B`, then C2
> eagerly assumes that the parameter must be of type `B`. This property, in the
> presence of concurrent class loading/unloading, does not always hold, as the
> VM can always load another concrete subclass `C` of `A` that is unrelated to
> `B`. To avoid incorrect execution arised from this issue, C2 relies on
> `dependencies`, which encodes various kinds of assumptions the compiler made.
> Among those, we have an assumption called `unique_concrete_subclass`, which
> will be invalidated when `C` is loaded, triggering the deoptimization and
> invalidation of the aforementioned compiled method.
>
> However, this mechanism is both inadequate and not timely. The main issue is
> that the concurrent load of `C` may not only happen after the compilation has
> finished, but also during the compilation process. This leads to CHA
> computation giving contradictory results when called at different time in the
> compilation process. There are 2 examples which were reported:
>
> 1.
>
> if (b) {
> A a1 = ...;
> } else {
> C a2 = ...;
> }
> A a = Phi(a1, a2);
>
> If `A a = Phi(a1, a2)` is parsed before `C a2 = ...`, and `C` has not been
> loaded then, the compiler will assume that `a` must be of type `B`. This
> leads to a `Phi` of type `B` with one of its inputs being a node of type `C`
> which is unrelated. This graph shape is nonsense, and all kinds of issues can
> appear from various optimization passes which do not expect such a
> conflicting graph.
>
> 2.
>
> A a = call();
>
> If when `a` is parsed, `C` has not been loaded, the compiler will assume `a`
> is of type `B`. However, if `call` is late-inlined, and it turns out that the
> returned value is of type `C`. All uses of `a` will suddenly have their input
> changed from a `B` to a `C`, which may also lead to unexpected failures.
>
> ## Overview of this PR
>
> This PR tries to detect CHA inconsistencies, and triggers a compilation
> failure when such a paradox happens. The core invariant that needs holding is
> that, given type `X` and `Y`, if `X` is a subtype of `Y`, then the result of
> narrowing `X` should also be a subtype of the result of narrowing `Y`. This
> invariant does not need to hold in the VM, it only needs to hold in the local
> `ciEnv`, which is the local view of the compiler with respect to the class
> hierarchy.
>
> In some...
I very much like the idea to guard CI view of class hierarchy from
inconsistencies caused by its incremental construction.
> In some cases, we can avoid such a paradox from arising, such as when CHA
> would load a class that would lead to a paradoxical hierarchy view. In those
> cases, we can do a pre-check and avoid loading that class into the ciEnv, so
> that the compiler is not exposed to that paradox.
Can you elaborate what do you mean by "avoid loading that class into the
ciEnv"? Does it mean "skip loading" or "returning unloaded ciKlass"?
> In other cases, loading the class into the ciEnv is inevitable, as it appears
> in the class file.
Can you give more details about problematic cases/scenarios?
-------------
PR Comment: https://git.openjdk.org/jdk/pull/33753#issuecomment-7055975398