Skip to content

Commit 0111767

Browse files
committed
Unify ReadAttributeFromObjectNode 2 subclasses into ReadAttributeFromObjectNode
* So the correct semantics and the native type optimization is always used, reviewing usages revealed several incorrect usages. * Subclasses make host inlining unable to see through any `@Cached ReadAttributeFromObjectNode`. * Add ReadAttributeFromModuleNode for the simple case of reading from a PythonModule. * Remove extra specialization for PythonModule in ReadAttributeFromObjectNode: * The specialization below readObjectAttribute() does exactly the same optimization, so it is redundant. * Also checked by running micro/function-call-sized.py on jvm-ce-libgraal with and without --engine.Compilation=false that the performance is the same. * Add execute() overloads for ReadAttributeFromObjectNode, more efficent and also clearer which kind of input is passed.
1 parent 3d65724 commit 0111767

25 files changed

+175
-156
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
import com.oracle.graal.python.nodes.StringLiterals;
210210
import com.oracle.graal.python.nodes.argument.ReadArgumentNode;
211211
import com.oracle.graal.python.nodes.attributes.GetFixedAttributeNode;
212+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromModuleNode;
212213
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
213214
import com.oracle.graal.python.nodes.builtins.ListNodes;
214215
import com.oracle.graal.python.nodes.builtins.ListNodes.ConstructListNode;
@@ -1730,7 +1731,7 @@ static Object ord(@SuppressWarnings("unused") Object obj,
17301731
"flush: whether to forcibly flush the stream.")
17311732
@GenerateNodeFactory
17321733
public abstract static class PrintNode extends PythonBuiltinNode {
1733-
@Child private ReadAttributeFromObjectNode readStdout;
1734+
@Child private ReadAttributeFromModuleNode readStdout;
17341735
@CompilationFinal private PythonModule cachedSys;
17351736

17361737
@Specialization
@@ -1836,7 +1837,7 @@ private Object getStdout() {
18361837
}
18371838
if (readStdout == null) {
18381839
CompilerDirectives.transferToInterpreterAndInvalidate();
1839-
readStdout = insert(ReadAttributeFromObjectNode.create());
1840+
readStdout = insert(ReadAttributeFromModuleNode.create());
18401841
}
18411842
Object stdout = readStdout.execute(sys, T_STDOUT);
18421843
if (stdout == NO_VALUE) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.List;
5050
import java.util.Objects;
5151

52+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromModuleNode;
5253
import org.graalvm.collections.EconomicMap;
5354

5455
import com.oracle.graal.python.builtins.Builtin;
@@ -80,7 +81,6 @@
8081
import com.oracle.graal.python.nodes.PNodeWithContext;
8182
import com.oracle.graal.python.nodes.PRaiseNode;
8283
import com.oracle.graal.python.nodes.attributes.GetFixedAttributeNode;
83-
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
8484
import com.oracle.graal.python.nodes.call.CallNode;
8585
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
8686
import com.oracle.graal.python.nodes.function.builtins.PythonSenaryBuiltinNode;
@@ -622,7 +622,7 @@ static Object doSingleContext(
622622
@Specialization(replaces = "doSingleContext")
623623
static Object doRead(
624624
@Bind PythonContext context,
625-
@Cached ReadAttributeFromObjectNode read) {
625+
@Cached ReadAttributeFromModuleNode read) {
626626
PythonModule module = context.lookupBuiltinModule(BuiltinNames.T__SRE);
627627
return read.execute(module, T_MATCH_CONSTRUCTOR);
628628
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
import com.oracle.graal.python.nodes.ErrorMessages;
8686
import com.oracle.graal.python.nodes.PRaiseNode;
8787
import com.oracle.graal.python.nodes.StringLiterals;
88-
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
88+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromModuleNode;
8989
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromPythonObjectNode;
9090
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
9191
import com.oracle.graal.python.nodes.call.CallNode;
@@ -172,7 +172,7 @@ static Object getName(PythonModule module,
172172
@Cached PythonCextBuiltins.PromoteBorrowedValue promoteBorrowedValue,
173173
@Cached PyUnicodeCheckNode pyUnicodeCheckNode,
174174
// CPython reads from the module dict directly
175-
@Cached ReadAttributeFromObjectNode read,
175+
@Cached ReadAttributeFromModuleNode read,
176176
@Cached WriteAttributeToObjectNode write) {
177177
/*
178178
* Even thought the function returns a new reference, CPython assumes that the unicode
@@ -308,7 +308,7 @@ abstract static class PyModule_GetFilenameObject extends CApiUnaryBuiltinNode {
308308
@Specialization
309309
static Object getFilename(PythonModule module,
310310
@Bind Node inliningTarget,
311-
@Cached ReadAttributeFromObjectNode read,
311+
@Cached ReadAttributeFromModuleNode read,
312312
@Cached PyUnicodeCheckNode check,
313313
@Cached PRaiseNode raiseNode) {
314314
Object file = read.execute(module, T___FILE__);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import com.oracle.graal.python.nodes.ErrorMessages;
7575
import com.oracle.graal.python.nodes.PRaiseNode;
7676
import com.oracle.graal.python.nodes.SpecialAttributeNames;
77+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromModuleNode;
7778
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
7879
import com.oracle.graal.python.nodes.call.CallNode;
7980
import com.oracle.graal.python.nodes.util.CannotCastException;
@@ -137,7 +138,7 @@ abstract static class GraalPyPrivate_StructSequence_NewType extends CApiQuaterna
137138
@TruffleBoundary
138139
Object doGeneric(TruffleString typeName, TruffleString typeDoc, Object fields, int nInSequence,
139140
@Cached GraalPyPrivate_StructSequence_InitType2 initNode,
140-
@Cached ReadAttributeFromObjectNode readTypeBuiltinNode,
141+
@Cached ReadAttributeFromModuleNode readTypeBuiltinNode,
141142
@CachedLibrary(limit = "1") DynamicObjectLibrary dylib,
142143
@Cached CallNode callTypeNewNode,
143144
@Bind PythonLanguage language) {
@@ -159,7 +160,7 @@ abstract static class PyStructSequence_New extends CApiUnaryBuiltinNode {
159160
@Specialization
160161
static Object doGeneric(Object cls,
161162
@Bind Node inliningTarget,
162-
@Cached("createForceType()") ReadAttributeFromObjectNode readRealSizeNode,
163+
@Cached ReadAttributeFromObjectNode readRealSizeNode,
163164
@Cached CastToJavaIntExactNode castToIntNode,
164165
@Bind PythonLanguage language,
165166
@Cached TypeNodes.GetInstanceShape getInstanceShape,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/csv/CSVModuleBuiltins.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
import com.oracle.graal.python.nodes.ErrorMessages;
7575
import com.oracle.graal.python.nodes.PRaiseNode;
7676
import com.oracle.graal.python.nodes.SpecialAttributeNames;
77-
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
77+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromModuleNode;
7878
import com.oracle.graal.python.nodes.builtins.ListNodes;
7979
import com.oracle.graal.python.nodes.call.CallNode;
8080
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -134,7 +134,7 @@ public abstract static class CSVRegisterDialectNode extends PythonBuiltinNode {
134134
static PNone register(VirtualFrame frame, PythonModule module, Object nameObj, Object dialectObj, PKeyword[] keywords,
135135
@Bind Node inliningTarget,
136136
@Cached CastToTruffleStringNode nameNode,
137-
@Cached ReadAttributeFromObjectNode readNode,
137+
@Cached ReadAttributeFromModuleNode readNode,
138138
@Cached CallNode callNode,
139139
@Cached PyDictSetItem setItem,
140140
@Cached PRaiseNode raiseNode) {
@@ -164,7 +164,7 @@ public abstract static class CSVUnregisterDialectNode extends PythonBuiltinNode
164164
@Specialization
165165
static PNone unregister(VirtualFrame frame, PythonModule module, Object nameObj,
166166
@Bind Node inliningTarget,
167-
@Cached ReadAttributeFromObjectNode readNode,
167+
@Cached ReadAttributeFromModuleNode readNode,
168168
@Cached PyDictDelItem delItem,
169169
@Cached HashingStorageGetItem getItem,
170170
@Cached PRaiseNode raiseNode) {
@@ -198,7 +198,7 @@ protected static CSVGetDialectNode create() {
198198
static CSVDialect get(VirtualFrame frame, PythonModule module, Object nameObj,
199199
@Bind Node inliningTarget,
200200
@Cached PyDictGetItem getItemNode,
201-
@Cached ReadAttributeFromObjectNode readNode,
201+
@Cached ReadAttributeFromModuleNode readNode,
202202
@Cached PRaiseNode raiseNode) {
203203

204204
// TODO GR-38165: unchecked cast to PDict
@@ -220,7 +220,7 @@ static CSVDialect get(VirtualFrame frame, PythonModule module, Object nameObj,
220220
public abstract static class CSVListDialectsNode extends PythonBuiltinNode {
221221
@Specialization
222222
PList listDialects(VirtualFrame frame, PythonModule module,
223-
@Cached ReadAttributeFromObjectNode readNode,
223+
@Cached ReadAttributeFromModuleNode readNode,
224224
@Cached ListNodes.ConstructListNode constructListNode) {
225225

226226
Object dialects = readNode.execute(module, T__DIALECTS);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/CtypesModuleBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
import com.oracle.graal.python.nodes.PRaiseNode;
146146
import com.oracle.graal.python.nodes.StringLiterals;
147147
import com.oracle.graal.python.nodes.attributes.GetFixedAttributeNode;
148-
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
148+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromModuleNode;
149149
import com.oracle.graal.python.nodes.attributes.WriteAttributeToPythonObjectNode;
150150
import com.oracle.graal.python.nodes.call.CallNode;
151151
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
@@ -269,7 +269,7 @@ public void postInitialize(Python3Core core) {
269269
handle = DlOpenNode.loadNFILibrary(context, NFIBackend.NATIVE, J_DEFAULT_LIBRARY, rtldLocal);
270270
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_WIN32) {
271271
PythonModule sysModule = context.getSysModule();
272-
Object loadLibraryMethod = ReadAttributeFromObjectNode.getUncached().execute(ctypesModule, toTruffleStringUncached("LoadLibrary"));
272+
Object loadLibraryMethod = ReadAttributeFromModuleNode.getUncached().execute(ctypesModule, toTruffleStringUncached("LoadLibrary"));
273273
Object pythonLib = CallNode.executeUncached(loadLibraryMethod, toTruffleStringUncached(PythonContext.getSupportLibName("python-native")), 0);
274274
WriteAttributeToPythonObjectNode.getUncached().execute(sysModule, toTruffleStringUncached("dllhandle"), pythonLib);
275275
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
import com.oracle.graal.python.nodes.argument.keywords.NonMappingException;
127127
import com.oracle.graal.python.nodes.argument.keywords.SameDictKeyException;
128128
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
129+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromModuleNode;
129130
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
130131
import com.oracle.graal.python.nodes.call.CallNode;
131132
import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode;
@@ -1165,7 +1166,7 @@ public abstract static class PKeyInfoNode extends Node {
11651166
@Specialization
11661167
static boolean access(Object object, TruffleString attrKeyName, int type,
11671168
@Bind Node inliningTarget,
1168-
@Cached("createForceType()") ReadAttributeFromObjectNode readTypeAttrNode,
1169+
@Cached ReadAttributeFromObjectNode readTypeAttrNode,
11691170
@Cached ReadAttributeFromObjectNode readObjectAttrNode,
11701171
@Cached PyCallableCheckNode callableCheck,
11711172
@Cached LookupInheritedAttributeNode.Dynamic getGetNode,
@@ -1477,7 +1478,7 @@ public abstract static class ToDisplaySideEffectingNode extends Node {
14771478

14781479
@Specialization
14791480
public static TruffleString doDefault(Node inliningTarget, PythonAbstractObject receiver,
1480-
@Cached(inline = false) ReadAttributeFromObjectNode readStr,
1481+
@Cached ReadAttributeFromModuleNode readStr,
14811482
@Cached(inline = false) CallNode callNode,
14821483
@Cached CastToTruffleStringNode castStr,
14831484
@Cached InlinedConditionProfile toStringUsed) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ public static long lookupNativeI64MemberInMRO(Object cls, CFields nativeMemberNa
943943
if (managedMemberName instanceof HiddenAttr ha) {
944944
attr = HiddenAttr.ReadNode.executeUncached((PythonAbstractObject) mroCls, ha, NO_VALUE);
945945
} else {
946-
attr = ReadAttributeFromObjectNode.getUncachedForceType().execute(mroCls, CompilerDirectives.castExact(managedMemberName, TruffleString.class));
946+
attr = ReadAttributeFromObjectNode.getUncached().execute(mroCls, CompilerDirectives.castExact(managedMemberName, TruffleString.class));
947947
}
948948
if (attr != NO_VALUE) {
949949
return PyNumberAsSizeNode.executeExactUncached(attr);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/getsetdescriptor/MemberDescriptorBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotDescrGet.DescrGetBuiltinNode;
6464
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotDescrSet.DescrSetBuiltinNode;
6565
import com.oracle.graal.python.nodes.BuiltinNames;
66-
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
66+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromModuleNode;
6767
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6868
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
6969
import com.oracle.graal.python.runtime.object.PFactory;
@@ -107,7 +107,7 @@ static TruffleString repr(GetSetDescriptor descr,
107107
abstract static class MemberDescriptorReduceNode extends PythonUnaryBuiltinNode {
108108
@Specialization
109109
Object doGeneric(GetSetDescriptor descr,
110-
@Cached ReadAttributeFromObjectNode readAttributeFromObjectNode,
110+
@Cached ReadAttributeFromModuleNode readAttributeFromObjectNode,
111111
@Cached GetIdNode getIdNode,
112112
@Bind PythonLanguage language) {
113113
Object getattr = readAttributeFromObjectNode.execute(getContext().getBuiltins(), BuiltinNames.T_GETATTR);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/ModuleBuiltins.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import com.oracle.graal.python.nodes.ErrorMessages;
8787
import com.oracle.graal.python.nodes.PNodeWithContext;
8888
import com.oracle.graal.python.nodes.PRaiseNode;
89+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromModuleNode;
8990
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
9091
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
9192
import com.oracle.graal.python.nodes.builtins.ListNodes;
@@ -290,7 +291,8 @@ public final Object execute(VirtualFrame frame, PythonModule self, TruffleString
290291
static Object getattribute(VirtualFrame frame, PythonModule self, TruffleString key, PException e,
291292
@Bind Node inliningTarget,
292293
@Cached IsBuiltinObjectProfile isAttrError,
293-
@Cached ReadAttributeFromObjectNode readGetattr,
294+
@Cached ReadAttributeFromModuleNode readGetattr,
295+
@Cached ReadAttributeFromObjectNode readInitializing,
294296
@Cached InlinedConditionProfile customGetAttr,
295297
@Cached CallNode callNode,
296298
@Cached PyObjectIsTrueNode castToBooleanNode,
@@ -311,7 +313,7 @@ static Object getattribute(VirtualFrame frame, PythonModule self, TruffleString
311313
if (moduleName != null) {
312314
Object moduleSpec = readGetattr.execute(self, T___SPEC__);
313315
if (moduleSpec != PNone.NO_VALUE) {
314-
Object isInitializing = readGetattr.execute(moduleSpec, T__INITIALIZING);
316+
Object isInitializing = readInitializing.execute(moduleSpec, T__INITIALIZING);
315317
if (isInitializing != PNone.NO_VALUE && castToBooleanNode.execute(frame, isInitializing)) {
316318
throw raiseNode.raise(inliningTarget, AttributeError, ErrorMessages.MODULE_PARTIALLY_INITIALIZED_S_HAS_NO_ATTR_S, moduleName, key);
317319
}

0 commit comments

Comments
 (0)