Skip to content

Commit b8bc521

Browse files
committed
Revert "speculate that a loop is always executed at least once"
This reverts commit 2972f2ffb5225c604ae4ce500db24554aeb59ef3.
1 parent f99d777 commit b8bc521

File tree

1 file changed

+19
-34
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control

1 file changed

+19
-34
lines changed

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

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.oracle.graal.python.runtime.PythonContext;
4040
import com.oracle.graal.python.runtime.PythonOptions;
4141
import com.oracle.graal.python.runtime.exception.PException;
42+
import com.oracle.graal.python.runtime.exception.PythonErrorType;
4243
import com.oracle.truffle.api.CompilerDirectives;
4344
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4445
import com.oracle.truffle.api.Truffle;
@@ -48,15 +49,14 @@
4849
import com.oracle.truffle.api.dsl.Specialization;
4950
import com.oracle.truffle.api.frame.FrameSlot;
5051
import com.oracle.truffle.api.frame.FrameSlotKind;
51-
import com.oracle.truffle.api.frame.FrameUtil;
52+
import com.oracle.truffle.api.frame.FrameSlotTypeException;
5253
import com.oracle.truffle.api.frame.VirtualFrame;
5354
import com.oracle.truffle.api.nodes.NodeInfo;
5455
import com.oracle.truffle.api.nodes.RepeatingNode;
5556

5657
final class ForRepeatingNode extends PNodeWithContext implements RepeatingNode {
57-
@CompilationFinal FrameSlot firstIterationSlot;
58+
5859
@CompilationFinal FrameSlot iteratorSlot;
59-
@CompilationFinal boolean loopNeverSkipped = true;
6060
private final ContextReference<PythonContext> contextRef = PythonLanguage.getContextRef();
6161
@Child ForNextElementNode nextElement;
6262
@Child StatementNode body;
@@ -68,30 +68,16 @@ public ForRepeatingNode(StatementNode target, StatementNode body) {
6868
}
6969

7070
public boolean executeRepeating(VirtualFrame frame) {
71-
Object iteratorObject = FrameUtil.getObjectSafe(frame, iteratorSlot);
72-
if (loopNeverSkipped) {
73-
boolean firstIteration = FrameUtil.getBooleanSafe(frame, firstIterationSlot);
74-
if (firstIteration) {
75-
// first loop iteration, write the iterator
76-
if (!nextElement.execute(frame, iteratorObject)) {
77-
// if we ever skip the loop we invalidate here. this helps the
78-
// compiler understand if a loop is never skipped, that the loop
79-
// variable can never be null after the loop, so it doesn't have
80-
// to merge the last value of the loop variable with null
81-
CompilerDirectives.transferToInterpreterAndInvalidate();
82-
loopNeverSkipped = false;
83-
return false;
84-
}
85-
frame.setBoolean(firstIterationSlot, false);
86-
} else {
87-
if (!nextElement.execute(frame, iteratorObject)) {
88-
return false;
89-
}
90-
}
91-
} else {
92-
if (!nextElement.execute(frame, iteratorObject)) {
71+
try {
72+
if (!nextElement.execute(frame, frame.getObject(iteratorSlot))) {
9373
return false;
9474
}
75+
} catch (FrameSlotTypeException e) {
76+
if (raise == null) {
77+
CompilerDirectives.transferToInterpreterAndInvalidate();
78+
raise = insert(PRaiseNode.create());
79+
}
80+
throw raise.raise(PythonErrorType.RuntimeError, "internal error: unexpected frame slot type");
9581
}
9682
body.executeVoid(frame);
9783
contextRef.get().triggerAsyncActions(frame, this);
@@ -101,6 +87,7 @@ public boolean executeRepeating(VirtualFrame frame) {
10187

10288
@ImportStatic({PythonOptions.class, SpecialMethodNames.class})
10389
abstract class ForNextElementNode extends PNodeWithContext {
90+
10491
@Child StatementNode target;
10592

10693
public ForNextElementNode(StatementNode target) {
@@ -165,10 +152,11 @@ protected boolean doIterator(VirtualFrame frame, Object object,
165152

166153
@NodeInfo(shortName = "for")
167154
public final class ForNode extends LoopNode {
155+
168156
@CompilationFinal private FrameSlot iteratorSlot;
169-
@CompilationFinal private FrameSlot firstIterationSlot;
170-
@Child private ExpressionNode iterator;
157+
171158
@Child private com.oracle.truffle.api.nodes.LoopNode loopNode;
159+
@Child private ExpressionNode iterator;
172160

173161
public ForNode(StatementNode body, StatementNode target, ExpressionNode iterator) {
174162
this.iterator = iterator;
@@ -179,25 +167,22 @@ public StatementNode getTarget() {
179167
return ((ForRepeatingNode) loopNode.getRepeatingNode()).nextElement.target;
180168
}
181169

170+
public ExpressionNode getIterator() {
171+
return iterator;
172+
}
173+
182174
@Override
183175
public StatementNode getBody() {
184176
return ((ForRepeatingNode) loopNode.getRepeatingNode()).body;
185177
}
186178

187-
public ExpressionNode getIterator() {
188-
return iterator;
189-
}
190-
191179
@Override
192180
public void executeVoid(VirtualFrame frame) {
193181
if (iteratorSlot == null) {
194182
CompilerDirectives.transferToInterpreterAndInvalidate();
195183
iteratorSlot = frame.getFrameDescriptor().addFrameSlot(new Object(), FrameSlotKind.Object);
196184
((ForRepeatingNode) loopNode.getRepeatingNode()).iteratorSlot = iteratorSlot;
197-
firstIterationSlot = frame.getFrameDescriptor().addFrameSlot(new Object(), FrameSlotKind.Boolean);
198-
((ForRepeatingNode) loopNode.getRepeatingNode()).firstIterationSlot = firstIterationSlot;
199185
}
200-
frame.setBoolean(firstIterationSlot, true);
201186
frame.setObject(iteratorSlot, iterator.execute(frame));
202187
try {
203188
loopNode.executeLoop(frame);

0 commit comments

Comments
 (0)