Skip to content

Commit a344e5b

Browse files
committed
[GR-30978] Cache so ABI in PythonContext
PullRequest: graalpython/1767
2 parents cd1579a + f5860f0 commit a344e5b

File tree

6 files changed

+66
-49
lines changed

6 files changed

+66
-49
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ImpModuleBuiltins.java

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import com.oracle.graal.python.builtins.PythonBuiltins;
5858
import com.oracle.graal.python.builtins.modules.PythonCextBuiltinsFactory.DefaultCheckFunctionResultNodeGen;
5959
import com.oracle.graal.python.builtins.objects.PNone;
60-
import com.oracle.graal.python.builtins.objects.PythonAbstractObjectFactory.PInteropGetAttributeNodeGen;
6160
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
6261
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
6362
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.AsPythonObjectNodeGen;
@@ -117,10 +116,8 @@
117116
import com.oracle.truffle.api.interop.UnsupportedMessageException;
118117
import com.oracle.truffle.api.interop.UnsupportedTypeException;
119118
import com.oracle.truffle.api.library.CachedLibrary;
120-
import com.oracle.truffle.api.nodes.LanguageInfo;
121119
import com.oracle.truffle.api.source.Source;
122120
import com.oracle.truffle.api.source.Source.SourceBuilder;
123-
import com.oracle.truffle.llvm.api.Toolchain;
124121

