Skip to content

Commit 5b21c9b

Browse files
committed
Don't create PCode objects in MakeFunctionNode#create
1 parent 2ab2706 commit 5b21c9b

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/MakeFunctionNode.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5757
import com.oracle.truffle.api.Assumption;
5858
import com.oracle.truffle.api.CompilerDirectives;
59+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
5960
import com.oracle.truffle.api.RootCallTarget;
6061
import com.oracle.truffle.api.Truffle;
6162
import com.oracle.truffle.api.dsl.Cached;
@@ -70,19 +71,18 @@ public abstract class MakeFunctionNode extends PNodeWithContext {
7071
private final RootCallTarget callTarget;
7172
private final CodeUnit code;
7273
private final Signature signature;
73-
private final PCode cachedCode;
7474
private final TruffleString doc;
75+
@CompilationFinal private PCode cachedCode;
7576

7677
private final Assumption sharedCodeStableAssumption = Truffle.getRuntime().createAssumption("shared code stable assumption");
7778
private final Assumption sharedDefaultsStableAssumption = Truffle.getRuntime().createAssumption("shared defaults stable assumption");
7879

7980
public abstract int execute(VirtualFrame frame, Object globals, int initialStackTop, int flags);
8081

81-
public MakeFunctionNode(RootCallTarget callTarget, CodeUnit code, Signature signature, PCode cachedCode, TruffleString doc) {
82+
public MakeFunctionNode(RootCallTarget callTarget, CodeUnit code, Signature signature, TruffleString doc) {
8283
this.callTarget = callTarget;
8384
this.code = code;
8485
this.signature = signature;
85-
this.cachedCode = cachedCode;
8686
this.doc = doc;
8787
}
8888

@@ -94,8 +94,17 @@ int makeFunction(VirtualFrame frame, Object globals, int initialStackTop, int fl
9494

9595
PCode codeObj = cachedCode;
9696
if (codeObj == null) {
97-
// Multi-context mode
98-
codeObj = factory.createCode(callTarget, signature, code);
97+
if (PythonLanguage.get(this).isSingleContext()) {
98+
CompilerDirectives.transferToInterpreterAndInvalidate();
99+
/*
100+
* We cannot initialize the cached code in create, because that may be called
101+
* without langauge context when materializing nodes for instrumentation
102+
*/
103+
cachedCode = codeObj = factory.createCode(callTarget, signature, code);
104+
} else {
105+
// In multi-context mode we have to create the code for every execution
106+
codeObj = factory.createCode(callTarget, signature, code);
107+
}
99108
}
100109

101110
PCell[] closure = null;
@@ -151,15 +160,11 @@ public static MakeFunctionNode create(PythonLanguage language, CodeUnit code, So
151160
} else {
152161
callTarget = bytecodeRootNode.getCallTarget();
153162
}
154-
PCode cachedCode = null;
155-
if (language.isSingleContext()) {
156-
cachedCode = PythonObjectFactory.getUncached().createCode(callTarget, bytecodeRootNode.getSignature(), code);
157-
}
158163
TruffleString doc = null;
159164
if (code.constants.length > 0 && code.constants[0] instanceof TruffleString) {
160165
doc = (TruffleString) code.constants[0];
161166
}
162-
return MakeFunctionNodeGen.create(callTarget, code, bytecodeRootNode.getSignature(), cachedCode, doc);
167+
return MakeFunctionNodeGen.create(callTarget, code, bytecodeRootNode.getSignature(), doc);
163168
}
164169

165170
public RootCallTarget getCallTarget() {

0 commit comments

Comments
 (0)