Skip to content

Commit 5e23fc5

Browse files
fangereransalond
authored andcommitted
Share cell assumptions across instances.
(cherry picked from commit 3c2e991)
1 parent ad050cd commit 5e23fc5

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cell/PCell.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@
4444
import com.oracle.truffle.api.Assumption;
4545
import com.oracle.truffle.api.CompilerAsserts;
4646
import com.oracle.truffle.api.CompilerDirectives;
47-
import com.oracle.truffle.api.Truffle;
4847

4948
public final class PCell extends PythonAbstractObject {
50-
private Assumption effectivelyFinal;
49+
private final Assumption effectivelyFinal;
5150
private Object ref;
5251

52+
public PCell(Assumption effectivelyFinalAssumption) {
53+
this.effectivelyFinal = effectivelyFinalAssumption;
54+
}
55+
5356
public Object getRef() {
5457
return ref;
5558
}
@@ -59,7 +62,7 @@ public void clearRef() {
5962
}
6063

6164
public void setRef(Object ref) {
62-
if (effectivelyFinal != null && effectivelyFinal.isValid()) {
65+
if (effectivelyFinal.isValid()) {
6366
if (this.ref != null) {
6467
effectivelyFinal.invalidate();
6568
}
@@ -68,10 +71,6 @@ public void setRef(Object ref) {
6871
}
6972

7073
public Assumption isEffectivelyFinalAssumption() {
71-
CompilerAsserts.neverPartOfCompilation();
72-
if (effectivelyFinal == null) {
73-
effectivelyFinal = Truffle.getRuntime().createAssumption("cell is effectively final");
74-
}
7574
return effectivelyFinal;
7675
}
7776

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/CodeNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public PCode execute(LazyPythonClass cls, int argcount, int kwonlyargcount,
115115
Object ident = freevars[i];
116116
FrameSlot slot = frameDescriptor.addFrameSlot(ident);
117117
frameDescriptor.setFrameSlotKind(slot, FrameSlotKind.Object);
118-
frame.setObject(slot, new PCell());
118+
frame.setObject(slot, new PCell(Truffle.getRuntime().createAssumption("cell is effectively final")));
119119
}
120120
}
121121
rootNode = (RootNode) core.getParser().parse(ParserMode.File, core, Source.newBuilder(PythonLanguage.ID, new String(codestring), name).build(), frame);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
3232
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
3333
import com.oracle.graal.python.parser.ExecutionCellSlots;
34+
import com.oracle.truffle.api.Assumption;
3435
import com.oracle.truffle.api.CompilerDirectives;
3536
import com.oracle.truffle.api.RootCallTarget;
3637
import com.oracle.truffle.api.Truffle;
@@ -61,6 +62,7 @@ public static PGenerator create(LazyPythonClass clazz, String name, RootCallTarg
6162
// set generator closure to the generator frame locals
6263
FrameSlot[] freeVarSlots = cellSlots.getFreeVarSlots();
6364
FrameSlot[] cellVarSlots = cellSlots.getCellVarSlots();
65+
Assumption[] cellVarAssumptions = cellSlots.getCellVarAssumptions();
6466

6567
if (closure != null) {
6668
assert closure.length == freeVarSlots.length : "generator creation: the closure must have the same length as the free var slots array";
@@ -72,8 +74,8 @@ public static PGenerator create(LazyPythonClass clazz, String name, RootCallTarg
7274
}
7375
// initialize own cell vars to new cells (these cells will be used by nested functions to
7476
// create their own closures)
75-
for (FrameSlot frameSlot : cellVarSlots) {
76-
generatorFrame.setObject(frameSlot, new PCell());
77+
for (int i = 0; i < cellVarSlots.length; i++) {
78+
generatorFrame.setObject(cellVarSlots[i], new PCell(cellVarAssumptions[i]));
7779
}
7880
return new PGenerator(clazz, name, callTarget, frameDescriptor, arguments, closure);
7981
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,21 @@
4242

4343
import com.oracle.graal.python.builtins.objects.function.Signature;
4444
import com.oracle.graal.python.parser.ExecutionCellSlots;
45+
import com.oracle.truffle.api.Assumption;
4546
import com.oracle.truffle.api.CompilerDirectives;
4647
import com.oracle.truffle.api.TruffleLanguage;
4748
import com.oracle.truffle.api.frame.FrameDescriptor;
4849
import com.oracle.truffle.api.frame.FrameSlot;
4950

5051
public abstract class PClosureFunctionRootNode extends PClosureRootNode {
5152
@CompilerDirectives.CompilationFinal(dimensions = 1) protected final FrameSlot[] cellVarSlots;
53+
@CompilerDirectives.CompilationFinal(dimensions = 1) protected final Assumption[] cellEffectivelyFinalAssumptions;
5254
private final Signature signature;
5355

5456
protected PClosureFunctionRootNode(TruffleLanguage<?> language, FrameDescriptor frameDescriptor, ExecutionCellSlots executionCellSlots, Signature signature) {
5557
super(language, frameDescriptor, executionCellSlots.getFreeVarSlots());
5658
this.cellVarSlots = executionCellSlots.getCellVarSlots();
59+
this.cellEffectivelyFinalAssumptions = executionCellSlots.getCellVarAssumptions();
5760
this.signature = signature;
5861
}
5962

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private void initializeCellVars(Frame frame) {
111111
cell = (PCell) FrameUtil.getObjectSafe(frame, frameSlot);
112112
}
113113
if (cell == null) {
114-
cell = new PCell();
114+
cell = new PCell(cellEffectivelyFinalAssumptions[i]);
115115
}
116116

117117
// store the cell as a local var

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/ExecutionCellSlots.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -25,15 +25,22 @@
2525
*/
2626
package com.oracle.graal.python.parser;
2727

28+
import com.oracle.truffle.api.Assumption;
29+
import com.oracle.truffle.api.Truffle;
2830
import com.oracle.truffle.api.frame.FrameSlot;
2931

3032
public final class ExecutionCellSlots implements CellSlots {
3133
private final FrameSlot[] cellVarSlots;
3234
private final FrameSlot[] freeVarSlots;
35+
private final Assumption[] cellVarAssumptions;
3336

3437
ExecutionCellSlots(CellFrameSlotSupplier supplier) {
3538
this.cellVarSlots = supplier.getCellVarSlots();
3639
this.freeVarSlots = supplier.getFreeVarSlots();
40+
this.cellVarAssumptions = new Assumption[cellVarSlots.length];
41+
for (int i = 0; i < cellVarAssumptions.length; i++) {
42+
cellVarAssumptions[i] = Truffle.getRuntime().createAssumption("cell is effectively final");
43+
}
3744
}
3845

3946
@Override
@@ -45,4 +52,8 @@ public FrameSlot[] getCellVarSlots() {
4552
public FrameSlot[] getFreeVarSlots() {
4653
return freeVarSlots;
4754
}
55+
56+
public Assumption[] getCellVarAssumptions() {
57+
return cellVarAssumptions;
58+
}
4859
}

0 commit comments

Comments
 (0)