Skip to content

Commit c5c24af

Browse files
committed
store the class again also on the python object, and in multi-context case, not on the shape
1 parent ab87b0a commit c5c24af

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ Object doObjectCachedInstanceShape(@SuppressWarnings("unused") PythonManagedClas
14971497
if (varargs.length > 0 || kwargs.length > 0) {
14981498
// TODO: tfel: this should throw an error only if init isn't overridden
14991499
}
1500-
return factory().createPythonObject(cachedInstanceShape);
1500+
return factory().createPythonObject(self, cachedInstanceShape);
15011501
}
15021502

15031503
@Specialization(guards = "!self.needsNativeAllocation()", replaces = "doObjectCachedInstanceShape")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
import com.oracle.graal.python.nodes.object.GetClassNode;
9494
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
9595
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
96+
import com.oracle.truffle.api.Assumption;
9697
import com.oracle.truffle.api.CompilerDirectives;
9798
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
9899
import com.oracle.truffle.api.dsl.Cached;
@@ -142,6 +143,7 @@ LazyPythonClass setClass(@SuppressWarnings("unused") Object self, @SuppressWarni
142143

143144
@Specialization
144145
PNone setClass(VirtualFrame frame, PythonObject self, PythonAbstractClass value,
146+
@Cached("singleContextAssumption()") Assumption singleContextAssumption,
145147
@Cached("create()") BranchProfile errorValueBranch,
146148
@Cached("create()") BranchProfile errorSelfBranch,
147149
@Cached("create()") BranchProfile errorSlotsBranch,
@@ -163,7 +165,7 @@ PNone setClass(VirtualFrame frame, PythonObject self, PythonAbstractClass value,
163165
throw raise(TypeError, "__class__ assignment: '%s' object layout differs from '%s'", getTypeName(value), getTypeName(lazyClass));
164166
}
165167
}
166-
self.setLazyPythonClass(value);
168+
self.setLazyPythonClass(value, singleContextAssumption);
167169
return PNone.NONE;
168170
}
169171

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/PythonObject.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
3838
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
3939
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
40+
import com.oracle.truffle.api.Assumption;
4041
import com.oracle.truffle.api.CompilerAsserts;
4142
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4243
import com.oracle.truffle.api.object.DynamicObject;
@@ -47,16 +48,20 @@
4748
import com.oracle.truffle.api.object.dsl.Nullable;
4849

