Skip to content

Commit 19f21c8

Browse files
committed
Move generator function passing to invoke nodes
1 parent a053677 commit 19f21c8

File tree

9 files changed

+52
-50
lines changed

9 files changed

+52
-50
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Object execute(Object[] arguments,
127127

128128
static CallTargetInvokeNode createInvokeNode(ContextReference<PythonContext> contextRef) {
129129
CApiContext cApiContext = contextRef.get().getCApiContext();
130-
return CallTargetInvokeNode.create(cApiContext.getTriggerAsyncActionsCallTarget(), false, false);
130+
return CallTargetInvokeNode.create(cApiContext.getTriggerAsyncActionsCallTarget(), null, false, false);
131131
}
132132

133133
static AllocationReporter getAllocationReporter(ContextReference<PythonContext> contextRef) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public PCell[] getClosure() {
134134
}
135135

136136
public boolean isGeneratorFunction() {
137-
return code.isGenerator();
137+
return code.getRootCallTarget().getRootNode() instanceof GeneratorFunctionRootNode;
138138
}
139139

140140
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/generator/GeneratorBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static Object generic(VirtualFrame frame, PGenerator self, Object sendValue,
183183
}
184184

185185
protected static CallTargetInvokeNode createDirectCall(CallTarget target) {
186-
return CallTargetInvokeNode.create(target, false, true);
186+
return CallTargetInvokeNode.create(target, null, false, true);
187187
}
188188

189189
protected static boolean sameCallTarget(RootCallTarget target1, CallTarget target2) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/CallDispatchNode.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@
2727

2828
import com.oracle.graal.python.PythonLanguage;
2929
import com.oracle.graal.python.builtins.objects.code.PCode;
30-
import com.oracle.graal.python.builtins.objects.function.PArguments;
3130
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
3231
import com.oracle.graal.python.builtins.objects.function.PFunction;
3332
import com.oracle.graal.python.nodes.builtins.FunctionNodes.GetFunctionCodeNode;
34-
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
3533
import com.oracle.graal.python.runtime.PythonOptions;
3634
import com.oracle.truffle.api.Assumption;
37-
import com.oracle.truffle.api.CompilerAsserts;
3835
import com.oracle.truffle.api.RootCallTarget;
3936
import com.oracle.truffle.api.dsl.Cached;
4037
import com.oracle.truffle.api.dsl.GenerateUncached;
@@ -44,7 +41,6 @@
4441
import com.oracle.truffle.api.frame.Frame;
4542
import com.oracle.truffle.api.frame.VirtualFrame;
4643
import com.oracle.truffle.api.nodes.Node;
47-
import com.oracle.truffle.api.profiles.ConditionProfile;
4844

4945
@ImportStatic(PythonOptions.class)
5046
@ReportPolymorphism
@@ -96,11 +92,6 @@ public final Object executeCall(VirtualFrame frame, PBuiltinFunction callee, Obj
9692
protected Object callFunctionCached(VirtualFrame frame, @SuppressWarnings("unused") PFunction callee, Object[] arguments,
9793
@SuppressWarnings("unused") @Cached("callee") PFunction cachedCallee,
9894
@Cached("createInvokeNode(cachedCallee)") FunctionInvokeNode invoke) {
99-
boolean isGenerator = cachedCallee.getFunctionRootNode() instanceof GeneratorFunctionRootNode;
100-
CompilerAsserts.partialEvaluationConstant(isGenerator);
101-
if (isGenerator) {
102-
PArguments.setGeneratorFunction(arguments, cachedCallee);
103-
}
10495
return invoke.execute(frame, arguments);
10596
}
10697

@@ -116,11 +107,6 @@ protected Object callFunctionCachedCode(VirtualFrame frame, @SuppressWarnings("u
116107
@SuppressWarnings("unused") @Cached("create()") GetFunctionCodeNode getFunctionCodeNode,
117108
@SuppressWarnings("unused") @Cached("getCode(getFunctionCodeNode, callee)") PCode cachedCode,
118109
@Cached("createInvokeNode(cachedCallee)") FunctionInvokeNode invoke) {
119-
boolean isGenerator = cachedCode.getRootNode() instanceof GeneratorFunctionRootNode;
120-
CompilerAsserts.partialEvaluationConstant(isGenerator);
121-
if (isGenerator) {
122-
PArguments.setGeneratorFunction(arguments, cachedCallee);
123-
}
124110
return invoke.execute(frame, arguments);
125111
}
126112

@@ -129,11 +115,6 @@ protected Object callFunctionCachedCode(VirtualFrame frame, @SuppressWarnings("u
129115
protected Object callFunctionCachedCt(VirtualFrame frame, PFunction callee, Object[] arguments,
130116
@SuppressWarnings("unused") @Cached("callee.getCallTarget()") RootCallTarget ct,
131117
@Cached("createCtInvokeNode(callee)") CallTargetInvokeNode invoke) {
132-
boolean isGenerator = ct.getRootNode() instanceof GeneratorFunctionRootNode;
133-
CompilerAsserts.partialEvaluationConstant(isGenerator);
134-
if (isGenerator) {
135-
PArguments.setGeneratorFunction(arguments, callee);
136-
}
137118
return invoke.execute(frame, callee.getGlobals(), callee.getClosure(), arguments);
138119
}
139120

@@ -153,11 +134,7 @@ protected Object callBuiltinFunctionCachedCt(VirtualFrame frame, @SuppressWarnin
153134

154135
@Specialization(replaces = {"callFunctionCached", "callFunctionCachedCode", "callFunctionCachedCt"})
155136
protected Object callFunctionUncached(Frame frame, PFunction callee, Object[] arguments,
156-
@Cached GenericInvokeNode invoke,
157-
@Cached ConditionProfile isGeneratorProfile) {
158-
if (isGeneratorProfile.profile(callee.isGeneratorFunction())) {
159-
PArguments.setGeneratorFunction(arguments, callee);
160-
}
137+
@Cached GenericInvokeNode invoke) {
161138
return invoke.executeInternal(frame, callee, arguments);
162139
}
163140

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/CallTargetInvokeNode.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, 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
@@ -65,16 +65,20 @@
6565
public abstract class CallTargetInvokeNode extends DirectInvokeNode {
6666
@Child private DirectCallNode callNode;
6767
@Child private CallContext callContext;
68+
// Needed only for generator functions, will be null for builtins and other callables that
69+
// cannot be generator functions
70+
private final PFunction callee;
6871
protected final boolean isBuiltin;
6972

70-
protected CallTargetInvokeNode(CallTarget callTarget, boolean isBuiltin, boolean isGenerator) {
73+
protected CallTargetInvokeNode(CallTarget callTarget, PFunction callee, boolean isBuiltin, boolean isGenerator) {
7174
this.callNode = Truffle.getRuntime().createDirectCallNode(callTarget);
7275
if (isBuiltin) {
7376
callNode.cloneCallTarget();
7477
}
7578
if (isGenerator && shouldInlineGenerators()) {
7679
this.callNode.forceInlining();
7780
}
81+
this.callee = callee;
7882
this.callContext = CallContext.create();
7983
this.isBuiltin = isBuiltin;
8084
}
@@ -83,18 +87,18 @@ protected CallTargetInvokeNode(CallTarget callTarget, boolean isBuiltin, boolean
8387
public static CallTargetInvokeNode create(PFunction callee) {
8488
RootCallTarget callTarget = getCallTarget(callee);
8589
boolean builtin = isBuiltin(callee);
86-
return CallTargetInvokeNodeGen.create(callTarget, builtin, callee.isGeneratorFunction());
90+
return CallTargetInvokeNodeGen.create(callTarget, callee, builtin, callee.isGeneratorFunction());
8791
}
8892

8993
@TruffleBoundary
9094
public static CallTargetInvokeNode create(PBuiltinFunction callee) {
9195
RootCallTarget callTarget = getCallTarget(callee);
9296
boolean builtin = isBuiltin(callee);
93-
return CallTargetInvokeNodeGen.create(callTarget, builtin, false);
97+
return CallTargetInvokeNodeGen.create(callTarget, null, builtin, false);
9498
}
9599

96-
public static CallTargetInvokeNode create(CallTarget callTarget, boolean isBuiltin, boolean isGenerator) {
97-
return CallTargetInvokeNodeGen.create(callTarget, isBuiltin, isGenerator);
100+
public static CallTargetInvokeNode create(CallTarget callTarget, PFunction callee, boolean isBuiltin, boolean isGenerator) {
101+
return CallTargetInvokeNodeGen.create(callTarget, callee, isBuiltin, isGenerator);
98102
}
99103

100104
public final Object execute(VirtualFrame frame, Object[] arguments) {
@@ -106,10 +110,11 @@ public final Object execute(VirtualFrame frame, Object[] arguments) {
106110
@Specialization(guards = {"globals == null", "closure == null"})
107111
protected Object doNoClosure(VirtualFrame frame, @SuppressWarnings("unused") PythonObject globals, @SuppressWarnings("unused") PCell[] closure, Object[] arguments,
108112
@Cached("createBinaryProfile()") ConditionProfile classBodyProfile,
113+
@Cached("createBinaryProfile()") ConditionProfile generatorFunctionProfile,
109114
@CachedContext(PythonLanguage.class) PythonContext context) {
110115
RootCallTarget ct = (RootCallTarget) callNode.getCurrentCallTarget();
111116
optionallySetClassBodySpecial(arguments, ct, classBodyProfile);
112-
117+
optionallySetGeneratorFunction(arguments, ct, generatorFunctionProfile, callee);
113118
// If the frame is 'null', we expect the execution state (i.e. caller info and exception
114119
// state) in the context. There are two common reasons for having a 'null' frame:
115120
// 1. This node is the first invoke node used via interop.
@@ -131,10 +136,11 @@ protected Object doNoClosure(VirtualFrame frame, @SuppressWarnings("unused") Pyt
131136
@Specialization(replaces = "doNoClosure")
132137
protected Object doGeneric(VirtualFrame frame, PythonObject globals, PCell[] closure, Object[] arguments,
133138
@Cached("createBinaryProfile()") ConditionProfile classBodyProfile,
139+
@Cached("createBinaryProfile()") ConditionProfile generatorFunctionProfile,
134140
@CachedContext(PythonLanguage.class) PythonContext context) {
135141
PArguments.setGlobals(arguments, globals);
136142
PArguments.setClosure(arguments, closure);
137-
return doNoClosure(frame, null, null, arguments, classBodyProfile, context);
143+
return doNoClosure(frame, null, null, arguments, classBodyProfile, generatorFunctionProfile, context);
138144
}
139145

140146
public final CallTarget getCallTarget() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/FunctionInvokeNode.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
4848
import com.oracle.graal.python.builtins.objects.function.PFunction;
4949
import com.oracle.graal.python.builtins.objects.object.PythonObject;
50+
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
5051
import com.oracle.graal.python.runtime.ExecutionContext.CallContext;
5152
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCalleeContext;
5253
import com.oracle.graal.python.runtime.PythonContext;
@@ -66,11 +67,14 @@ public abstract class FunctionInvokeNode extends DirectInvokeNode {
6667
@Child private DirectCallNode callNode;
6768
@Child private CallContext callContext;
6869

70+
// Needed only for generator functions, will be null for builtins
71+
private final PFunction callee;
6972
private final PythonObject globals;
7073
private final PCell[] closure;
7174
protected final boolean isBuiltin;
7275

73-
protected FunctionInvokeNode(CallTarget callTarget, PythonObject globals, PCell[] closure, boolean isBuiltin, boolean isGenerator) {
76+
protected FunctionInvokeNode(PFunction callee, CallTarget callTarget, PythonObject globals, PCell[] closure, boolean isBuiltin, boolean isGenerator) {
77+
this.callee = callee;
7478
this.callNode = Truffle.getRuntime().createDirectCallNode(callTarget);
7579
if (isBuiltin && forceSplitBuiltins()) {
7680
callNode.cloneCallTarget();
@@ -89,11 +93,13 @@ protected FunctionInvokeNode(CallTarget callTarget, PythonObject globals, PCell[
8993
@Specialization
9094
protected Object doDirect(VirtualFrame frame, Object[] arguments,
9195
@CachedContext(PythonLanguage.class) PythonContext context,
92-
@Cached("createBinaryProfile()") ConditionProfile isClassBodyProfile) {
96+
@Cached("createBinaryProfile()") ConditionProfile isClassBodyProfile,
97+
@Cached("createBinaryProfile()") ConditionProfile isGeneratorFunctionProfile) {
9398
PArguments.setGlobals(arguments, globals);
9499
PArguments.setClosure(arguments, closure);
95100
RootCallTarget ct = (RootCallTarget) callNode.getCurrentCallTarget();
96101
optionallySetClassBodySpecial(arguments, ct, isClassBodyProfile);
102+
optionallySetGeneratorFunction(arguments, ct, isGeneratorFunctionProfile, callee);
97103
if (profileIsNullFrame(frame == null)) {
98104
PFrame.Reference frameInfo = IndirectCalleeContext.enter(context, arguments, ct);
99105
try {
@@ -115,14 +121,14 @@ public final RootNode getCurrentRootNode() {
115121
public static FunctionInvokeNode create(PFunction callee) {
116122
RootCallTarget callTarget = getCallTarget(callee);
117123
boolean builtin = isBuiltin(callee);
118-
return FunctionInvokeNodeGen.create(callTarget, callee.getGlobals(), callee.getClosure(), builtin, callee.isGeneratorFunction());
124+
return FunctionInvokeNodeGen.create(callee, callTarget, callee.getGlobals(), callee.getClosure(), builtin, callTarget.getRootNode() instanceof GeneratorFunctionRootNode);
119125
}
120126

121127
@TruffleBoundary
122128
public static FunctionInvokeNode create(PBuiltinFunction callee) {
123129
RootCallTarget callTarget = getCallTarget(callee);
124130
boolean builtin = isBuiltin(callee);
125-
return FunctionInvokeNodeGen.create(callTarget, null, null, builtin, false);
131+
return FunctionInvokeNodeGen.create(null, callTarget, null, null, builtin, false);
126132
}
127133

128134
/**
@@ -132,6 +138,6 @@ public static FunctionInvokeNode create(PBuiltinFunction callee) {
132138
*/
133139
@TruffleBoundary
134140
public static FunctionInvokeNode createBuiltinFunction(RootCallTarget callTarget) {
135-
return FunctionInvokeNodeGen.create(callTarget, null, null, true, false);
141+
return FunctionInvokeNodeGen.create(null, callTarget, null, null, true, false);
136142
}
137143
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/GenericInvokeNode.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ public final Object execute(Object callee, Object[] arguments) {
117117
return executeInternal(null, callee, arguments);
118118
}
119119

120-
private Object doCall(Frame frame, RootCallTarget callTarget, Object[] arguments,
121-
PythonContext context, IndirectCallNode callNode, CallContext callContext, ConditionProfile isNullFrameProfile, ConditionProfile isClassBodyProfile) {
120+
private Object doCall(Frame frame, PFunction callee, RootCallTarget callTarget, Object[] arguments,
121+
PythonContext context, IndirectCallNode callNode, CallContext callContext, ConditionProfile isNullFrameProfile,
122+
ConditionProfile isClassBodyProfile, ConditionProfile isGeneratorFunctionProfile) {
122123
optionallySetClassBodySpecial(arguments, callTarget, isClassBodyProfile);
124+
optionallySetGeneratorFunction(arguments, callTarget, isGeneratorFunctionProfile, callee);
123125
if (isNullFrameProfile.profile(frame == null)) {
124126
PFrame.Reference frameInfo = IndirectCalleeContext.enterIndirect(context, arguments);
125127
try {
@@ -140,11 +142,12 @@ Object invokeFunction(Frame frame, PFunction callee, Object[] arguments,
140142
@Shared("callNode") @Cached IndirectCallNode callNode,
141143
@Shared("callContext") @Cached CallContext callContext,
142144
@Shared("isNullFrameProfile") @Cached("createBinaryProfile()") ConditionProfile isNullFrameProfile,
143-
@Shared("isClassBodyProfile") @Cached("createBinaryProfile()") ConditionProfile isClassBodyProfile) {
145+
@Shared("isClassBodyProfile") @Cached("createBinaryProfile()") ConditionProfile isClassBodyProfile,
146+
@Shared("isGeneratorFunctionProfile") @Cached("createBinaryProfile()") ConditionProfile isGeneratorFunctionProfile) {
144147
PArguments.setGlobals(arguments, callee.getGlobals());
145148
PArguments.setClosure(arguments, callee.getClosure());
146149
RootCallTarget callTarget = getCallTarget(callee);
147-
return doCall(frame, callTarget, arguments, context, callNode, callContext, isNullFrameProfile, isClassBodyProfile);
150+
return doCall(frame, callee, callTarget, arguments, context, callNode, callContext, isNullFrameProfile, isClassBodyProfile, isGeneratorFunctionProfile);
148151
}
149152

150153
@Specialization
@@ -153,9 +156,10 @@ Object invokeBuiltin(Frame frame, PBuiltinFunction callee, Object[] arguments,
153156
@Shared("callNode") @Cached IndirectCallNode callNode,
154157
@Shared("callContext") @Cached CallContext callContext,
155158
@Shared("isNullFrameProfile") @Cached("createBinaryProfile()") ConditionProfile isNullFrameProfile,
156-
@Shared("isClassBodyProfile") @Cached("createBinaryProfile()") ConditionProfile isClassBodyProfile) {
159+
@Shared("isClassBodyProfile") @Cached("createBinaryProfile()") ConditionProfile isClassBodyProfile,
160+
@Shared("isGeneratorFunctionProfile") @Cached("createBinaryProfile()") ConditionProfile isGeneratorFunctionProfile) {
157161
RootCallTarget callTarget = getCallTarget(callee);
158-
return doCall(frame, callTarget, arguments, context, callNode, callContext, isNullFrameProfile, isClassBodyProfile);
162+
return doCall(frame, null, callTarget, arguments, context, callNode, callContext, isNullFrameProfile, isClassBodyProfile, isGeneratorFunctionProfile);
159163
}
160164

161165
@Specialization
@@ -164,7 +168,8 @@ Object invokeCallTarget(Frame frame, RootCallTarget callTarget, Object[] argumen
164168
@Shared("callNode") @Cached IndirectCallNode callNode,
165169
@Shared("callContext") @Cached CallContext callContext,
166170
@Shared("isNullFrameProfile") @Cached("createBinaryProfile()") ConditionProfile isNullFrameProfile,
167-
@Shared("isClassBodyProfile") @Cached("createBinaryProfile()") ConditionProfile isClassBodyProfile) {
168-
return doCall(frame, callTarget, arguments, context, callNode, callContext, isNullFrameProfile, isClassBodyProfile);
171+
@Shared("isClassBodyProfile") @Cached("createBinaryProfile()") ConditionProfile isClassBodyProfile,
172+
@Shared("isGeneratorFunctionProfile") @Cached("createBinaryProfile()") ConditionProfile isGeneratorFunctionProfile) {
173+
return doCall(frame, null, callTarget, arguments, context, callNode, callContext, isNullFrameProfile, isClassBodyProfile, isGeneratorFunctionProfile);
169174
}
170175
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/InvokeNode.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
3333
import com.oracle.graal.python.nodes.IndirectCallNode;
3434
import com.oracle.graal.python.nodes.function.ClassBodyRootNode;
35+
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
3536
import com.oracle.graal.python.runtime.PythonOptions;
3637
import com.oracle.truffle.api.Assumption;
3738
import com.oracle.truffle.api.CallTarget;
@@ -67,14 +68,22 @@ protected static RootCallTarget getCallTarget(Object callee) {
6768
return callTarget;
6869
}
6970

70-
protected static final void optionallySetClassBodySpecial(Object[] arguments, CallTarget callTarget, ConditionProfile isClassBodyProfile) {
71+
protected static void optionallySetClassBodySpecial(Object[] arguments, CallTarget callTarget, ConditionProfile isClassBodyProfile) {
7172
RootNode rootNode = ((RootCallTarget) callTarget).getRootNode();
7273
if (isClassBodyProfile.profile(rootNode instanceof ClassBodyRootNode)) {
7374
assert PArguments.getSpecialArgument(arguments) == null : "there cannot be a special argument in a class body";
7475
PArguments.setSpecialArgument(arguments, rootNode);
7576
}
7677
}
7778

79+
protected static void optionallySetGeneratorFunction(Object[] arguments, CallTarget callTarget, ConditionProfile isGeneratorFunctionProfile, PFunction callee) {
80+
RootNode rootNode = ((RootCallTarget) callTarget).getRootNode();
81+
if (isGeneratorFunctionProfile.profile(rootNode instanceof GeneratorFunctionRootNode)) {
82+
assert callee != null : "generator function callee not passed to invoke node";
83+
PArguments.setGeneratorFunction(arguments, callee);
84+
}
85+
}
86+
7887
protected static boolean isBuiltin(Object callee) {
7988
return callee instanceof PBuiltinFunction || callee instanceof PBuiltinMethod;
8089
}

0 commit comments

Comments
 (0)