Skip to content

Commit 138d01c

Browse files
committed
share the PCode instance across function definition nodes in the single context case
1 parent 561c8f8 commit 138d01c

File tree

7 files changed

+65
-35
lines changed

7 files changed

+65
-35
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,27 +1945,27 @@ public abstract static class FunctionNode extends PythonBuiltinNode {
19451945

19461946
@Specialization
19471947
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, @SuppressWarnings("unused") PNone closure) {
1948-
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, null);
1948+
return factory().createFunction(name, getTypeName(cls), code, globals, null);
19491949
}
19501950

19511951
@Specialization
19521952
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, PTuple closure,
19531953
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
1954-
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, (PCell[]) getObjectArrayNode.execute(closure));
1954+
return factory().createFunction(name, getTypeName(cls), code, globals, (PCell[]) getObjectArrayNode.execute(closure));
19551955
}
19561956

19571957
@Specialization
19581958
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, PTuple defaultArgs, @SuppressWarnings("unused") PNone closure,
19591959
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
19601960
// TODO split defaults of positional args from kwDefaults
1961-
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, getObjectArrayNode.execute(defaultArgs), null, null);
1961+
return factory().createFunction(name, getTypeName(cls), code, globals, getObjectArrayNode.execute(defaultArgs), null, null);
19621962
}
19631963

19641964
@Specialization
19651965
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, PTuple defaultArgs, PTuple closure,
19661966
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
19671967
// TODO split defaults of positional args from kwDefaults
1968-
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, getObjectArrayNode.execute(defaultArgs), null, (PCell[]) getObjectArrayNode.execute(closure));
1968+
return factory().createFunction(name, getTypeName(cls), code, globals, getObjectArrayNode.execute(defaultArgs), null, (PCell[]) getObjectArrayNode.execute(closure));
19691969
}
19701970

19711971
@Fallback

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,10 +1863,9 @@ public boolean visit(Node node) {
18631863
});
18641864

18651865
String name = func.getName();
1866-
builtinFunc =
1867-
1868-
factory().createFunction(name, func.getEnclosingClassName(), Truffle.getRuntime().createCallTarget(functionRootNode),
1869-
func.getGlobals(), func.getDefaults(), func.getKwDefaults(), func.getClosure());
1866+
builtinFunc = factory().createFunction(name, func.getEnclosingClassName(),
1867+
new PCode(PythonBuiltinClassType.PCode, Truffle.getRuntime().createCallTarget(functionRootNode)),
1868+
func.getGlobals(), func.getDefaults(), func.getKwDefaults(), func.getClosure());
18701869
}
18711870

18721871
return builtinFunc;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PFunction.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
2929
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__QUALNAME__;
3030

31-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3231
import com.oracle.graal.python.builtins.objects.cell.PCell;
3332
import com.oracle.graal.python.builtins.objects.code.PCode;
3433
import com.oracle.graal.python.builtins.objects.object.PythonObject;
@@ -63,20 +62,20 @@ public class PFunction extends PythonObject {
6362
@CompilationFinal(dimensions = 1) private Object[] defaultValues;
6463
@CompilationFinal(dimensions = 1) private PKeyword[] kwDefaultValues;
6564

66-
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, PCell[] closure) {
67-
this(clazz, name, enclosingClassName, callTarget, globals, EMPTY_DEFAULTS, PKeyword.EMPTY_KEYWORDS, closure);
65+
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, PCode code, PythonObject globals, PCell[] closure) {
66+
this(clazz, name, enclosingClassName, code, globals, EMPTY_DEFAULTS, PKeyword.EMPTY_KEYWORDS, closure);
6867
}
6968

70-
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
69+
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, PCode code, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
7170
PCell[] closure) {
72-
this(clazz, name, enclosingClassName, callTarget, globals, defaultValues, kwDefaultValues, closure, null, Truffle.getRuntime().createAssumption(), Truffle.getRuntime().createAssumption());
71+
this(clazz, name, enclosingClassName, code, globals, defaultValues, kwDefaultValues, closure, null, Truffle.getRuntime().createAssumption(), Truffle.getRuntime().createAssumption());
7372
}
7473

