Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.
/ jdk17 Public archive

Commit 63bcd33

Browse files
author
Paul Sandoz
committed
8269246: Scoped ByteBuffer vector access
Reviewed-by: mcimadamore
1 parent 3fb28d3 commit 63bcd33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+360
-269
lines changed

src/java.base/share/classes/jdk/internal/misc/X-ScopedMemoryAccess.java.template

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ import java.lang.annotation.RetentionPolicy;
3131
import java.lang.annotation.Target;
3232
import java.lang.ref.Reference;
3333
import java.io.FileDescriptor;
34+
import java.nio.Buffer;
35+
import java.nio.ByteBuffer;
3436

37+
import jdk.internal.access.JavaNioAccess;
3538
import jdk.internal.access.SharedSecrets;
39+
import jdk.internal.access.foreign.MemorySegmentProxy;
3640
import jdk.internal.util.ArraysSupport;
3741
import jdk.internal.vm.annotation.ForceInline;
42+
import jdk.internal.vm.vector.VectorSupport;
3843

3944

4045
/**
@@ -334,6 +339,126 @@ public class ScopedMemoryAccess {
334339
Reference.reachabilityFence(scope);
335340
}
336341
}
342+
343+
// ByteBuffer vector access ops
344+
345+
// Buffer access constants, to be initalized when required.
346+
// Avoids a null value for NIO_ACCESS, due to class initalization dependencies
347+
static final class BufferAccess {
348+
// Buffer.address
349+
static final long BUFFER_ADDRESS
350+
= UNSAFE.objectFieldOffset(Buffer.class, "address");
351+
352+
// ByteBuffer.hb
353+
static final long BYTE_BUFFER_HB
354+
= UNSAFE.objectFieldOffset(ByteBuffer.class, "hb");
355+
356+
@ForceInline
357+
static Object bufferBase(ByteBuffer bb) {
358+
return UNSAFE.getReference(bb, BYTE_BUFFER_HB);
359+
}
360+
361+
@ForceInline
362+
static long bufferAddress(ByteBuffer bb, long offset) {
363+
return UNSAFE.getLong(bb, BUFFER_ADDRESS) + offset;
364+
}
365+
366+
static final JavaNioAccess NIO_ACCESS = SharedSecrets.getJavaNioAccess();
367+
368+
@ForceInline
369+
static ScopedMemoryAccess.Scope scope(ByteBuffer bb) {
370+
MemorySegmentProxy segmentProxy = NIO_ACCESS.bufferSegment(bb);
371+
return segmentProxy != null ?
372+
segmentProxy.scope() : null;
373+
}
374+
}
375+
376+
@ForceInline
377+
public static
378+
<V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>>
379+
V loadFromByteBuffer(Class<? extends V> vmClass, Class<E> e, int length,
380+
ByteBuffer bb, int offset,
381+
S s,
382+
VectorSupport.LoadOperation<ByteBuffer, V, E, S> defaultImpl) {
383+
try {
384+
return loadFromByteBufferScoped(
385+
BufferAccess.scope(bb),
386+
vmClass, e, length,
387+
bb, offset,
388+
s,
389+
defaultImpl);
390+
} catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
391+
throw new IllegalStateException("This segment is already closed");
392+
}
393+
}
394+
395+
@Scoped
396+
@ForceInline
397+
private static
398+
<V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>>
399+
V loadFromByteBufferScoped(ScopedMemoryAccess.Scope scope,
400+
Class<? extends V> vmClass, Class<E> e, int length,
401+
ByteBuffer bb, int offset,
402+
S s,
403+
VectorSupport.LoadOperation<ByteBuffer, V, E, S> defaultImpl) {
404+
try {
405+
if (scope != null) {
406+
scope.checkValidState();
407+
}
408+
409+
return VectorSupport.load(vmClass, e, length,
410+
BufferAccess.bufferBase(bb), BufferAccess.bufferAddress(bb, offset),
411+
bb, offset, s,
412+
defaultImpl);
413+
} finally {
414+
Reference.reachabilityFence(scope);
415+
}
416+
}
417+
418+
@ForceInline
419+
public static
420+
<V extends VectorSupport.Vector<E>, E>
421+
void storeIntoByteBuffer(Class<? extends V> vmClass, Class<E> e, int length,
422+
V v,
423+
ByteBuffer bb, int offset,
424+
VectorSupport.StoreVectorOperation<ByteBuffer, V> defaultImpl) {
425+
try {
426+
storeIntoByteBufferScoped(
427+
BufferAccess.scope(bb),
428+
vmClass, e, length,
429+
v,
430+
bb, offset,
431+
defaultImpl);
432+
} catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
433+
throw new IllegalStateException("This segment is already closed");
434+
}
435+
}
436+
437+
@Scoped
438+
@ForceInline
439+
private static
440+
<V extends VectorSupport.Vector<E>, E>
441+
void storeIntoByteBufferScoped(ScopedMemoryAccess.Scope scope,
442+
Class<? extends V> vmClass, Class<E> e, int length,
443+
V v,
444+
ByteBuffer bb, int offset,
445+
VectorSupport.StoreVectorOperation<ByteBuffer, V> defaultImpl) {
446+
try {
447+
if (scope != null) {
448+
scope.checkValidState();
449+
}
450+
451+
VectorSupport.store(vmClass, e, length,
452+
BufferAccess.bufferBase(bb), BufferAccess.bufferAddress(bb, offset),
453+
v,
454+
bb, offset,
455+
defaultImpl);
456+
} finally {
457+
Reference.reachabilityFence(scope);
458+
}
459+
}
460+
461+
337462
// typed-ops here
338463

339464
// Note: all the accessor methods defined below take advantage of argument type profiling

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/ByteVector.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.function.Function;
3434
import java.util.function.UnaryOperator;
3535

36+
import jdk.internal.misc.ScopedMemoryAccess;
3637
import jdk.internal.misc.Unsafe;
3738
import jdk.internal.vm.annotation.ForceInline;
3839
import jdk.internal.vm.vector.VectorSupport;
@@ -3562,15 +3563,14 @@ a, byteArrayAddress(a, offset),
35623563
final
35633564
ByteVector fromByteBuffer0Template(ByteBuffer bb, int offset) {
35643565
ByteSpecies vsp = vspecies();
3565-
return VectorSupport.load(
3566-
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3567-
bufferBase(bb), bufferAddress(bb, offset),
3568-
bb, offset, vsp,
3569-
(buf, off, s) -> {
3570-
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3571-
return s.ldOp(wb, off,
3572-
(wb_, o, i) -> wb_.get(o + i * 1));
3573-
});
3566+
return ScopedMemoryAccess.loadFromByteBuffer(
3567+
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3568+
bb, offset, vsp,
3569+
(buf, off, s) -> {
3570+
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3571+
return s.ldOp(wb, off,
3572+
(wb_, o, i) -> wb_.get(o + i * 1));
3573+
});
35743574
}
35753575

35763576
// Unchecked storing operations in native byte order.
@@ -3613,15 +3613,14 @@ a, byteArrayAddress(a, offset),
36133613
final
36143614
void intoByteBuffer0(ByteBuffer bb, int offset) {
36153615
ByteSpecies vsp = vspecies();
3616-
VectorSupport.store(
3617-
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3618-
bufferBase(bb), bufferAddress(bb, offset),
3619-
this, bb, offset,
3620-
(buf, off, v) -> {
3621-
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3622-
v.stOp(wb, off,
3623-
(wb_, o, i, e) -> wb_.put(o + i * 1, e));
3624-
});
3616+
ScopedMemoryAccess.storeIntoByteBuffer(
3617+
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3618+
this, bb, offset,
3619+
(buf, off, v) -> {
3620+
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3621+
v.stOp(wb, off,
3622+
(wb_, o, i, e) -> wb_.put(o + i * 1, e));
3623+
});
36253624
}
36263625

36273626
// End of low-level memory operations.

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/DoubleVector.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.function.Function;
3434
import java.util.function.UnaryOperator;
3535

36+
import jdk.internal.misc.ScopedMemoryAccess;
3637
import jdk.internal.misc.Unsafe;
3738
import jdk.internal.vm.annotation.ForceInline;
3839
import jdk.internal.vm.vector.VectorSupport;
@@ -3173,15 +3174,14 @@ a, byteArrayAddress(a, offset),
31733174
final
31743175
DoubleVector fromByteBuffer0Template(ByteBuffer bb, int offset) {
31753176
DoubleSpecies vsp = vspecies();
3176-
return VectorSupport.load(
3177-
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3178-
bufferBase(bb), bufferAddress(bb, offset),
3179-
bb, offset, vsp,
3180-
(buf, off, s) -> {
3181-
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3182-
return s.ldOp(wb, off,
3183-
(wb_, o, i) -> wb_.getDouble(o + i * 8));
3184-
});
3177+
return ScopedMemoryAccess.loadFromByteBuffer(
3178+
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3179+
bb, offset, vsp,
3180+
(buf, off, s) -> {
3181+
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3182+
return s.ldOp(wb, off,
3183+
(wb_, o, i) -> wb_.getDouble(o + i * 8));
3184+
});
31853185
}
31863186

31873187
// Unchecked storing operations in native byte order.
@@ -3224,15 +3224,14 @@ a, byteArrayAddress(a, offset),
32243224
final
32253225
void intoByteBuffer0(ByteBuffer bb, int offset) {
32263226
DoubleSpecies vsp = vspecies();
3227-
VectorSupport.store(
3228-
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3229-
bufferBase(bb), bufferAddress(bb, offset),
3230-
this, bb, offset,
3231-
(buf, off, v) -> {
3232-
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3233-
v.stOp(wb, off,
3234-
(wb_, o, i, e) -> wb_.putDouble(o + i * 8, e));
3235-
});
3227+
ScopedMemoryAccess.storeIntoByteBuffer(
3228+
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3229+
this, bb, offset,
3230+
(buf, off, v) -> {
3231+
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3232+
v.stOp(wb, off,
3233+
(wb_, o, i, e) -> wb_.putDouble(o + i * 8, e));
3234+
});
32363235
}
32373236

32383237
// End of low-level memory operations.

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/FloatVector.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.function.Function;
3434
import java.util.function.UnaryOperator;
3535

36+
import jdk.internal.misc.ScopedMemoryAccess;
3637
import jdk.internal.misc.Unsafe;
3738
import jdk.internal.vm.annotation.ForceInline;
3839
import jdk.internal.vm.vector.VectorSupport;
@@ -3160,15 +3161,14 @@ a, byteArrayAddress(a, offset),
31603161
final
31613162
FloatVector fromByteBuffer0Template(ByteBuffer bb, int offset) {
31623163
FloatSpecies vsp = vspecies();
3163-
return VectorSupport.load(
3164-
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3165-
bufferBase(bb), bufferAddress(bb, offset),
3166-
bb, offset, vsp,
3167-
(buf, off, s) -> {
3168-
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3169-
return s.ldOp(wb, off,
3170-
(wb_, o, i) -> wb_.getFloat(o + i * 4));
3171-
});
3164+
return ScopedMemoryAccess.loadFromByteBuffer(
3165+
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3166+
bb, offset, vsp,
3167+
(buf, off, s) -> {
3168+
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3169+
return s.ldOp(wb, off,
3170+
(wb_, o, i) -> wb_.getFloat(o + i * 4));
3171+
});
31723172
}
31733173

31743174
// Unchecked storing operations in native byte order.
@@ -3211,15 +3211,14 @@ a, byteArrayAddress(a, offset),
32113211
final
32123212
void intoByteBuffer0(ByteBuffer bb, int offset) {
32133213
FloatSpecies vsp = vspecies();
3214-
VectorSupport.store(
3215-
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3216-
bufferBase(bb), bufferAddress(bb, offset),
3217-
this, bb, offset,
3218-
(buf, off, v) -> {
3219-
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3220-
v.stOp(wb, off,
3221-
(wb_, o, i, e) -> wb_.putFloat(o + i * 4, e));
3222-
});
3214+
ScopedMemoryAccess.storeIntoByteBuffer(
3215+
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3216+
this, bb, offset,
3217+
(buf, off, v) -> {
3218+
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3219+
v.stOp(wb, off,
3220+
(wb_, o, i, e) -> wb_.putFloat(o + i * 4, e));
3221+
});
32233222
}
32243223

32253224
// End of low-level memory operations.

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/IntVector.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.function.Function;
3434
import java.util.function.UnaryOperator;
3535

36+
import jdk.internal.misc.ScopedMemoryAccess;
3637
import jdk.internal.misc.Unsafe;
3738
import jdk.internal.vm.annotation.ForceInline;
3839
import jdk.internal.vm.vector.VectorSupport;
@@ -3269,15 +3270,14 @@ a, byteArrayAddress(a, offset),
32693270
final
32703271
IntVector fromByteBuffer0Template(ByteBuffer bb, int offset) {
32713272
IntSpecies vsp = vspecies();
3272-
return VectorSupport.load(
3273-
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3274-
bufferBase(bb), bufferAddress(bb, offset),
3275-
bb, offset, vsp,
3276-
(buf, off, s) -> {
3277-
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3278-
return s.ldOp(wb, off,
3279-
(wb_, o, i) -> wb_.getInt(o + i * 4));
3280-
});
3273+
return ScopedMemoryAccess.loadFromByteBuffer(
3274+
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3275+
bb, offset, vsp,
3276+
(buf, off, s) -> {
3277+
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3278+
return s.ldOp(wb, off,
3279+
(wb_, o, i) -> wb_.getInt(o + i * 4));
3280+
});
32813281
}
32823282

32833283
// Unchecked storing operations in native byte order.
@@ -3320,15 +3320,14 @@ a, byteArrayAddress(a, offset),
33203320
final
33213321
void intoByteBuffer0(ByteBuffer bb, int offset) {
33223322
IntSpecies vsp = vspecies();
3323-
VectorSupport.store(
3324-
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3325-
bufferBase(bb), bufferAddress(bb, offset),
3326-
this, bb, offset,
3327-
(buf, off, v) -> {
3328-
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3329-
v.stOp(wb, off,
3330-
(wb_, o, i, e) -> wb_.putInt(o + i * 4, e));
3331-
});
3323+
ScopedMemoryAccess.storeIntoByteBuffer(
3324+
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
3325+
this, bb, offset,
3326+
(buf, off, v) -> {
3327+
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
3328+
v.stOp(wb, off,
3329+
(wb_, o, i, e) -> wb_.putInt(o + i * 4, e));
3330+
});
33323331
}
33333332

33343333
// End of low-level memory operations.

0 commit comments

Comments
 (0)