Skip to content

Commit 69b17d0

Browse files
committed
replace ReferenceLibrary with InteropLibrary
1 parent f5ea50b commit 69b17d0

File tree

7 files changed

+75
-84
lines changed

7 files changed

+75
-84
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@
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;
171170

172171
public abstract class CExtNodes {
173172

@@ -2710,12 +2709,12 @@ public abstract static class GetTypeMemberNode extends PNodeWithContext {
27102709
* native context, so we can be sure that the "nativeClassStableAssumption" (which is
27112710
* per-context) is from the context in which this native object was created.
27122711
*/
2713-
@Specialization(guards = {"referenceLibrary.isSame(cachedObj, obj)", "memberName == cachedMemberName"}, //
2712+
@Specialization(guards = {"interopLibrary.isIdentical(cachedObj, obj, interopLibrary)", "memberName == cachedMemberName"}, //
27142713
limit = "1", //
27152714
assumptions = {"getNativeClassStableAssumption(cachedObj)", "singleContextAssumption()"})
27162715
public Object doCachedObj(@SuppressWarnings("unused") PythonAbstractNativeObject obj, @SuppressWarnings("unused") NativeMember memberName,
27172716
@Cached("obj") @SuppressWarnings("unused") PythonAbstractNativeObject cachedObj,
2718-
@CachedLibrary("cachedObj") @SuppressWarnings("unused") ReferenceLibrary referenceLibrary,
2717+
@CachedLibrary(limit = "1") @SuppressWarnings("unused") InteropLibrary interopLibrary, // cachedObj and obj should always be the same type
27192718
@Cached("memberName") @SuppressWarnings("unused") NativeMember cachedMemberName,
27202719
@Cached("doSlowPath(obj, memberName)") Object result) {
27212720
return result;
@@ -3157,13 +3156,12 @@ static PythonNativeWrapper resolveLongCached(@SuppressWarnings("unused") long po
31573156
return cachedValue;
31583157
}
31593158

3160-
@Specialization(limit = "3", //
3161-
guards = {"isSame(referenceLibrary, cachedPointerObject, pointerObject)", "cachedValue != null"}, //
3159+
@Specialization(guards = {"interopLib.isIdentical(cachedPointerObject, pointerObject, interopLib)", "cachedValue != null"}, //
31623160
assumptions = "singleContextAssumption()", //
31633161
rewriteOn = InvalidAssumptionException.class)
31643162
static PythonNativeWrapper resolveObjectCached(@SuppressWarnings("unused") Object pointerObject,
31653163
@Cached("pointerObject") @SuppressWarnings("unused") Object cachedPointerObject,
3166-
@CachedLibrary("cachedPointerObject") @SuppressWarnings("unused") ReferenceLibrary referenceLibrary,
3164+
@CachedLibrary(limit = "3") @SuppressWarnings("unused") InteropLibrary interopLib,
31673165
@Cached("resolveHandleUncached(pointerObject)") PythonNativeWrapper cachedValue,
31683166
@Cached("getHandleValidAssumption(cachedValue)") Assumption associationValidAssumption) throws InvalidAssumptionException {
31693167
associationValidAssumption.check();
@@ -3195,10 +3193,6 @@ static PythonNativeWrapper resolveHandleUncached(Object pointerObject) {
31953193
return null;
31963194
}
31973195

3198-
static boolean isSame(ReferenceLibrary referenceLibrary, Object left, Object right) {
3199-
return referenceLibrary.isSame(left, right);
3200-
}
3201-
32023196
static Assumption singleContextAssumption() {
32033197
return PythonLanguage.getCurrent().singleContextAssumption;
32043198
}

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

Lines changed: 17 additions & 7 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;
175176
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,7 +1461,6 @@ static boolean isModifiableCached(PythonObjectNativeWrapper receiver, String nam
14611461
}
14621462
}
14631463

1464-
@ExportLibrary(ReferenceLibrary.class)
14651464
public static final class PrimitiveNativeWrapper extends DynamicObjectNativeWrapper {
14661465

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

17061705
@ExportMessage
1707-
static class IsSame {
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 {
17081718

17091719
@Specialization
1710-
static boolean doPrimitiveWrapper(PrimitiveNativeWrapper receiver, PrimitiveNativeWrapper other) {
1720+
static TriState doPrimitiveWrapper(PrimitiveNativeWrapper receiver, PrimitiveNativeWrapper other) {
17111721
// This basically emulates singletons for boxed values. However, we need to do so to
17121722
// preserve the invariant that storing an object into a list and getting it out (in
17131723
// the same critical region) returns the same object.
1714-
return other.state == receiver.state && other.value == receiver.value && (other.dvalue == receiver.dvalue || Double.isNaN(receiver.dvalue) && Double.isNaN(other.dvalue));
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;
17151725
}
17161726

17171727
@Fallback
17181728
@SuppressWarnings("unused")
1719-
static boolean doGeneric(PrimitiveNativeWrapper receiver, Object other) {
1720-
return false;
1729+
static TriState doGeneric(PrimitiveNativeWrapper receiver, Object other) {
1730+
return TriState.FALSE;
17211731
}
17221732
}
17231733
}

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

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
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;
4746
import java.util.Objects;
4847

4948
import com.oracle.graal.python.PythonLanguage;
@@ -75,7 +74,6 @@
7574
import com.oracle.truffle.api.dsl.Cached;
7675
import com.oracle.truffle.api.dsl.Cached.Exclusive;
7776
import com.oracle.truffle.api.dsl.Cached.Shared;
78-
import com.oracle.truffle.api.dsl.Fallback;
7977
import com.oracle.truffle.api.dsl.GenerateUncached;
8078
import com.oracle.truffle.api.dsl.Specialization;
8179
import com.oracle.truffle.api.interop.ArityException;
@@ -91,17 +89,16 @@
9189
import com.oracle.truffle.api.object.Shape;
9290
import com.oracle.truffle.api.profiles.ConditionProfile;
9391
import com.oracle.truffle.api.profiles.ValueProfile;
94-
import com.oracle.truffle.llvm.spi.ReferenceLibrary;
92+
import com.oracle.truffle.api.utilities.TriState;
9593

9694
@ExportLibrary(PythonObjectLibrary.class)
97-
@ExportLibrary(ReferenceLibrary.class)
9895
@ExportLibrary(InteropLibrary.class)
9996
public final class PythonAbstractNativeObject extends PythonAbstractObject implements PythonNativeObject, PythonNativeClass {
10097

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

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

107104
public int compareTo(Object o) {
@@ -121,14 +118,14 @@ public void lookupChanged() {
121118
}
122119

123120
public TruffleObject getPtr() {
124-
return object;
121+
return ptr;
125122
}
126123

127124
@Override
128125
public int hashCode() {
129126
CompilerAsserts.neverPartOfCompilation();
130127
// this is important for the default '__hash__' implementation
131-
return Objects.hashCode(object);
128+
return Objects.hashCode(ptr);
132129
}
133130

134131
@Ignore
@@ -141,7 +138,7 @@ public boolean equals(Object obj) {
141138
return false;
142139
}
143140
PythonAbstractNativeObject other = (PythonAbstractNativeObject) obj;
144-
return Objects.equals(object, other.object);
141+
return Objects.equals(ptr, other.ptr);
145142
}
146143

147144
public boolean equalsProfiled(Object obj, ValueProfile profile) {
@@ -152,13 +149,13 @@ public boolean equalsProfiled(Object obj, ValueProfile profile) {
152149
return false;
153150
}
154151
PythonAbstractNativeObject other = (PythonAbstractNativeObject) obj;
155-
return Objects.equals(profile.profile(object), profile.profile(other.object));
152+
return Objects.equals(profile.profile(ptr), profile.profile(other.ptr));
156153
}
157154

158155
@Override
159156
public String toString() {
160157
CompilerAsserts.neverPartOfCompilation();
161-
return String.format("PythonAbstractNativeObject(%s)", object);
158+
return String.format("PythonAbstractNativeObject(%s)", ptr);
162159
}
163160

164161
@ExportMessage
@@ -228,10 +225,11 @@ public static Assumption getSingleContextAssumption() {
228225
return PythonLanguage.getCurrent().singleContextAssumption;
229226
}
230227

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

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

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

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

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();
273+
static boolean isSame(InteropLibrary lib, PythonAbstractNativeObject cachedObject, PythonAbstractNativeObject object) {
279274
if (cachedObject != null) {
280-
return referenceLibrary.isSame(cachedObject.object, object.object);
275+
return lib.isIdentical(cachedObject, object, lib);
281276
}
282277
return false;
283278
}
@@ -289,19 +284,14 @@ public static Object getNativeClassUncached(PythonAbstractNativeObject object) {
289284
}
290285

291286
@ExportMessage
292-
static class IsSame {
293-
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-
}
287+
int identityHashCode(@CachedLibrary("this.ptr") InteropLibrary lib) throws UnsupportedMessageException {
288+
return lib.identityHashCode(ptr);
289+
}
299290

300-
@Fallback
301-
@SuppressWarnings("unused")
302-
static boolean doOther(PythonAbstractNativeObject receiver, Object other) {
303-
return false;
304-
}
291+
@ExportMessage
292+
TriState isIdenticalOrUndefined(Object other,
293+
@CachedLibrary(limit = "3") InteropLibrary lib) {
294+
return lib.isIdentical(ptr, other, lib) ? TriState.TRUE : TriState.FALSE;
305295
}
306296

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

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@
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;
4950
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)
5857
public class PythonNativeNull implements TruffleObject {
5958
private Object ptr;
6059

@@ -96,8 +95,14 @@ public Object getNativeType() {
9695
}
9796

9897
@ExportMessage(limit = "1")
99-
boolean isSame(Object other,
100-
@CachedLibrary("this.getPtr()") ReferenceLibrary delegateLib) {
101-
return delegateLib.isSame(ptr, other);
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;
102107
}
103108
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
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;
9392

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

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

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

524523
/**
525524
* Checks if the given {@link NativeObjectReference} objects point to the same native object.
526-
* This method lazily initializes {@link #referenceLibrary} as a side-effect.
525+
* This method lazily initializes {@link #interopLibrary} as a side-effect.
527526
*/
528527
private boolean isReferenceToSameNativeObject(NativeObjectReference old, NativeObjectReference ref) {
529-
if (referenceLibrary == null) {
528+
if (interopLibrary == null) {
530529
CompilerDirectives.transferToInterpreterAndInvalidate();
531-
referenceLibrary = ReferenceLibrary.getFactory().getUncached(old.ptrObject);
530+
interopLibrary = InteropLibrary.getFactory().getUncached();
532531
}
533-
return referenceLibrary.isSame(old.ptrObject, ref.ptrObject);
532+
return interopLibrary.isIdentical(old.ptrObject, ref.ptrObject, interopLibrary);
534533
}
535534

536535
static int idFromRefCnt(long refCnt) {

0 commit comments

Comments
 (0)