Skip to content

Commit 1830b87

Browse files
committed
further simplifications in generator nodes
1 parent 68122bd commit 1830b87

File tree

17 files changed

+209
-617
lines changed

17 files changed

+209
-617
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_generator-if-and-loop.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,22 @@ def foo(arg):
4040

4141
assert foo([1,2,3,4]) == [1,2,3,4]
4242
assert foo("hello") == ["h", "e", "l", "l", "o"]
43+
44+
def gen_effect(n):
45+
if effect(n) == 5:
46+
yield 1
47+
yield 2
48+
else:
49+
yield 3
50+
yield 4
51+
52+
effect_count = 0
53+
def effect(n):
54+
global effect_count
55+
effect_count += 1
56+
return n
57+
58+
def test_sideeffect():
59+
l = list(gen_effect(5))
60+
assert l == [1,2]
61+
assert effect_count == 1

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import com.oracle.graal.python.nodes.function.ClassBodyRootNode;
7676
import com.oracle.graal.python.nodes.function.FunctionRootNode;
7777
import com.oracle.graal.python.nodes.generator.DictConcatNode;
78-
import com.oracle.graal.python.nodes.generator.ListAppendNode;
7978
import com.oracle.graal.python.nodes.generator.YieldNode;
8079
import com.oracle.graal.python.nodes.generator.YieldResumeNode;
8180
import com.oracle.graal.python.nodes.literal.BooleanLiteralNode;
@@ -281,11 +280,6 @@ public PNode createSetLiteral(Set<PNode> values) {
281280
return new SetLiteralNode(convertedValues);
282281
}
283282

284-
public PNode createListAppend(FrameSlot frameSlot, PNode right) {
285-
PNode readList = createReadLocal(frameSlot);
286-
return ListAppendNode.create(readList, right);
287-
}
288-
289283
public PNode createUnaryOperation(String string, PNode operand) {
290284
switch (string) {
291285
case "+":
@@ -523,10 +517,6 @@ public PNode createDictionaryConcat(PNode... dictNodes) {
523517
return DictConcatNode.create(dictNodes);
524518
}
525519

526-
public PNode createListAppend(PNode leftNode, PNode rightNode) {
527-
return ListAppendNode.create(leftNode, rightNode);
528-
}
529-
530520
public PNode callBuiltin(String string, PNode argument) {
531521
return PythonCallNode.create(getBuiltin(string), new PNode[]{argument}, new PNode[0], EmptyNode.create(), EmptyNode.create());
532522
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/LoopNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public abstract class LoopNode extends StatementNode {
3232

3333
public abstract PNode getBody();
3434

35-
protected final void reportLoopCount(@SuppressWarnings("unused") int value) {
36-
// ignore for the time being
35+
protected final void reportLoopCount(int value) {
36+
com.oracle.truffle.api.nodes.LoopNode.reportLoopCount(this, value);
3737
}
3838
}

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

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,39 @@
2727

2828
import java.util.List;
2929

30+
import com.oracle.graal.python.builtins.objects.PNone;
3031
import com.oracle.graal.python.nodes.PNode;
3132
import com.oracle.graal.python.nodes.control.BaseBlockNode;
33+
import com.oracle.graal.python.runtime.exception.YieldException;
34+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
3235
import com.oracle.truffle.api.frame.VirtualFrame;
3336
import com.oracle.truffle.api.nodes.ExplodeLoop;
37+
import com.oracle.truffle.api.profiles.BranchProfile;
38+
import com.oracle.truffle.api.profiles.ConditionProfile;
3439

3540
public final class GeneratorBlockNode extends BaseBlockNode implements GeneratorControlNode {
3641

42+
private static final boolean[] EMPTY = new boolean[0];
43+
3744
@Child private GeneratorAccessNode gen = GeneratorAccessNode.create();
45+
46+
private final ConditionProfile needsUpdateProfile = ConditionProfile.createBinaryProfile();
47+
private final BranchProfile seenYield = BranchProfile.create();
48+
@CompilationFinal(dimensions = 1) private final boolean[] isYield;
3849
private final int indexSlot;
3950

4051
public GeneratorBlockNode(PNode[] statements, int indexSlot) {
4152
super(statements);
53+
boolean[] yields = EMPTY;
54+
for (int i = 0; i < statements.length; i++) {
55+
if (statements[i] instanceof YieldNode) {
56+
if (yields.length == 0) {
57+
yields = new boolean[statements.length];
58+
}
59+
yields[i] = true;
60+
}
61+
}
62+
this.isYield = yields;
4263
this.indexSlot = indexSlot;
4364
}
4465

@@ -58,24 +79,30 @@ public GeneratorBlockNode insertNodesBefore(PNode insertBefore, List<PNode> inse
5879
@ExplodeLoop
5980
@Override
6081
public Object execute(VirtualFrame frame) {
61-
Object result = null;
62-
63-
for (int i = 0; i < statements.length; i++) {
64-
final int currentIndex = gen.getIndex(frame, indexSlot);
65-
66-
if (i < currentIndex) {
67-
continue;
82+
int startIndex = gen.getIndex(frame, indexSlot);
83+
int i = 0;
84+
int nextIndex = 0;
85+
try {
86+
Object result = PNone.NONE;
87+
for (i = 0; i < statements.length; i++) {
88+
if (i >= startIndex) {
89+
result = statements[i].execute(frame);
90+
}
91+
}
92+
return result;
93+
} catch (YieldException e) {
94+
seenYield.enter();
95+
if (i < isYield.length && isYield[i]) {
96+
// continue after the yield
97+
nextIndex = i + 1;
98+
} else {
99+
nextIndex = i;
100+
}
101+
throw e;
102+
} finally {
103+
if (needsUpdateProfile.profile(nextIndex != startIndex)) {
104+
gen.setIndex(frame, indexSlot, nextIndex);
68105
}
69-
70-
result = statements[i].execute(frame);
71-
gen.setIndex(frame, indexSlot, currentIndex + 1);
72106
}
73-
74-
reset(frame);
75-
return result;
76-
}
77-
78-
public void reset(VirtualFrame frame) {
79-
gen.setIndex(frame, indexSlot, 0);
80107
}
81108
}

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

Lines changed: 0 additions & 75 deletions
This file was deleted.

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

Lines changed: 0 additions & 71 deletions
This file was deleted.

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,5 @@
2525
*/
2626
package com.oracle.graal.python.nodes.generator;
2727

28-
import com.oracle.truffle.api.frame.VirtualFrame;
29-
3028
public interface GeneratorControlNode {
31-
32-
void reset(VirtualFrame frame);
3329
}

0 commit comments

Comments
 (0)