Skip to content

Commit defbc1d

Browse files
committed
[GR-25637] Small fixes in fallback specializations.
PullRequest: graalpython/1204
2 parents 02d75f4 + 17bc18d commit defbc1d

File tree

6 files changed

+73
-57
lines changed

6 files changed

+73
-57
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2542,7 +2542,8 @@ SequenceStorage doGeneric(SequenceStorage s, int times,
25422542
@Cached("create()") LenNode lenNode) {
25432543
try {
25442544
int len = lenNode.execute(s);
2545-
SequenceStorage repeated = createEmptyNode.execute(s, Math.multiplyExact(len, times), -1);
2545+
int newLen = Math.multiplyExact(len, times);
2546+
SequenceStorage repeated = createEmptyNode.execute(s, newLen, -1);
25462547

25472548
for (int i = 0; i < len; i++) {
25482549
setItemNode.execute(repeated, i, getGetItemNode().execute(s, i));
@@ -2555,6 +2556,7 @@ SequenceStorage doGeneric(SequenceStorage s, int times,
25552556
}
25562557
}
25572558

2559+
repeated.setNewLength(newLen);
25582560
return repeated;
25592561
} catch (OutOfMemoryError | ArithmeticException e) {
25602562
outOfMemProfile.enter();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
public abstract class LookupAndCallBinaryNode extends Node {
7979

8080
public abstract static class NotImplementedHandler extends PNodeWithContext {
81-
public abstract Object execute(Object arg, Object arg2);
81+
public abstract Object execute(VirtualFrame frame, Object arg, Object arg2);
8282
}
8383

8484
protected final String name;
@@ -367,7 +367,7 @@ Object callObject(VirtualFrame frame, Object left, Object right,
367367
Object leftCallable = getattr.execute(frame, leftClass, leftValue);
368368
if (leftCallable == PNone.NO_VALUE) {
369369
if (handlerFactory != null) {
370-
return runErrorHandler(leftValue, right);
370+
return runErrorHandler(frame, leftValue, right);
371371
} else {
372372
return PNotImplemented.NOT_IMPLEMENTED;
373373
}
@@ -428,7 +428,7 @@ Object callObjectR(VirtualFrame frame, Object left, Object right,
428428
result = ensureReverseDispatch().executeObject(frame, rightCallable, rightValue, leftValue);
429429
}
430430
if (handlerFactory != null && result == PNotImplemented.NOT_IMPLEMENTED) {
431-
return runErrorHandler(leftValue, rightValue);
431+
return runErrorHandler(frame, leftValue, rightValue);
432432
}
433433
return result;
434434
}
@@ -445,12 +445,12 @@ Object callObjectRUncached(VirtualFrame frame, Object left, Object right,
445445
return callObjectR(frame, left, right, getattr, getattrR, libLeft, libRight, isSameTypeNode, isSubtype, notImplementedBranch);
446446
}
447447

448-
private Object runErrorHandler(Object left, Object right) {
448+
private Object runErrorHandler(VirtualFrame frame, Object left, Object right) {
449449
if (handler == null) {
450450
CompilerDirectives.transferToInterpreterAndInvalidate();
451451
handler = insert(handlerFactory.get());
452452
}
453-
return handler.execute(left, right);
453+
return handler.execute(frame, left, right);
454454
}
455455

456456
public String getName() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/classes/DeleteClassAttributeNode.java

Lines changed: 22 additions & 9 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
@@ -46,6 +46,7 @@
4646
import com.oracle.graal.python.nodes.argument.ReadIndexedArgumentNode;
4747
import com.oracle.graal.python.nodes.statement.StatementNode;
4848
import com.oracle.graal.python.nodes.subscript.DeleteItemNode;
49+
import com.oracle.truffle.api.dsl.Bind;
4950
import com.oracle.truffle.api.dsl.Cached;
5051
import com.oracle.truffle.api.dsl.Specialization;
5152
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -55,12 +56,13 @@
5556
public abstract class DeleteClassAttributeNode extends StatementNode {
5657
private final String identifier;
5758

58-
@Child private StatementNode deleteNsItem;
59-
6059
DeleteClassAttributeNode(String identifier) {
6160
this.identifier = identifier;
61+
}
62+
63+
protected StatementNode createDeleteNsItem() {
6264
ReadIndexedArgumentNode namespace = ReadIndexedArgumentNode.create(0);
63-
this.deleteNsItem = PythonLanguage.getCurrent().getNodeFactory().createDeleteItem(namespace.asExpression(), this.identifier);
65+
return PythonLanguage.getCurrent().getNodeFactory().createDeleteItem(namespace.asExpression(), identifier);
6466
}
6567

6668
public static DeleteClassAttributeNode create(String name) {
@@ -76,16 +78,27 @@ Object getLocalsDict(VirtualFrame frame) {
7678
return null;
7779
}
7880

79-
@Specialization(guards = "localsDict != null")
80-
void deleteFromLocals(@SuppressWarnings("unused") VirtualFrame frame,
81-
@Cached("getLocalsDict(frame)") Object localsDict,
81+
@Specialization(guards = {"localsDict != null", "getLocalsDict(frame) == localsDict"}, //
82+
assumptions = "singleContextAssumption()", limit = "2")
83+
void deleteFromLocalsSingleCtx(VirtualFrame frame,
84+
@Cached(value = "getLocalsDict(frame)", weak = true) Object localsDict,
85+
@Cached("create()") DeleteItemNode delItemNode) {
86+
// class namespace overrides closure
87+
delItemNode.executeWith(frame, localsDict, identifier);
88+
}
89+
90+
@Specialization(guards = "localsDict != null", replaces = "deleteFromLocalsSingleCtx")
91+
void deleteFromLocals(VirtualFrame frame,
92+
@Bind("getLocalsDict(frame)") Object localsDict,
8293
@Cached("create()") DeleteItemNode delItemNode) {
8394
// class namespace overrides closure
8495
delItemNode.executeWith(frame, localsDict, identifier);
8596
}
8697

87-
@Specialization
88-
void delete(VirtualFrame frame) {
98+
@Specialization(guards = "localsDict == null")
99+
void deleteSingleCtx(VirtualFrame frame,
100+
@SuppressWarnings("unused") @Bind("getLocalsDict(frame)") Object localsDict,
101+
@Cached("createDeleteNsItem()") StatementNode deleteNsItem) {
89102
// delete attribute actual attribute
90103
deleteNsItem.executeVoid(frame);
91104
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public enum BinaryArithmetic {
7979
this.operator = operator;
8080
this.notImplementedHandler = () -> new NotImplementedHandler() {
8181
@Override
82-
public Object execute(Object arg, Object arg2) {
82+
public Object execute(VirtualFrame frame, Object arg, Object arg2) {
8383
throw PRaiseNode.getUncached().raise(TypeError, ErrorMessages.UNSUPPORTED_OPERAND_TYPES_FOR_S_P_AND_P, operator, arg, arg2);
8484
}
8585
};

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/subscript/DeleteItemNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public DeleteItemNode() {
7676
@Child private PRaiseNode raiseNode = PRaiseNode.create();
7777

7878
@Override
79-
public Object execute(Object arg, Object arg2) {
79+
public Object execute(VirtualFrame frame, Object arg, Object arg2) {
8080
throw raiseNode.raise(TypeError, ErrorMessages.OBJ_DOESNT_SUPPORT_DELETION, arg);
8181
}
8282
};

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/subscript/GetItemNode.java

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
4242
import com.oracle.graal.python.nodes.call.CallNode;
4343
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
44+
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode.NotImplementedHandler;
4445
import com.oracle.graal.python.nodes.expression.BinaryOpNode;
4546
import com.oracle.graal.python.nodes.expression.ExpressionNode;
4647
import com.oracle.graal.python.nodes.frame.ReadNode;
@@ -58,8 +59,6 @@
5859
public abstract class GetItemNode extends BinaryOpNode implements ReadNode {
5960
private static final String P_OBJECT_IS_NOT_SUBSCRIPTABLE = "'%p' object is not subscriptable";
6061

61-
@Child private LookupAndCallBinaryNode callGetitemNode;
62-
6362
public ExpressionNode getPrimary() {
6463
return getLeftNode();
6564
}
@@ -124,47 +123,49 @@ public Object doTupleError(VirtualFrame frame, PTuple primary, Object index,
124123
}
125124

126125
@Specialization(replaces = {"doBuiltinList", "doBuiltinTuple"})
127-
Object doAnyObject(VirtualFrame frame, Object primary, Object index) {
128-
if (callGetitemNode == null) {
129-
CompilerDirectives.transferToInterpreterAndInvalidate();
130-
callGetitemNode = insert(LookupAndCallBinaryNode.create(__GETITEM__, null, () -> new LookupAndCallBinaryNode.NotImplementedHandler() {
131-
@Child private IsBuiltinClassProfile isBuiltinClassProfile;
132-
@Child private PRaiseNode raiseNode;
133-
@Child private CallNode callClassGetItemNode;
134-
@Child private GetAttributeNode getClassGetItemNode;
135-
136-
@Override
137-
public Object execute(Object arg, Object arg2) {
138-
if (PGuards.isPythonClass(arg)) {
139-
if (getClassGetItemNode == null) {
140-
CompilerDirectives.transferToInterpreterAndInvalidate();
141-
getClassGetItemNode = insert(GetAttributeNode.create(__CLASS_GETITEM__));
142-
isBuiltinClassProfile = insert(IsBuiltinClassProfile.create());
143-
}
144-
Object classGetItem = null;
145-
try {
146-
classGetItem = getClassGetItemNode.executeObject(frame, arg);
147-
} catch (PException e) {
148-
e.expect(AttributeError, isBuiltinClassProfile);
149-
// fall through to normal error handling
150-
}
151-
if (classGetItem != null) {
152-
if (callClassGetItemNode == null) {
153-
CompilerDirectives.transferToInterpreterAndInvalidate();
154-
callClassGetItemNode = insert(CallNode.create());
155-
}
156-
return callClassGetItemNode.execute(frame, classGetItem, arg2);
157-
}
158-
}
159-
if (raiseNode == null) {
126+
Object doAnyObject(VirtualFrame frame, Object primary, Object index,
127+
@Cached("createGetItemLookupAndCall()") LookupAndCallBinaryNode callGetitemNode) {
128+
return callGetitemNode.executeObject(frame, primary, index);
129+
}
130+
131+
public static LookupAndCallBinaryNode createGetItemLookupAndCall() {
132+
return LookupAndCallBinaryNode.create(__GETITEM__, null, GetItemNodeNotImplementedHandler::new);
133+
}
134+
135+
private static final class GetItemNodeNotImplementedHandler extends NotImplementedHandler {
136+
@Child private IsBuiltinClassProfile isBuiltinClassProfile;
137+
@Child private PRaiseNode raiseNode;
138+
@Child private CallNode callClassGetItemNode;
139+
@Child private GetAttributeNode getClassGetItemNode;
140+
141+
@Override
142+
public Object execute(VirtualFrame frame, Object arg, Object arg2) {
143+
if (PGuards.isPythonClass(arg)) {
144+
if (getClassGetItemNode == null) {
145+
CompilerDirectives.transferToInterpreterAndInvalidate();
146+
getClassGetItemNode = insert(GetAttributeNode.create(__CLASS_GETITEM__));
147+
isBuiltinClassProfile = insert(IsBuiltinClassProfile.create());
148+
}
149+
Object classGetItem = null;
150+
try {
151+
classGetItem = getClassGetItemNode.executeObject(frame, arg);
152+
} catch (PException e) {
153+
e.expect(AttributeError, isBuiltinClassProfile);
154+
// fall through to normal error handling
155+
}
156+
if (classGetItem != null) {
157+
if (callClassGetItemNode == null) {
160158
CompilerDirectives.transferToInterpreterAndInvalidate();
161-
raiseNode = insert(PRaiseNode.create());
159+
callClassGetItemNode = insert(CallNode.create());
162160
}
163-
throw raiseNode.raise(TypeError, P_OBJECT_IS_NOT_SUBSCRIPTABLE, arg);
161+
return callClassGetItemNode.execute(frame, classGetItem, arg2);
164162
}
165-
}));
163+
}
164+
if (raiseNode == null) {
165+
CompilerDirectives.transferToInterpreterAndInvalidate();
166+
raiseNode = insert(PRaiseNode.create());
167+
}
168+
throw raiseNode.raise(TypeError, P_OBJECT_IS_NOT_SUBSCRIPTABLE, arg);
166169
}
167-
return callGetitemNode.executeObject(frame, primary, index);
168170
}
169-
170171
}

0 commit comments

Comments
 (0)