Skip to content

Commit 39515d4

Browse files
committed
Avoid instanceof PythonAbstractClass interface
1 parent 7ae61e2 commit 39515d4

File tree

11 files changed

+65
-39
lines changed

11 files changed

+65
-39
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ boolean check(Object object) {
173173
@Builtin(name = "instanceof", minNumOfPositionalArgs = 2)
174174
@GenerateNodeFactory
175175
abstract static class InstanceOfNode extends PythonBinaryBuiltinNode {
176-
@Specialization(guards = {"!isForeignObject(object)", "isForeignObject(klass)"})
176+
@Specialization(guards = {"!isForeignObject(object)", "isForeignTruffleObject(klass)"})
177177
boolean check(Object object, TruffleObject klass) {
178178
Env env = getContext().getEnv();
179179
try {
@@ -187,7 +187,7 @@ boolean check(Object object, TruffleObject klass) {
187187
return false;
188188
}
189189

190-
@Specialization(guards = {"isForeignObject(object)", "isForeignObject(klass)"})
190+
@Specialization(guards = {"isForeignObject(object)", "isForeignTruffleObject(klass)"})
191191
boolean checkForeign(Object object, TruffleObject klass) {
192192
Env env = getContext().getEnv();
193193
try {

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,14 @@ static Object runAbstractObject(@SuppressWarnings("unused") CExtContext cextCont
461461
return PythonObjectNativeWrapper.wrap(object, noWrapperProfile);
462462
}
463463

464-
@Specialization(guards = {"isForeignObject(object)", "!isNativeWrapper(object)", "!isNativeNull(object)"})
464+
@Specialization(guards = {"isForeignTruffleObject(object)", "!isNativeWrapper(object)", "!isNativeNull(object)"})
465465
static Object doForeignObject(@SuppressWarnings("unused") CExtContext cextContext, TruffleObject object) {
466466
return TruffleObjectNativeWrapper.wrap(object);
467467
}
468468

469-
@Specialization(guards = "isFallback(object)")
470-
static Object run(@SuppressWarnings("unused") CExtContext cextContext, Object object) {
469+
@Specialization(guards = "isFallback(object, lib)", limit = "1")
470+
static Object run(@SuppressWarnings("unused") CExtContext cextContext, Object object,
471+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib) {
471472
assert object != null : "Java 'null' cannot be a Sulong value";
472473
assert CApiGuards.isNativeWrapper(object) : "unknown object cannot be a Sulong value";
473474
return object;
@@ -477,9 +478,10 @@ protected static PythonClassNativeWrapper wrapNativeClass(PythonManagedClass obj
477478
return PythonClassNativeWrapper.wrap(object, GetNameNode.doSlowPath(object));
478479
}
479480

480-
static boolean isFallback(Object object) {
481+
static boolean isFallback(Object object, InteropLibrary lib) {
481482
return !(object instanceof String || object instanceof Boolean || object instanceof Integer || object instanceof Long || object instanceof Double ||
482-
object instanceof PythonNativeNull || object instanceof PythonAbstractObject || (PGuards.isForeignObject(object) && !CApiGuards.isNativeWrapper(object)));
483+
object instanceof PythonNativeNull || object instanceof PythonAbstractObject) &&
484+
!(PGuards.isForeignObject(object, lib) && !CApiGuards.isNativeWrapper(object));
483485
}
484486

485487
protected static boolean isNaN(double d) {
@@ -694,23 +696,24 @@ static Object runAbstractObject(@SuppressWarnings("unused") CExtContext cextCont
694696
return PythonObjectNativeWrapper.wrapNewRef(object, noWrapperProfile);
695697
}
696698

697-
@Specialization(guards = {"isForeignObject(object)", "!isNativeWrapper(object)", "!isNativeNull(object)"})
699+
@Specialization(guards = {"isForeignTruffleObject(object)", "!isNativeWrapper(object)", "!isNativeNull(object)"})
698700
static Object doForeignObject(CExtContext cextContext, TruffleObject object) {
699701
// this will always be a new wrapper; it's implicitly always a new reference in any case
700702
return ToSulongNode.doForeignObject(cextContext, object);
701703
}
702704

703-
@Specialization(guards = "isFallback(object)")
704-
static Object run(CExtContext cextContext, Object object) {
705-
return ToSulongNode.run(cextContext, object);
705+
@Specialization(guards = "isFallback(object, lib)", limit = "1")
706+
static Object run(CExtContext cextContext, Object object,
707+
@CachedLibrary("object") InteropLibrary lib) {
708+
return ToSulongNode.run(cextContext, object, lib);
706709
}
707710

708711
protected static PythonClassNativeWrapper wrapNativeClass(PythonManagedClass object) {
709712
return PythonClassNativeWrapper.wrap(object, GetNameNode.doSlowPath(object));
710713
}
711714

712-
static boolean isFallback(Object object) {
713-
return ToSulongNode.isFallback(object);
715+
static boolean isFallback(Object object, InteropLibrary lib) {
716+
return ToSulongNode.isFallback(object, lib);
714717
}
715718

716719
protected static boolean isNaN(double d) {
@@ -725,7 +728,7 @@ protected static boolean isNaN(double d) {
725728
* ref count of all {@link PythonNativeWrapper} (and subclasses) (but not if they are newly
726729
* created since the ref count is already one in this case). But it does not increase the ref
727730
* count on {@link PythonAbstractNativeObject}.
728-
*
731+
*
729732
* The reason for this behavior is that after the native function returns, one can decrease the
730733
* ref count by one and therefore release any allocated handles that would cause a memory leak.
731734
* This is not necessary for {@link PythonAbstractNativeObject} since they are managed by a weak
@@ -849,23 +852,24 @@ static Object runAbstractObject(@SuppressWarnings("unused") CExtContext cextCont
849852
return PythonObjectNativeWrapper.wrapNewRef(object, noWrapperProfile);
850853
}
851854

852-
@Specialization(guards = {"isForeignObject(object)", "!isNativeWrapper(object)", "!isNativeNull(object)"})
855+
@Specialization(guards = {"isForeignTruffleObject(object)", "!isNativeWrapper(object)", "!isNativeNull(object)"})
853856
static Object doForeignObject(CExtContext cextContext, TruffleObject object) {
854857
// this will always be a new wrapper; it's implicitly always a new reference in any case
855858
return ToSulongNode.doForeignObject(cextContext, object);
856859
}
857860

858-
@Specialization(guards = "isFallback(object)")
859-
static Object run(CExtContext cextContext, Object object) {
860-
return ToSulongNode.run(cextContext, object);
861+
@Specialization(guards = "isFallback(object, lib)", limit = "1")
862+
static Object run(CExtContext cextContext, Object object,
863+
@CachedLibrary("object") InteropLibrary lib) {
864+
return ToSulongNode.run(cextContext, object, lib);
861865
}
862866

863867
protected static PythonClassNativeWrapper wrapNativeClass(PythonManagedClass object) {
864868
return PythonClassNativeWrapper.wrap(object, GetNameNode.doSlowPath(object));
865869
}
866870

867-
static boolean isFallback(Object object) {
868-
return ToSulongNode.isFallback(object);
871+
static boolean isFallback(Object object, InteropLibrary lib) {
872+
return ToSulongNode.isFallback(object, lib);
869873
}
870874

871875
protected static boolean isNaN(double d) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import com.oracle.truffle.api.dsl.Cached;
5757
import com.oracle.truffle.api.dsl.Specialization;
5858
import com.oracle.truffle.api.interop.InteropLibrary;
59-
import com.oracle.truffle.api.interop.TruffleObject;
6059
import com.oracle.truffle.api.interop.UnknownIdentifierException;
6160
import com.oracle.truffle.api.interop.UnsupportedMessageException;
6261
import com.oracle.truffle.api.interop.UnsupportedTypeException;
@@ -73,11 +72,11 @@ public class TruffleObjectNativeWrapper extends PythonNativeWrapper {
7372
// every 'PyObject *' provides 'ob_base', 'ob_type', and 'ob_refcnt'
7473
@CompilationFinal(dimensions = 1) private static final String[] MEMBERS = {GP_OBJECT, OB_BASE.getMemberName(), OB_TYPE.getMemberName(), OB_REFCNT.getMemberName()};
7574

76-
public TruffleObjectNativeWrapper(TruffleObject foreignObject) {
75+
public TruffleObjectNativeWrapper(Object foreignObject) {
7776
super(foreignObject);
7877
}
7978

80-
public static TruffleObjectNativeWrapper wrap(TruffleObject foreignObject) {
79+
public static TruffleObjectNativeWrapper wrap(Object foreignObject) {
8180
assert !CApiGuards.isNativeWrapper(foreignObject) : "attempting to wrap a native wrapper";
8281
return new TruffleObjectNativeWrapper(foreignObject);
8382
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/foreign/ForeignObjectBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ abstract static class NewNode extends PythonBuiltinNode {
549549
* A foreign function call specializes on the length of the passed arguments. Any
550550
* optimization based on the callee has to happen on the other side.a
551551
*/
552-
@Specialization(guards = {"isForeignObject(callee)", "!isNoValue(callee)", "keywords.length == 0"})
552+
@Specialization(guards = {"isForeignObject(callee, lib)", "!isNoValue(callee)", "keywords.length == 0"})
553553
protected Object doInteropCall(Object callee, Object[] arguments, @SuppressWarnings("unused") PKeyword[] keywords,
554554
@CachedLibrary(limit = "3") InteropLibrary lib,
555555
@Cached("create()") PTypeToForeignNode toForeignNode,
@@ -586,7 +586,7 @@ public final Object executeWithArgs(VirtualFrame frame, Object callee, Object[]
586586
* A foreign function call specializes on the length of the passed arguments. Any
587587
* optimization based on the callee has to happen on the other side.
588588
*/
589-
@Specialization(guards = {"isForeignObject(callee)", "!isNoValue(callee)", "keywords.length == 0"}, limit = "3")
589+
@Specialization(guards = {"isForeignObject(callee, lib)", "!isNoValue(callee)", "keywords.length == 0"}, limit = "4")
590590
protected Object doInteropCall(VirtualFrame frame, Object callee, Object[] arguments, @SuppressWarnings("unused") PKeyword[] keywords,
591591
@CachedLibrary("callee") InteropLibrary lib,
592592
@CachedContext(PythonLanguage.class) PythonContext context,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeBuiltins.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,17 @@ protected static boolean accept(Object[] ary, Object cachedSelf) {
219219
}
220220

221221
/* self is in the arguments */
222-
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()", guards = {"accept(arguments, cachedSelf)", "isPythonBuiltinClass(cachedSelf)"}, assumptions = "singleContextAssumption()")
222+
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()", guards = {"accept(arguments, cachedSelf)",
223+
"isPythonBuiltinClass(cachedSelf)"}, assumptions = "singleContextAssumption()")
223224
protected Object doItUnboxedBuiltin(VirtualFrame frame, @SuppressWarnings("unused") PNone noSelf, Object[] arguments, PKeyword[] keywords,
224225
@Cached("first(arguments)") Object cachedSelf) {
225226
PythonBuiltinClassType type = ((PythonBuiltinClass) cachedSelf).getType();
226227
arguments[0] = type;
227228
return op(frame, type, arguments, keywords, false);
228229
}
229230

230-
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()", guards = {"accept(arguments, cachedSelf)", "!isPythonBuiltinClass(cachedSelf)"}, assumptions = "singleContextAssumption()")
231+
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()", guards = {"accept(arguments, cachedSelf)",
232+
"!isPythonBuiltinClass(cachedSelf)"}, assumptions = "singleContextAssumption()")
231233
protected Object doItUnboxedUser(VirtualFrame frame, @SuppressWarnings("unused") PNone noSelf, Object[] arguments, PKeyword[] keywords,
232234
@Cached("first(arguments)") Object cachedSelf) {
233235
return op(frame, (PythonAbstractClass) cachedSelf, arguments, keywords, false);
@@ -241,7 +243,7 @@ protected Object doItUnboxedBuiltinType(VirtualFrame frame, @SuppressWarnings("u
241243
}
242244

243245
@Specialization(replaces = {"doItUnboxedUser", "doItUnboxedBuiltin", "doItUnboxedBuiltinType"})
244-
protected Object doItUnboxedIndirect(VirtualFrame frame, @SuppressWarnings("unused") PNone noSelf, Object[] arguments, PKeyword[] keywords) {
246+
protected Object doItUnboxedIndirect(VirtualFrame frame, PNone noSelf, Object[] arguments, PKeyword[] keywords) {
245247
Object self = arguments[0];
246248
if (self instanceof PythonBuiltinClassType) {
247249
return doItUnboxedBuiltinType(frame, noSelf, arguments, keywords, self);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ boolean doNativeClass(PythonAbstractNativeObject obj,
804804
return profile.profileClass(getClassNode.execute(obj), PythonBuiltinClassType.PythonClass);
805805
}
806806

807-
@Specialization(guards = "!isClass(obj)")
807+
@Fallback
808808
boolean doOther(@SuppressWarnings("unused") Object obj) {
809809
return false;
810810
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PGuards.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
*/
4141
package com.oracle.graal.python.nodes;
4242

43+
import com.oracle.graal.python.PythonLanguage;
4344
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4445
import com.oracle.graal.python.builtins.objects.PNone;
4546
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
4647
import com.oracle.graal.python.builtins.objects.array.PArray;
4748
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
4849
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
50+
import com.oracle.graal.python.builtins.objects.cext.PythonAbstractNativeObject;
4951
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
5052
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
5153
import com.oracle.graal.python.builtins.objects.code.PCode;
@@ -72,7 +74,6 @@
7274
import com.oracle.graal.python.builtins.objects.str.PString;
7375
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
7476
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
75-
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
7677
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
7778
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
7879
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
@@ -88,8 +89,11 @@
8889
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
8990
import com.oracle.graal.python.runtime.sequence.storage.TupleSequenceStorage;
9091
import com.oracle.graal.python.util.WeakASTReference;
92+
import com.oracle.truffle.api.CompilerDirectives;
9193
import com.oracle.truffle.api.frame.VirtualFrame;
94+
import com.oracle.truffle.api.interop.InteropLibrary;
9295
import com.oracle.truffle.api.interop.TruffleObject;
96+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
9397
import com.oracle.truffle.api.nodes.UnexpectedResultException;
9498
import com.oracle.truffle.api.profiles.ConditionProfile;
9599

@@ -155,7 +159,7 @@ public static boolean isCallable(Object value) {
155159
}
156160

157161
public static boolean isClass(Object value) {
158-
return value instanceof PythonAbstractClass;
162+
return value instanceof PythonBuiltinClassType || value instanceof PythonManagedClass || value instanceof PythonAbstractNativeObject;
159163
}
160164

161165
public static boolean isEmptyStorage(PSequence sequence) {
@@ -354,6 +358,21 @@ public static boolean isForeignObject(Object obj) {
354358
return obj instanceof TruffleObject && !(obj instanceof PythonAbstractObject) && !(obj instanceof PythonBuiltinClassType);
355359
}
356360

361+
public static boolean isForeignTruffleObject(TruffleObject obj) {
362+
return !(obj instanceof PythonAbstractObject) && !(obj instanceof PythonBuiltinClassType);
363+
}
364+
365+
public static boolean isForeignObject(Object obj, InteropLibrary lib) {
366+
try {
367+
return !(obj instanceof String || obj instanceof Integer || obj instanceof Long || obj instanceof Double || obj instanceof Boolean ||
368+
(lib.hasLanguage(obj) && lib.getLanguage(obj) == PythonLanguage.class));
369+
} catch (UnsupportedMessageException e) {
370+
// cannot happen due to check
371+
CompilerDirectives.transferToInterpreterAndInvalidate();
372+
throw new IllegalStateException(e);
373+
}
374+
}
375+
357376
public static boolean isPInt(Object obj) {
358377
return obj instanceof PInt;
359378
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/ReadAttributeFromObjectNode.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ protected Object readFromDict(PythonObject object, Object key,
202202
}
203203

204204
// foreign Object
205-
@Specialization(guards = "isForeignObject(object)")
205+
@Specialization(guards = "isForeignTruffleObject(object)")
206206
protected Object readForeign(TruffleObject object, Object key,
207207
@Cached PForeignToPTypeNode fromForeign,
208208
@CachedLibrary(limit = "getAttributeAccessInlineCacheMaxDepth()") InteropLibrary read) {
@@ -218,8 +218,9 @@ protected Object readForeign(TruffleObject object, Object key,
218218

219219
// not a Python or Foreign Object
220220
@SuppressWarnings("unused")
221-
@Specialization(guards = {"!isPythonObject(object)", "!isNativeObject(object)", "!isForeignObject(object)"})
222-
protected PNone readUnboxed(Object object, Object key) {
221+
@Specialization(guards = {"!isPythonObject(object)", "!isNativeObject(object)", "!isForeignObject(object, lib)"})
222+
protected PNone readUnboxed(Object object, Object key,
223+
@SuppressWarnings("unused") @CachedLibrary(limit = "1") InteropLibrary lib) {
223224
return PNone.NO_VALUE;
224225
}
225226

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/PythonCallNode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,14 @@ protected GetCallAttributeNode(String key) {
218218
this.key = key;
219219
}
220220

221-
@Specialization(guards = "isForeignObject(object)")
221+
@Specialization(guards = "isForeignTruffleObject(object)")
222222
Object getForeignInvoke(TruffleObject object) {
223223
return new ForeignInvoke(object, key);
224224
}
225225

226-
@Specialization(guards = "!isForeignObject(object)")
226+
@Specialization(guards = "!isForeignObject(object, lib)", limit = "getCallSiteInlineCacheMaxDepth()")
227227
Object getCallAttribute(VirtualFrame frame, Object object,
228+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib,
228229
@Cached("create(key)") GetAttributeNode getAttributeNode) {
229230
return getAttributeNode.executeObject(frame, object);
230231
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/object/GetLazyClassNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -202,7 +202,7 @@ protected static LazyPythonClass getPythonClassGeneric(PythonAbstractObject obje
202202
return lib.getLazyPythonClass(object);
203203
}
204204

205-
@Specialization(guards = "isForeignObject(object)")
205+
@Specialization(guards = "isForeignTruffleObject(object)")
206206
protected static LazyPythonClass getIt(@SuppressWarnings("unused") TruffleObject object) {
207207
return PythonBuiltinClassType.ForeignObject;
208208
}

0 commit comments

Comments
 (0)