Skip to content

Commit dbfc8ff

Browse files
committed
Implement JNI upcall for ctx_Tuple_FromArray
1 parent 59af830 commit dbfc8ff

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

graalpython/com.oracle.graal.python.jni/src/hpy_jni.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static JNIEnv* jniEnv;
8888
UPCALL(UnicodeFromJCharArray, SIG_JCHARARRAY, SIG_HPY) \
8989
UPCALL(DictNew, , SIG_HPY) \
9090
UPCALL(ListNew, SIG_SIZE_T, SIG_HPY) \
91+
UPCALL(TupleFromArray, SIG_JLONGARRAY SIG_BOOL, SIG_HPY) \
9192

9293
#define UPCALL(name, jniSigArgs, jniSigRet) static jmethodID jniMethod_ ## name;
9394
ALL_UPCALLS
@@ -267,6 +268,7 @@ static int (*original_NumberCheck)(HPyContext *ctx, HPy h);
267268
static int (*original_TypeCheck)(HPyContext *ctx, HPy h, HPy type);
268269
static void (*original_Close)(HPyContext *ctx, HPy h);
269270
static HPy (*original_UnicodeFromWideChar)(HPyContext *ctx, const wchar_t *arr, HPy_ssize_t size);
271+
static HPy (*original_TupleFromArray)(HPyContext *ctx, HPy *items, HPy_ssize_t nitems);
270272

271273
static void *augment_AsStruct(HPyContext *ctx, HPy h) {
272274
uint64_t bits = toBits(h);
@@ -428,6 +430,17 @@ static HPy augment_UnicodeFromWideChar(HPyContext *ctx, const wchar_t *u, HPy_ss
428430
}
429431
}
430432