75-
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
74+
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, PCode code, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
7675
PCell[] closure, WriteAttributeToDynamicObjectNode writeAttrNode, Assumption codeStableAssumption, Assumption defaultsStableAssumption) {
7776
super(clazz);
7877
this.name = name;
79-
this.code = new PCode(PythonBuiltinClassType.PCode, callTarget);
78+
this.code = code;
8079
this.isStatic = name.equals(SpecialMethodNames.__NEW__);
8180
this.enclosingClassName = enclosingClassName;
8281
this.globals = globals;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PGeneratorFunction.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@
2626
package com.oracle.graal.python.builtins.objects.function;
2727

2828
import com.oracle.graal.python.builtins.objects.cell.PCell;
29+
import com.oracle.graal.python.builtins.objects.code.PCode;
2930
import com.oracle.graal.python.builtins.objects.object.PythonObject;
3031
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
31-
import com.oracle.truffle.api.RootCallTarget;
3232

3333
public final class PGeneratorFunction extends PFunction {
3434

35-
public static PGeneratorFunction create(LazyPythonClass clazz, String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, PCell[] closure,
35+
public static PGeneratorFunction create(LazyPythonClass clazz, String name, String enclosingClassName, PCode code, PythonObject globals, PCell[] closure,
3636
Object[] defaultValues, PKeyword[] kwDefaultValues) {
37-
return new PGeneratorFunction(clazz, name, enclosingClassName, callTarget, globals, closure, defaultValues, kwDefaultValues);
37+
return new PGeneratorFunction(clazz, name, enclosingClassName, code, globals, closure, defaultValues, kwDefaultValues);
3838
}
3939

40-
public PGeneratorFunction(LazyPythonClass clazz, String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, PCell[] closure, Object[] defaultValues,
40+
private PGeneratorFunction(LazyPythonClass clazz, String name, String enclosingClassName, PCode code, PythonObject globals, PCell[] closure, Object[] defaultValues,
4141
PKeyword[] kwDefaultValues) {
42-
super(clazz, name, enclosingClassName, callTarget, globals, defaultValues, kwDefaultValues, closure);
42+
super(clazz, name, enclosingClassName, code, globals, defaultValues, kwDefaultValues, closure);
4343
}
4444

4545
@Override

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import com.oracle.graal.python.PythonLanguage;
3333
import com.oracle.graal.python.builtins.objects.cell.PCell;
34+
import com.oracle.graal.python.builtins.objects.code.PCode;
3435
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
3536
import com.oracle.graal.python.builtins.objects.dict.PDict;
3637
import com.oracle.graal.python.builtins.objects.function.PArguments;
@@ -45,6 +46,7 @@
4546
import com.oracle.graal.python.runtime.PythonContext;
4647
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4748
import com.oracle.truffle.api.Assumption;
49+
import com.oracle.truffle.api.CompilerAsserts;
4850
import com.oracle.truffle.api.CompilerDirectives;
4951
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
5052
import com.oracle.truffle.api.RootCallTarget;
@@ -59,6 +61,7 @@ public class FunctionDefinitionNode extends ExpressionDefinitionNode {
5961
protected final String functionName;
6062
protected final String enclosingClassName;
6163
protected final RootCallTarget callTarget;
64+
@CompilationFinal private PCode cachedCode;
6265

6366
@Children protected ExpressionNode[] defaults;
6467
@Children protected KwDefaultExpressionNode[] kwDefaults;
@@ -68,7 +71,7 @@ public class FunctionDefinitionNode extends ExpressionDefinitionNode {
6871
@Child private PythonObjectFactory factory = PythonObjectFactory.create();
6972
@Child private HashingCollectionNodes.SetItemNode setItemNode;
7073

71-
@CompilerDirectives.CompilationFinal(dimensions = 1) private final String[] annotationNames;
74+
@CompilationFinal(dimensions = 1) private final String[] annotationNames;
7275
@Children private ExpressionNode[] annotationTypes;
7376

7477
private final Assumption sharedCodeStableAssumption = Truffle.getRuntime().createAssumption("shared code stable assumption");
@@ -132,8 +135,22 @@ public Object execute(VirtualFrame frame) {
132135
codeStableAssumption = Truffle.getRuntime().createAssumption();
133136
defaultsStableAssumption = Truffle.getRuntime().createAssumption();
134137
}
135-
PFunction func = withDocString(frame,
136-
factory().createFunction(functionName, enclosingClassName, callTarget, PArguments.getGlobals(frame), defaultValues, kwDefaultValues, closure, writeNameNode,
138+
139+
PythonLanguage lang = lookupLanguageReference(PythonLanguage.class).get();
140+
CompilerAsserts.partialEvaluationConstant(lang);
141+
PCode code;
142+
if (lang.singleContextAssumption.isValid()) {
143+
if (cachedCode == null) {
144+
CompilerDirectives.transferToInterpreterAndInvalidate();
145+
cachedCode = factory().createCode(callTarget);
146+
}
147+
code = cachedCode;
148+
} else {
149+
code = factory().createCode(callTarget);
150+
}
151+
152+
PFunction func = withDocString(frame, factory().createFunction(functionName, enclosingClassName, code, PArguments.getGlobals(frame),
153+
defaultValues, kwDefaultValues, closure, writeNameNode,
137154
codeStableAssumption, defaultsStableAssumption));
138155

139156
// Processing annotated arguments.

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
*/
2626
package com.oracle.graal.python.nodes.function;
2727

28+
import java.util.Map;
29+
30+
import com.oracle.graal.python.PythonLanguage;
2831
import com.oracle.graal.python.builtins.objects.cell.PCell;
32+
import com.oracle.graal.python.builtins.objects.code.PCode;
2933
import com.oracle.graal.python.builtins.objects.function.PArguments;
3034
import com.oracle.graal.python.builtins.objects.function.PGeneratorFunction;
3135
import com.oracle.graal.python.builtins.objects.function.PKeyword;
@@ -34,14 +38,14 @@
3438
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
3539
import com.oracle.graal.python.parser.DefinitionCellSlots;
3640
import com.oracle.graal.python.parser.ExecutionCellSlots;
41+
import com.oracle.truffle.api.CompilerAsserts;
3742
import com.oracle.truffle.api.CompilerDirectives;
3843
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
3944
import com.oracle.truffle.api.RootCallTarget;
4045
import com.oracle.truffle.api.Truffle;
4146
import com.oracle.truffle.api.frame.FrameDescriptor;
4247
import com.oracle.truffle.api.frame.VirtualFrame;
4348
import com.oracle.truffle.api.nodes.ExplodeLoop;
44-
import java.util.Map;
4549

4650
public class GeneratorFunctionDefinitionNode extends FunctionDefinitionNode {
4751
protected final int numOfActiveFlags;
@@ -50,6 +54,7 @@ public class GeneratorFunctionDefinitionNode extends FunctionDefinitionNode {
5054
protected final FrameDescriptor frameDescriptor;
5155

5256
@CompilationFinal private RootCallTarget generatorCallTarget;
57+
@CompilationFinal private PCode generatorCode;
5358

5459
public GeneratorFunctionDefinitionNode(String name, String enclosingClassName, ExpressionNode doc, ExpressionNode[] defaults, KwDefaultExpressionNode[] kwDefaults,
5560
RootCallTarget callTarget, FrameDescriptor frameDescriptor, DefinitionCellSlots definitionCellSlots, ExecutionCellSlots executionCellSlots, int numOfActiveFlags,
@@ -88,18 +93,28 @@ public PGeneratorFunction execute(VirtualFrame frame) {
8893
}
8994
}
9095
PCell[] closure = getClosureFromGeneratorOrFunctionLocals(frame);
91-
return withDocString(frame, factory().createGeneratorFunction(functionName, enclosingClassName, getGeneratorCallTarget(), PArguments.getGlobals(frame), closure, defaultValues,
96+
return withDocString(frame, factory().createGeneratorFunction(functionName, enclosingClassName, getGeneratorCode(), PArguments.getGlobals(frame), closure, defaultValues,
9297
kwDefaultValues));
9398
}
9499

95-
protected RootCallTarget getGeneratorCallTarget() {
100+
protected PCode getGeneratorCode() {
96101
if (generatorCallTarget == null) {
97102
CompilerDirectives.transferToInterpreterAndInvalidate();
98103
GeneratorFunctionRootNode generatorFunctionRootNode = new GeneratorFunctionRootNode(getContext().getLanguage(), callTarget, functionName, frameDescriptor,
99104
executionCellSlots, ((PRootNode) callTarget.getRootNode()).getSignature(), numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode);
100105
generatorCallTarget = Truffle.getRuntime().createCallTarget(generatorFunctionRootNode);
101106
}
102-
return generatorCallTarget;
107+
PythonLanguage lang = lookupLanguageReference(PythonLanguage.class).get();
108+
CompilerAsserts.partialEvaluationConstant(lang);
109+
if (lang.singleContextAssumption.isValid()) {
110+
if (generatorCode == null) {
111+
CompilerDirectives.transferToInterpreterAndInvalidate();
112+
generatorCode = factory().createCode(generatorCallTarget);
113+
}
114+
return generatorCode;
115+
} else {
116+
return factory().createCode(generatorCallTarget);
117+
}
103118
}
104119

105120
public int getNumOfActiveFlags() {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,18 +377,18 @@ public final PBuiltinMethod createBuiltinMethod(Object self, PBuiltinFunction fu
377377
return createBuiltinMethod(PythonBuiltinClassType.PBuiltinMethod, self, function);
378378
}
379379

380-
public PFunction createFunction(String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, PCell[] closure) {
381-
return trace(new PFunction(PythonBuiltinClassType.PFunction, name, enclosingClassName, callTarget, globals, closure));
380+
public PFunction createFunction(String name, String enclosingClassName, PCode code, PythonObject globals, PCell[] closure) {
381+
return trace(new PFunction(PythonBuiltinClassType.PFunction, name, enclosingClassName, code, globals, closure));
382382
}
383383

384-
public PFunction createFunction(String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
384+
public PFunction createFunction(String name, String enclosingClassName, PCode code, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
385385
PCell[] closure) {
386-
return trace(new PFunction(PythonBuiltinClassType.PFunction, name, enclosingClassName, callTarget, globals, defaultValues, kwDefaultValues, closure));
386+
return trace(new PFunction(PythonBuiltinClassType.PFunction, name, enclosingClassName, code, globals, defaultValues, kwDefaultValues, closure));
387387
}
388388

389-
public PFunction createFunction(String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
389+
public PFunction createFunction(String name, String enclosingClassName, PCode code, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
390390
PCell[] closure, WriteAttributeToDynamicObjectNode writeAttrNode, Assumption codeStableAssumption, Assumption defaultsStableAssumption) {
391-
return trace(new PFunction(PythonBuiltinClassType.PFunction, name, enclosingClassName, callTarget, globals, defaultValues, kwDefaultValues, closure, writeAttrNode, codeStableAssumption,
391+
return trace(new PFunction(PythonBuiltinClassType.PFunction, name, enclosingClassName, code, globals, defaultValues, kwDefaultValues, closure, writeAttrNode, codeStableAssumption,
392392
defaultsStableAssumption));
393393
}
394394

@@ -538,9 +538,9 @@ public PGenerator createGenerator(String name, RootCallTarget[] callTargets, Fra
538538
numOfGeneratorForNode, this));
539539
}
540540

541-
public PGeneratorFunction createGeneratorFunction(String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, PCell[] closure, Object[] defaultValues,
541+
public PGeneratorFunction createGeneratorFunction(String name, String enclosingClassName, PCode code, PythonObject globals, PCell[] closure, Object[] defaultValues,
542542
PKeyword[] kwDefaultValues) {
543-
return trace(PGeneratorFunction.create(PythonBuiltinClassType.PFunction, name, enclosingClassName, callTarget, globals, closure, defaultValues, kwDefaultValues));
543+
return trace(PGeneratorFunction.create(PythonBuiltinClassType.PFunction, name, enclosingClassName, code, globals, closure, defaultValues, kwDefaultValues));
544544
}
545545

546546
public PMappingproxy createMappingproxy(PythonObject object) {

0 commit comments

Comments
 (0)