[foreign-memaccess] RFR: JDK-8242011: Add support for memory address combinator

Maurizio Cimadamore mcimadamore at openjdk.java.net
Thu Apr 2 00:27:27 UTC 2020


On Thu, 2 Apr 2020 00:20:19 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> Following recent changes (see https://github.com/openjdk/panama-foreign/pull/77)which backport some of the goodies in
> foreign-abi into foreign-memaccess, this patch brings support for a VarHandle combinator which turns a regular memory
> access var handle into a var handle which gets/sets a MemoryAddress (e.g. instead of a long).
> This patch also addresses the general problem of the co-existence between combinators in MemoryHandles and the general
> var handle combinators in MethodHandles.
> This coexistence has always been tricky, because the combinators in MemoryHandles like to create a new 'flattened'
> memory access var handle, which provides best possible performances, and also performs certain alignment checks
> upfront.  However, if a memory access handle is adapted (e.g. the carrier type is changed) this simplistic approach no
> longer works, as, by reconstructing the memory access var handle from scratch we will also lose all the adaptations.
> The solution is to detect as to wheter the target handle is a "direct" memory access var handle or not (special
> provisions are made for the ubiquitous adaptation added on all memory access var handle creation as a workaround for
> JDK-8237349). If that's the case, and, if the stride or the offset matches the alignment constraint, then we go the
> fast/flattened path and we can adapt by reconstructing the handle from scratch. If any of these two conditions are not
> met (there's complex adaption that would be dropped on the floor, or the offset/stride parameter doesn't match with
> alignment constraint), then the slow path is taken - the target var handle is kept around and is adapted using the
> standard combinator API. This leads to a less performant VarHandle (because, unfortunately, calling addOffset()
> currently breaks C2 optimizations), but also guarantees that alignment constraints will be checked in full (this is
> because the memory access var handle implementation always checks the alignment of the base address passed to it - only
> for the offset part is the alignment check skipped - on the basis that this has already been verified by
> construction).  The result is that we can lift a lot of the restrictions surrounding the combinators in MemoryHandles;
> such combinators can now work on _any_ var handle (provided the var handle has a first coordinate type of type
> MemoryAddress). Also, I've lifted the alignment restrictions, since these will either be enforced dynamically (by
> taking the slow path), or they won't be enforced because we have already statically proven that the constraints are
> satisfied (fast path).  I've also took the chance to rename some of the classes surrounding memory access var handles
> to use the "memory access var handle" terminology, which is the one that stuck (currently, some classes use the word
> "address var hande" which is ambiguous).

src/java.base/share/classes/java/lang/invoke/MethodHandles.java line 5258:

> 5257:
> 5258:         List<Class<?>> newCoordinates = new ArrayList<>(targetCoordinates);
> 5259:         newCoordinates.remove(pos);

This was a bug I discovered in the collectCoordinates implementation - the old coordinate was not removed - leading to
WrongMethodTypeException.

src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemoryAddress.java line 133:

> 132:      */
> 133:     MemoryAddress NULL = MemorySegmentImpl.NOTHING.baseAddress();
> 134:

In the end, this was added back :-)

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

PR: https://git.openjdk.java.net/panama-foreign/pull/84


More information about the panama-dev mailing list