Skip to content

Commit 196915e

Browse files
cosminbascalukasstadler
authored andcommitted
on setting class attributes, invalidate the finality assumption up the MRO chain in order to de-specialize from the constant cached class attribute value read
1 parent 5bf34c3 commit 196915e

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
import com.oracle.truffle.api.dsl.Fallback;
4949
import com.oracle.truffle.api.dsl.Specialization;
5050
import com.oracle.truffle.api.nodes.ExplodeLoop;
51+
import com.oracle.truffle.api.object.Location;
52+
import com.oracle.truffle.api.object.Property;
53+
import com.oracle.truffle.api.object.Shape;
5154

5255
public abstract class LookupAttributeInMRONode extends PBaseNode {
5356

@@ -113,12 +116,20 @@ protected PythonClass findClassInMRO(PythonClass klass) {
113116
return null;
114117
}
115118

116-
@Specialization(guards = {"klass == cachedKlass", "cachedAttrKlass != null"}, limit = "5", assumptions = "lookupStable", rewriteOn = IllegalStateException.class)
119+
protected Location getLocationOrNull(Shape shape) {
120+
Property prop = shape.getProperty(key);
121+
return prop == null ? null : prop.getLocation();
122+
}
123+
124+
@Specialization(guards = {"klass == cachedKlass", "cachedAttrKlass != null"}, limit = "5", assumptions = {"lookupStable", "finalAssumption"}, rewriteOn = IllegalStateException.class)
117125
protected Object lookupConstantMROCached(@SuppressWarnings("unused") PythonClass klass,
118126
@Cached("klass") @SuppressWarnings("unused") PythonClass cachedKlass,
119127
@Cached("cachedKlass.getLookupStableAssumption()") @SuppressWarnings("unused") Assumption lookupStable,
120128
@Cached("create()") ReadAttributeFromObjectNode readAttrNode,
121-
@Cached("findClassInMRO(cachedKlass)") @SuppressWarnings("unused") PythonClass cachedAttrKlass) {
129+
@Cached("findClassInMRO(cachedKlass)") PythonClass cachedAttrKlass,
130+
@Cached("cachedAttrKlass.getStorage().getShape()") @SuppressWarnings("unused") Shape cachedShape,
131+
@Cached("getLocationOrNull(cachedShape)") @SuppressWarnings("unused") Location loc,
132+
@Cached("loc.getFinalAssumption()") @SuppressWarnings("unused") Assumption finalAssumption) {
122133
Object value = readAttrNode.execute(cachedAttrKlass, key);
123134
if (value == PNone.NO_VALUE) {
124135
// in case the attribute was deleted

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.oracle.truffle.api.dsl.NodeChildren;
5252
import com.oracle.truffle.api.dsl.Specialization;
5353
import com.oracle.truffle.api.frame.VirtualFrame;
54+
import com.oracle.truffle.api.object.DynamicObject;
5455
import com.oracle.truffle.api.profiles.ValueProfile;
5556

5657
@NodeChildren({@NodeChild(value = "object", type = PNode.class), @NodeChild(value = "key", type = PNode.class), @NodeChild(value = "rhs", type = PNode.class)})
@@ -88,6 +89,23 @@ public PNode getPrimaryNode() {
8889
return getObject();
8990
}
9091

92+
@Specialization
93+
protected Object doClass(PythonClass cls, Object key, Object value,
94+
@Cached("createIdentityProfile()") ValueProfile setattributeProfile,
95+
@Cached("create(__SETATTR__)") LookupAttributeInMRONode setattributeLookup,
96+
@Cached("create()") CallTernaryMethodNode callSetattr) {
97+
Object descr = setattributeProfile.profile(setattributeLookup.execute(cls));
98+
PythonClass[] mro = cls.getMethodResolutionOrder();
99+
for (int i = 0; i < mro.length; i++) {
100+
PythonClass kls = mro[i];
101+
DynamicObject storage = kls.getStorage();
102+
if (storage.containsKey(key)) {
103+
storage.getShape().getProperty(key).getLocation().getFinalAssumption().invalidate();
104+
}
105+
}
106+
return callSetattr.execute(descr, cls, key, value);
107+
}
108+
91109
@Specialization
92110
protected Object doIt(Object object, Object key, Object value,
93111
@Cached("createIdentityProfile()") ValueProfile setattributeProfile,

0 commit comments

Comments
 (0)