125122
@CoreFunctions(defineModule = "_imp")
126123
public class ImpModuleBuiltins extends PythonBuiltins {
@@ -341,7 +338,7 @@ private Object loadDynamicModuleWithSpec(String name, String path, InteropLibrar
341338
String basename = name.substring(name.lastIndexOf('.') + 1);
342339
TruffleObject sulongLibrary;
343340
try {
344-
String extSuffix = ExtensionSuffixesNode.getSoAbi(context);
341+
String extSuffix = context.getSoAbi();
345342
CallTarget callTarget = env.parseInternal(Source.newBuilder(LLVM_LANGUAGE, context.getPublicTruffleFileRelaxed(path, extSuffix)).build());
346343
sulongLibrary = (TruffleObject) callTarget.call();
347344
} catch (SecurityException e) {
@@ -443,7 +440,7 @@ private void ensureCapiWasLoaded(String name, String path) throws IOException, I
443440
Env env = context.getEnv();
444441
CompilerDirectives.transferToInterpreterAndInvalidate();
445442

446-
String libPythonName = "libpython" + ExtensionSuffixesNode.getSoAbi(context);
443+
String libPythonName = "libpython" + context.getSoAbi();
447444
TruffleFile homePath = env.getInternalTruffleFile(context.getCAPIHome());
448445
TruffleFile capiFile = homePath.resolve(libPythonName);
449446
Object capi;
@@ -481,7 +478,7 @@ private GraalHPyContext ensureHPyWasLoaded(PythonContext context, String name, S
481478
Env env = context.getEnv();
482479
CompilerDirectives.transferToInterpreterAndInvalidate();
483480

484-
String libPythonName = "libhpy" + ExtensionSuffixesNode.getSoAbi(context);
481+
String libPythonName = "libhpy" + context.getSoAbi();
485482
TruffleFile homePath = env.getInternalTruffleFile(context.getCAPIHome());
486483
TruffleFile capiFile = homePath.resolve(libPythonName);
487484
try {
@@ -703,34 +700,7 @@ public abstract static class ExtensionSuffixesNode extends PythonBuiltinNode {
703700
@Specialization
704701
Object run(
705702
@CachedContext(PythonLanguage.class) PythonContext ctxt) {
706-
String soAbi = getSoAbi(ctxt);
707-
return factory().createList(new Object[]{soAbi, HPY_SUFFIX, ".so", ".dylib", ".su"});
708-
}
709-
710-
@TruffleBoundary
711-
public static String getSoAbi(PythonContext ctxt) {
712-
PythonModule sysModule = ctxt.getCore().lookupBuiltinModule("sys");
713-
Object implementationObj = ReadAttributeFromObjectNode.getUncached().execute(sysModule, "implementation");
714-
// sys.implementation.cache_tag
715-
String cacheTag = (String) PInteropGetAttributeNodeGen.getUncached().execute(implementationObj, "cache_tag");
716-
// sys.implementation._multiarch
717-
String multiArch = (String) PInteropGetAttributeNodeGen.getUncached().execute(implementationObj, "_multiarch");
718-
719-
Env env = ctxt.getEnv();
720-
LanguageInfo llvmInfo = env.getInternalLanguages().get(GraalPythonModuleBuiltins.LLVM_LANGUAGE);
721-
Toolchain toolchain = env.lookup(llvmInfo, Toolchain.class);
722-
String toolchainId = toolchain.getIdentifier();
723-
724-
// only use '.dylib' if we are on 'Darwin-native'
725-
String soExt;
726-
if ("darwin".equals(PythonUtils.getPythonOSName()) && "native".equals(toolchainId)) {
727-
soExt = ".dylib";
728-
} else {
729-
soExt = ".so";
730-
}
731-
732-
return "." + cacheTag + "-" + toolchainId + "-" + multiArch + soExt;
703+
return factory().createList(new Object[]{ctxt.getSoAbi(), HPY_SUFFIX, ".so", ".dylib", ".su"});
733704
}
734705
}
735-
736706
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/SocketBuiltins.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
import com.oracle.truffle.api.profiles.ConditionProfile;
9999

100100
@CoreFunctions(extendClasses = PythonBuiltinClassType.PSocket)
101-
@SuppressWarnings("unused")
102101
public class SocketBuiltins extends PythonBuiltins {
103102

104103
@Override
@@ -342,7 +341,7 @@ Object listen(PSocket socket, int backlog) {
342341

343342
@Specialization
344343
@TruffleBoundary
345-
Object listen(PSocket socket, PNone backlog) {
344+
Object listen(PSocket socket, @SuppressWarnings("unused") PNone backlog) {
346345
return listen(socket, 50);
347346
}
348347
}
@@ -354,7 +353,7 @@ Object listen(PSocket socket, PNone backlog) {
354353
@GenerateNodeFactory
355354
abstract static class RecvNode extends PythonTernaryClinicBuiltinNode {
356355
@Specialization
357-
Object recv(VirtualFrame frame, PSocket socket, int bufsize, int flags,
356+
Object recv(VirtualFrame frame, PSocket socket, int bufsize, @SuppressWarnings("unused") int flags,
358357
@Cached GilNode gil) {
359358
if (socket.getSocket() == null) {
360359
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
@@ -385,11 +384,13 @@ protected ArgumentClinicProvider getArgumentClinic() {
385384
@Builtin(name = "recvfrom", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
386385
@GenerateNodeFactory
387386
abstract static class RecvFromNode extends PythonTernaryBuiltinNode {
387+
@SuppressWarnings("unused")
388388
@Specialization
389389
Object recvFrom(PSocket socket, int bufsize, int flags) {
390390
return PNotImplemented.NOT_IMPLEMENTED;
391391
}
392392

393+
@SuppressWarnings("unused")
393394
@Specialization
394395
Object recvFrom(PSocket socket, int bufsize, PNone flags) {
395396
return PNotImplemented.NOT_IMPLEMENTED;
@@ -405,8 +406,7 @@ protected static SequenceStorageNodes.SetItemNode createSetItem() {
405406
}
406407

407408
@Specialization
408-
Object recvInto(VirtualFrame frame, PSocket socket, PMemoryView buffer, Object flags,
409-
@Cached ConditionProfile byteStorage,
409+
Object recvInto(VirtualFrame frame, PSocket socket, PMemoryView buffer, @SuppressWarnings("unused") Object flags,
410410
@Cached PyNumberAsSizeNode asSizeNode,
411411
@Cached("create(__LEN__)") LookupAndCallUnaryNode callLen,
412412
@Cached("create(__SETITEM__)") LookupAndCallTernaryNode setItem) {
@@ -435,7 +435,7 @@ Object recvInto(VirtualFrame frame, PSocket socket, PMemoryView buffer, Object f
435435
}
436436

437437
@Specialization
438-
Object recvInto(VirtualFrame frame, PSocket socket, PByteArray buffer, Object flags,
438+
Object recvInto(VirtualFrame frame, PSocket socket, PByteArray buffer, @SuppressWarnings("unused") Object flags,
439439
@Cached GilNode gil,
440440
@Cached ConditionProfile byteStorage,
441441
@Cached SequenceStorageNodes.LenNode lenNode,
@@ -488,16 +488,19 @@ Object recvInto(VirtualFrame frame, PSocket socket, PByteArray buffer, Object fl
488488
@Builtin(name = "recvmsg", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 4)
489489
@GenerateNodeFactory
490490
abstract static class RecvMsgNode extends PythonBuiltinNode {
491+
@SuppressWarnings("unused")
491492
@Specialization
492493
Object recvFrom(PSocket socket, int bufsize, int ancbufsize, int flags) {
493494
return PNotImplemented.NOT_IMPLEMENTED;
494495
}
495496

497+
@SuppressWarnings("unused")
496498
@Specialization
497499
Object recvFrom(PSocket socket, int bufsize, int ancbufsize, PNone flags) {
498500
return PNotImplemented.NOT_IMPLEMENTED;
499501
}
500502

503+
@SuppressWarnings("unused")
501504
@Specialization
502505
Object recvFrom(PSocket socket, int bufsize, PNone ancbufsize, PNone flags) {
503506
return PNotImplemented.NOT_IMPLEMENTED;
@@ -509,7 +512,7 @@ Object recvFrom(PSocket socket, int bufsize, PNone ancbufsize, PNone flags) {
509512
@GenerateNodeFactory
510513
abstract static class SendNode extends PythonTernaryBuiltinNode {
511514
@Specialization
512-
Object send(VirtualFrame frame, PSocket socket, PBytes bytes, Object flags,
515+
Object send(VirtualFrame frame, PSocket socket, PBytes bytes, @SuppressWarnings("unused") Object flags,
513516
@Cached GilNode gil,
514517
@Cached SequenceStorageNodes.ToByteArrayNode toBytes) {
515518
// TODO: do not ignore flags
@@ -542,7 +545,7 @@ Object send(VirtualFrame frame, PSocket socket, PBytes bytes, Object flags,
542545
@GenerateNodeFactory
543546
abstract static class SendAllNode extends PythonTernaryBuiltinNode {
544547
@Specialization
545-
Object sendAll(VirtualFrame frame, PSocket socket, PBytesLike bytes, Object flags,
548+
Object sendAll(VirtualFrame frame, PSocket socket, PBytesLike bytes, @SuppressWarnings("unused") Object flags,
546549
@Cached GilNode gil,
547550
@Cached SequenceStorageNodes.ToByteArrayNode toBytes,
548551
@Cached ConditionProfile hasTimeoutProfile) {
@@ -586,11 +589,13 @@ Object sendAll(VirtualFrame frame, PSocket socket, PBytesLike bytes, Object flag
586589
@Builtin(name = "sendto", minNumOfPositionalArgs = 3, maxNumOfPositionalArgs = 4)
587590
@GenerateNodeFactory
588591
abstract static class SendToNode extends PythonBuiltinNode {
592+
@SuppressWarnings("unused")
589593
@Specialization
590594
Object sendTo(PSocket socket, Object bytes, int flags, Object address) {
591595
return PNotImplemented.NOT_IMPLEMENTED;
592596
}
593597

598+
@SuppressWarnings("unused")
594599
@Specialization
595600
Object sendTo(PSocket socket, Object bytes, PNone flags, Object address) {
596601
return PNotImplemented.NOT_IMPLEMENTED;
@@ -601,6 +606,7 @@ Object sendTo(PSocket socket, Object bytes, PNone flags, Object address) {
601606
@Builtin(name = "sendmsg", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 5)
602607
@GenerateNodeFactory
603608
abstract static class SendMsgNode extends PythonBuiltinNode {
609+
@SuppressWarnings("unused")
604610
@Specialization
605611
Object sendMsg(PSocket socket, Object buffers, Object ancdata, int flags, Object address) {
606612
return PNotImplemented.NOT_IMPLEMENTED;
@@ -743,6 +749,7 @@ int detach(PSocket socket) {
743749
@Builtin(name = "_setsockopt", minNumOfPositionalArgs = 4)
744750
@GenerateNodeFactory
745751
abstract static class SetSockOptNode extends PythonBuiltinNode {
752+
@SuppressWarnings("unused")
746753
@Specialization
747754
Object setSockOpt(PSocket socket, Object level, Object optname, Object value, Object optlen) {
748755
return PNone.NONE;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/BinaryArithmetic.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import com.oracle.graal.python.nodes.SpecialMethodNames;
5353
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
5454
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode.NotImplementedHandler;
55-
import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode;
5655
import com.oracle.graal.python.util.Supplier;
5756
import com.oracle.truffle.api.CompilerDirectives;
5857
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -138,9 +137,8 @@ public NodeCost getCost() {
138137
}
139138

140139
/**
141-
* A helper root node that dispatches to {@link LookupAndCallTernaryNode} to execute the
142-
* provided ternary operator. Note: this is just a root node and won't do any signature
143-
* checking.
140+
* A helper root node that dispatches to {@link LookupAndCallBinaryNode} to execute the provided
141+
* ternary operator. Note: this is just a root node and won't do any signature checking.
144142
*/
145143
static final class CallBinaryArithmeticRootNode extends CallArithmeticRootNode {
146144
static final Signature SIGNATURE_BINARY = new Signature(2, false, -1, false, new String[]{"$self", "other"}, null);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.util.logging.Level;
4545

4646
import com.oracle.graal.python.PythonLanguage;
47-
import com.oracle.graal.python.builtins.modules.ImpModuleBuiltins;
4847
import com.oracle.graal.python.nodes.PNodeWithContext;
4948
import com.oracle.graal.python.runtime.NativeLibraryFactory.InvokeNativeFunctionNodeGen;
5049
import com.oracle.graal.python.runtime.exception.PythonControlFlowException;
@@ -241,7 +240,7 @@ private Object loadLibrary(PythonContext context) {
241240

242241
public static String getLibPath(PythonContext context, String name) {
243242
CompilerAsserts.neverPartOfCompilation();
244-
String libPythonName = name + ImpModuleBuiltins.ExtensionSuffixesNode.getSoAbi(context);
243+
String libPythonName = name + context.getSoAbi();
245244
TruffleFile homePath = context.getEnv().getInternalTruffleFile(context.getCAPIHome());
246245
TruffleFile file = homePath.resolve(libPythonName);
247246
return file.getPath();

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@
5858

5959
import com.oracle.graal.python.PythonLanguage;
6060
import com.oracle.graal.python.builtins.Python3Core;
61+
import com.oracle.graal.python.builtins.modules.GraalPythonModuleBuiltins;
6162
import com.oracle.graal.python.builtins.objects.PNone;
6263
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
64+
import com.oracle.graal.python.builtins.objects.PythonAbstractObjectFactory.PInteropGetAttributeNodeGen;
6365
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
6466
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext;
6567
import com.oracle.graal.python.builtins.objects.cext.capi.PThreadState;
@@ -90,6 +92,7 @@
9092
import com.oracle.graal.python.runtime.exception.PythonThreadKillException;
9193
import com.oracle.graal.python.runtime.object.IDUtils;
9294
import com.oracle.graal.python.util.Consumer;
95+
import com.oracle.graal.python.util.PythonUtils;
9396
import com.oracle.graal.python.util.ShutdownHook;
9497
import com.oracle.graal.python.util.Supplier;
9598
import com.oracle.truffle.api.Assumption;
@@ -104,12 +107,15 @@
104107
import com.oracle.truffle.api.TruffleLanguage;
105108
import com.oracle.truffle.api.TruffleLanguage.Env;
106109
import com.oracle.truffle.api.TruffleLogger;
110+
import com.oracle.truffle.api.instrumentation.AllocationReporter;
107111
import com.oracle.truffle.api.interop.ExceptionType;
108112
import com.oracle.truffle.api.interop.InteropLibrary;
109113
import com.oracle.truffle.api.interop.UnsupportedMessageException;
114+
import com.oracle.truffle.api.nodes.LanguageInfo;
110115
import com.oracle.truffle.api.profiles.ConditionProfile;
111116
import com.oracle.truffle.api.source.Source;
112117
import com.oracle.truffle.api.utilities.CyclicAssumption;
118+
import com.oracle.truffle.llvm.api.Toolchain;
113119

114120
public final class PythonContext {
115121
private static final Source IMPORT_WARNINGS_SOURCE = Source.newBuilder(PythonLanguage.ID, "import warnings\n", "<internal>").internal(true).build();
@@ -287,6 +293,8 @@ private static final class AtExitHook {
287293
@CompilationFinal private CApiContext cApiContext;
288294
@CompilationFinal private GraalHPyContext hPyContext;
289295

296+
private String soABI; // cache for soAPI
297+
290298
private static final Assumption singleNativeContext = Truffle.getRuntime().createAssumption("single native context assumption");
291299

292300
private final ReentrantLock globalInterpreterLock = new ReentrantLock();
@@ -307,6 +315,7 @@ private static final class AtExitHook {
307315
private final ThreadLocal<ArrayDeque<String>> currentImport = new ThreadLocal<>();
308316

309317
@CompilationFinal(dimensions = 1) private Object[] optionValues;
318+
private AllocationReporter allocationReporter;
310319

311320
/*
312321
* These maps are used to ensure that each "deserialization" of code in the parser gets a
@@ -334,6 +343,13 @@ public PythonContext(PythonLanguage language, TruffleLanguage.Env env, Python3Co
334343
this.err = env.err();
335344
}
336345

346+
public AllocationReporter getAllocationReporter() {
347+
if (allocationReporter == null) {
348+
return allocationReporter = env.lookup(AllocationReporter.class);
349+
}
350+
return allocationReporter;
351+
}
352+
337353
public ThreadGroup getThreadGroup() {
338354
return threadGroup;
339355
}
@@ -1321,4 +1337,31 @@ public String getCodeFilename(CallTarget callTarget) {
13211337
public long getDeserializationId(String fileName) {
13221338
return deserializationId.computeIfAbsent(fileName, f -> new AtomicLong()).incrementAndGet();
13231339
}
1340+
1341+
@TruffleBoundary
1342+
public String getSoAbi() {
1343+
if (soABI == null) {
1344+
PythonModule sysModule = getCore().lookupBuiltinModule("sys");
1345+
Object implementationObj = ReadAttributeFromObjectNode.getUncached().execute(sysModule, "implementation");
1346+
// sys.implementation.cache_tag
1347+
String cacheTag = (String) PInteropGetAttributeNodeGen.getUncached().execute(implementationObj, "cache_tag");
1348+
// sys.implementation._multiarch
1349+
String multiArch = (String) PInteropGetAttributeNodeGen.getUncached().execute(implementationObj, "_multiarch");
1350+
1351+
LanguageInfo llvmInfo = env.getInternalLanguages().get(GraalPythonModuleBuiltins.LLVM_LANGUAGE);
1352+
Toolchain toolchain = env.lookup(llvmInfo, Toolchain.class);
1353+
String toolchainId = toolchain.getIdentifier();
1354+
1355+
// only use '.dylib' if we are on 'Darwin-native'
1356+
String soExt;
1357+
if ("darwin".equals(PythonUtils.getPythonOSName()) && "native".equals(toolchainId)) {
1358+
soExt = ".dylib";
1359+
} else {
1360+
soExt = ".so";
1361+
}
1362+
1363+
soABI = "." + cacheTag + "-" + toolchainId + "-" + multiArch + soExt;
1364+
}
1365+
return soABI;
1366+
}
13241367
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ static PythonLanguage getLanguage(boolean marker, double marker2,
220220
}
221221

222222
protected static AllocationReporter getAllocationReporter(ContextReference<PythonContext> contextRef) {
223-
return contextRef.get().getEnv().lookup(AllocationReporter.class);
223+
return contextRef.get().getAllocationReporter();
224224
}
225225

226226
public final PythonLanguage getLanguage() {

0 commit comments

Comments
 (0)