RFR: 8327963: C2: fix construction of memory graph around Initialize node to prevent incorrect execution if allocation is removed [v16]

Emanuel Peter epeter at openjdk.org
Tue Oct 28 17:13:01 UTC 2025


On Tue, 28 Oct 2025 13:38:48 GMT, Roland Westrelin <roland at openjdk.org> wrote:

>> An `Initialize` node for an `Allocate` node is created with a memory
>> `Proj` of adr type raw memory. In order for stores to be captured, the
>> memory state out of the allocation is a `MergeMem` with slices for the
>> various object fields/array element set to the raw memory `Proj` of
>> the `Initialize` node. If `Phi`s need to be created during later
>> transformations from this memory state, The `Phi` for a particular
>> slice gets its adr type from the type of the `Proj` which is raw
>> memory. If during macro expansion, the `Allocate` is found to have no
>> use and so can be removed, the `Proj` out of the `Initialize` is
>> replaced by the memory state on input to the `Allocate`. A `Phi` for
>> some slice for a field of an object will end up with the raw memory
>> state on input to the `Allocate` node. As a result, memory state at
>> the `Phi` is incorrect and incorrect execution can happen.
>> 
>> The fix I propose is, rather than have a single `Proj` for the memory
>> state out of the `Initialize` with adr type raw memory, to use one
>> `Proj` per slice added to the memory state after the `Initalize`. Each
>> of the `Proj` should return the right adr type for its slice. For that
>> I propose having a new type of `Proj`: `NarrowMemProj` that captures
>> the right adr type.
>> 
>> Logic for the construction of the `Allocate`/`Initialize` subgraph is
>> tweaked so the right adr type captured in is own `NarrowMemProj` is
>> added to the memory sugraph. Code that removes an allocation or moves
>> it also has to be changed so it correctly takes the multiple memory
>> projections out of the `Initialize` node into account.
>> 
>> One tricky issue is that when EA split types for a scalar replaceable
>> `Allocate` node:
>> 
>> 1- the adr type captured in the `NarrowMemProj` becomes out of sync
>>   with the type of the slices for the allocation
>>   
>> 2- before EA, the memory state for one particular field out of the
>>   `Initialize` node can be used for a `Store` to the just allocated
>>   object or some other. So we can have a chain of `Store`s, some to
>>   the newly allocated object, some to some other objects, all of them
>>   using the state of `NarrowMemProj` out of the `Initialize`. After
>>   split unique types, the `NarrowMemProj` is for the slice of a
>>   particular allocation. So `Store`s to some other objects shouldn't
>>   use that memory state but the memory state before the `Allocate`.
>>   
>> For that, I added logic to update the adr type of `NarrowMemProj`
>> during split uni...
>
> Roland Westrelin has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains 57 commits:
> 
>  - review
>  - Merge branch 'master' into JDK-8327963
>  - review
>  - Roberto's patches
>  - review
>  - Update src/hotspot/share/opto/macro.cpp
>    
>    Co-authored-by: Roberto Castañeda Lozano <robcasloz at users.noreply.github.com>
>  - Update src/hotspot/share/opto/macro.cpp
>    
>    Co-authored-by: Roberto Castañeda Lozano <robcasloz at users.noreply.github.com>
>  - Update src/hotspot/share/opto/graphKit.cpp
>    
>    Co-authored-by: Roberto Castañeda Lozano <robcasloz at users.noreply.github.com>
>  - Update src/hotspot/share/opto/graphKit.cpp
>    
>    Co-authored-by: Roberto Castañeda Lozano <robcasloz at users.noreply.github.com>
>  - Update src/hotspot/share/opto/multnode.hpp
>    
>    Co-authored-by: Roberto Castañeda Lozano <robcasloz at users.noreply.github.com>
>  - ... and 47 more: https://git.openjdk.org/jdk/compare/96259936...957be06e

@rwestrel Thanks for the updates, it already looks better :)

I had a few minutes to look over the `apply_..` solutions. I left a few comments, and hope that we can make the code just a little slicker still ;)

src/hotspot/share/opto/memnode.cpp line 5484:

> 5482:   };
> 5483:   return apply_to_narrow_mem_projs(filter);
> 5484: }

It seems to me that the upper method is only used by the lower here. Why not just collapse them? It would also reduce the "overloading noise".

src/hotspot/share/opto/memnode.hpp line 1428:

> 1426:   template <class Callback> NarrowMemProjNode* apply_to_narrow_mem_projs(DUIterator& i, Callback callback) const {
> 1427:     return apply_to_narrow_mem_projs_any_iterator<Callback, UsesIterator>(UsesIterator(i, this), callback);
> 1428:   }

Is this one still needed?

src/hotspot/share/opto/multnode.hpp line 141:

> 139: 
> 140:   // Same but for matching _con and _is_io_use
> 141:   template <class Callback> ProjNode* apply_to_projs(Callback callback, uint which_proj, bool is_io_use) const;

Do these need to be `public`? Or could they be `protected`, so they are only available to subtypes?

And do we really need all the variants of `apply_to_projs`, or could we collapse them a little?

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

PR Review: https://git.openjdk.org/jdk/pull/24570#pullrequestreview-3389846667
PR Review Comment: https://git.openjdk.org/jdk/pull/24570#discussion_r2470344461
PR Review Comment: https://git.openjdk.org/jdk/pull/24570#discussion_r2470334385
PR Review Comment: https://git.openjdk.org/jdk/pull/24570#discussion_r2470369879


More information about the hotspot-compiler-dev mailing list