[vectorIntrinsics] RFR: Load store memory segment
Paul Sandoz
psandoz at openjdk.java.net
Wed Mar 30 18:30:03 UTC 2022
On Wed, 30 Mar 2022 00:58:10 GMT, Paul Sandoz <psandoz at openjdk.org> wrote:
>> Hi, may I ask why do we reject loading a vector from a heap `MemorySegment` not backed by a `byte[]`?
>>
>> Thank.
>
>> Hi, may I ask why do we reject loading a vector from a heap `MemorySegment` not backed by a `byte[]`?
>>
>
> I wanted to first keep to the equivalent functionality that is currently supported, then think through the implications of supporting any primitive backing array and what that means regarding alignment and byte order.
>
> MemorySegment now supports misaligned access on primitive arrays for values whose size is larger or smaller than the primitive array element size. However, by default the value layouts disallow that, for example (using the preview API for memory segments), for example:
>
>
> MemorySegment ias = MemorySegment.ofArray(
> new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
>
>
> // Access using element whose natural alignment is wider than the natural primitive array alignment
>
> // java.lang.IllegalArgumentException: Misaligned access at address: 16
> ias.set(JAVA_LONG, 0, 1L);
>
> // No exception
> ias.set(JAVA_LONG.withBitAlignment(8), 0, 1L);
>
> // No exception
> ias.set(JAVA_LONG.withBitAlignment(8), 1, 1L);
>
>
> // Access using element whose natural alignment is smaller than the natural primitive array alignment
>
> // java.lang.IllegalArgumentException: Misaligned access at address: 17
> ias.set(JAVA_SHORT, 1, (short) 1);
>
> // No exception
> ias.set(JAVA_SHORT, 0, (short) 1);
>
> // No exception
> ias.set(JAVA_SHORT.withBitAlignment(8), 1, (short) 1);
> @PaulSandoz I see, however, the problem of alignment here seems a little backward since a byte array is the most unaligned kind of array, so if we support loads, stores from byte arrays then alignment does not need any consideration anymore.
Agreed for `byte[]` arrays. There is not much precedent right now for writing to other primitive arrays, whose element size differs from the vector lane element size. (Not an issue for the Vector API, but alignment does matters for a `byte[]` array when performing atomic access, since we cannot guarantee stable alignment of `byte[]` address across all platforms).
-------------
PR: https://git.openjdk.java.net/panama-vector/pull/187
More information about the panama-dev
mailing list