56
56
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
57
57
import com .oracle .truffle .api .Assumption ;
58
58
import com .oracle .truffle .api .CompilerDirectives ;
59
+ import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
59
60
import com .oracle .truffle .api .RootCallTarget ;
60
61
import com .oracle .truffle .api .Truffle ;
61
62
import com .oracle .truffle .api .dsl .Cached ;
@@ -70,19 +71,18 @@ public abstract class MakeFunctionNode extends PNodeWithContext {
70
71
private final RootCallTarget callTarget ;
71
72
private final CodeUnit code ;
72
73
private final Signature signature ;
73
- private final PCode cachedCode ;
74
74
private final TruffleString doc ;
75
+ @ CompilationFinal private PCode cachedCode ;
75
76
76
77
private final Assumption sharedCodeStableAssumption = Truffle .getRuntime ().createAssumption ("shared code stable assumption" );
77
78
private final Assumption sharedDefaultsStableAssumption = Truffle .getRuntime ().createAssumption ("shared defaults stable assumption" );
78
79
79
80
public abstract int execute (VirtualFrame frame , Object globals , int initialStackTop , int flags );
80
81
81
- public MakeFunctionNode (RootCallTarget callTarget , CodeUnit code , Signature signature , PCode cachedCode , TruffleString doc ) {
82
+ public MakeFunctionNode (RootCallTarget callTarget , CodeUnit code , Signature signature , TruffleString doc ) {
82
83
this .callTarget = callTarget ;
83
84
this .code = code ;
84
85
this .signature = signature ;
85
- this .cachedCode = cachedCode ;
86
86
this .doc = doc ;
87
87
}
88
88
@@ -94,8 +94,17 @@ int makeFunction(VirtualFrame frame, Object globals, int initialStackTop, int fl
94
94
95
95
PCode codeObj = cachedCode ;
96
96
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
+ }
99
108
}
100
109
101
110
PCell [] closure = null ;
@@ -151,15 +160,11 @@ public static MakeFunctionNode create(PythonLanguage language, CodeUnit code, So
151
160
} else {
152
161
callTarget = bytecodeRootNode .getCallTarget ();
153
162
}
154
- PCode cachedCode = null ;
155
- if (language .isSingleContext ()) {
156
- cachedCode = PythonObjectFactory .getUncached ().createCode (callTarget , bytecodeRootNode .getSignature (), code );
157
- }
158
163
TruffleString doc = null ;
159
164
if (code .constants .length > 0 && code .constants [0 ] instanceof TruffleString ) {
160
165
doc = (TruffleString ) code .constants [0 ];
161
166
}
162
- return MakeFunctionNodeGen .create (callTarget , code , bytecodeRootNode .getSignature (), cachedCode , doc );
167
+ return MakeFunctionNodeGen .create (callTarget , code , bytecodeRootNode .getSignature (), doc );
163
168
}
164
169
165
170
public RootCallTarget getCallTarget () {
0 commit comments