Skip to content

Commit f5e2a33

Browse files
committed
[GR-35271] Migrate some NFI usages to newer interface.
PullRequest: graalpython/2042
2 parents a4b44eb + dfc5014 commit f5e2a33

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
import com.oracle.truffle.api.source.Source;
239239
import com.oracle.truffle.api.source.Source.SourceBuilder;
240240
import com.oracle.truffle.llvm.spi.NativeTypeLibrary;
241+
import com.oracle.truffle.nfi.api.SignatureLibrary;
241242

242243
import sun.misc.Unsafe;
243244

@@ -1122,6 +1123,12 @@ private static long castLong(Object value) {
11221123
}
11231124
}
11241125

1126+
private static Object evalNFI(PythonContext context, String source, String name) {
1127+
Source src = Source.newBuilder("nfi", source, name).build();
1128+
CallTarget ct = context.getEnv().parseInternal(src);
1129+
return ct.call();
1130+
}
1131+
11251132
@ExportMessage
11261133
final void toNative() {
11271134
if (!isPointer()) {
@@ -1136,14 +1143,17 @@ final void toNative() {
11361143
}
11371144
if (useNativeFastPaths) {
11381145
PythonContext context = getContext();
1139-
Source src = Source.newBuilder("nfi", "load \"" + getJNILibrary() + "\"", "load " + PythonContext.PYTHON_JNI_LIBRARY_NAME).build();
1140-
CallTarget lib = context.getEnv().parseInternal(src);
11411146
InteropLibrary interop = InteropLibrary.getUncached();
1147+
SignatureLibrary signatures = SignatureLibrary.getUncached();
11421148
try {
1143-
Object rlib = lib.call();
1144-
Object augmentFunction = interop.invokeMember(interop.readMember(rlib, "initDirectFastPaths"), "bind", "(POINTER):VOID");
1145-
interop.execute(augmentFunction, nativePointer);
1146-
setNativeSpaceFunction = interop.invokeMember(interop.readMember(rlib, "setHPyContextNativeSpace"), "bind", "(POINTER, SINT64):VOID");
1149+
Object rlib = evalNFI(context, "load \"" + getJNILibrary() + "\"", "load " + PythonContext.PYTHON_JNI_LIBRARY_NAME);
1150+
1151+
Object augmentSignature = evalNFI(context, "(POINTER):VOID", "hpy-nfi-signature");
1152+
Object augmentFunction = interop.readMember(rlib, "initDirectFastPaths");
1153+
signatures.call(augmentSignature, augmentFunction, nativePointer);
1154+
1155+
Object setNativeSpaceSignature = evalNFI(context, "(POINTER, SINT64):VOID", "hpy-nfi-signature");
1156+
setNativeSpaceFunction = signatures.bind(setNativeSpaceSignature, interop.readMember(rlib, "setHPyContextNativeSpace"));
11471157

11481158
/*
11491159
* Allocate a native array for the native space pointers of HPy objects and

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIPosixSupport.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import com.oracle.truffle.api.nodes.LanguageInfo;
108108
import com.oracle.truffle.api.nodes.Node;
109109
import com.oracle.truffle.api.source.Source;
110+
import com.oracle.truffle.nfi.api.SignatureLibrary;
110111
import com.oracle.truffle.llvm.api.Toolchain;
111112

112113
import sun.misc.Unsafe;
@@ -372,9 +373,15 @@ private static void loadFunction(NFIPosixSupport posix, Object library, PosixNat
372373
Object unbound;
373374
try {
374375
InteropLibrary interop = InteropLibrary.getUncached();
376+
SignatureLibrary sigs = SignatureLibrary.getUncached();
377+
378+
String sig = String.format("with %s %s", posix.nfiBackend, function.signature);
379+
Source sigSrc = Source.newBuilder("nfi", sig, "posix-nfi-signature").internal(true).build();
380+
Object signature = posix.context.getEnv().parseInternal(sigSrc).call();
381+
375382
unbound = interop.readMember(library, function.name());
376-
posix.cachedFunctions.set(function.ordinal(), interop.invokeMember(unbound, "bind", function.signature));
377-
} catch (UnsupportedMessageException | UnknownIdentifierException | ArityException | UnsupportedTypeException e) {
383+
posix.cachedFunctions.set(function.ordinal(), sigs.bind(signature, unbound));
384+
} catch (UnsupportedMessageException | UnknownIdentifierException e) {
378385
throw CompilerDirectives.shouldNotReachHere(function.name(), e);
379386
}
380387
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLibrary.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.truffle.api.library.CachedLibrary;
6363
import com.oracle.truffle.api.profiles.ValueProfile;
6464
import com.oracle.truffle.api.source.Source;
65+
import com.oracle.truffle.nfi.api.SignatureLibrary;
6566

6667
/**
6768
* Wraps a native library loaded via NFI and provides caching for functions looked up in the
@@ -174,7 +175,7 @@ private Object getCachedFunction(PythonContext context, NativeFunction function)
174175
// This should be a one-off thing for each context
175176
CompilerDirectives.transferToInterpreter();
176177
synchronized (this) {
177-
dummy = getFunction(lib, function);
178+
dummy = getFunction(context, lib, function);
178179
// it is OK to overwrite cachedFunctions[functionIndex] that may have been
179180
// written from another thread: no need to double check that it's still null.
180181
// dummy is volatile, the object must be fully initialized at this point
@@ -187,15 +188,21 @@ private Object getCachedFunction(PythonContext context, NativeFunction function)
187188
private Object getFunction(PythonContext context, NativeFunction function) {
188189
CompilerAsserts.neverPartOfCompilation();
189190
Object lib = getCachedLibrary(context);
190-
return getFunction(lib, function);
191+
return getFunction(context, lib, function);
191192
}
192193

193-
private Object getFunction(Object lib, NativeFunction function) {
194+
private Object parseSignature(PythonContext context, String signature) {
195+
Source sigSource = Source.newBuilder("nfi", nfiBackend.withClause + signature, "python-nfi-signature").build();
196+
return context.getEnv().parseInternal(sigSource).call();
197+
}
198+
199+
private Object getFunction(PythonContext context, Object lib, NativeFunction function) {
194200
CompilerAsserts.neverPartOfCompilation();
195201
try {
202+
Object signature = parseSignature(context, function.signature());
196203
Object symbol = cachedLibraryInterop.readMember(lib, function.name());
197-
return InteropLibrary.getUncached().invokeMember(symbol, "bind", function.signature());
198-
} catch (UnsupportedMessageException | UnknownIdentifierException | ArityException | UnsupportedTypeException e) {
204+
return SignatureLibrary.getUncached().bind(signature, symbol);
205+
} catch (UnsupportedMessageException | UnknownIdentifierException e) {
199206
throw new IllegalStateException(String.format("Cannot load symbol '%s' from the internal shared library '%s'", function.name(), name), e);
200207
}
201208
}
@@ -250,9 +257,9 @@ protected Object callUncached(PythonContext context, NativeFunction f, Object...
250257
final Object lib = getCachedLibrary(context);
251258
if (lib != null) {
252259
try {
260+
Object signature = parseSignature(context, f.signature());
253261
Object symbol = cachedLibraryInterop.readMember(lib, f.name());
254-
Object function = InteropLibrary.getUncached(symbol).invokeMember(symbol, "bind", f.signature());
255-
return InteropLibrary.getUncached(function).execute(function, args);
262+
return SignatureLibrary.getUncached().call(signature, symbol, args);
256263
} catch (Exception e) {
257264
throw CompilerDirectives.shouldNotReachHere(f.name(), e);
258265
}

0 commit comments

Comments
 (0)