Skip to content

Commit e6f0d17

Browse files
committed
Refactor ReadCallerFrameNode
1 parent e21e40e commit e6f0d17

19 files changed

+240
-326
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ public ExecutableNode parse(InlineParsingRequest request) {
786786
public Object execute(VirtualFrame frame) {
787787
Object[] arguments = PArguments.create();
788788
PFrame pFrame = materializeFrameNode.execute(this, false, true, frame);
789-
Object pLocals = getFrameLocalsNode.executeCached(pFrame);
789+
Object pLocals = getFrameLocalsNode.executeCached(frame, pFrame);
790790
PArguments.setSpecialArgument(arguments, pLocals);
791791
PArguments.setGlobals(arguments, PArguments.getGlobals(frame));
792792
boolean wasAcquired = gilNode.acquire();

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@
235235
import com.oracle.graal.python.nodes.call.special.SpecialMethodNotFound;
236236
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
237237
import com.oracle.graal.python.nodes.frame.GetFrameLocalsNode;
238-
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
238+
import com.oracle.graal.python.nodes.frame.ReadFrameNode;
239239
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
240240
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
241241
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -315,7 +315,6 @@
315315
import com.oracle.truffle.api.nodes.LoopNode;
316316
import com.oracle.truffle.api.nodes.Node;
317317
import com.oracle.truffle.api.nodes.RootNode;
318-
import com.oracle.truffle.api.profiles.ConditionProfile;
319318
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
320319
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
321320
import com.oracle.truffle.api.profiles.InlinedCountingConditionProfile;
@@ -805,15 +804,15 @@ abstract static class CreateEvalExecArgumentsNode extends Node {
805804

806805
@Specialization
807806
static Object[] inheritGlobals(VirtualFrame frame, Node inliningTarget, @SuppressWarnings("unused") PNone globals, Object locals, TruffleString mode,
808-
@Exclusive @Cached ReadCallerFrameNode readCallerFrameNode,
807+
@Exclusive @Cached ReadFrameNode readFrameNode,
809808
@Exclusive @Cached GetOrCreateDictNode getOrCreateDictNode,
810809
@Exclusive @Cached InlinedConditionProfile haveCallerFrameProfile,
811810
@Exclusive @Cached InlinedConditionProfile inheritLocalsProfile,
812811
@Exclusive @Cached PyMappingCheckNode mappingCheckNode,
813812
@Exclusive @Cached GetFrameLocalsNode getFrameLocalsNode,
814813
@Exclusive @Cached PRaiseNode raiseNode) {
815814
boolean inheritLocals = inheritLocalsProfile.profile(inliningTarget, locals instanceof PNone);
816-
PFrame callerFrame = readCallerFrameNode.executeWith(frame, 0, inheritLocals);
815+
PFrame callerFrame = readFrameNode.getCurrentPythonFrame(frame, inheritLocals);
817816
Object[] args = PArguments.create();
818817
boolean haveCallerFrame = haveCallerFrameProfile.profile(inliningTarget, callerFrame != null);
819818
if (haveCallerFrame) {
@@ -823,7 +822,7 @@ static Object[] inheritGlobals(VirtualFrame frame, Node inliningTarget, @Suppres
823822
}
824823
if (inheritLocals) {
825824
if (haveCallerFrame) {
826-
Object callerLocals = getFrameLocalsNode.execute(inliningTarget, callerFrame);
825+
Object callerLocals = getFrameLocalsNode.execute(frame, inliningTarget, callerFrame);
827826
setCustomLocals(args, callerLocals);
828827
} else {
829828
setCustomLocals(args, PArguments.getGlobals(args));
@@ -981,8 +980,8 @@ public final PCode compile(VirtualFrame frame, Object source, TruffleString file
981980
protected abstract Object executeInternal(VirtualFrame frame, Object source, TruffleString filename, TruffleString mode, int flags, boolean dontInherit, int optimize,
982981
int featureVersion);
983982

984-
private int inheritFlags(VirtualFrame frame, int flags, ReadCallerFrameNode readCallerFrame) {
985-
PFrame fr = readCallerFrame.executeWith(frame, 0, false);
983+
private int inheritFlags(VirtualFrame frame, int flags, ReadFrameNode readCallerFrame) {
984+
PFrame fr = readCallerFrame.getCurrentPythonFrame(frame);
986985
if (fr != null) {
987986
PCode code = PFactory.createCode(PythonLanguage.get(this), fr.getTarget());
988987
flags |= code.getFlags() & PyCF_MASK;
@@ -993,7 +992,7 @@ private int inheritFlags(VirtualFrame frame, int flags, ReadCallerFrameNode read
993992
@Specialization
994993
Object doCompile(VirtualFrame frame, TruffleString expression, TruffleString filename, TruffleString mode, int flags, boolean dontInherit, int optimize,
995994
int featureVersion,
996-
@Shared @Cached ReadCallerFrameNode readCallerFrame,
995+
@Shared @Cached ReadFrameNode readCallerFrame,
997996
@Exclusive @Cached("createFor($node)") BoundaryCallData boundaryCallData) {
998997
if (!dontInherit) {
999998
flags = inheritFlags(frame, flags, readCallerFrame);
@@ -1084,7 +1083,7 @@ Object generic(VirtualFrame frame, Object wSource, Object wFilename, TruffleStri
10841083
@CachedLibrary("wSource") InteropLibrary interopLib,
10851084
@Cached PyUnicodeFSDecoderNode asPath,
10861085
@Cached TruffleString.SwitchEncodingNode switchEncodingNode,
1087-
@Shared @Cached ReadCallerFrameNode readCallerFrame,
1086+
@Shared @Cached ReadFrameNode readCallerFrame,
10881087
@Cached PRaiseNode raiseNode) {
10891088
if (wSource instanceof PCode) {
10901089
return wSource;
@@ -2257,19 +2256,12 @@ private static long maybeInt(Node inliningTarget, InlinedConditionProfile result
22572256
@Builtin(name = "globals", needsFrame = true, callerFlags = CallerFlags.NEEDS_PFRAME)
22582257
@GenerateNodeFactory
22592258
abstract static class GlobalsNode extends PythonBuiltinNode {
2260-
private final ConditionProfile condProfile = ConditionProfile.create();
22612259

22622260
@Specialization
22632261
public Object globals(VirtualFrame frame,
22642262
@Bind Node inliningTarget,
2265-
@Cached PyEvalGetGlobals getGlobals,
2266-
@Cached GetOrCreateDictNode getDict) {
2267-
Object globals = getGlobals.execute(frame, inliningTarget);
2268-
if (condProfile.profile(globals instanceof PythonModule)) {
2269-
return getDict.execute(inliningTarget, globals);
2270-
} else {
2271-
return globals;
2272-
}
2263+
@Cached PyEvalGetGlobals getGlobals) {
2264+
return getGlobals.execute(frame, inliningTarget);
22732265
}
22742266
}
22752267

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

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@
175175
import com.oracle.graal.python.builtins.objects.exception.ExceptionNodes;
176176
import com.oracle.graal.python.builtins.objects.exception.GetEscapedExceptionNode;
177177
import com.oracle.graal.python.builtins.objects.frame.PFrame;
178-
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
179178
import com.oracle.graal.python.builtins.objects.function.PArguments;
180179
import com.oracle.graal.python.builtins.objects.function.PKeyword;
181180
import com.oracle.graal.python.builtins.objects.ints.PInt;
@@ -218,11 +217,10 @@
218217
import com.oracle.graal.python.nodes.call.CallNode;
219218
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
220219
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode.NoAttributeHandler;
221-
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
220+
import com.oracle.graal.python.nodes.frame.ReadFrameNode;
222221
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
223222
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
224223
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
225-
import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
226224
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
227225
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
228226
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
@@ -260,7 +258,6 @@
260258
import com.oracle.truffle.api.frame.VirtualFrame;
261259
import com.oracle.truffle.api.nodes.Node;
262260
import com.oracle.truffle.api.profiles.BranchProfile;
263-
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
264261
import com.oracle.truffle.api.strings.TruffleString;
265262

266263
@CoreFunctions(defineModule = "sys", isEager = true)
@@ -853,37 +850,27 @@ static Object run(VirtualFrame frame,
853850
}
854851
}
855852

856-
// ATTENTION: this is intentionally a PythonBuiltinNode and not PythonUnaryBuiltinNode,
857-
// because we need a guarantee that this builtin will get its own stack frame in order to
858-
// be able to count how many frames down the call stack we need to walk
859853
@Builtin(name = "_getframe", parameterNames = "depth", minNumOfPositionalArgs = 0, needsFrame = true, callerFlags = CallerFlags.NEEDS_PFRAME)
860854
@ArgumentClinic(name = "depth", defaultValue = "0", conversion = ClinicConversion.Int)
861855
@GenerateNodeFactory
862-
public abstract static class GetFrameNode extends PythonClinicBuiltinNode {
856+
public abstract static class GetFrameNode extends PythonUnaryClinicBuiltinNode {
863857
@Override
864858
protected ArgumentClinicProvider getArgumentClinic() {
865859
return GetFrameNodeClinicProviderGen.INSTANCE;
866860
}
867861

868862
@Specialization
869-
static PFrame counted(VirtualFrame frame, int num,
863+
static PFrame counted(VirtualFrame frame, int depth,
870864
@Bind Node inliningTarget,
871-
@Cached ReadCallerFrameNode readCallerNode,
872-
@Cached InlinedConditionProfile callStackDepthProfile,
865+
@Cached ReadFrameNode readFrameNode,
873866
@Cached PRaiseNode raiseNode) {
874-
PFrame requested = escapeFrame(frame, num, readCallerNode);
875-
if (callStackDepthProfile.profile(inliningTarget, requested == null)) {
867+
PFrame requested = readFrameNode.getFrameForReference(frame, PArguments.getCurrentFrameInfo(frame), ReadFrameNode.VisiblePythonFramesSelector.INSTANCE, depth, false);
868+
if (requested == null) {
876869
throw raiseNode.raise(inliningTarget, ValueError, ErrorMessages.CALL_STACK_NOT_DEEP_ENOUGH);
877870
}
871+
requested.getRef().markAsEscaped();
878872
return requested;
879873
}
880-
881-
private static PFrame escapeFrame(VirtualFrame frame, int num, ReadCallerFrameNode readCallerNode) {
882-
Reference currentFrameInfo = PArguments.getCurrentFrameInfo(frame);
883-
currentFrameInfo.markAsEscaped();
884-
return readCallerNode.executeWith(currentFrameInfo, num, false);
885-
}
886-
887874
}
888875

889876
@Builtin(name = "_current_frames")
@@ -894,14 +881,14 @@ Object currentFrames(VirtualFrame frame,
894881
@Bind Node inliningTarget,
895882
@Cached AuditNode auditNode,
896883
@Cached WarningsModuleBuiltins.WarnNode warnNode,
897-
@Cached ReadCallerFrameNode readCallerFrameNode,
884+
@Cached ReadFrameNode readFrameNode,
898885
@Cached HashingStorageSetItem setHashingStorageItem,
899886
@Bind PythonLanguage language) {
900887
auditNode.audit(inliningTarget, "sys._current_frames");
901888
if (!getLanguage().singleThreadedAssumption.isValid()) {
902889
warnNode.warn(frame, RuntimeWarning, ErrorMessages.WARN_CURRENT_FRAMES_MULTITHREADED);
903890
}
904-
PFrame currentFrame = readCallerFrameNode.executeWith(frame, 0, false);
891+
PFrame currentFrame = readFrameNode.getCurrentPythonFrame(frame);
905892
PDict result = PFactory.createDict(language);
906893
result.setDictStorage(setHashingStorageItem.execute(frame, inliningTarget, result.getDictStorage(), PThread.getThreadId(Thread.currentThread()), currentFrame));
907894
return result;

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,15 @@
5858
import com.oracle.graal.python.builtins.PythonBuiltins;
5959
import com.oracle.graal.python.builtins.objects.PNone;
6060
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ToArrayNode;
61-
import com.oracle.graal.python.builtins.objects.frame.FrameBuiltins.GetGlobalsNode;
62-
import com.oracle.graal.python.builtins.objects.frame.PFrame;
63-
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
64-
import com.oracle.graal.python.builtins.objects.function.PArguments;
6561
import com.oracle.graal.python.builtins.objects.function.PKeyword;
6662
import com.oracle.graal.python.builtins.objects.module.PythonModule;
6763
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6864
import com.oracle.graal.python.builtins.objects.typing.PTypeVarTuple;
65+
import com.oracle.graal.python.lib.PyEvalGetGlobals;
6966
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
7067
import com.oracle.graal.python.lib.PyObjectGetAttr;
7168
import com.oracle.graal.python.lib.PyObjectGetItem;
7269
import com.oracle.graal.python.nodes.call.CallNode;
73-
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
7470
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7571
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7672
import com.oracle.graal.python.nodes.statement.AbstractImportNode;
@@ -209,14 +205,10 @@ public abstract static class CallerNode extends Node {
209205
public abstract Object execute(VirtualFrame frame, Node inliningTarget);
210206

211207
@Specialization
212-
static Object caller(VirtualFrame frame,
213-
@Cached(inline = false) GetGlobalsNode getGlobalsNode,
208+
static Object caller(VirtualFrame frame, Node inliningTarget,
214209
@Cached(inline = false) PyObjectCallMethodObjArgs callMethod,
215-
@Cached(inline = false) ReadCallerFrameNode readCallerNode) {
216-
Reference currentFrameInfo = PArguments.getCurrentFrameInfo(frame);
217-
PFrame pFrame = readCallerNode.executeWith(currentFrameInfo, 0, false);
218-
Object globals = getGlobalsNode.execute(frame, pFrame);
219-
210+
@Cached PyEvalGetGlobals getGlobalsNode) {
211+
Object globals = getGlobalsNode.execute(frame, inliningTarget);
220212
return callMethod.executeCached(frame, globals, T_GET, T___NAME__, PNone.NONE);
221213
}
222214

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
import com.oracle.graal.python.nodes.SpecialAttributeNames;
107107
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromPythonObjectNode;
108108
import com.oracle.graal.python.nodes.call.CallNode;
109-
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
109+
import com.oracle.graal.python.nodes.frame.ReadFrameNode;
110110
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
111111
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
112112
import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
@@ -216,7 +216,7 @@ static final class WarningsModuleNode extends Node {
216216
@Child IsSubClassNode isSubClassNode;
217217
@Child GetOrCreateDictNode getDictNode;
218218
@Child GetDictFromGlobalsNode getDictFromGlobalsNode;
219-
@Child ReadCallerFrameNode readCallerNode;
219+
@Child ReadFrameNode readFrameNode;
220220
@Child PyObjectLookupAttr lookupAttrNode;
221221
@Child PyObjectCallMethodObjArgs callMethodNode;
222222
@Child PyDictGetItem dictGetItemNode;
@@ -408,13 +408,12 @@ private PDict getGlobalsDict(Object globals) {
408408
}
409409

410410
private PFrame getCallerFrame(VirtualFrame frame, int stackLevel, TruffleString[] skipFilePrefixes) {
411-
if (readCallerNode == null) {
411+
if (readFrameNode == null) {
412412
CompilerDirectives.transferToInterpreterAndInvalidate();
413413
reportPolymorphicSpecialize();
414-
readCallerNode = insert(ReadCallerFrameNode.create());
414+
readFrameNode = insert(ReadFrameNode.create());
415415
}
416-
PFrame.Reference ref = PArguments.getCurrentFrameInfo(frame);
417-
ReadCallerFrameNode.FrameSelector selector = ReadCallerFrameNode.SkipInternalFramesSelector.INSTANCE;
416+
ReadFrameNode.FrameSelector selector = ReadFrameNode.VisiblePythonFramesSelector.INSTANCE;
418417
if (skipFilePrefixes != null) {
419418
/*
420419
* CPython would always count the first frame into the stacklevel even if it is
@@ -423,9 +422,9 @@ private PFrame getCallerFrame(VirtualFrame frame, int stackLevel, TruffleString[
423422
* always skipped by the filter.
424423
*/
425424
stackLevel--;
426-
selector = rootNode -> ReadCallerFrameNode.SkipInternalFramesSelector.INSTANCE.skip(rootNode) || isFilenameToSkip(skipFilePrefixes, rootNode);
425+
selector = rootNode -> ReadFrameNode.VisiblePythonFramesSelector.INSTANCE.skip(rootNode) || isFilenameToSkip(skipFilePrefixes, rootNode);
427426
}
428-
return readCallerNode.executeWith(ref, selector, stackLevel, false);
427+
return readFrameNode.getFrameForReference(frame, PArguments.getCurrentFrameInfo(frame), selector, stackLevel, false);
429428
}
430429

431430
@TruffleBoundary

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@
7272
import com.oracle.graal.python.builtins.objects.function.Signature;
7373
import com.oracle.graal.python.builtins.objects.module.PythonModule;
7474
import com.oracle.graal.python.builtins.objects.object.PythonObject;
75+
import com.oracle.graal.python.lib.PyEvalGetGlobals;
7576
import com.oracle.graal.python.nodes.ErrorMessages;
7677
import com.oracle.graal.python.nodes.PGuards;
7778
import com.oracle.graal.python.nodes.PRaiseNode;
7879
import com.oracle.graal.python.nodes.argument.CreateArgumentsNode;
7980
import com.oracle.graal.python.nodes.call.CallDispatchers;
80-
import com.oracle.graal.python.nodes.frame.GetCurrentFrameRef;
81-
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
81+
import com.oracle.graal.python.nodes.frame.ReadFrameNode;
8282
import com.oracle.graal.python.nodes.object.GetDictIfExistsNode;
8383
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
8484
import com.oracle.graal.python.runtime.GilNode;
@@ -143,11 +143,9 @@ Object release(
143143
abstract static class PyEval_GetFrame extends CApiNullaryBuiltinNode {
144144
@Specialization
145145
Object getFrame(
146-
@Bind Node inliningTarget,
147-
@Cached GetCurrentFrameRef getCurrentFrameRef,
148-
@Cached ReadCallerFrameNode readCallerFrameNode) {
149-
PFrame.Reference reference = getCurrentFrameRef.execute(null, inliningTarget);
150-
return readCallerFrameNode.executeWith(reference, 0, false);
146+
@Cached ReadFrameNode readFrameNode) {
147+
PFrame pFrame = readFrameNode.getCurrentPythonFrame(null);
148+
return pFrame != null ? pFrame : getNativeNull();
151149
}
152150
}
153151

@@ -212,11 +210,9 @@ abstract static class PyEval_GetGlobals extends CApiNullaryBuiltinNode {
212210
@Specialization
213211
Object get(
214212
@Bind Node inliningTarget,
215-
@Cached GetCurrentFrameRef getCurrentFrameRef,
216-
@Cached ReadCallerFrameNode readCallerFrameNode) {
217-
PFrame.Reference frameRef = getCurrentFrameRef.execute(null, inliningTarget);
218-
PFrame pFrame = readCallerFrameNode.executeWith(frameRef, 0, false);
219-
return pFrame.getGlobals();
213+
@Cached PyEvalGetGlobals getGlobals) {
214+
PythonObject globals = getGlobals.execute(null, inliningTarget);
215+
return globals != null ? globals : getNativeNull();
220216
}
221217
}
222218
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ abstract static class PyFrame_GetLocals extends CApiUnaryBuiltinNode {
9393
static Object get(PFrame frame,
9494
@Bind Node inliningTarget,
9595
@Cached GetFrameLocalsNode getFrameLocalsNode) {
96-
return getFrameLocalsNode.execute(inliningTarget, frame);
96+
return getFrameLocalsNode.execute(null, inliningTarget, frame);
9797
}
9898
}
9999

0 commit comments

Comments
 (0)