4950
public class PythonObject extends PythonAbstractObject {
51+
private LazyPythonClass storedPythonClass;
5052
private final DynamicObject storage;
5153

5254
public PythonObject(LazyPythonClass pythonClass) {
5355
assert pythonClass != null : getClass().getSimpleName();
54-
storage = TypeNodes.GetInstanceShape.doSlowPath(pythonClass).newInstance();
56+
this.storedPythonClass = pythonClass;
57+
this.storage = TypeNodes.GetInstanceShape.doSlowPath(pythonClass).newInstance();
5558
assert getLazyPythonClass() == pythonClass;
5659
}
5760

58-
public PythonObject(Shape instanceShape) {
59-
storage = instanceShape.newInstance();
61+
public PythonObject(LazyPythonClass pythonClass, Shape instanceShape) {
62+
assert pythonClass != null;
63+
this.storedPythonClass = pythonClass;
64+
this.storage = instanceShape.newInstance();
6065
}
6166

6267
public final PythonAbstractClass getPythonClass() {
@@ -69,15 +74,25 @@ public final PythonAbstractClass getPythonClass() {
6974
}
7075
}
7176

72-
public final void setLazyPythonClass(PythonAbstractClass cls) {
73-
PythonObjectLayoutImpl.INSTANCE.setLazyPythonClass(storage, cls);
77+
public final void setLazyPythonClass(PythonAbstractClass cls, Assumption storingClassesInShapes) {
78+
storedPythonClass = cls;
79+
if (storingClassesInShapes.isValid()) {
80+
PythonObjectLayoutImpl.INSTANCE.setLazyPythonClass(storage, cls);
81+
} else {
82+
if (PythonObjectLayoutImpl.INSTANCE.getLazyPythonClass(storage) != null) {
83+
// for the builtin class enums, we now should change the shape
84+
// to the generic one that just doesn't store the class
85+
Shape shape = storage.getShape();
86+
storage.setShapeAndGrow(shape, shape.changeType(emptyShape.getObjectType()));
87+
}
88+
}
7489
}
7590

7691
/**
7792
* This is usually final, a fact may be optimized further if the storage turns into a constant.
7893
*/
7994
public final LazyPythonClass getLazyPythonClass() {
80-
return PythonObjectLayoutImpl.INSTANCE.getLazyPythonClass(storage);
95+
return storedPythonClass;
8196
}
8297

8398
public final DynamicObject getStorage() {
@@ -132,7 +147,7 @@ public String toString() {
132147
}
133148

134149
/**
135-
* Returns the dictionary backed by {@link #storage} (only available for user objects).
150+
* Returns the dictionary (only available for user objects).
136151
*/
137152
public final PHashingCollection getDict() {
138153
return PythonObjectLayoutImpl.INSTANCE.getDict(storage);
@@ -161,10 +176,17 @@ protected static interface PythonObjectLayout {
161176
void setLazyPythonClass(DynamicObject object, LazyPythonClass value);
162177
}
163178

179+
private static final Shape emptyShape = PythonObjectLayoutImpl.INSTANCE.createPythonObjectShape(null).getShape();
180+
164181
public static Shape freshShape(LazyPythonClass klass) {
182+
assert (PythonLanguage.getCurrent().singleContextAssumption.isValid() && klass != null) || klass instanceof PythonBuiltinClassType;
165183
return PythonObjectLayoutImpl.INSTANCE.createPythonObjectShape(klass).getShape();
166184
}
167185

186+
public static Shape freshShape() {
187+
return emptyShape;
188+
}
189+
168190
public static LazyPythonClass getLazyPythonClass(ObjectType type) {
169191
return PythonObjectLayoutImpl.INSTANCE.getLazyPythonClass(type);
170192
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Set;
3434
import java.util.WeakHashMap;
3535

36+
import com.oracle.graal.python.PythonLanguage;
3637
import com.oracle.graal.python.builtins.objects.PNone;
3738
import com.oracle.graal.python.builtins.objects.cext.PythonClassNativeWrapper;
3839
import com.oracle.graal.python.builtins.objects.object.PythonObject;
@@ -64,7 +65,7 @@ public abstract class PythonManagedClass extends PythonObject implements PythonA
6465

6566
@TruffleBoundary
6667
public PythonManagedClass(LazyPythonClass typeClass, String name, PythonAbstractClass... baseClasses) {
67-
super(PythonObject.freshShape(typeClass) /* do not inherit layout from the */);
68+
super(typeClass);
6869
this.className = name;
6970

7071
this.methodResolutionOrder = new MroSequenceStorage(name, 0);
@@ -85,7 +86,11 @@ public PythonManagedClass(LazyPythonClass typeClass, String name, PythonAbstract
8586
setAttribute(__QUALNAME__, className);
8687
setAttribute(__DOC__, PNone.NONE);
8788
// provide our instances with a fresh shape tree
88-
this.instanceShape = PythonObject.freshShape(this);
89+
if (PythonLanguage.getCurrent().singleContextAssumption.isValid()) {
90+
this.instanceShape = PythonObject.freshShape(this);
91+
} else {
92+
this.instanceShape = PythonObject.freshShape();
93+
}
8994
}
9095

9196
private static String getBaseName(String qname) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ public PythonObject createPythonObject(LazyPythonClass cls) {
191191
* Creates a Python object with the given shape. Python object shapes store the class in the
192192
* ObjectType.
193193
*/
194-
public PythonObject createPythonObject(Shape instanceShape) {
195-
return trace(new PythonObject(instanceShape));
194+
public PythonObject createPythonObject(LazyPythonClass klass, Shape instanceShape) {
195+
return trace(new PythonObject(klass, instanceShape));
196196
}
197197

198198
public PythonNativeObject createNativeObjectWrapper(TruffleObject obj) {

0 commit comments

Comments
 (0)