Skip to content

Commit 1adbac7

Browse files
committed
Revert "replace ReferenceLibrary with InteropLibrary"
This reverts commit 69b17d0.
1 parent 69b17d0 commit 1adbac7

File tree

7 files changed

+84
-75
lines changed

7 files changed

+84
-75
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
import com.oracle.truffle.api.nodes.NodeUtil;
168168
import com.oracle.truffle.api.profiles.BranchProfile;
169169
import com.oracle.truffle.api.profiles.ConditionProfile;
170+
import com.oracle.truffle.llvm.spi.ReferenceLibrary;
170171

171172
public abstract class CExtNodes {
172173

@@ -2709,12 +2710,12 @@ public abstract static class GetTypeMemberNode extends PNodeWithContext {
27092710
* native context, so we can be sure that the "nativeClassStableAssumption" (which is
27102711
* per-context) is from the context in which this native object was created.
27112712
*/
2712-
@Specialization(guards = {"interopLibrary.isIdentical(cachedObj, obj, interopLibrary)", "memberName == cachedMemberName"}, //
2713+
@Specialization(guards = {"referenceLibrary.isSame(cachedObj, obj)", "memberName == cachedMemberName"}, //
27132714
limit = "1", //
27142715
assumptions = {"getNativeClassStableAssumption(cachedObj)", "singleContextAssumption()"})
27152716
public Object doCachedObj(@SuppressWarnings("unused") PythonAbstractNativeObject obj, @SuppressWarnings("unused") NativeMember memberName,
27162717
@Cached("obj") @SuppressWarnings("unused") PythonAbstractNativeObject cachedObj,
2717-
@CachedLibrary(limit = "1") @SuppressWarnings("unused") InteropLibrary interopLibrary, // cachedObj and obj should always be the same type
2718+
@CachedLibrary("cachedObj") @SuppressWarnings("unused") ReferenceLibrary referenceLibrary,
27182719
@Cached("memberName") @SuppressWarnings("unused") NativeMember cachedMemberName,
27192720
@Cached("doSlowPath(obj, memberName)") Object result) {
27202721
return result;
@@ -3156,12 +3157,13 @@ static PythonNativeWrapper resolveLongCached(@SuppressWarnings("unused") long po
31563157
return cachedValue;
31573158
}
31583159

3159-
@Specialization(guards = {"interopLib.isIdentical(cachedPointerObject, pointerObject, interopLib)", "cachedValue != null"}, //
3160+
@Specialization(limit = "3", //
3161+
guards = {"isSame(referenceLibrary, cachedPointerObject, pointerObject)", "cachedValue != null"}, //
31603162
assumptions = "singleContextAssumption()", //
31613163
rewriteOn = InvalidAssumptionException.class)
31623164
static PythonNativeWrapper resolveObjectCached(@SuppressWarnings("unused") Object pointerObject,
31633165
@Cached("pointerObject") @SuppressWarnings("unused") Object cachedPointerObject,
3164-
@CachedLibrary(limit = "3") @SuppressWarnings("unused") InteropLibrary interopLib,
3166+
@CachedLibrary("cachedPointerObject") @SuppressWarnings("unused") ReferenceLibrary referenceLibrary,
31653167
@Cached("resolveHandleUncached(pointerObject)") PythonNativeWrapper cachedValue,
31663168
@Cached("getHandleValidAssumption(cachedValue)") Assumption associationValidAssumption) throws InvalidAssumptionException {
31673169
associationValidAssumption.check();
@@ -3193,6 +3195,10 @@ static PythonNativeWrapper resolveHandleUncached(Object pointerObject) {
31933195
return null;
31943196
}
31953197

3198+
static boolean isSame(ReferenceLibrary referenceLibrary, Object left, Object right) {
3199+
return referenceLibrary.isSame(left, right);
3200+
}
3201+
31963202
static Assumption singleContextAssumption() {
31973203
return PythonLanguage.getCurrent().singleContextAssumption;
31983204
}

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@
172172
import com.oracle.truffle.api.profiles.BranchProfile;
173173
import com.oracle.truffle.api.profiles.ConditionProfile;
174174
import com.oracle.truffle.api.profiles.ValueProfile;
175-
import com.oracle.truffle.api.utilities.TriState;
176175
import com.oracle.truffle.llvm.spi.NativeTypeLibrary;
176+
import com.oracle.truffle.llvm.spi.ReferenceLibrary;
177177

178178
@ExportLibrary(InteropLibrary.class)
179179
@ExportLibrary(NativeTypeLibrary.class)
@@ -1461,6 +1461,7 @@ static boolean isModifiableCached(PythonObjectNativeWrapper receiver, String nam
14611461
}
14621462
}
14631463

1464+
@ExportLibrary(ReferenceLibrary.class)
14641465
public static final class PrimitiveNativeWrapper extends DynamicObjectNativeWrapper {
14651466

14661467
public static final byte PRIMITIVE_STATE_BOOL = 1;
@@ -1703,31 +1704,20 @@ protected static boolean isObType(String key) {
17031704
}
17041705

17051706
@ExportMessage
1706-
@TruffleBoundary
1707-
int identityHashCode() {
1708-
int val = System.identityHashCode(state) ^ System.identityHashCode(value);
1709-
if (Double.isNaN(dvalue)) {
1710-
return val;
1711-
} else {
1712-
return val ^ System.identityHashCode(dvalue);
1713-
}
1714-
}
1715-
1716-
@ExportMessage
1717-
static class IsIdenticalOrUndefined {
1707+
static class IsSame {
17181708

17191709
@Specialization
1720-
static TriState doPrimitiveWrapper(PrimitiveNativeWrapper receiver, PrimitiveNativeWrapper other) {
1710+
static boolean doPrimitiveWrapper(PrimitiveNativeWrapper receiver, PrimitiveNativeWrapper other) {
17211711
// This basically emulates singletons for boxed values. However, we need to do so to
17221712
// preserve the invariant that storing an object into a list and getting it out (in
17231713
// the same critical region) returns the same object.
1724-
return other.state == receiver.state && other.value == receiver.value && (other.dvalue == receiver.dvalue || Double.isNaN(receiver.dvalue) && Double.isNaN(other.dvalue)) ? TriState.TRUE : TriState.FALSE;
1714+
return other.state == receiver.state && other.value == receiver.value && (other.dvalue == receiver.dvalue || Double.isNaN(receiver.dvalue) && Double.isNaN(other.dvalue));
17251715
}
17261716

17271717
@Fallback
17281718
@SuppressWarnings("unused")
1729-
static TriState doGeneric(PrimitiveNativeWrapper receiver, Object other) {
1730-
return TriState.FALSE;
1719+
static boolean doGeneric(PrimitiveNativeWrapper receiver, Object other) {
1720+
return false;
17311721
}
17321722
}
17331723
}

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

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_GET_OB_TYPE;
4444
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_OBJECT_GENERIC_GET_DICT;
4545

46+
import java.lang.ref.WeakReference;
4647
import java.util.Objects;
4748

4849
import com.oracle.graal.python.PythonLanguage;
@@ -74,6 +75,7 @@
7475
import com.oracle.truffle.api.dsl.Cached;
7576
import com.oracle.truffle.api.dsl.Cached.Exclusive;
7677
import com.oracle.truffle.api.dsl.Cached.Shared;
78+
import com.oracle.truffle.api.dsl.Fallback;
7779
import com.oracle.truffle.api.dsl.GenerateUncached;
7880
import com.oracle.truffle.api.dsl.Specialization;
7981
import com.oracle.truffle.api.interop.ArityException;
@@ -89,16 +91,17 @@
8991
import com.oracle.truffle.api.object.Shape;
9092
import com.oracle.truffle.api.profiles.ConditionProfile;
9193
import com.oracle.truffle.api.profiles.ValueProfile;
92-
import com.oracle.truffle.api.utilities.TriState;
94+
import com.oracle.truffle.llvm.spi.ReferenceLibrary;
9395

9496
@ExportLibrary(PythonObjectLibrary.class)
97+
@ExportLibrary(ReferenceLibrary.class)
9598
@ExportLibrary(InteropLibrary.class)
9699
public final class PythonAbstractNativeObject extends PythonAbstractObject implements PythonNativeObject, PythonNativeClass {
97100

98-
public final TruffleObject ptr;
101+
public final TruffleObject object;
99102

100-
public PythonAbstractNativeObject(TruffleObject ptr) {
101-
this.ptr = ptr;
103+
public PythonAbstractNativeObject(TruffleObject object) {
104+
this.object = object;
102105
}
103106

104107
public int compareTo(Object o) {
@@ -118,14 +121,14 @@ public void lookupChanged() {
118121
}
119122

120123
public TruffleObject getPtr() {
121-
return ptr;
124+
return object;
122125
}
123126

124127
@Override
125128
public int hashCode() {
126129
CompilerAsserts.neverPartOfCompilation();
127130
// this is important for the default '__hash__' implementation
128-
return Objects.hashCode(ptr);
131+
return Objects.hashCode(object);
129132
}
130133

131134
@Ignore
@@ -138,7 +141,7 @@ public boolean equals(Object obj) {
138141
return false;
139142
}
140143
PythonAbstractNativeObject other = (PythonAbstractNativeObject) obj;
141-
return Objects.equals(ptr, other.ptr);
144+
return Objects.equals(object, other.object);
142145
}
143146

144147
public boolean equalsProfiled(Object obj, ValueProfile profile) {
@@ -149,13 +152,13 @@ public boolean equalsProfiled(Object obj, ValueProfile profile) {
149152
return false;
150153
}
151154
PythonAbstractNativeObject other = (PythonAbstractNativeObject) obj;
152-
return Objects.equals(profile.profile(ptr), profile.profile(other.ptr));
155+
return Objects.equals(profile.profile(object), profile.profile(other.object));
153156
}
154157

155158
@Override
156159
public String toString() {
157160
CompilerAsserts.neverPartOfCompilation();
158-
return String.format("PythonAbstractNativeObject(%s)", ptr);
161+
return String.format("PythonAbstractNativeObject(%s)", object);
159162
}
160163

161164
@ExportMessage
@@ -225,11 +228,10 @@ public static Assumption getSingleContextAssumption() {
225228
return PythonLanguage.getCurrent().singleContextAssumption;
226229
}
227230

228-
@Specialization(guards = "object == cachedObject", limit = "1", assumptions = "singleContextAssumption")
231+
@Specialization(guards = "object == cachedObject.get()", limit = "1", assumptions = "singleContextAssumption")
229232
static Object getNativeClassCachedIdentity(PythonAbstractNativeObject object,
230-
// @Shared("assumption")
231-
@Cached(value = "getSingleContextAssumption()") Assumption singleContextAssumption,
232-
@Exclusive @Cached(value = "object", weak = true) PythonAbstractNativeObject cachedObject,
233+
@Shared("assumption") @Cached(value = "getSingleContextAssumption()") Assumption singleContextAssumption,
234+
@Exclusive @Cached("weak(object)") WeakReference<PythonAbstractNativeObject> cachedObject,
233235
@Exclusive @Cached("getNativeClassUncached(object)") Object cachedClass) {
234236
// TODO: (tfel) is this really something we can do? It's so rare for this class to
235237
// change that it shouldn't be worth the effort, but in native code, anything can
@@ -238,18 +240,17 @@ static Object getNativeClassCachedIdentity(PythonAbstractNativeObject object,
238240
return cachedClass;
239241
}
240242

241-
@Specialization(guards = "lib.isIdentical(cachedObject, object, lib)", assumptions = "getSingleContextAssumption()")
243+
@Specialization(guards = "isSame(referenceLibrary, cachedObject, object)", limit = "1", assumptions = "singleContextAssumption")
242244
static Object getNativeClassCached(PythonAbstractNativeObject object,
243-
@Exclusive @Cached("object") PythonAbstractNativeObject cachedObject,
245+
@Shared("assumption") @Cached(value = "getSingleContextAssumption()") Assumption singleContextAssumption,
246+
@Exclusive @Cached("weak(object)") WeakReference<PythonAbstractNativeObject> cachedObject,
244247
@Exclusive @Cached("getNativeClassUncached(object)") Object cachedClass,
245-
@CachedLibrary(limit = "3") @SuppressWarnings("unused") InteropLibrary lib) {
248+
@CachedLibrary("object.object") @SuppressWarnings("unused") ReferenceLibrary referenceLibrary) {
246249
// TODO same as for 'getNativeClassCachedIdentity'
247250
return cachedClass;
248251
}
249252

250-
@Specialization(guards = {"lib.hasMembers(object.getPtr())"}, // replaces = {// "getNativeClassCached",
251-
// "getNativeClassCachedIdentity"},
252-
limit = "1", rewriteOn = {UnknownIdentifierException.class,
253+
@Specialization(guards = {"lib.hasMembers(object.getPtr())"}, replaces = {"getNativeClassCached", "getNativeClassCachedIdentity"}, limit = "1", rewriteOn = {UnknownIdentifierException.class,
253254
UnsupportedMessageException.class})
254255
static Object getNativeClassByMember(PythonAbstractNativeObject object,
255256
@CachedLibrary("object.getPtr()") InteropLibrary lib,
@@ -260,8 +261,7 @@ static Object getNativeClassByMember(PythonAbstractNativeObject object,
260261
return classProfile.profile(toJavaNode.execute(lib.readMember(object.getPtr(), NativeMember.OB_TYPE.getMemberName())));
261262
}
262263

263-
// @Specialization(replaces = {"getNativeClassCached", "getNativeClassCachedIdentity", "getNativeClassByMember"})
264-
@Specialization(replaces = {"getNativeClassByMember", "getNativeClassCached"})
264+
@Specialization(replaces = {"getNativeClassCached", "getNativeClassCachedIdentity", "getNativeClassByMember"})
265265
static Object getNativeClass(PythonAbstractNativeObject object,
266266
@Exclusive @Cached PCallCapiFunction callGetObTypeNode,
267267
@Exclusive @Cached AsPythonObjectNode toJavaNode,
@@ -270,9 +270,14 @@ static Object getNativeClass(PythonAbstractNativeObject object,
270270
return classProfile.profile(toJavaNode.execute(callGetObTypeNode.call(FUN_GET_OB_TYPE, object.getPtr())));
271271
}
272272

273-
static boolean isSame(InteropLibrary lib, PythonAbstractNativeObject cachedObject, PythonAbstractNativeObject object) {
273+
static WeakReference<PythonAbstractNativeObject> weak(PythonAbstractNativeObject object) {
274+
return new WeakReference<>(object);
275+
}
276+
277+
static boolean isSame(ReferenceLibrary referenceLibrary, WeakReference<PythonAbstractNativeObject> cachedObjectRef, PythonAbstractNativeObject object) {
278+
PythonAbstractNativeObject cachedObject = cachedObjectRef.get();
274279
if (cachedObject != null) {
275-
return lib.isIdentical(cachedObject, object, lib);
280+
return referenceLibrary.isSame(cachedObject.object, object.object);
276281
}
277282
return false;
278283
}
@@ -284,14 +289,19 @@ public static Object getNativeClassUncached(PythonAbstractNativeObject object) {
284289
}
285290

286291
@ExportMessage
287-
int identityHashCode(@CachedLibrary("this.ptr") InteropLibrary lib) throws UnsupportedMessageException {
288-
return lib.identityHashCode(ptr);
289-
}
292+
static class IsSame {
290293

291-
@ExportMessage
292-
TriState isIdenticalOrUndefined(Object other,
293-
@CachedLibrary(limit = "3") InteropLibrary lib) {
294-
return lib.isIdentical(ptr, other, lib) ? TriState.TRUE : TriState.FALSE;
294+
@Specialization
295+
static boolean doNativeObject(PythonAbstractNativeObject receiver, PythonAbstractNativeObject other,
296+
@CachedLibrary("receiver.object") ReferenceLibrary referenceLibrary) {
297+
return referenceLibrary.isSame(receiver.object, other.object);
298+
}
299+
300+
@Fallback
301+
@SuppressWarnings("unused")
302+
static boolean doOther(PythonAbstractNativeObject receiver, Object other) {
303+
return false;
304+
}
295305
}
296306

297307
@ExportMessage(library = PythonObjectLibrary.class, name = "isLazyPythonClass")

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@
4646
import com.oracle.truffle.api.library.CachedLibrary;
4747
import com.oracle.truffle.api.library.ExportLibrary;
4848
import com.oracle.truffle.api.library.ExportMessage;
49-
import com.oracle.truffle.api.utilities.TriState;
5049
import com.oracle.truffle.llvm.spi.NativeTypeLibrary;
50+
import com.oracle.truffle.llvm.spi.ReferenceLibrary;
5151

5252
/**
5353
* A simple wrapper around native {@code NULL}.
5454
*/
5555
@ExportLibrary(InteropLibrary.class)
5656
@ExportLibrary(NativeTypeLibrary.class)
57+
@ExportLibrary(ReferenceLibrary.class)
5758
public class PythonNativeNull implements TruffleObject {
5859
private Object ptr;
5960

@@ -95,14 +96,8 @@ public Object getNativeType() {
9596
}
9697

9798
@ExportMessage(limit = "1")
98-
int identityHashCode(@CachedLibrary("this.getPtr()") InteropLibrary delegateLib) throws UnsupportedMessageException {
99-
return delegateLib.identityHashCode(ptr);
100-
}
101-
102-
@ExportMessage(limit = "1")
103-
TriState isIdenticalOrUndefined(Object other,
104-
@CachedLibrary("this.getPtr()") InteropLibrary delegateLib,
105-
@CachedLibrary("other") InteropLibrary otherLib) {
106-
return delegateLib.isIdentical(ptr, other, otherLib) ? TriState.TRUE : TriState.FALSE;
99+
boolean isSame(Object other,
100+
@CachedLibrary("this.getPtr()") ReferenceLibrary delegateLib) {
101+
return delegateLib.isSame(ptr, other);
107102
}
108103
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import com.oracle.truffle.api.interop.UnsupportedMessageException;
9090
import com.oracle.truffle.api.profiles.BranchProfile;
9191
import com.oracle.truffle.api.profiles.ConditionProfile;
92+
import com.oracle.truffle.llvm.spi.ReferenceLibrary;
9293

9394
public final class CApiContext extends CExtContext {
9495
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(CApiContext.class);
@@ -118,7 +119,7 @@ public final class CApiContext extends CExtContext {
118119
@CompilationFinal(dimensions = 1) private final PrimitiveNativeWrapper[] primitiveNativeWrapperCache;
119120

120121
/** Just used for integrity checks if assertions are enabled. */
121-
@CompilationFinal private InteropLibrary interopLibrary;
122+
@CompilationFinal private ReferenceLibrary referenceLibrary;
122123

123124
/**
124125
* Required to emulate PyLongObject's ABI; number of bits per digit (equal to
@@ -522,14 +523,14 @@ PythonAbstractNativeObject createPythonAbstractNativeObject(TruffleObject native
522523

523524
/**
524525
* Checks if the given {@link NativeObjectReference} objects point to the same native object.
525-
* This method lazily initializes {@link #interopLibrary} as a side-effect.
526+
* This method lazily initializes {@link #referenceLibrary} as a side-effect.
526527
*/
527528
private boolean isReferenceToSameNativeObject(NativeObjectReference old, NativeObjectReference ref) {
528-
if (interopLibrary == null) {
529+
if (referenceLibrary == null) {
529530
CompilerDirectives.transferToInterpreterAndInvalidate();
530-
interopLibrary = InteropLibrary.getFactory().getUncached();
531+
referenceLibrary = ReferenceLibrary.getFactory().getUncached(old.ptrObject);
531532
}
532-
return interopLibrary.isIdentical(old.ptrObject, ref.ptrObject, interopLibrary);
533+
return referenceLibrary.isSame(old.ptrObject, ref.ptrObject);
533534
}
534535

535536
static int idFromRefCnt(long refCnt) {

0 commit comments

Comments
 (0)