Skip to content

Commit 7685754

Browse files
committed
LookupAndCallInplaceNode: use slots for __iadd__ and __imul__
1 parent 09e6e74 commit 7685754

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/ListBuiltins.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -953,10 +953,7 @@ PNotImplemented doGeneric(Object left, Object right) {
953953

954954
@Builtin(name = __IMUL__, minNumOfPositionalArgs = 2)
955955
@GenerateNodeFactory
956-
abstract static class IMulNode extends PythonBuiltinNode {
957-
958-
public abstract PList execute(VirtualFrame frame, PList list, Object value);
959-
956+
abstract static class IMulNode extends PythonBinaryBuiltinNode {
960957
@Specialization
961958
Object doGeneric(VirtualFrame frame, PList list, Object right,
962959
@Cached ConditionProfile updatedProfile,
@@ -971,7 +968,7 @@ Object doGeneric(VirtualFrame frame, PList list, Object right,
971968
}
972969

973970
protected IMulNode createIMulNode() {
974-
return ListBuiltinsFactory.IMulNodeFactory.create(null);
971+
return ListBuiltinsFactory.IMulNodeFactory.create();
975972
}
976973
}
977974

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GE__;
6767
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GT__;
6868
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
69+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IADD__;
70+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IMUL__;
6971
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
7072
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INIT__;
7173
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INSTANCECHECK__;
@@ -249,6 +251,9 @@ public enum SpecialMethodSlot {
249251
RShift(__RSHIFT__),
250252
RRShift(__RRSHIFT__),
251253

254+
IAdd(__IADD__),
255+
IMul(__IMUL__),
256+
252257
Reversed(__REVERSED__),
253258
Bytes(__BYTES__);
254259

@@ -818,6 +823,10 @@ public static SpecialMethodSlot findSpecialSlot(String name) {
818823
return RShift;
819824
case __RRSHIFT__:
820825
return RRShift;
826+
case __IADD__:
827+
return IAdd;
828+
case __IMUL__:
829+
return IMul;
821830
case __REVERSED__:
822831
return Reversed;
823832
case __BYTES__:

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

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

43-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETATTR__;
44-
4543
import com.oracle.graal.python.builtins.objects.type.SpecialMethodSlot;
4644
import com.oracle.graal.python.nodes.PNodeWithContext;
4745
import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupAndCallReversibleTernaryNode.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public LookupAndCallReversibleTernaryNode(SpecialMethodSlot slot, Supplier<NotIm
8181
@Specialization(guards = "v.getClass() == cachedVClass", limit = "getCallSiteInlineCacheMaxDepth()")
8282
Object callObjectR(VirtualFrame frame, Object v, Object w, Object z,
8383
@SuppressWarnings("unused") @Cached("v.getClass()") Class<?> cachedVClass,
84-
@Cached("createLookup()") LookupSpecialMethodNode getattr,
85-
@Cached("createLookup()") LookupSpecialMethodNode getattrR,
84+
@Cached("createLookup()") LookupSpecialBaseNode getattr,
85+
@Cached("createLookup()") LookupSpecialBaseNode getattrR,
8686
@Cached GetClassNode getClass,
8787
@Cached GetClassNode getClassR,
8888
@Cached IsSubtypeNode isSubtype,
@@ -94,8 +94,8 @@ Object callObjectR(VirtualFrame frame, Object v, Object w, Object z,
9494
@Specialization(replaces = "callObjectR")
9595
@Megamorphic
9696
Object callObjectRMegamorphic(VirtualFrame frame, Object v, Object w, Object z,
97-
@Cached("createLookup()") LookupSpecialMethodNode getattr,
98-
@Cached("createLookup()") LookupSpecialMethodNode getattrR,
97+
@Cached("createLookup()") LookupSpecialBaseNode getattr,
98+
@Cached("createLookup()") LookupSpecialBaseNode getattrR,
9999
@Cached GetClassNode getClass,
100100
@Cached GetClassNode getClassR,
101101
@Cached IsSubtypeNode isSubtype,
@@ -104,7 +104,7 @@ Object callObjectRMegamorphic(VirtualFrame frame, Object v, Object w, Object z,
104104
return doCallObjectR(frame, v, w, z, getattr, getattrR, getClass, getClassR, isSubtype, isSameTypeNode, notImplementedBranch);
105105
}
106106

107-
private Object doCallObjectR(VirtualFrame frame, Object v, Object w, Object z, LookupSpecialMethodNode getattr, LookupSpecialMethodNode getattrR, GetClassNode getClass, GetClassNode getClassR,
107+
private Object doCallObjectR(VirtualFrame frame, Object v, Object w, Object z, LookupSpecialBaseNode getattr, LookupSpecialBaseNode getattrR, GetClassNode getClass, GetClassNode getClassR,
108108
IsSubtypeNode isSubtype, IsSameTypeNode isSameTypeNode, BranchProfile notImplementedBranch) {
109109
// c.f. mostly slot_nb_power and wrap_ternaryfunc_r. like
110110
// cpython://Object/abstract.c#ternary_op we try all three combinations, and the structure

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.oracle.graal.python.builtins.objects.PNone;
4747
import com.oracle.graal.python.builtins.objects.function.PArguments;
4848
import com.oracle.graal.python.builtins.objects.function.Signature;
49+
import com.oracle.graal.python.builtins.objects.type.SpecialMethodSlot;
4950
import com.oracle.graal.python.nodes.ErrorMessages;
5051
import com.oracle.graal.python.nodes.PRaiseNode;
5152
import com.oracle.graal.python.nodes.SpecialMethodNames;
@@ -58,9 +59,9 @@
5859
import com.oracle.truffle.api.nodes.RootNode;
5960

6061
public enum InplaceArithmetic {
61-
IAdd(SpecialMethodNames.__IADD__, "+=", BinaryArithmetic.Add),
62+
IAdd(SpecialMethodSlot.IAdd, "+=", BinaryArithmetic.Add),
6263
ISub(SpecialMethodNames.__ISUB__, "-=", BinaryArithmetic.Sub),
63-
IMul(SpecialMethodNames.__IMUL__, "*=", BinaryArithmetic.Mul),
64+
IMul(SpecialMethodSlot.IMul, "*=", BinaryArithmetic.Mul),
6465
ITrueDiv(SpecialMethodNames.__ITRUEDIV__, "/=", BinaryArithmetic.TrueDiv),
6566
IFloorDiv(SpecialMethodNames.__IFLOORDIV__, "//=", BinaryArithmetic.FloorDiv),
6667
IMod(SpecialMethodNames.__IMOD__, "%=", BinaryArithmetic.Mod),
@@ -73,24 +74,32 @@ public enum InplaceArithmetic {
7374
IMatMul(SpecialMethodNames.__IMATMUL__, "@", BinaryArithmetic.MatMul);
7475

7576
final String methodName;
77+
final SpecialMethodSlot slot;
7678
final String operator;
7779
final boolean isTernary;
7880
final Supplier<LookupAndCallInplaceNode.NotImplementedHandler> notImplementedHandler;
7981
final BinaryArithmetic binary;
8082
final String binaryOpName;
81-
final String reverseBinaryOpName;
8283

8384
InplaceArithmetic(String methodName, String operator, BinaryArithmetic binary) {
8485
this(methodName, operator, binary, false);
8586
}
8687

88+
InplaceArithmetic(SpecialMethodSlot slot, String operator, BinaryArithmetic binary) {
89+
this(slot.getName(), operator, binary, false, slot);
90+
}
91+
8792
InplaceArithmetic(String methodName, String operator, BinaryArithmetic binary, boolean isTernary) {
93+
this(methodName, operator, binary, isTernary, null);
94+
}
95+
96+
InplaceArithmetic(String methodName, String operator, BinaryArithmetic binary, boolean isTernary, SpecialMethodSlot slot) {
8897
this.methodName = methodName;
8998
this.operator = operator;
9099
this.binary = binary;
91100
this.isTernary = isTernary;
92101
this.binaryOpName = methodName.replaceFirst("__i", "__");
93-
this.reverseBinaryOpName = methodName.replaceFirst("__i", "__r");
102+
this.slot = slot;
94103

95104
this.notImplementedHandler = () -> new LookupAndCallInplaceNode.NotImplementedHandler() {
96105
@Child private PRaiseNode raiseNode = PRaiseNode.create();

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@
4444
import com.oracle.graal.python.builtins.objects.PNotImplemented;
4545
import com.oracle.graal.python.nodes.PGuards;
4646
import com.oracle.graal.python.nodes.PNodeWithContext;
47-
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
47+
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
48+
import com.oracle.graal.python.nodes.attributes.LookupCallableSlotInMRONode;
49+
import com.oracle.graal.python.nodes.attributes.LookupInMROBaseNode;
4850
import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode;
4951
import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode;
52+
import com.oracle.graal.python.nodes.object.GetClassNode;
5053
import com.oracle.truffle.api.CompilerDirectives;
5154
import com.oracle.truffle.api.dsl.Cached;
5255
import com.oracle.truffle.api.dsl.NodeChild;
@@ -107,11 +110,20 @@ public final Object execute(VirtualFrame frame, Object left, Object right) {
107110

108111
public abstract Object executeTernary(VirtualFrame frame, Object x, Object y, Object z);
109112

113+
protected final LookupInMROBaseNode createInplaceLookup() {
114+
if (arithmetic.slot != null) {
115+
return LookupCallableSlotInMRONode.create(arithmetic.slot);
116+
} else {
117+
return LookupAttributeInMRONode.create(arithmetic.methodName);
118+
}
119+
}
120+
110121
@Specialization
111122
Object doBinary(VirtualFrame frame, Object left, Object right, Object z,
112-
@Cached("create(arithmetic.methodName)") LookupInheritedAttributeNode getattrInplace) {
123+
@Cached GetClassNode getClassNode,
124+
@Cached("createInplaceLookup()") LookupInMROBaseNode lookupInplace) {
113125
Object result;
114-
Object inplaceCallable = getattrInplace.execute(left);
126+
Object inplaceCallable = lookupInplace.execute(getClassNode.execute(left));
115127
if (inplaceCallable != PNone.NO_VALUE) {
116128
// nb.: The only ternary in-place operator is '__ipow__' but according to 'typeobject.c:
117129
// slot_nb_inplace_power', this is always called as binary.

0 commit comments

Comments
 (0)