Skip to content

Commit 59bbe5d

Browse files
committed
[GR-9747] Don't use GetAttributeNode for lookups without children
PullRequest: graalpython/106
2 parents 7e412cb + bbd727e commit 59bbe5d

File tree

19 files changed

+101
-121
lines changed

19 files changed

+101
-121
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
import com.oracle.truffle.api.nodes.NodeUtil;
106106
import com.oracle.truffle.api.source.Source;
107107

108-
@SuppressWarnings({"unchecked"})
109108
public class TestParserTranslator {
110109
PythonContext context;
111110

@@ -132,7 +131,7 @@ <T> T getChild(Node result, int num, Class<? extends T> klass) {
132131
continue;
133132
}
134133
assertTrue("Expected an instance of " + klass + ", got " + n.getClass(), klass.isInstance(n));
135-
return (T) n;
134+
return klass.cast(n);
136135
}
137136
assertFalse("Expected an instance of " + klass + ", got null", true);
138137
return null;
@@ -158,7 +157,12 @@ Object literalAs(String src, Class<? extends PNode> klass) {
158157
<T> T literalAs(String src, Class<? extends PNode> klass, Class<? extends T> rklass) {
159158
Object literal = literalAs(src, klass);
160159
assertTrue("Expected an instance of " + rklass + ", got " + literal.getClass(), rklass.isInstance(literal));
161-
return (T) literal;
160+
return rklass.cast(literal);
161+
}
162+
163+
<T> T assertInstanceOf(PNode value, Class<? extends T> klass) {
164+
assertTrue("Expected an instance of " + klass + ", got " + value.getClass(), klass.isInstance(value));
165+
return klass.cast(value);
162166
}
163167

164168
@Test
@@ -247,8 +251,8 @@ public void parseGlobalVariable() {
247251
public void parsePropertyAccess() {
248252
parseAs("foobar13_ddsA.property", GetAttributeNode.class);
249253
GetAttributeNode anotherProperty = parseAs("foobar13_ddsA.property.anotherProperty", GetAttributeNode.class);
250-
GetAttributeNode property = getFirstChild(anotherProperty, GetAttributeNode.class);
251-
getFirstChild(property, ReadGlobalOrBuiltinNode.class);
254+
GetAttributeNode property = assertInstanceOf(anotherProperty.getObject(), GetAttributeNode.class);
255+
assertInstanceOf(property.getObject(), ReadGlobalOrBuiltinNode.class);
252256
}
253257

254258
@Test

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
import com.oracle.graal.python.nodes.GraalPythonTranslationErrorNode;
9797
import com.oracle.graal.python.nodes.PGuards;
9898
import com.oracle.graal.python.nodes.SpecialMethodNames;
99-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
10099
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
101100
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
102101
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
@@ -560,18 +559,18 @@ public abstract static class GetAttrNode extends PythonTernaryBuiltinNode {
560559
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", guards = {"name.equals(cachedName)", "isNoValue(defaultValue)"})
561560
public Object getAttrDefault(Object primary, String name, PNone defaultValue,
562561
@Cached("name") String cachedName,
563-
@Cached("create()") GetAttributeNode getter) {
564-
return getter.execute(primary, cachedName);
562+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getter) {
563+
return getter.executeObject(primary, cachedName);
565564
}
566565

567566
@SuppressWarnings("unused")
568567
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", guards = {"name.equals(cachedName)", "!isNoValue(defaultValue)"})
569568
public Object getAttr(Object primary, String name, Object defaultValue,
570569
@Cached("name") String cachedName,
571-
@Cached("create()") GetAttributeNode getter,
570+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getter,
572571
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
573572
try {
574-
return getter.execute(primary, cachedName);
573+
return getter.executeObject(primary, cachedName);
575574
} catch (PException e) {
576575
e.expect(AttributeError, getCore(), errorProfile);
577576
return defaultValue;
@@ -580,16 +579,16 @@ public Object getAttr(Object primary, String name, Object defaultValue,
580579

581580
@Specialization(replaces = {"getAttr", "getAttrDefault"}, guards = "isNoValue(defaultValue)")
582581
public Object getAttrFromObject(Object primary, String name, @SuppressWarnings("unused") PNone defaultValue,
583-
@Cached("create()") GetAttributeNode getter) {
584-
return getter.execute(primary, name);
582+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getter) {
583+
return getter.executeObject(primary, name);
585584
}
586585

587586
@Specialization(replaces = {"getAttr", "getAttrDefault"}, guards = "!isNoValue(defaultValue)")
588587
public Object getAttrFromObject(Object primary, String name, Object defaultValue,
589-
@Cached("create()") GetAttributeNode getter,
588+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getter,
590589
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
591590
try {
592-
return getter.execute(primary, name);
591+
return getter.executeObject(primary, name);
593592
} catch (PException e) {
594593
e.expect(AttributeError, getCore(), errorProfile);
595594
return defaultValue;
@@ -603,13 +602,13 @@ public Object getAttr(Object object, PString name, Object defaultValue) {
603602

604603
@Specialization(guards = "!isString(name)")
605604
public Object getAttrGeneric(Object primary, Object name, Object defaultValue,
606-
@Cached("create()") GetAttributeNode getter,
605+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getter,
607606
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
608607
if (PGuards.isNoValue(defaultValue)) {
609-
return getter.execute(primary, name);
608+
return getter.executeObject(primary, name);
610609
} else {
611610
try {
612-
return getter.execute(primary, name);
611+
return getter.executeObject(primary, name);
613612
} catch (PException e) {
614613
e.expect(AttributeError, getCore(), errorProfile);
615614
return defaultValue;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ abstract static class ExecuteNode extends Node {
7979
private CallDispatchNode getDispatchNode() {
8080
if (dispatch == null) {
8181
CompilerDirectives.transferToInterpreterAndInvalidate();
82-
dispatch = insert(CallDispatchNode.create("<foreign-invoke>"));
82+
dispatch = insert(CallDispatchNode.create());
8383
}
8484
return dispatch;
8585
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171
import com.oracle.graal.python.nodes.PGuards;
7272
import com.oracle.graal.python.nodes.SpecialAttributeNames;
7373
import com.oracle.graal.python.nodes.SpecialMethodNames;
74-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
7574
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
7675
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
76+
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
7777
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
7878
import com.oracle.graal.python.nodes.object.GetClassNode;
7979
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
@@ -348,8 +348,8 @@ Object doState(PString object, @SuppressWarnings("unused") String key) {
348348

349349
@Specialization(guards = "eq(MD_DICT, key)")
350350
Object doMdDict(PythonObject object, @SuppressWarnings("unused") String key,
351-
@Cached("create()") GetAttributeNode getDictNode) {
352-
return getToSulongNode().execute(getDictNode.execute(object, SpecialAttributeNames.__DICT__));
351+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getDictNode) {
352+
return getToSulongNode().execute(getDictNode.executeObject(object, SpecialAttributeNames.__DICT__));
353353
}
354354

355355
@Specialization(guards = "eq(BUF_DELEGATE, key)")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/FunctionBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Object repr(PBuiltinFunction self) {
100100
@Builtin(name = __CALL__, minNumOfArguments = 1, takesVariableArguments = true, takesVariableKeywords = true)
101101
@GenerateNodeFactory
102102
public abstract static class CallNode extends PythonBuiltinNode {
103-
@Child private CallDispatchNode dispatch = CallDispatchNode.create("callCall");
103+
@Child private CallDispatchNode dispatch = CallDispatchNode.create();
104104
@Child private CreateArgumentsNode createArgs = CreateArgumentsNode.create();
105105

106106
@Specialization

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/MethodBuiltins.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
import com.oracle.graal.python.nodes.SpecialAttributeNames;
3838
import com.oracle.graal.python.nodes.SpecialMethodNames;
3939
import com.oracle.graal.python.nodes.argument.CreateArgumentsNode;
40-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
4140
import com.oracle.graal.python.nodes.call.CallDispatchNode;
41+
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
4242
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
4343
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
4444
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -59,7 +59,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
5959
@Builtin(name = __CALL__, minNumOfArguments = 1, takesVariableArguments = true, takesVariableKeywords = true)
6060
@GenerateNodeFactory
6161
public abstract static class CallNode extends PythonBuiltinNode {
62-
@Child private CallDispatchNode dispatch = CallDispatchNode.create("callCall");
62+
@Child private CallDispatchNode dispatch = CallDispatchNode.create();
6363
@Child private CreateArgumentsNode createArgs = CreateArgumentsNode.create();
6464

6565
@Specialization
@@ -106,8 +106,8 @@ protected Object doIt(PBuiltinMethod self) {
106106
public abstract static class NameNode extends PythonBuiltinNode {
107107
@Specialization
108108
protected Object doIt(PMethod self,
109-
@Cached("create()") GetAttributeNode getCode) {
110-
return getCode.execute(self.__func__(), SpecialAttributeNames.__NAME__);
109+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getCode) {
110+
return getCode.executeObject(self.__func__(), SpecialAttributeNames.__NAME__);
111111
}
112112
}
113113

@@ -116,8 +116,8 @@ protected Object doIt(PMethod self,
116116
public abstract static class CodeNode extends PythonBuiltinNode {
117117
@Specialization
118118
protected Object doIt(PMethod self,
119-
@Cached("create()") GetAttributeNode getCode) {
120-
return getCode.execute(self.__func__(), SpecialAttributeNames.__CODE__);
119+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getCode) {
120+
return getCode.executeObject(self.__func__(), SpecialAttributeNames.__CODE__);
121121
}
122122
}
123123

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858
import com.oracle.graal.python.builtins.objects.type.TypeBuiltinsFactory.CallNodeFactory;
5959
import com.oracle.graal.python.nodes.SpecialMethodNames;
6060
import com.oracle.graal.python.nodes.argument.positional.PositionalArgumentsNode;
61-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
6261
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
6362
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
6463
import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
6564
import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode;
65+
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
6666
import com.oracle.graal.python.nodes.classes.AbstractObjectGetBasesNode;
6767
import com.oracle.graal.python.nodes.classes.AbstractObjectIsSubclassNode;
6868
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
@@ -381,7 +381,7 @@ Object dict(PythonClass self) {
381381
@Builtin(name = __INSTANCECHECK__, fixedNumOfArguments = 2)
382382
@GenerateNodeFactory
383383
public static abstract class InstanceCheckNode extends PythonBinaryBuiltinNode {
384-
@Child private GetAttributeNode getAttributeNode = GetAttributeNode.create();
384+
@Child private LookupAndCallBinaryNode getAttributeNode = LookupAndCallBinaryNode.create(__GETATTRIBUTE__);
385385
@Child private AbstractObjectIsSubclassNode abstractIsSubclassNode = AbstractObjectIsSubclassNode.create();
386386
@Child private AbstractObjectGetBasesNode getBasesNode = AbstractObjectGetBasesNode.create();
387387

@@ -392,7 +392,7 @@ public static InstanceCheckNode create() {
392392
}
393393

394394
private PythonObject getInstanceClassAttr(Object instance) {
395-
Object classAttr = getAttributeNode.execute(instance, __CLASS__);
395+
Object classAttr = getAttributeNode.executeObject(instance, __CLASS__);
396396
if (classAttr instanceof PythonObject) {
397397
return (PythonObject) classAttr;
398398
}
@@ -408,7 +408,7 @@ public boolean isInstance(PythonClass cls, Object instance,
408408
return true;
409409
}
410410

411-
Object instanceClass = getAttributeNode.execute(instance, __CLASS__);
411+
Object instanceClass = getAttributeNode.executeObject(instance, __CLASS__);
412412
return instanceClass instanceof PythonClass && isSubtypeNode.execute(instanceClass, cls);
413413
}
414414

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected Object doIt(Object object, Object key,
7070
@Cached("createIdentityProfile()") ValueProfile setattributeProfile,
7171
@Cached("create()") GetClassNode getClassNode,
7272
@Cached("create()") LookupAttributeInMRONode setattributeLookup,
73-
@Cached("create(__DELATTR__)") CallDispatchNode dispatchSetattribute,
73+
@Cached("create()") CallDispatchNode dispatchSetattribute,
7474
@Cached("create()") CreateArgumentsNode createArgs) {
7575
PythonClass type = getClassNode.execute(object);
7676
Object descr = setattributeProfile.profile(setattributeLookup.execute(type, __DELATTR__));

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

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,25 @@
3838
*/
3939
package com.oracle.graal.python.nodes.attributes;
4040

41-
import com.oracle.graal.python.builtins.objects.function.PKeyword;
41+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
42+
4243
import com.oracle.graal.python.nodes.PNode;
4344
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
4445
import com.oracle.graal.python.nodes.frame.ReadNode;
45-
import com.oracle.truffle.api.dsl.Cached;
46-
import com.oracle.truffle.api.dsl.ImportStatic;
4746
import com.oracle.truffle.api.dsl.NodeChild;
4847
import com.oracle.truffle.api.dsl.NodeChildren;
4948
import com.oracle.truffle.api.dsl.Specialization;
50-
import com.oracle.truffle.api.frame.VirtualFrame;
51-
import com.oracle.truffle.api.interop.Message;
49+
import com.oracle.truffle.api.nodes.UnexpectedResultException;
5250

53-
@ImportStatic(Message.class)
5451
@NodeChildren({@NodeChild(value = "object", type = PNode.class), @NodeChild(value = "key", type = PNode.class)})
5552
public abstract class GetAttributeNode extends PNode implements ReadNode {
5653

54+
@Child LookupAndCallBinaryNode dispatchNode = LookupAndCallBinaryNode.create(__GETATTRIBUTE__);
55+
5756
public PNode makeWriteNode(PNode rhs) {
5857
return SetAttributeNode.create(getObject(), getKey(), rhs);
5958
}
6059

61-
public static GetAttributeNode create() {
62-
return create(null, null);
63-
}
64-
6560
public static GetAttributeNode create(PNode object, PNode key) {
6661
return GetAttributeNodeGen.create(object, key);
6762
}
@@ -70,23 +65,18 @@ public static GetAttributeNode create(PNode object, PNode key) {
7065

7166
public abstract PNode getKey();
7267

73-
public abstract Object execute(Object object, Object key);
74-
75-
public abstract Object executeWithObject(Object object);
76-
77-
public Object executeWithKey(VirtualFrame frame, Object key) {
78-
return execute(getObject().execute(frame), key);
68+
@Specialization(rewriteOn = UnexpectedResultException.class)
69+
protected int doItInt(Object object, Object key) throws UnexpectedResultException {
70+
return dispatchNode.executeInt(object, key);
7971
}
8072

81-
@Specialization
82-
protected Object doIt(PKeyword[] keywords, Object key,
83-
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode dispatchNode) {
84-
return dispatchNode.executeObject(factory().createDict(keywords), key);
73+
@Specialization(rewriteOn = UnexpectedResultException.class)
74+
protected boolean doItBoolean(Object object, Object key) throws UnexpectedResultException {
75+
return dispatchNode.executeBool(object, key);
8576
}
8677

87-
@Specialization
88-
protected Object doIt(Object object, Object key,
89-
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode dispatchNode) {
78+
@Specialization(replaces = {"doItInt", "doItBoolean"})
79+
protected Object doIt(Object object, Object key) {
9080
return dispatchNode.executeObject(object, key);
9181
}
9282
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/CallDispatchNode.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,12 @@
4040
@ImportStatic(PythonOptions.class)
4141
public abstract class CallDispatchNode extends Node {
4242

43-
protected final String calleeName;
44-
45-
public CallDispatchNode(String calleeName) {
46-
this.calleeName = calleeName;
47-
}
48-
4943
protected static InvokeNode createInvokeNode(PythonCallable callee) {
5044
return InvokeNode.create(callee);
5145
}
5246

53-
public static CallDispatchNode create(String name) {
54-
return CallDispatchNodeGen.create(name);
55-
}
56-
5747
public static CallDispatchNode create() {
58-
return CallDispatchNodeGen.create("~unspecified");
48+
return CallDispatchNodeGen.create();
5949
}
6050

6151
public abstract Object executeCall(Object callee, Object[] arguments, PKeyword[] keywords);

0 commit comments

Comments
 (0)