Skip to content

Commit 4ea63df

Browse files
committed
Move remaining direct callers of __HASH__ to use PythonObjectLibrary
1 parent 4484bb9 commit 4ea63df

File tree

4 files changed

+16
-78
lines changed

4 files changed

+16
-78
lines changed

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@
4141
package com.oracle.graal.python.builtins.objects.common;
4242

4343
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
44-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
4544

4645
import java.util.Iterator;
4746

4847
import com.oracle.graal.python.PythonLanguage;
4948
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5049
import com.oracle.graal.python.nodes.PNodeWithContext;
51-
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
5250
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
5351
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5452
import com.oracle.truffle.api.CompilerDirectives.ValueType;
@@ -111,31 +109,6 @@ private boolean objectEquals(Object a, Object b) {
111109
}
112110
};
113111

114-
public static class HashRootNode extends RootNode {
115-
@Child private LookupAndCallUnaryNode callHashNode = LookupAndCallUnaryNode.create(__HASH__);
116-
117-
protected HashRootNode() {
118-
super(PythonLanguage.getCurrent());
119-
Truffle.getRuntime().createCallTarget(this);
120-
}
121-
122-
@Override
123-
public Object execute(VirtualFrame frame) {
124-
Object[] args = frame.getArguments();
125-
return callHashNode.executeObject(frame, args[0]);
126-
}
127-
128-
@Override
129-
public SourceSection getSourceSection() {
130-
return null;
131-
}
132-
133-
@Override
134-
public boolean isCloningAllowed() {
135-
return true;
136-
}
137-
}
138-
139112
private static class EqualsRootNode extends RootNode {
140113
@Child private BinaryComparisonNode callEqNode = BinaryComparisonNode.create(__EQ__, __EQ__, "==");
141114

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.SetItemNodeGen;
7474
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.UnionNodeGen;
7575
import com.oracle.graal.python.builtins.objects.dict.PDict;
76+
import com.oracle.graal.python.builtins.objects.function.PArguments;
7677
import com.oracle.graal.python.builtins.objects.function.PKeyword;
7778
import com.oracle.graal.python.builtins.objects.object.PythonObject;
7879
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
@@ -1147,11 +1148,10 @@ public static GetItemNode create() {
11471148
@Specialization(guards = "lib.isHashable(key)", limit = "1")
11481149
@SuppressWarnings("unused")
11491150
Object doEmptyStorage(VirtualFrame frame, EmptyStorage storage, Object key,
1150-
@Cached("create(__HASH__)") LookupAndCallUnaryNode hashNode,
11511151
@CachedLibrary("key") PythonObjectLibrary lib) {
11521152
// n.b.: we need to call the __hash__ function here for the
11531153
// side-effect to comply with Python semantics.
1154-
hashNode.executeObject(frame, key);
1154+
lib.hashWithState(key, PArguments.getThreadState(frame));
11551155
return null;
11561156
}
11571157

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/random/RandomBuiltins.java

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,15 @@
4949
import com.oracle.graal.python.builtins.PythonBuiltins;
5050
import com.oracle.graal.python.builtins.objects.PNone;
5151
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode;
52+
import com.oracle.graal.python.builtins.objects.function.PArguments;
5253
import com.oracle.graal.python.builtins.objects.ints.PInt;
54+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5355
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
54-
import com.oracle.graal.python.nodes.PGuards;
55-
import com.oracle.graal.python.nodes.SpecialMethodNames;
56-
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
5756
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5857
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
58+
import com.oracle.graal.python.runtime.PythonOptions;
5959
import com.oracle.graal.python.runtime.exception.PythonErrorType;
6060
import com.oracle.truffle.api.CompilerDirectives;
61-
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
6261
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6362
import com.oracle.truffle.api.dsl.Cached;
6463
import com.oracle.truffle.api.dsl.Fallback;
@@ -67,7 +66,6 @@
6766
import com.oracle.truffle.api.dsl.Specialization;
6867
import com.oracle.truffle.api.dsl.TypeSystemReference;
6968
import com.oracle.truffle.api.frame.VirtualFrame;
70-
import com.oracle.truffle.api.nodes.UnexpectedResultException;
7169

7270
@CoreFunctions(extendClasses = PythonBuiltinClassType.PRandom)
7371
public class RandomBuiltins extends PythonBuiltins {
@@ -106,44 +104,18 @@ PNone seed(PRandom random, double inputSeed) {
106104
return PNone.NONE;
107105
}
108106

109-
@CompilationFinal boolean gotUnexpectedHashResult = false;
110-
@Child LookupAndCallUnaryNode callHash;
107+
@Child PythonObjectLibrary objectLib;
111108

112109
@Fallback
113110
PNone seedNonLong(VirtualFrame frame, Object random, Object inputSeed) {
114111
if (random instanceof PRandom) {
115-
if (callHash == null) {
112+
if (objectLib == null) {
116113
CompilerDirectives.transferToInterpreterAndInvalidate();
117-
callHash = insert(LookupAndCallUnaryNode.create(SpecialMethodNames.__HASH__));
118-
}
119-
Object hashResult = null;
120-
if (!gotUnexpectedHashResult) {
121-
try {
122-
long hash = callHash.executeLong(frame, inputSeed);
123-
((PRandom) random).setSeed(hash);
124-
return PNone.NONE;
125-
} catch (UnexpectedResultException e) {
126-
CompilerDirectives.transferToInterpreterAndInvalidate();
127-
gotUnexpectedHashResult = true;
128-
hashResult = e.getResult();
129-
}
130-
}
131-
if (gotUnexpectedHashResult) {
132-
if (hashResult == null) {
133-
hashResult = callHash.executeObject(frame, inputSeed);
134-
}
135-
if (PGuards.isInteger(hashResult)) {
136-
((PRandom) random).setSeed(((Number) hashResult).intValue());
137-
} else if (PGuards.isPInt(hashResult)) {
138-
((PRandom) random).setSeed(((PInt) hashResult).intValue());
139-
} else {
140-
throw raise(PythonErrorType.TypeError, "__hash__ method should return an integer");
141-
}
142-
return PNone.NONE;
143-
} else {
144-
assert false : "cannot reach here";
145-
return PNone.NONE;
114+
objectLib = insert(PythonObjectLibrary.getFactory().createDispatched(PythonOptions.getCallSiteInlineCacheMaxDepth()));
146115
}
116+
long hash = objectLib.hashWithState(inputSeed, PArguments.getThreadState(frame));
117+
((PRandom) random).setSeed(hash);
118+
return PNone.NONE;
147119
} else {
148120
throw raise(PythonErrorType.TypeError, "descriptor 'seed' requires a '_random.Random' object but received a '%p'", random);
149121
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/set/FrozenSetBuiltins.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import com.oracle.graal.python.builtins.CoreFunctions;
4747
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4848
import com.oracle.graal.python.builtins.PythonBuiltins;
49-
import com.oracle.graal.python.builtins.modules.BuiltinFunctions;
5049
import com.oracle.graal.python.builtins.objects.PNone;
5150
import com.oracle.graal.python.builtins.objects.PNotImplemented;
5251
import com.oracle.graal.python.builtins.objects.common.EconomicMapStorage;
@@ -56,13 +55,14 @@
5655
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.PythonEquivalence;
5756
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
5857
import com.oracle.graal.python.builtins.objects.dict.PDictView;
58+
import com.oracle.graal.python.builtins.objects.function.PArguments;
59+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5960
import com.oracle.graal.python.builtins.objects.set.FrozenSetBuiltinsFactory.BinaryUnionNodeGen;
6061
import com.oracle.graal.python.builtins.objects.str.PString;
6162
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6263
import com.oracle.graal.python.nodes.IndirectCallNode;
6364
import com.oracle.graal.python.nodes.PNodeWithContext;
6465
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
65-
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
6666
import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
6767
import com.oracle.graal.python.nodes.control.GetNextNode;
6868
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -71,7 +71,6 @@
7171
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7272
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
7373
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
74-
import com.oracle.graal.python.nodes.util.CoerceToJavaLongNode;
7574
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
7675
import com.oracle.graal.python.runtime.PythonContext;
7776
import com.oracle.graal.python.runtime.exception.PException;
@@ -87,6 +86,7 @@
8786
import com.oracle.truffle.api.dsl.NodeFactory;
8887
import com.oracle.truffle.api.dsl.Specialization;
8988
import com.oracle.truffle.api.frame.VirtualFrame;
89+
import com.oracle.truffle.api.library.CachedLibrary;
9090
import com.oracle.truffle.api.nodes.Node;
9191
import com.oracle.truffle.api.profiles.ConditionProfile;
9292
import com.oracle.truffle.api.profiles.ValueProfile;
@@ -624,9 +624,7 @@ public long getHash(@SuppressWarnings("unused") VirtualFrame frame, PFrozenSet s
624624
public long computeHash(VirtualFrame frame, PFrozenSet self,
625625
@Cached HashingStorageNodes.LenNode getLen,
626626
@Cached HashingStorageNodes.GetItemNode getItemNode,
627-
@Cached("create(__HASH__)") LookupAndCallUnaryNode lookupHashAttributeNode,
628-
@Cached BuiltinFunctions.IsInstanceNode isInstanceNode,
629-
@Cached("createLossy()") CoerceToJavaLongNode castToLongNode) {
627+
@CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib) {
630628
// adapted from https://github.com/python/cpython/blob/master/Objects/setobject.c#L758
631629
HashingStorage storage = self.getDictStorage();
632630
int len = getLen.execute(storage);
@@ -635,15 +633,10 @@ public long computeHash(VirtualFrame frame, PFrozenSet self,
635633
long c1 = 0x3611c3e3;
636634
long c2 = 0x2338c7c1;
637635
long hash = 0;
638-
long tmp;
639636

640637
for (Object key : storage.keys()) {
641638
Object value = getItemNode.execute(frame, storage, key);
642-
Object hashValue = lookupHashAttributeNode.executeObject(frame, value);
643-
if (!isInstanceNode.executeWith(frame, hashValue, getBuiltinPythonClass(PythonBuiltinClassType.PInt))) {
644-
throw raise(PythonErrorType.TypeError, "__hash__ method should return an integer");
645-
}
646-
tmp = castToLongNode.execute(hashValue);
639+
long tmp = lib.hashWithState(value, PArguments.getThreadState(frame));
647640
hash ^= shuffleBits(tmp);
648641
}
649642

0 commit comments

Comments
 (0)