Skip to content

Commit 006d79f

Browse files
msimacektimfel
authored andcommitted
Fix caching of slots in LookupNativeSlotNode
1 parent a3aae38 commit 006d79f

File tree

8 files changed

+236
-217
lines changed

8 files changed

+236
-217
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextSlotBuiltins.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,9 +1247,8 @@ Object get(PythonManagedClass object,
12471247
abstract static class Py_get_PyTypeObject_tp_as_mapping extends CApiUnaryBuiltinNode {
12481248

12491249
@Specialization
1250-
public Object get(PythonManagedClass object,
1251-
@Cached LookupNativeSlotNode lookupLen) {
1252-
if (lookupLen.execute(object, SlotMethodDef.MP_LENGTH) != getNULL()) {
1250+
public Object get(PythonManagedClass object) {
1251+
if (LookupNativeSlotNode.executeUncached(object, SlotMethodDef.MP_LENGTH) != getNULL()) {
12531252
return new PyMappingMethodsWrapper(object);
12541253
} else {
12551254
return getNULL();
@@ -1271,9 +1270,8 @@ static Object get(PythonManagedClass object) {
12711270
abstract static class Py_get_PyTypeObject_tp_as_sequence extends CApiUnaryBuiltinNode {
12721271

12731272
@Specialization
1274-
public Object get(PythonManagedClass object,
1275-
@Cached LookupNativeSlotNode lookupLen) {
1276-
if (lookupLen.execute(object, SlotMethodDef.SQ_LENGTH) != getNULL()) {
1273+
public Object get(PythonManagedClass object) {
1274+
if (LookupNativeSlotNode.executeUncached(object, SlotMethodDef.SQ_LENGTH) != getNULL()) {
12771275
return new PySequenceMethodsWrapper(object);
12781276
} else {
12791277
return getNULL();

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

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@
6565
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeMember.TP_NAME;
6666
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeMember.TP_SUBCLASSES;
6767
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeMember.TP_VECTORCALL_OFFSET;
68-
import static com.oracle.graal.python.builtins.objects.cext.capi.SlotMethodDef.TP_CALL;
69-
import static com.oracle.graal.python.builtins.objects.cext.capi.SlotMethodDef.TP_GETATTRO;
70-
import static com.oracle.graal.python.builtins.objects.cext.capi.SlotMethodDef.TP_HASH;
71-
import static com.oracle.graal.python.builtins.objects.cext.capi.SlotMethodDef.TP_INIT;
72-
import static com.oracle.graal.python.builtins.objects.cext.capi.SlotMethodDef.TP_ITER;
73-
import static com.oracle.graal.python.builtins.objects.cext.capi.SlotMethodDef.TP_ITERNEXT;
74-
import static com.oracle.graal.python.builtins.objects.cext.capi.SlotMethodDef.TP_REPR;
75-
import static com.oracle.graal.python.builtins.objects.cext.capi.SlotMethodDef.TP_SETATTRO;
76-
import static com.oracle.graal.python.builtins.objects.cext.capi.SlotMethodDef.TP_STR;
7768
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___BASICSIZE__;
7869
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___DICTOFFSET__;
7970
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___ITEMSIZE__;
@@ -404,12 +395,17 @@ protected static boolean eq(NativeMember expected, String actual) {
404395
return expected.getMemberNameJavaString().equals(actual);
405396
}
406397

398+
protected static boolean eq(SlotMethodDef expected, String actual) {
399+
return expected.getMemberNameJavaString().equals(actual);
400+
}
401+
407402
protected final PythonContext getContext() {
408403
return PythonContext.get(this);
409404
}
410405
}
411406

412407
@GenerateUncached
408+
@ImportStatic(SlotMethodDef.class)
413409
abstract static class ReadTypeNativeMemberNode extends ReadNativeMemberNode {
414410

415411
public static final TruffleString T_SEQUENCE_CLEAR = tsLiteral("sequence_clear");
@@ -514,9 +510,9 @@ Object doTpAsBuffer(PythonManagedClass object, @SuppressWarnings("unused") Pytho
514510

515511
@Specialization(guards = "eq(TP_AS_SEQUENCE, key)")
516512
Object doTpAsSequence(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
517-
@Cached LookupNativeSlotNode lookupLen) {
513+
@Cached(parameters = "SQ_LENGTH") LookupNativeSlotNode lookupLen) {
518514
Object nativeNull = getContext().getNativeNull().getPtr();
519-
if (lookupLen.execute(object, SlotMethodDef.SQ_LENGTH) != nativeNull) {
515+
if (lookupLen.execute(object) != nativeNull) {
520516
return new PySequenceMethodsWrapper(object);
521517
} else {
522518
return nativeNull;
@@ -525,9 +521,9 @@ Object doTpAsSequence(PythonManagedClass object, @SuppressWarnings("unused") Pyt
525521

526522
@Specialization(guards = "eq(TP_AS_MAPPING, key)")
527523
Object doTpAsMapping(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
528-
@Cached LookupNativeSlotNode lookupLen) {
524+
@Cached(parameters = "MP_LENGTH") LookupNativeSlotNode lookupLen) {
529525
Object nativeNull = getContext().getNativeNull().getPtr();
530-
if (lookupLen.execute(object, SlotMethodDef.MP_LENGTH) != nativeNull) {
526+
if (lookupLen.execute(object) != nativeNull) {
531527
return new PyMappingMethodsWrapper(object);
532528
} else {
533529
return nativeNull;
@@ -550,14 +546,14 @@ static Object doTpNew(PythonManagedClass object, @SuppressWarnings("unused") Pyt
550546

551547
@Specialization(guards = "eq(TP_INIT, key)")
552548
static Object doTpInit(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
553-
@Cached LookupNativeSlotNode lookup) {
554-
return lookup.execute(object, TP_INIT);
549+
@Cached(parameters = "TP_INIT") LookupNativeSlotNode lookup) {
550+
return lookup.execute(object);
555551
}
556552

557553
@Specialization(guards = "eq(TP_HASH, key)")
558554
static Object doTpHash(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
559-
@Cached LookupNativeSlotNode lookup) {
560-
return lookup.execute(object, TP_HASH);
555+
@Cached(parameters = "TP_HASH") LookupNativeSlotNode lookup) {
556+
return lookup.execute(object);
561557
}
562558

563559
@Specialization(guards = "eq(TP_BASICSIZE, key)")
@@ -655,38 +651,38 @@ static Object doTpSetattr(@SuppressWarnings("unused") PythonManagedClass object,
655651

656652
@Specialization(guards = "eq(TP_GETATTRO, key)")
657653
static Object doTpGetattro(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
658-
@Cached LookupNativeSlotNode lookup) {
659-
return lookup.execute(object, TP_GETATTRO);
654+
@Cached(parameters = "TP_GETATTRO") LookupNativeSlotNode lookup) {
655+
return lookup.execute(object);
660656
}
661657

662658
@Specialization(guards = "eq(TP_SETATTRO, key)")
663659
static Object doTpSetattro(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
664-
@Cached LookupNativeSlotNode lookup) {
665-
return lookup.execute(object, TP_SETATTRO);
660+
@Cached(parameters = "TP_SETATTRO") LookupNativeSlotNode lookup) {
661+
return lookup.execute(object);
666662
}
667663

668664
@Specialization(guards = "eq(TP_ITER, key)")
669665
static Object doTpIter(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
670-
@Cached LookupNativeSlotNode lookup) {
671-
return lookup.execute(object, TP_ITER);
666+
@Cached(parameters = "TP_ITER") LookupNativeSlotNode lookup) {
667+
return lookup.execute(object);
672668
}
673669

674670
@Specialization(guards = "eq(TP_ITERNEXT, key)")
675671
static Object doTpIternext(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
676-
@Cached LookupNativeSlotNode lookup) {
677-
return lookup.execute(object, TP_ITERNEXT);
672+
@Cached(parameters = "TP_ITERNEXT") LookupNativeSlotNode lookup) {
673+
return lookup.execute(object);
678674
}
679675

680676
@Specialization(guards = "eq(TP_STR, key)")
681677
static Object doTpStr(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
682-
@Cached LookupNativeSlotNode lookup) {
683-
return lookup.execute(object, TP_STR);
678+
@Cached(parameters = "TP_STR") LookupNativeSlotNode lookup) {
679+
return lookup.execute(object);
684680
}
685681

686682
@Specialization(guards = "eq(TP_REPR, key)")
687683
static Object doTpRepr(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
688-
@Cached LookupNativeSlotNode lookup) {
689-
return lookup.execute(object, TP_REPR);
684+
@Cached(parameters = "TP_REPR") LookupNativeSlotNode lookup) {
685+
return lookup.execute(object);
690686
}
691687

692688
@Specialization(guards = "eq(TP_DICT, key)")
@@ -732,8 +728,8 @@ static Object doTpTraverse(PythonManagedClass object, @SuppressWarnings("unused"
732728
@Specialization(guards = "eq(TP_CALL, key)")
733729
@SuppressWarnings("unused")
734730
static Object doTpCall(PythonManagedClass object, PythonNativeWrapper nativeWrapper, String key,
735-
@Cached LookupNativeSlotNode lookup) {
736-
return lookup.execute(object, TP_CALL);
731+
@Cached(parameters = "TP_CALL") LookupNativeSlotNode lookup) {
732+
return lookup.execute(object);
737733
}
738734

739735
@Specialization(guards = "eq(TP_MRO, key)")

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,47 +40,41 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cext.capi;
4242

43-
import static com.oracle.graal.python.builtins.objects.cext.capi.ReadSlotByNameNode.getSlot;
44-
43+
import com.oracle.graal.python.builtins.objects.cext.capi.SlotMethodDef.SlotGroup;
4544
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
4645
import com.oracle.graal.python.runtime.GilNode;
47-
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
48-
import com.oracle.truffle.api.dsl.Bind;
4946
import com.oracle.truffle.api.dsl.Cached;
5047
import com.oracle.truffle.api.dsl.Cached.Exclusive;
48+
import com.oracle.truffle.api.dsl.Cached.Shared;
49+
import com.oracle.truffle.api.dsl.ImportStatic;
5150
import com.oracle.truffle.api.interop.InteropLibrary;
5251
import com.oracle.truffle.api.interop.UnknownIdentifierException;
5352
import com.oracle.truffle.api.interop.UnsupportedMessageException;
5453
import com.oracle.truffle.api.library.ExportLibrary;
5554
import com.oracle.truffle.api.library.ExportMessage;
56-
import com.oracle.truffle.api.nodes.Node;
5755
import com.oracle.truffle.llvm.spi.NativeTypeLibrary;
5856

5957
/**
6058
* Wraps a PythonObject to provide a native view with a shape like {@code PyNumberMethods}.
6159
*/
6260
@ExportLibrary(InteropLibrary.class)
6361
@ExportLibrary(value = NativeTypeLibrary.class, useForAOT = false)
62+
@ImportStatic(SlotGroup.class)
6463
public class PyMappingMethodsWrapper extends PythonNativeWrapper {
6564

66-
@CompilationFinal(dimensions = 1) private static SlotMethodDef[] SLOTS = new SlotMethodDef[]{SlotMethodDef.MP_LENGTH, SlotMethodDef.MP_SUBSCRIPT, SlotMethodDef.MP_ASS_SUBSCRIPT};
67-
6865
public PyMappingMethodsWrapper(PythonManagedClass delegate) {
6966
super(delegate);
7067
}
7168

72-
public PythonManagedClass getPythonClass() {
73-
return (PythonManagedClass) getDelegate();
74-
}
75-
7669
@ExportMessage
7770
protected boolean hasMembers() {
7871
return true;
7972
}
8073

8174
@ExportMessage
82-
protected boolean isMemberReadable(String member) {
83-
return getSlot(member, SLOTS) != null;
75+
protected boolean isMemberReadable(String member,
76+
@Shared("readSlot") @Cached(parameters = "AS_MAPPING") ReadSlotByNameNode readSlotByNameNode) {
77+
return readSlotByNameNode.getSlot(member) != null;
8478
}
8579

8680
@ExportMessage
@@ -90,12 +84,11 @@ protected Object getMembers(@SuppressWarnings("unused") boolean includeInternal)
9084

9185
@ExportMessage
9286
protected Object readMember(String member,
93-
@Bind("$node") Node inliningTarget,
94-
@Cached ReadSlotByNameNode readSlotByNameNode,
87+
@Shared("readSlot") @Cached(parameters = "AS_MAPPING") ReadSlotByNameNode readSlotByNameNode,
9588
@Exclusive @Cached GilNode gil) throws UnknownIdentifierException {
9689
boolean mustRelease = gil.acquire();
9790
try {
98-
Object result = readSlotByNameNode.execute(inliningTarget, this, member, SLOTS);
91+
Object result = readSlotByNameNode.execute(this, member);
9992
if (result == null) {
10093
throw UnknownIdentifierException.create(member);
10194
}

0 commit comments

Comments
 (0)