Skip to content

Commit 94422ed

Browse files
timfeltomasstupka
authored andcommitted
mark static bases with a shape flag rather than just instanceof PythonClass
1 parent 0148f1c commit 94422ed

File tree

6 files changed

+50
-16
lines changed

6 files changed

+50
-16
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextStructSeqBuiltins.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.graal.python.builtins.objects.ints.PInt;
6363
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6464
import com.oracle.graal.python.builtins.objects.tuple.StructSequence;
65+
import com.oracle.graal.python.builtins.objects.type.PythonClass;
6566
import com.oracle.graal.python.nodes.BuiltinNames;
6667
import com.oracle.graal.python.nodes.ErrorMessages;
6768
import com.oracle.graal.python.nodes.SpecialAttributeNames;
@@ -84,6 +85,7 @@
8485
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
8586
import com.oracle.truffle.api.interop.UnsupportedMessageException;
8687
import com.oracle.truffle.api.library.CachedLibrary;
88+
import com.oracle.truffle.api.object.DynamicObjectLibrary;
8789

8890
@CoreFunctions(extendsModule = PythonCextBuiltins.PYTHON_CEXT)
8991
@GenerateNodeFactory
@@ -159,6 +161,7 @@ abstract static class PyStructSequenceNewType extends PythonCextBuiltins.NativeB
159161
@Specialization(limit = "1")
160162
Object doGeneric(VirtualFrame frame, String typeName, String typeDoc, Object fieldNamesObj, Object fieldDocsObj, int nInSequence,
161163
@Cached ReadAttributeFromObjectNode readTypeBuiltinNode,
164+
@CachedLibrary(limit = "1") DynamicObjectLibrary dylib,
162165
@Cached CallNode callTypeNewNode,
163166
@CachedLibrary("fieldNamesObj") InteropLibrary lib,
164167
@Cached(parameters = "true") WriteAttributeToObjectNode clearNewNode,
@@ -169,6 +172,9 @@ Object doGeneric(VirtualFrame frame, String typeName, String typeDoc, Object fie
169172
PDict namespace = factory().createDict(new PKeyword[]{new PKeyword(SpecialAttributeNames.__DOC__, typeDoc)});
170173
Object cls = callTypeNewNode.execute(typeBuiltin, typeName, bases, namespace);
171174
PyStructSequenceInitType2.initializeStructType(cls, fieldNamesObj, fieldDocsObj, nInSequence, getLanguage(), lib, clearNewNode);
175+
if (cls instanceof PythonClass) {
176+
((PythonClass) cls).makeStaticBase(dylib);
177+
}
172178
return toNewRefNode.execute(cls);
173179
} catch (PException e) {
174180
transformToNative(frame, e);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
import com.oracle.truffle.api.library.CachedLibrary;
169169
import com.oracle.truffle.api.nodes.ExplodeLoop;
170170
import com.oracle.truffle.api.nodes.Node;
171+
import com.oracle.truffle.api.object.DynamicObjectLibrary;
171172
import com.oracle.truffle.api.object.HiddenKey;
172173
import com.oracle.truffle.api.profiles.BranchProfile;
173174
import com.oracle.truffle.api.profiles.ConditionProfile;
@@ -1922,6 +1923,7 @@ abstract static class HPyCreateTypeFromSpecNode extends Node {
19221923
static Object doGeneric(GraalHPyContext context, Object typeSpec, Object typeSpecParamArray,
19231924
@CachedLibrary(limit = "3") InteropLibrary ptrLib,
19241925
@CachedLibrary(limit = "3") InteropLibrary valueLib,
1926+
@CachedLibrary(limit = "1") DynamicObjectLibrary dylib,
19251927
@Cached FromCharPointerNode fromCharPointerNode,
19261928
@Cached CastToJavaStringNode castToJavaStringNode,
19271929
@Cached PythonObjectFactory factory,
@@ -1996,6 +1998,7 @@ static Object doGeneric(GraalHPyContext context, Object typeSpec, Object typeSpe
19961998
clazz.basicSize = basicSize;
19971999
clazz.flags = flags;
19982000
clazz.itemSize = itemSize;
2001+
clazz.makeStaticBase(dylib);
19992002
}
20002003

20012004
boolean seenNew = false;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public class PythonObject extends PythonAbstractObject {
6262
* Indicates that the object has a dict in the form of an actual dictionary
6363
*/
6464
public static final byte HAS_MATERIALIZED_DICT = 0b1000;
65+
/**
66+
* Indicates that the object is a static base in the CPython's tp_new_wrapper sense.
67+
*
68+
* @see com.oracle.graal.python.nodes.function.builtins.WrapTpNew
69+
*/
70+
public static final byte IS_STATIC_BASE = 0b10000;
6571

6672
private final Object initialPythonClass;
6773

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ public void setDictHiddenProp(DynamicObjectLibrary dylib, BranchProfile hasMroSh
238238
}
239239
}
240240

241+
public void makeStaticBase(DynamicObjectLibrary dylib) {
242+
dylib.setShapeFlags(this, dylib.getShapeFlags(this) | IS_STATIC_BASE);
243+
}
244+
245+
public boolean isStaticBase(DynamicObjectLibrary dylib) {
246+
return (dylib.getShapeFlags(this) & IS_STATIC_BASE) != 0;
247+
}
248+
241249
public MroShape getMroShape() {
242250
return mroShape;
243251
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,8 +1198,8 @@ private static boolean extraivars(Object type, Object base, Object typeSlots) {
11981198
if (typeSlots != null && length(typeSlots) != 0) {
11991199
return true;
12001200
}
1201-
Object typeNewMethod = LookupAttributeInMRONode.lookup(type, __NEW__, GetMroStorageNode.getUncached(), ReadAttributeFromObjectNode.getUncached(), true);
1202-
Object baseNewMethod = LookupAttributeInMRONode.lookup(base, __NEW__, GetMroStorageNode.getUncached(), ReadAttributeFromObjectNode.getUncached(), true);
1201+
Object typeNewMethod = LookupAttributeInMRONode.lookup(type, __NEW__, GetMroStorageNode.getUncached(), ReadAttributeFromObjectNode.getUncached(), true, DynamicObjectLibrary.getUncached());
1202+
Object baseNewMethod = LookupAttributeInMRONode.lookup(base, __NEW__, GetMroStorageNode.getUncached(), ReadAttributeFromObjectNode.getUncached(), true, DynamicObjectLibrary.getUncached());
12031203
return typeNewMethod != baseNewMethod;
12041204
}
12051205

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.oracle.graal.python.builtins.objects.dict.PDict;
5252
import com.oracle.graal.python.builtins.objects.type.MroShape;
5353
import com.oracle.graal.python.builtins.objects.type.MroShape.MroShapeLookupResult;
54+
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
5455
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5556
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
5657
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroStorageNode;
@@ -70,7 +71,9 @@
7071
import com.oracle.truffle.api.dsl.ImportStatic;
7172
import com.oracle.truffle.api.dsl.ReportPolymorphism.Megamorphic;
7273
import com.oracle.truffle.api.dsl.Specialization;
74+
import com.oracle.truffle.api.library.CachedLibrary;
7375
import com.oracle.truffle.api.nodes.ExplodeLoop;
76+
import com.oracle.truffle.api.object.DynamicObjectLibrary;
7477

7578
@ImportStatic(PythonOptions.class)
7679
public abstract class LookupAttributeInMRONode extends LookupInMROBaseNode {
@@ -99,7 +102,7 @@ protected Object lookupInBuiltinType(PythonBuiltinClassType klass, Object key,
99102
protected static Object lookupGeneric(Object klass, Object key,
100103
@Cached GetMroStorageNode getMroNode,
101104
@Cached(value = "createForceType()", uncached = "getUncachedForceType()") ReadAttributeFromObjectNode readAttrNode) {
102-
return lookup(klass, key, getMroNode, readAttrNode, false);
105+
return lookup(klass, key, getMroNode, readAttrNode, false, DynamicObjectLibrary.getUncached());
103106
}
104107

105108
public static LookupAttributeInMRONode.Dynamic create() {
@@ -111,14 +114,14 @@ public static LookupAttributeInMRONode.Dynamic getUncached() {
111114
}
112115
}
113116

114-
private final boolean skipPythonClasses;
117+
private final boolean skipNonStaticBases;
115118
protected final String key;
116119
@Child private TypeNodes.IsSameTypeNode isSameTypeNode = IsSameTypeNodeGen.create();
117120
@Child private GetMroStorageNode getMroNode;
118121

119-
public LookupAttributeInMRONode(String key, boolean skipPythonClasses) {
122+
public LookupAttributeInMRONode(String key, boolean skipNonStaticBases) {
120123
this.key = key;
121-
this.skipPythonClasses = skipPythonClasses;
124+
this.skipNonStaticBases = skipNonStaticBases;
122125
}
123126

124127
public static LookupAttributeInMRONode create(String key) {
@@ -215,7 +218,11 @@ static final class AttributeAssumptionPair {
215218
}
216219
}
217220

218-
protected AttributeAssumptionPair findAttrAndAssumptionInMRO(Object klass) {
221+
private static boolean skipNonStaticBase(Object clsObj, boolean skipNonStaticBases, DynamicObjectLibrary dylib) {
222+
return skipNonStaticBases && clsObj instanceof PythonClass && !((PythonClass) clsObj).isStaticBase(dylib);
223+
}
224+
225+
protected AttributeAssumptionPair findAttrAndAssumptionInMRO(Object klass, DynamicObjectLibrary dylib) {
219226
CompilerAsserts.neverPartOfCompilation();
220227
// - avoid cases when attributes are stored in a dict containing elements
221228
// with a potential MRO sideeffect on access.
@@ -236,12 +243,12 @@ protected AttributeAssumptionPair findAttrAndAssumptionInMRO(Object klass) {
236243
MroSequenceStorage mro = getMro(klass);
237244
Assumption attrAssumption = mro.createAttributeInMROFinalAssumption(key);
238245
for (int i = 0; i < mro.length(); i++) {
239-
Object clsObj = mro.getItemNormalized(i);
246+
PythonAbstractClass clsObj = mro.getItemNormalized(i);
240247
if (i > 0) {
241248
assert clsObj != klass : "MRO chain is incorrect: '" + klass + "' was found at position " + i;
242249
getMro(clsObj).addAttributeInMROFinalAssumption(key, attrAssumption);
243250
}
244-
if (skipPythonClasses && clsObj instanceof PythonClass) {
251+
if (skipNonStaticBase(clsObj, skipNonStaticBases, dylib)) {
245252
continue;
246253
}
247254
Object value = ReadAttributeFromObjectNode.getUncachedForceType().execute(clsObj, key);
@@ -257,7 +264,8 @@ protected AttributeAssumptionPair findAttrAndAssumptionInMRO(Object klass) {
257264
assumptions = "cachedAttrInMROInfo.assumption")
258265
protected static Object lookupConstantMROCached(@SuppressWarnings("unused") Object klass,
259266
@Cached("klass") @SuppressWarnings("unused") Object cachedKlass,
260-
@Cached("findAttrAndAssumptionInMRO(cachedKlass)") AttributeAssumptionPair cachedAttrInMROInfo) {
267+
@CachedLibrary(limit = "1") DynamicObjectLibrary dylib,
268+
@Cached("findAttrAndAssumptionInMRO(cachedKlass, dylib)") AttributeAssumptionPair cachedAttrInMROInfo) {
261269
return cachedAttrInMROInfo.value;
262270
}
263271

@@ -295,10 +303,11 @@ protected Object lookupConstantMRO(@SuppressWarnings("unused") Object klass,
295303
@Cached("getMro(cachedKlass)") MroSequenceStorage mro,
296304
@Cached("mro.getLookupStableAssumption()") @SuppressWarnings("unused") Assumption lookupStable,
297305
@Cached("mro.length()") int mroLength,
306+
@CachedLibrary(limit = "1") DynamicObjectLibrary dylib,
298307
@Cached("create(mroLength)") ReadAttributeFromObjectNode[] readAttrNodes) {
299308
for (int i = 0; i < mroLength; i++) {
300309
Object kls = mro.getItemNormalized(i);
301-
if (skipPythonClasses && kls instanceof PythonClass) {
310+
if (skipNonStaticBase(kls, skipNonStaticBases, dylib)) {
302311
continue;
303312
}
304313
Object value = readAttrNodes[i].execute(kls, key);
@@ -317,10 +326,11 @@ protected Object lookupCachedLen(@SuppressWarnings("unused") Object klass,
317326
@Bind("getMro(klass)") MroSequenceStorage mro,
318327
@Bind("mro.length()") @SuppressWarnings("unused") int mroLength,
319328
@Cached("mro.length()") int cachedMroLength,
329+
@CachedLibrary(limit = "1") DynamicObjectLibrary dylib,
320330
@Cached("create(cachedMroLength)") ReadAttributeFromObjectNode[] readAttrNodes) {
321331
for (int i = 0; i < cachedMroLength; i++) {
322332
Object kls = mro.getItemNormalized(i);
323-
if (skipPythonClasses && kls instanceof PythonClass) {
333+
if (skipNonStaticBase(kls, skipNonStaticBases, dylib)) {
324334
continue;
325335
}
326336
Object value = readAttrNodes[i].execute(kls, key);
@@ -334,8 +344,9 @@ protected Object lookupCachedLen(@SuppressWarnings("unused") Object klass,
334344
@Specialization(replaces = {"lookupConstantMROCached", "lookupConstantMRO", "lookupCachedLen"})
335345
@Megamorphic
336346
protected Object lookupGeneric(Object klass,
347+
@CachedLibrary(limit = "1") DynamicObjectLibrary dylib,
337348
@Cached("createForceType()") ReadAttributeFromObjectNode readAttrNode) {
338-
return lookup(klass, key, ensureGetMroNode(), readAttrNode, skipPythonClasses);
349+
return lookup(klass, key, ensureGetMroNode(), readAttrNode, skipNonStaticBases, dylib);
339350
}
340351

341352
protected GetMroStorageNode ensureGetMroNode() {
@@ -352,14 +363,14 @@ protected MroSequenceStorage getMro(Object clazz) {
352363

353364
@TruffleBoundary
354365
public static Object lookupSlowPath(Object klass, Object key) {
355-
return lookup(klass, key, GetMroStorageNode.getUncached(), ReadAttributeFromObjectNode.getUncachedForceType(), false);
366+
return lookup(klass, key, GetMroStorageNode.getUncached(), ReadAttributeFromObjectNode.getUncachedForceType(), false, DynamicObjectLibrary.getUncached());
356367
}
357368

358-
public static Object lookup(Object klass, Object key, GetMroStorageNode getMroNode, ReadAttributeFromObjectNode readAttrNode, boolean skipPythonClasses) {
369+
public static Object lookup(Object klass, Object key, GetMroStorageNode getMroNode, ReadAttributeFromObjectNode readAttrNode, boolean skipNonStaticBases, DynamicObjectLibrary dylib) {
359370
MroSequenceStorage mro = getMroNode.execute(klass);
360371
for (int i = 0; i < mro.length(); i++) {
361372
Object kls = mro.getItemNormalized(i);
362-
if (skipPythonClasses && kls instanceof PythonClass) {
373+
if (skipNonStaticBase(kls, skipNonStaticBases, dylib)) {
363374
continue;
364375
}
365376
Object value = readAttrNode.execute(kls, key);

0 commit comments

Comments
 (0)