433+
static HPy augment_TupleFromArray(HPyContext *ctx, HPy *items, HPy_ssize_t nitems) {
434+
jarray jLongArray = (*jniEnv)->NewLongArray(jniEnv, (jsize) nitems);
435+
jlong *content = (*jniEnv)->GetPrimitiveArrayCritical(jniEnv, jLongArray, 0);
436+
HPy_ssize_t i;
437+
for (i = 0; i < nitems; i++) {
438+
content[i] = (jlong) toBits(items[i]);
439+
}
440+
(*jniEnv)->ReleasePrimitiveArrayCritical(jniEnv, jLongArray, content, 0);
441+
return DO_UPCALL_HPY(CONTEXT_INSTANCE(ctx), TupleFromArray, jLongArray, JNI_FALSE);
442+
}
443+
431444
void initDirectFastPaths(HPyContext *context) {
432445
LOG("%p", context);
433446
context->name = "HPy Universal ABI (GraalVM backend, JNI)";
@@ -437,10 +450,10 @@ void initDirectFastPaths(HPyContext *context) {
437450

438451
original_FloatAsDouble = context->ctx_Float_AsDouble;
439452
context->ctx_Float_AsDouble = augment_FloatAsDouble;
440-
453+
441454
original_LongAsLong = context->ctx_Long_AsLong;
442455
context->ctx_Long_AsLong = augment_LongAsLong;
443-
456+
444457
original_LongAsDouble = context->ctx_Long_AsDouble;
445458
context->ctx_Long_AsDouble = augment_LongAsDouble;
446459

@@ -467,6 +480,9 @@ void initDirectFastPaths(HPyContext *context) {
467480

468481
original_UnicodeFromWideChar = context->ctx_Unicode_FromWideChar;
469482
context->ctx_Unicode_FromWideChar = augment_UnicodeFromWideChar;
483+
484+
original_TupleFromArray = context->ctx_Tuple_FromArray;
485+
context->ctx_Tuple_FromArray = augment_TupleFromArray;
470486
}
471487

472488
void setHPyContextNativeSpace(HPyContext *context, void** nativeSpace) {
@@ -524,11 +540,13 @@ JNIEXPORT jint JNICALL Java_com_oracle_graal_python_builtins_objects_cext_hpy_Gr
524540
#define SIG_SIZE_T "J"
525541
#define SIG_PTR "J"
526542
#define SIG_VOID "V"
543+
#define SIG_BOOL "Z"
527544
#define SIG_INT "I"
528545
#define SIG_LONG "J"
529546
#define SIG_DOUBLE "D"
530547
#define SIG_TRACKER "J"
531548
#define SIG_JCHARARRAY "[C"
549+
#define SIG_JLONGARRAY "[J"
532550

533551
#define UPCALL(name, jniSigArgs, jniSigRet) \
534552
jniMethod_ ## name = (*env)->GetMethodID(env, clazz, "ctx" #name, "(" jniSigArgs ")" jniSigRet); \

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContext.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
import com.oracle.graal.python.builtins.objects.ints.PInt;
188188
import com.oracle.graal.python.builtins.objects.list.PList;
189189
import com.oracle.graal.python.builtins.objects.object.PythonObject;
190+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
190191
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
191192
import com.oracle.graal.python.builtins.objects.type.PythonClass;
192193
import com.oracle.graal.python.builtins.objects.type.SpecialMethodSlot;
@@ -1369,7 +1370,8 @@ public enum Counter {
13691370
UpcallUnicodeFromWideChar,
13701371
UpcallUnicodeFromJCharArray,
13711372
UpcallDictNew,
1372-
UpcallListNew;
1373+
UpcallListNew,
1374+
UpcallTupleFromArray;
13731375

13741376
long count;
13751377

@@ -1535,13 +1537,20 @@ public final long ctxTypeGenericNew(long typeHandle) {
15351537
throw CompilerDirectives.shouldNotReachHere("not implemented");
15361538
}
15371539

1538-
public final void ctxClose(long handle) {
1539-
Counter.UpcallClose.increment();
1540+
/**
1541+
* Close a native handle received from a JNI upcall (hence represented by a Java {code long}).
1542+
*/
1543+
private void closeNativeHandle(long handle) {
15401544
if (GraalHPyBoxing.isBoxedHandle(handle)) {
15411545
getObjectForHPyHandle(GraalHPyBoxing.unboxHandle(handle)).closeAndInvalidate(this);
15421546
}
15431547
}
15441548

1549+
public final void ctxClose(long handle) {
1550+
Counter.UpcallClose.increment();
1551+
closeNativeHandle(handle);
1552+
}
1553+
15451554
public final long ctxDup(long handle) {
15461555
Counter.UpcallDup.increment();
15471556
if (GraalHPyBoxing.isBoxedHandle(handle)) {
@@ -1843,7 +1852,7 @@ public final long ctxListNew(long llen) {
18431852
int len = CastToJavaIntExactNode.getUncached().execute(llen);
18441853
Object[] data = new Object[len];
18451854
Arrays.fill(data, PNone.NONE);
1846-
PList list = PythonObjectFactory.getUncached().createList(data);
1855+
PList list = getSlowPathFactory().createList(data);
18471856
return createHandle(list).getId(this, ConditionProfile.getUncached());
18481857
} catch (PException e) {
18491858
HPyTransformExceptionToNativeNodeGen.getUncached().execute(this, e);
@@ -1852,6 +1861,26 @@ public final long ctxListNew(long llen) {
18521861
}
18531862
}
18541863

1864+
/**
1865+
* Implementation of context function {@code ctx_Tuple_FromArray} (JNI upcall). This method can
1866+
* optionally steal the item handles in order to avoid repeated upcalls just to close them. This
1867+
* is useful to implement, e.g., tuple builder.
1868+
*/
1869+
public final long ctxTupleFromArray(long[] hItems, boolean steal) {
1870+
Counter.UpcallTupleFromArray.increment();
1871+
1872+
Object[] objects = new Object[hItems.length];
1873+
for (int i = 0; i < hItems.length; i++) {
1874+
long hBits = hItems[i];
1875+
objects[i] = HPyAsPythonObjectNodeGen.getUncached().execute(this, hBits);
1876+
if (steal) {
1877+
closeNativeHandle(hBits);
1878+
}
1879+
}
1880+
PTuple tuple = getSlowPathFactory().createTuple(objects);
1881+
return createHandle(tuple).getId(this, ConditionProfile.getUncached());
1882+
}
1883+
18551884
@ExportMessage
18561885
@SuppressWarnings("static-method")
18571886
final boolean hasMembers() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/resources/jni-config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
{"name":"ctxTypeCheck","parameterTypes":["long", "long"] },
2323
{"name":"ctxTypeGenericNew","parameterTypes":["long"] },
2424
{"name":"ctxUnicodeFromWideChar","parameterTypes":["long","long"] },
25-
{"name":"ctxUnicodeFromJCharArray","parameterTypes":["char[]"] }
25+
{"name":"ctxUnicodeFromJCharArray","parameterTypes":["char[]"] },
26+
{"name":"ctxTupleFromArray","parameterTypes":["long[]", "boolean"] }
2627
]
2728
}
2829
]

0 commit comments

Comments
 (0)