Skip to content

Commit 8917713

Browse files
committed
add frame profiles to generator nodes
1 parent 528d1af commit 8917713

18 files changed

+233
-126
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ public static void setControlData(Object[] arguments, GeneratorControlData gener
217217
generatorFrame.getArguments()[INDEX_GENERATOR_FRAME] = generatorArguments;
218218
}
219219

220-
public static GeneratorControlData getControlData(Frame frame) {
221-
Frame generatorFrame = getGeneratorFrame(frame);
220+
public static GeneratorControlData getControlDataFromGeneratorFrame(Frame generatorFrame) {
222221
return (GeneratorControlData) generatorFrame.getArguments()[INDEX_GENERATOR_FRAME];
223222
}
224223

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@
3535
import com.oracle.truffle.api.frame.FrameSlot;
3636
import com.oracle.truffle.api.frame.FrameUtil;
3737
import com.oracle.truffle.api.nodes.ExplodeLoop;
38+
import com.oracle.truffle.api.profiles.ConditionProfile;
39+
import com.oracle.truffle.api.profiles.ValueProfile;
3840

3941
abstract class ExpressionDefinitionNode extends PNode {
42+
private final ValueProfile frameProfile = ValueProfile.createClassProfile();
43+
private final ConditionProfile isGeneratorProfile = ConditionProfile.createBinaryProfile();
44+
4045
final ExecutionCellSlots executionCellSlots;
4146
@CompilerDirectives.CompilationFinal(dimensions = 1) private final FrameSlot[] freeVarDefinitionSlots;
4247

@@ -66,8 +71,8 @@ PCell[] getClosureFromLocals(Frame frame) {
6671
PCell[] getClosureFromGeneratorOrFunctionLocals(Frame frame) {
6772
PCell[] closure;
6873
Frame generatorFrame = PArguments.getGeneratorFrame(frame);
69-
if (generatorFrame != null) {
70-
closure = getClosureFromLocals(generatorFrame);
74+
if (isGeneratorProfile.profile(generatorFrame != null)) {
75+
closure = getClosureFromLocals(frameProfile.profile(generatorFrame));
7176
} else {
7277
closure = getClosureFromLocals(frame);
7378
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.oracle.truffle.api.frame.VirtualFrame;
4141
import com.oracle.truffle.api.nodes.ExplodeLoop;
4242
import com.oracle.truffle.api.nodes.NodeUtil;
43+
import com.oracle.truffle.api.profiles.ValueProfile;
4344
import com.oracle.truffle.api.source.SourceSection;
4445

4546
/**
@@ -52,6 +53,7 @@ public class FunctionRootNode extends PClosureFunctionRootNode implements CellSu
5253
private final String functionName;
5354
private final SourceSection sourceSection;
5455
private final boolean isGenerator;
56+
private final ValueProfile generatorFrameProfile;
5557

5658
@Child private PNode body;
5759
private PNode uninitializedBody;
@@ -68,6 +70,7 @@ public FunctionRootNode(PythonLanguage language, SourceSection sourceSection, St
6870
this.isGenerator = isGenerator;
6971
this.body = NodeUtil.cloneNode(body);
7072
this.uninitializedBody = NodeUtil.cloneNode(body);
73+
this.generatorFrameProfile = isGenerator ? ValueProfile.createClassProfile() : null;
7174
}
7275

7376
public String getFunctionName() {
@@ -125,7 +128,7 @@ private void initializeCellVars(Frame frame) {
125128
private void initClosureAndCellVars(VirtualFrame frame) {
126129
Frame accessingFrame = frame;
127130
if (isGenerator) {
128-
accessingFrame = PArguments.getGeneratorFrame(frame);
131+
accessingFrame = generatorFrameProfile.profile(PArguments.getGeneratorFrame(frame));
129132
}
130133

131134
addClosureCellsToLocals(accessingFrame);

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.oracle.truffle.api.frame.Frame;
3636
import com.oracle.truffle.api.frame.FrameSlot;
3737
import com.oracle.truffle.api.frame.VirtualFrame;
38+
import com.oracle.truffle.api.profiles.ValueProfile;
3839

3940
/**
4041
* Transfer a local variable value from the current frame to a cargo frame.
@@ -43,6 +44,8 @@
4344
@GenerateNodeFactory
4445
public abstract class FrameTransferNode extends FrameSlotNode {
4546

47+
private final ValueProfile frameProfile = ValueProfile.createClassProfile();
48+
4649
public FrameTransferNode(FrameSlot slot) {
4750
super(slot);
4851
}
@@ -58,46 +61,43 @@ public Object doWrite(VirtualFrame frame, Object value) {
5861

5962
protected abstract Object execute(VirtualFrame frame, Object value);
6063

64+
private Frame getCargoFrame(VirtualFrame frame) {
65+
return frameProfile.profile(PArguments.getGeneratorFrame(frame));
66+
}
67+
6168
@Specialization(guards = "isBooleanKind(frame)")
6269
public boolean write(VirtualFrame frame, boolean right) {
63-
Frame cargoFrame = PArguments.getGeneratorFrame(frame);
64-
cargoFrame.setBoolean(frameSlot, right);
70+
getCargoFrame(frame).setBoolean(frameSlot, right);
6571
return right;
6672
}
6773

6874
@Specialization(guards = "isIntegerKind(frame)")
6975
public int doInteger(VirtualFrame frame, int value) {
70-
Frame cargoFrame = PArguments.getGeneratorFrame(frame);
71-
cargoFrame.setInt(frameSlot, value);
76+
getCargoFrame(frame).setInt(frameSlot, value);
7277
return value;
7378
}
7479

7580
@Specialization(guards = "isIntOrObjectKind(frame)")
7681
public PInt write(VirtualFrame frame, PInt value) {
77-
Frame cargoFrame = PArguments.getGeneratorFrame(frame);
78-
setObject(cargoFrame, value);
82+
setObject(getCargoFrame(frame), value);
7983
return value;
8084
}
8185

8286
@Specialization(guards = "isLongKind(frame)")
8387
public long doLong(VirtualFrame frame, long value) {
84-
Frame cargoFrame = PArguments.getGeneratorFrame(frame);
85-
cargoFrame.setLong(frameSlot, value);
88+
getCargoFrame(frame).setLong(frameSlot, value);
8689
return value;
8790
}
8891

8992
@Specialization(guards = "isDoubleKind(frame)")
9093
public double doDouble(VirtualFrame frame, double right) {
91-
Frame cargoFrame = PArguments.getGeneratorFrame(frame);
92-
cargoFrame.setDouble(frameSlot, right);
94+
getCargoFrame(frame).setDouble(frameSlot, right);
9395
return right;
9496
}
9597

9698
@Specialization(guards = "isObjectKind(frame)")
9799
public Object write(VirtualFrame frame, Object right) {
98-
Frame cargoFrame = PArguments.getGeneratorFrame(frame);
99-
assert !(right instanceof PInt);
100-
setObject(cargoFrame, right);
100+
setObject(getCargoFrame(frame), right);
101101
return right;
102102
}
103103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
3+
* Copyright (c) 2014, Regents of the University of California
4+
*
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without modification, are
8+
* permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice, this list of
11+
* conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13+
* conditions and the following disclaimer in the documentation and/or other materials provided
14+
* with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
17+
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21+
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
24+
* OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.oracle.graal.python.nodes.generator;
27+
28+
import com.oracle.graal.python.builtins.objects.function.PArguments;
29+
import com.oracle.graal.python.builtins.objects.generator.GeneratorControlData;
30+
import com.oracle.graal.python.runtime.exception.PException;
31+
import com.oracle.truffle.api.frame.VirtualFrame;
32+
import com.oracle.truffle.api.nodes.Node;
33+
import com.oracle.truffle.api.nodes.NodeCost;
34+
import com.oracle.truffle.api.profiles.ValueProfile;
35+
36+
final class GeneratorAccessNode extends Node {
37+
38+
private final ValueProfile frameProfile = ValueProfile.createClassProfile();
39+
40+
private GeneratorAccessNode() {
41+
// private constructor
42+
}
43+
44+
@Override
45+
public NodeCost getCost() {
46+
return NodeCost.NONE;
47+
}
48+
49+
private GeneratorControlData getControlData(VirtualFrame frame) {
50+
return PArguments.getControlDataFromGeneratorFrame(frameProfile.profile(PArguments.getGeneratorFrame(frame)));
51+
}
52+
53+
public boolean isActive(VirtualFrame frame, int flagSlot) {
54+
return getControlData(frame).getActive(flagSlot);
55+
}
56+
57+
public void setActive(VirtualFrame frame, int flagSlot, boolean value) {
58+
getControlData(frame).setActive(flagSlot, value);
59+
}
60+
61+
public int getIndex(VirtualFrame frame, int blockIndexSlot) {
62+
return getControlData(frame).getBlockIndexAt(blockIndexSlot);
63+
}
64+
65+
public void setIndex(VirtualFrame frame, int blockIndexSlot, int value) {
66+
getControlData(frame).setBlockIndexAt(blockIndexSlot, value);
67+
}
68+
69+
public Object getIterator(VirtualFrame frame, int iteratorSlot) {
70+
return getControlData(frame).getIteratorAt(iteratorSlot);
71+
}
72+
73+
public void setIterator(VirtualFrame frame, int iteratorSlot, Object value) {
74+
getControlData(frame).setIteratorAt(iteratorSlot, value);
75+
}
76+
77+
public PException getActiveException(VirtualFrame frame) {
78+
return getControlData(frame).getActiveException();
79+
}
80+
81+
public void setActiveException(VirtualFrame frame, PException ex) {
82+
getControlData(frame).setActiveException(ex);
83+
}
84+
85+
public static GeneratorAccessNode create() {
86+
return new GeneratorAccessNode();
87+
}
88+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
public final class GeneratorBlockNode extends BaseBlockNode implements GeneratorControlNode {
3636

37+
@Child private GeneratorAccessNode gen = GeneratorAccessNode.create();
3738
private final int indexSlot;
3839

3940
public GeneratorBlockNode(PNode[] statements, int indexSlot) {
@@ -60,21 +61,21 @@ public Object execute(VirtualFrame frame) {
6061
Object result = null;
6162

6263
for (int i = 0; i < statements.length; i++) {
63-
final int currentIndex = getIndex(frame, indexSlot);
64+
final int currentIndex = gen.getIndex(frame, indexSlot);
6465

6566
if (i < currentIndex) {
6667
continue;
6768
}
6869

6970
result = statements[i].execute(frame);
70-
setIndex(frame, indexSlot, currentIndex + 1);
71+
gen.setIndex(frame, indexSlot, currentIndex + 1);
7172
}
7273

7374
reset(frame);
7475
return result;
7576
}
7677

7778
public void reset(VirtualFrame frame) {
78-
setIndex(frame, indexSlot, 0);
79+
gen.setIndex(frame, indexSlot, 0);
7980
}
8081
}

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,49 @@
2727

2828
import com.oracle.graal.python.nodes.statement.StatementNode;
2929
import com.oracle.graal.python.runtime.exception.BreakException;
30+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
3031
import com.oracle.truffle.api.frame.VirtualFrame;
3132
import com.oracle.truffle.api.nodes.ExplodeLoop;
3233

3334
public final class GeneratorBreakNode extends StatementNode implements GeneratorControlNode {
3435

36+
@Child private GeneratorAccessNode gen = GeneratorAccessNode.create();
37+
3538
private final int targetLoopIteratorSlot;
36-
private final int[] enclosingBlockIndexSlots;
37-
private final int[] enclosingIfFlagSlots;
39+
@CompilationFinal(dimensions = 1) private final int[] enclosingBlockIndexSlots;
40+
@CompilationFinal(dimensions = 1) private final int[] enclosingIfFlagSlots;
3841

3942
public GeneratorBreakNode(int targetLoopIteratorSlot, int[] enclosingBlockIndexSlots, int[] enclosingIfFlagSlots) {
4043
this.targetLoopIteratorSlot = targetLoopIteratorSlot;
4144
this.enclosingBlockIndexSlots = enclosingBlockIndexSlots;
4245
this.enclosingIfFlagSlots = enclosingIfFlagSlots;
4346
}
4447

45-
@ExplodeLoop
4648
@Override
4749
public Object execute(VirtualFrame frame) {
48-
setIterator(frame, targetLoopIteratorSlot, null);
50+
gen.setIterator(frame, targetLoopIteratorSlot, null);
4951

50-
for (int indexSlot : enclosingBlockIndexSlots) {
51-
setIndex(frame, indexSlot, 0);
52-
}
52+
iterateBlocks(frame);
53+
iterateIfs(frame);
54+
55+
throw BreakException.INSTANCE;
56+
}
5357

58+
@ExplodeLoop
59+
private void iterateIfs(VirtualFrame frame) {
5460
for (int flagSlot : enclosingIfFlagSlots) {
55-
setActive(frame, flagSlot, false);
61+
gen.setActive(frame, flagSlot, false);
5662
}
63+
}
5764

58-
throw BreakException.INSTANCE;
65+
@ExplodeLoop
66+
private void iterateBlocks(VirtualFrame frame) {
67+
for (int indexSlot : enclosingBlockIndexSlots) {
68+
gen.setIndex(frame, indexSlot, 0);
69+
}
5970
}
6071

6172
public void reset(VirtualFrame frame) {
73+
// empty
6274
}
6375
}

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,45 @@
2727

2828
import com.oracle.graal.python.nodes.statement.StatementNode;
2929
import com.oracle.graal.python.runtime.exception.ContinueException;
30+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
3031
import com.oracle.truffle.api.frame.VirtualFrame;
3132
import com.oracle.truffle.api.nodes.ExplodeLoop;
3233

3334
public final class GeneratorContinueNode extends StatementNode implements GeneratorControlNode {
3435

35-
private final int[] enclosingBlockIndexSlots;
36-
private final int[] enclosingIfFlagSlots;
36+
@Child private GeneratorAccessNode gen = GeneratorAccessNode.create();
37+
38+
@CompilationFinal(dimensions = 1) private final int[] enclosingBlockIndexSlots;
39+
@CompilationFinal(dimensions = 1) private final int[] enclosingIfFlagSlots;
3740

3841
public GeneratorContinueNode(int[] enclosingBlockIndexSlots, int[] enclosingIfFlagSlots) {
3942
this.enclosingBlockIndexSlots = enclosingBlockIndexSlots;
4043
this.enclosingIfFlagSlots = enclosingIfFlagSlots;
4144
}
4245

43-
@ExplodeLoop
4446
@Override
4547
public Object execute(VirtualFrame frame) {
46-
for (int indexSlot : enclosingBlockIndexSlots) {
47-
setIndex(frame, indexSlot, 0);
48-
}
48+
iterateBlocks(frame);
49+
iterateIfs(frame);
50+
51+
throw ContinueException.INSTANCE;
52+
}
4953

54+
@ExplodeLoop
55+
private void iterateIfs(VirtualFrame frame) {
5056
for (int flagSlot : enclosingIfFlagSlots) {
51-
setActive(frame, flagSlot, false);
57+
gen.setActive(frame, flagSlot, false);
5258
}
59+
}
5360

54-
throw ContinueException.INSTANCE;
61+
@ExplodeLoop
62+
private void iterateBlocks(VirtualFrame frame) {
63+
for (int indexSlot : enclosingBlockIndexSlots) {
64+
gen.setIndex(frame, indexSlot, 0);
65+
}
5566
}
5667

5768
public void reset(VirtualFrame frame) {
69+
// empty
5870
}
5971
}

0 commit comments

Comments
 (0)