Skip to content

Commit 2a20f37

Browse files
committed
[GR-9307] Wrap the body of FunctionRootNode and ModuleRootNode in a node that has the RootTag, so restarting with clearing the frame works
1 parent ae9263b commit 2a20f37

File tree

10 files changed

+111
-48
lines changed

10 files changed

+111
-48
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import com.oracle.graal.python.nodes.frame.WriteNode;
8787
import com.oracle.graal.python.nodes.function.FunctionDefinitionNode;
8888
import com.oracle.graal.python.nodes.function.GeneratorExpressionNode;
89+
import com.oracle.graal.python.nodes.function.InnerRootNode;
8990
import com.oracle.graal.python.nodes.generator.DictConcatNode;
9091
import com.oracle.graal.python.nodes.literal.BooleanLiteralNode;
9192
import com.oracle.graal.python.nodes.literal.ComplexLiteralNode;
@@ -155,6 +156,8 @@ private Node unpackModuleBodyWrappers(Node n) {
155156
if (((WriteLocalVariableNode) n).getIdentifier().equals(FrameSlotIDs.RETURN_SLOT_ID)) {
156157
actual = ((WriteLocalVariableNode) n).getRhs();
157158
}
159+
} else if (n instanceof InnerRootNode) {
160+
actual = n.getChildren().iterator().next();
158161
}
159162
if (actual == n) {
160163
return n;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private static String extractName(RootNode rootNode) {
204204
if (rootNode instanceof ModuleRootNode) {
205205
name = rootNode.getName();
206206
} else if (rootNode instanceof FunctionRootNode) {
207-
name = ((FunctionRootNode) rootNode).getFunctionName();
207+
name = ((FunctionRootNode) rootNode).getName();
208208
} else {
209209
name = rootNode.getName();
210210
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ModuleRootNode.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.oracle.graal.python.PythonLanguage;
2929
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3030
import com.oracle.graal.python.nodes.frame.WriteGlobalNode;
31+
import com.oracle.graal.python.nodes.function.InnerRootNode;
3132
import com.oracle.truffle.api.CompilerAsserts;
3233
import com.oracle.truffle.api.CompilerDirectives;
3334
import com.oracle.truffle.api.frame.FrameDescriptor;
@@ -49,7 +50,7 @@ public ModuleRootNode(PythonLanguage language, String name, String doc, Expressi
4950
super(language, descriptor, freeVarSlots);
5051
this.name = "<module '" + name + "'>";
5152
this.doc = doc;
52-
this.body = file;
53+
this.body = new InnerRootNode(this, file);
5354
}
5455

5556
private WriteGlobalNode getWriteModuleDoc() {
@@ -62,15 +63,15 @@ private WriteGlobalNode getWriteModuleDoc() {
6263

6364
@Override
6465
public Object execute(VirtualFrame frame) {
66+
return body.execute(frame);
67+
}
68+
69+
@Override
70+
public void initializeFrame(VirtualFrame frame) {
6571
addClosureCellsToLocals(frame);
6672
if (doc != null) {
6773
getWriteModuleDoc().doWrite(frame, doc);
6874
}
69-
return body.execute(frame);
70-
}
71-
72-
public ExpressionNode getBody() {
73-
return body;
7475
}
7576

7677
@Override
@@ -84,10 +85,6 @@ public String getName() {
8485
return name;
8586
}
8687

87-
public String getDoc() {
88-
return doc;
89-
}
90-
9188
@Override
9289
public SourceSection getSourceSection() {
9390
return body.getSourceSection();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/NodeFactory.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,15 @@ public <T> T duplicate(Node orig, Class<T> clazz) {
134134
}
135135

136136
public ModuleRootNode createModuleRoot(String name, String doc, ExpressionNode file, FrameDescriptor fd) {
137-
file.markAsRoot();
138137
return new ModuleRootNode(language, name, doc, file, fd, null);
139138
}
140139

141140
public FunctionRootNode createFunctionRoot(SourceSection sourceSection, String functionName, boolean isGenerator, FrameDescriptor frameDescriptor, ExpressionNode body,
142141
ExecutionCellSlots cellSlots) {
143-
body.markAsRoot();
144142
return new FunctionRootNode(language, sourceSection, functionName, isGenerator, frameDescriptor, body, cellSlots);
145143
}
146144

147145
public ClassBodyRootNode createClassBodyRoot(SourceSection sourceSection, String functionName, FrameDescriptor frameDescriptor, ExpressionNode body, ExecutionCellSlots cellSlots) {
148-
body.markAsRoot();
149146
return new ClassBodyRootNode(language, sourceSection, functionName, frameDescriptor, body, cellSlots);
150147
}
151148

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PClosureRootNode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.oracle.truffle.api.frame.Frame;
4848
import com.oracle.truffle.api.frame.FrameDescriptor;
4949
import com.oracle.truffle.api.frame.FrameSlot;
50+
import com.oracle.truffle.api.frame.VirtualFrame;
5051
import com.oracle.truffle.api.nodes.ExplodeLoop;
5152

5253
public abstract class PClosureRootNode extends PRootNode {
@@ -89,6 +90,8 @@ public boolean hasFreeVars() {
8990
return freeVarSlots != null && freeVarSlots.length > 0;
9091
}
9192

93+
public abstract void initializeFrame(VirtualFrame frame);
94+
9295
public String[] getFreeVars() {
9396
if (freeVarSlots != null) {
9497
String[] freeVars = new String[freeVarSlots.length];

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PNode.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -32,18 +32,13 @@
3232
import com.oracle.truffle.api.dsl.ImportStatic;
3333
import com.oracle.truffle.api.dsl.TypeSystemReference;
3434
import com.oracle.truffle.api.instrumentation.InstrumentableNode;
35-
import com.oracle.truffle.api.instrumentation.StandardTags;
36-
import com.oracle.truffle.api.instrumentation.Tag;
3735
import com.oracle.truffle.api.source.SourceSection;
3836

3937
@TypeSystemReference(PythonTypes.class)
4038
@ImportStatic({PGuards.class, PythonOptions.class, SpecialMethodNames.class, SpecialAttributeNames.class, BuiltinNames.class})
4139
public abstract class PNode extends PNodeWithContext implements InstrumentableNode {
42-
4340
public static final PNode[] EMPTY_ARRAY = new PNode[0];
44-
4541
@CompilationFinal private SourceSection sourceSection;
46-
@CompilationFinal private boolean isRoot = false;
4742

4843
@Override
4944
public String toString() {
@@ -68,19 +63,7 @@ public void assignSourceSection(SourceSection source) {
6863
this.sourceSection = source;
6964
}
7065

71-
public boolean hasTag(Class<? extends Tag> tag) {
72-
return isRoot && tag == StandardTags.RootTag.class;
73-
}
74-
7566
public boolean isInstrumentable() {
7667
return getSourceSection() != null;
7768
}
78-
79-
public void markAsRoot() {
80-
isRoot = true;
81-
}
82-
83-
public boolean isRoot() {
84-
return isRoot;
85-
}
8669
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/FunctionRootNode.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,11 @@ public FunctionRootNode(PythonLanguage language, SourceSection sourceSection, St
7373
assert sourceSection != null;
7474
this.functionName = functionName;
7575
this.isGenerator = isGenerator;
76-
this.body = NodeUtil.cloneNode(body);
76+
this.body = new InnerRootNode(this, NodeUtil.cloneNode(body));
7777
this.uninitializedBody = NodeUtil.cloneNode(body);
7878
this.generatorFrameProfile = isGenerator ? ValueProfile.createClassProfile() : null;
7979
}
8080

81-
public String getFunctionName() {
82-
return functionName;
83-
}
84-
85-
public ExpressionNode getBody() {
86-
return body;
87-
}
88-
89-
public ExpressionNode getUninitializedBody() {
90-
return uninitializedBody;
91-
}
92-
9381
@Override
9482
public String getName() {
9583
return functionName;
@@ -145,7 +133,6 @@ public Object execute(VirtualFrame frame) {
145133
if (CompilerDirectives.inInterpreter() || CompilerDirectives.inCompilationRoot()) {
146134
contextRef.get().triggerAsyncActions();
147135
}
148-
initClosureAndCellVars(frame);
149136
return body.execute(frame);
150137
}
151138

@@ -167,4 +154,9 @@ public boolean isRewritten() {
167154
public void setRewritten() {
168155
this.isRewritten = true;
169156
}
157+
158+
@Override
159+
public void initializeFrame(VirtualFrame frame) {
160+
initClosureAndCellVars(frame);
161+
}
170162
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.nodes.function;
42+
43+
import com.oracle.graal.python.nodes.PClosureRootNode;
44+
import com.oracle.graal.python.nodes.expression.ExpressionNode;
45+
import com.oracle.truffle.api.frame.VirtualFrame;
46+
import com.oracle.truffle.api.instrumentation.StandardTags;
47+
import com.oracle.truffle.api.instrumentation.Tag;
48+
49+
/**
50+
* The sole purpose of this class is to handle the pre-amble of the root node, so that frame
51+
* restarting works when instrumented.
52+
*/
53+
public class InnerRootNode extends ExpressionNode {
54+
@Child private ExpressionNode body;
55+
private final PClosureRootNode root;
56+
57+
public InnerRootNode(PClosureRootNode functionRootNode, ExpressionNode body) {
58+
this.body = body;
59+
this.assignSourceSection(body.getSourceSection());
60+
this.root = functionRootNode;
61+
}
62+
63+
@Override
64+
public Object execute(VirtualFrame frame) {
65+
root.initializeFrame(frame);
66+
return body.execute(frame);
67+
}
68+
69+
@Override
70+
public boolean hasTag(Class<? extends Tag> tag) {
71+
return StandardTags.RootTag.class == tag;
72+
}
73+
74+
@Override
75+
public boolean hasSideEffectAsAnExpression() {
76+
return true;
77+
}
78+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator/GeneratorFunctionRootNode.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -87,4 +87,9 @@ public RootNode getFunctionRootNode() {
8787
public String getName() {
8888
return name;
8989
}
90+
91+
@Override
92+
public void initializeFrame(VirtualFrame frame) {
93+
// nothing to do
94+
}
9095
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/GeneratorTranslator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -46,6 +46,7 @@
4646
import com.oracle.graal.python.nodes.frame.WriteNode;
4747
import com.oracle.graal.python.nodes.function.FunctionRootNode;
4848
import com.oracle.graal.python.nodes.function.GeneratorExpressionNode;
49+
import com.oracle.graal.python.nodes.function.InnerRootNode;
4950
import com.oracle.graal.python.nodes.generator.AbstractYieldNode;
5051
import com.oracle.graal.python.nodes.generator.GeneratorBlockNode;
5152
import com.oracle.graal.python.nodes.generator.GeneratorControlNode;
@@ -316,6 +317,10 @@ private void replaceControl(PNode node) {
316317

317318
} else if (node instanceof StatementNode) {
318319
// do nothing for now
320+
321+
} else if (node instanceof InnerRootNode) {
322+
// nothing to do
323+
319324
} else {
320325
replaceYieldExpression(node);
321326
}

0 commit comments

Comments
 (0)