Skip to content

Commit 463f3f1

Browse files
committed
Make generator name reflect the name of generator function
1 parent d3b713d commit 463f3f1

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,18 @@ public static void setGeneratorFrame(Object[] arguments, MaterializedFrame gener
315315
arguments[INDEX_GENERATOR_FRAME] = generatorFrame;
316316
}
317317

318+
/**
319+
* This should be used only in GeneratorFunctionRootNode, later the slot is overwritten with
320+
* generator frame
321+
*/
322+
public static PFunction getGeneratorFunction(Object[] arguments) {
323+
return (PFunction) arguments[INDEX_GENERATOR_FRAME];
324+
}
325+
326+
public static void setGeneratorFunction(Object[] arguments, PFunction generatorFunction) {
327+
arguments[INDEX_GENERATOR_FRAME] = generatorFunction;
328+
}
329+
318330
public static void setControlData(Object[] arguments, GeneratorControlData generatorArguments) {
319331
MaterializedFrame generatorFrame = (MaterializedFrame) arguments[INDEX_GENERATOR_FRAME];
320332
generatorFrame.getArguments()[INDEX_GENERATOR_FRAME] = generatorArguments;

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.
@@ -27,8 +27,10 @@
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;
3031
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
3132
import com.oracle.graal.python.builtins.objects.function.PFunction;
33+
import com.oracle.graal.python.builtins.objects.function.PGeneratorFunction;
3234
import com.oracle.graal.python.nodes.builtins.FunctionNodes.GetFunctionCodeNode;
3335
import com.oracle.graal.python.runtime.PythonOptions;
3436
import com.oracle.truffle.api.Assumption;
@@ -41,6 +43,7 @@
4143
import com.oracle.truffle.api.frame.Frame;
4244
import com.oracle.truffle.api.frame.VirtualFrame;
4345
import com.oracle.truffle.api.nodes.Node;
46+
import com.oracle.truffle.api.profiles.ConditionProfile;
4447

4548
@ImportStatic(PythonOptions.class)
4649
@ReportPolymorphism
@@ -89,9 +92,13 @@ public final Object executeCall(VirtualFrame frame, PBuiltinFunction callee, Obj
8992

9093
// We only have a single context and this function never changed its code
9194
@Specialization(guards = {"callee == cachedCallee"}, limit = "getCallSiteInlineCacheMaxDepth()", assumptions = {"singleContextAssumption()", "cachedCallee.getCodeStableAssumption()"})
92-
protected Object callFunctionCached(VirtualFrame frame, @SuppressWarnings("unused") PFunction callee, Object[] arguments,
95+
protected Object callFunctionCached(VirtualFrame frame, PFunction callee, Object[] arguments,
9396
@SuppressWarnings("unused") @Cached("callee") PFunction cachedCallee,
94-
@Cached("createInvokeNode(cachedCallee)") FunctionInvokeNode invoke) {
97+
@Cached("createInvokeNode(cachedCallee)") FunctionInvokeNode invoke,
98+
@Cached ConditionProfile isGeneratorProfile) {
99+
if (isGeneratorProfile.profile(callee instanceof PGeneratorFunction)) {
100+
PArguments.setGeneratorFunction(arguments, callee);
101+
}
95102
return invoke.execute(frame, arguments);
96103
}
97104

@@ -106,15 +113,23 @@ protected Object callFunctionCachedCode(VirtualFrame frame, @SuppressWarnings("u
106113
@SuppressWarnings("unused") @Cached("callee") PFunction cachedCallee,
107114
@SuppressWarnings("unused") @Cached("create()") GetFunctionCodeNode getFunctionCodeNode,
108115
@SuppressWarnings("unused") @Cached("getCode(getFunctionCodeNode, callee)") PCode cachedCode,
109-
@Cached("createInvokeNode(cachedCallee)") FunctionInvokeNode invoke) {
116+
@Cached("createInvokeNode(cachedCallee)") FunctionInvokeNode invoke,
117+
@Cached ConditionProfile isGeneratorProfile) {
118+
if (isGeneratorProfile.profile(callee instanceof PGeneratorFunction)) {
119+
PArguments.setGeneratorFunction(arguments, callee);
120+
}
110121
return invoke.execute(frame, arguments);
111122
}
112123

113124
// We have multiple contexts, don't cache the objects so that contexts can be cleaned up
114125
@Specialization(guards = {"callee.getCallTarget() == ct"}, limit = "getCallSiteInlineCacheMaxDepth()", replaces = "callFunctionCachedCode")
115126
protected Object callFunctionCachedCt(VirtualFrame frame, PFunction callee, Object[] arguments,
116127
@SuppressWarnings("unused") @Cached("callee.getCallTarget()") RootCallTarget ct,
117-
@Cached("createCtInvokeNode(callee)") CallTargetInvokeNode invoke) {
128+
@Cached("createCtInvokeNode(callee)") CallTargetInvokeNode invoke,
129+
@Cached ConditionProfile isGeneratorProfile) {
130+
if (isGeneratorProfile.profile(callee instanceof PGeneratorFunction)) {
131+
PArguments.setGeneratorFunction(arguments, callee);
132+
}
118133
return invoke.execute(frame, callee.getGlobals(), callee.getClosure(), arguments);
119134
}
120135

@@ -134,7 +149,11 @@ protected Object callBuiltinFunctionCachedCt(VirtualFrame frame, @SuppressWarnin
134149

135150
@Specialization(replaces = {"callFunctionCached", "callFunctionCachedCode", "callFunctionCachedCt"})
136151
protected Object callFunctionUncached(Frame frame, PFunction callee, Object[] arguments,
137-
@Cached GenericInvokeNode invoke) {
152+
@Cached GenericInvokeNode invoke,
153+
@Cached ConditionProfile isGeneratorProfile) {
154+
if (isGeneratorProfile.profile(callee instanceof PGeneratorFunction)) {
155+
PArguments.setGeneratorFunction(arguments, callee);
156+
}
138157
return invoke.executeInternal(frame, callee, arguments);
139158
}
140159

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ public PGeneratorFunction execute(VirtualFrame frame) {
9696

9797
public GeneratorFunctionRootNode getGeneratorFunctionRootNode(PythonContext ctx) {
9898
if (generatorCallTarget == null) {
99-
// TODO msimacek: the name/qualname passing is not entirely correct, it can change later
100-
// and the change should be reflected on the created generators
101-
return new GeneratorFunctionRootNode(ctx.getLanguage(), callTarget, functionName, qualname, frameDescriptor,
99+
return new GeneratorFunctionRootNode(ctx.getLanguage(), callTarget, functionName, frameDescriptor,
102100
executionCellSlots, ((PRootNode) callTarget.getRootNode()).getSignature(), generatorInfo);
103101
}
104102
return (GeneratorFunctionRootNode) generatorCallTarget.getRootNode();

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import com.oracle.graal.python.PythonLanguage;
4444
import com.oracle.graal.python.builtins.objects.function.PArguments;
45+
import com.oracle.graal.python.builtins.objects.function.PFunction;
4546
import com.oracle.graal.python.builtins.objects.function.Signature;
4647
import com.oracle.graal.python.nodes.PClosureFunctionRootNode;
4748
import com.oracle.graal.python.nodes.PRootNode;
@@ -65,17 +66,15 @@ public class GeneratorFunctionRootNode extends PClosureFunctionRootNode {
6566
private final FrameDescriptor frameDescriptor;
6667
private final GeneratorInfo generatorInfo;
6768
private final ExecutionCellSlots cellSlots;
68-
private final String name;
69-
private final String qualname;
69+
private final String originalName;
7070

7171
@Child private PythonObjectFactory factory = PythonObjectFactory.create();
7272

73-
public GeneratorFunctionRootNode(PythonLanguage language, RootCallTarget callTarget, String name, String qualname, FrameDescriptor frameDescriptor, ExecutionCellSlots executionCellSlots,
73+
public GeneratorFunctionRootNode(PythonLanguage language, RootCallTarget callTarget, String originalName, FrameDescriptor frameDescriptor, ExecutionCellSlots executionCellSlots,
7474
Signature signature, GeneratorInfo generatorInfo) {
7575
super(language, frameDescriptor, executionCellSlots, signature);
7676
this.callTarget = callTarget;
77-
this.name = name;
78-
this.qualname = qualname;
77+
this.originalName = originalName;
7978
this.frameDescriptor = frameDescriptor;
8079
this.cellSlots = executionCellSlots;
8180
this.generatorInfo = generatorInfo;
@@ -88,7 +87,14 @@ public Object execute(VirtualFrame frame) {
8887
callTargets = createYieldTargets(callTarget);
8988
}
9089
CompilerAsserts.partialEvaluationConstant(cellSlots);
91-
return factory.createGenerator(name, qualname, callTargets, frameDescriptor, frame.getArguments(), PArguments.getClosure(frame), cellSlots, generatorInfo, null);
90+
91+
Object[] arguments = frame.getArguments();
92+
93+
// This is passed from CallDispatch node
94+
PFunction generatorFunction = PArguments.getGeneratorFunction(arguments);
95+
96+
return factory.createGenerator(generatorFunction.getName(), generatorFunction.getQualname(), callTargets, frameDescriptor, arguments, PArguments.getClosure(frame), cellSlots,
97+
generatorInfo, null);
9298
}
9399

94100
public static RootCallTarget[] createYieldTargets(RootCallTarget callTarget) {
@@ -108,13 +114,13 @@ public RootNode getFunctionRootNode() {
108114

109115
@Override
110116
public String getName() {
111-
return name;
117+
return originalName;
112118
}
113119

114120
@Override
115121
public String toString() {
116122
CompilerAsserts.neverPartOfCompilation();
117-
return "<generator function " + qualname + ">";
123+
return "<generator function root" + originalName + ">";
118124
}
119125

120126
@Override

0 commit comments

Comments
 (0)