Skip to content

Commit c0da72a

Browse files
committed
some improvements in FunctionNodes and CallTargetInvokeNode
1 parent f05db68 commit c0da72a

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/builtins/FunctionNodes.java

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@
5656
import com.oracle.graal.python.nodes.builtins.FunctionNodesFactory.GetKeywordDefaultsNodeGen;
5757
import com.oracle.graal.python.nodes.builtins.FunctionNodesFactory.GetSignatureNodeGen;
5858
import com.oracle.truffle.api.Assumption;
59+
import com.oracle.truffle.api.dsl.Bind;
5960
import com.oracle.truffle.api.dsl.Cached;
61+
import com.oracle.truffle.api.dsl.Cached.Shared;
6062
import com.oracle.truffle.api.dsl.GenerateUncached;
6163
import com.oracle.truffle.api.dsl.ImportStatic;
6264
import com.oracle.truffle.api.dsl.Specialization;
6365

6466
public abstract class FunctionNodes {
6567
@GenerateUncached
6668
public abstract static class GetFunctionDefaultsNode extends PNodeWithContext {
69+
6770
public abstract Object[] execute(PFunction function);
6871

6972
@Specialization(guards = {"self == cachedSelf"}, assumptions = {"singleContextAssumption()", "defaultsStableAssumption"})
@@ -78,42 +81,41 @@ Object[] getDefaultsCached(@SuppressWarnings("unused") PFunction self,
7881
Object[] getDefaults(PFunction self) {
7982
return self.getDefaults();
8083
}
81-
82-
public static GetFunctionDefaultsNode create() {
83-
return GetFunctionDefaultsNodeGen.create();
84-
}
8584
}
8685

8786
@ImportStatic(PGuards.class)
8887
@GenerateUncached
8988
public abstract static class GetDefaultsNode extends PNodeWithContext {
90-
private static Object[] doFunctionInternal(GetFunctionDefaultsNode getFunctionDefaultsNode, PFunction function) {
91-
return getFunctionDefaultsNode.execute(function);
92-
}
9389

9490
public abstract Object[] execute(Object function);
9591

9692
@Specialization
9793
Object[] doFunction(PFunction function,
98-
@Cached("create()") GetFunctionDefaultsNode getFunctionDefaultsNode) {
99-
return doFunctionInternal(getFunctionDefaultsNode, function);
94+
@Shared("get") @Cached GetFunctionDefaultsNode getFunctionDefaultsNode) {
95+
return getFunctionDefaultsNode.execute(function);
10096
}
10197

10298
@Specialization
10399
Object[] doBuiltinFunction(PBuiltinFunction builtinFunction) {
104100
return builtinFunction.getDefaults();
105101
}
106102

107-
@Specialization(guards = "!isSameObject(method, method.getFunction())")
108-
Object[] doMethod(PMethod method,
109-
@Cached("create()") GetDefaultsNode getDefaultsNode) {
110-
return getDefaultsNode.execute(method.getFunction());
103+
@Specialization(guards = "isPFunction(function)")
104+
Object[] doMethod(@SuppressWarnings("unused") PMethod method,
105+
@Bind("method.getFunction()") Object function,
106+
@Shared("get") @Cached GetFunctionDefaultsNode getFunctionDefaultsNode) {
107+
return getFunctionDefaultsNode.execute((PFunction) function);
111108
}
112109

113-
@Specialization(guards = "!isSameObject(builtinMethod, builtinMethod.getFunction())")
114-
Object[] doBuiltinMethod(PBuiltinMethod builtinMethod,
115-
@Cached("create()") GetDefaultsNode getDefaultsNode) {
116-
return getDefaultsNode.execute(builtinMethod.getFunction());
110+
@Specialization(guards = "isPBuiltinFunction(method.getFunction())")
111+
Object[] doMethod(@SuppressWarnings("unused") PMethod method,
112+
@Bind("method.getFunction()") Object function) {
113+
return ((PBuiltinFunction) function).getDefaults();
114+
}
115+
116+
@Specialization
117+
Object[] doBuiltinMethod(PBuiltinMethod builtinMethod) {
118+
return builtinMethod.getFunction().getDefaults();
117119
}
118120

119121
public static GetDefaultsNode create() {
@@ -123,6 +125,7 @@ public static GetDefaultsNode create() {
123125

124126
@GenerateUncached
125127
public abstract static class GetFunctionKeywordDefaultsNode extends PNodeWithContext {
128+
126129
public abstract PKeyword[] execute(PFunction function);
127130

128131
@Specialization(guards = {"self == cachedSelf"}, assumptions = {"singleContextAssumption()", "defaultsStableAssumption"})
@@ -137,42 +140,41 @@ PKeyword[] getKwDefaultsCached(@SuppressWarnings("unused") PFunction self,
137140
PKeyword[] getKwDefaults(PFunction self) {
138141
return self.getKwDefaults();
139142
}
140-
141-
public static GetFunctionKeywordDefaultsNode create() {
142-
return GetFunctionKeywordDefaultsNodeGen.create();
143-
}
144143
}
145144

146145
@ImportStatic(PGuards.class)
147146
@GenerateUncached
148147
public abstract static class GetKeywordDefaultsNode extends PNodeWithContext {
149-
private static PKeyword[] doFunctionInternal(GetFunctionKeywordDefaultsNode getFunctionKeywordDefaultsNode, PFunction function) {
150-
return getFunctionKeywordDefaultsNode.execute(function);
151-
}
152148

153149
public abstract PKeyword[] execute(Object function);
154150

155151
@Specialization
156152
PKeyword[] doFunction(PFunction function,
157-
@Cached("create()") GetFunctionKeywordDefaultsNode getFunctionKeywordDefaultsNode) {
158-
return doFunctionInternal(getFunctionKeywordDefaultsNode, function);
153+
@Shared("get") @Cached GetFunctionKeywordDefaultsNode getFunctionKeywordDefaultsNode) {
154+
return getFunctionKeywordDefaultsNode.execute(function);
159155
}
160156

161157
@Specialization
162158
PKeyword[] doBuiltinFunction(PBuiltinFunction builtinFunction) {
163159
return builtinFunction.getKwDefaults();
164160
}
165161

166-
@Specialization(guards = "!isSameObject(method, method.getFunction())")
167-
PKeyword[] doMethod(PMethod method,
168-
@Cached("create()") GetKeywordDefaultsNode getKeywordDefaultsNode) {
169-
return getKeywordDefaultsNode.execute(method.getFunction());
162+
@Specialization(guards = "isPFunction(function)")
163+
PKeyword[] doMethod(@SuppressWarnings("unused") PMethod method,
164+
@Bind("method.getFunction()") Object function,
165+
@Shared("get") @Cached GetFunctionKeywordDefaultsNode getFunctionKeywordDefaultsNode) {
166+
return getFunctionKeywordDefaultsNode.execute((PFunction) function);
167+
}
168+
169+
@Specialization(guards = "isPBuiltinFunction(method.getFunction())")
170+
PKeyword[] doMethod(@SuppressWarnings("unused") PMethod method,
171+
@Bind("method.getFunction()") Object function) {
172+
return ((PBuiltinFunction) function).getKwDefaults();
170173
}
171174

172-
@Specialization(guards = "!isSameObject(builtinMethod, builtinMethod.getFunction())")
173-
PKeyword[] doBuiltinMethod(PBuiltinMethod builtinMethod,
174-
@Cached("create()") GetKeywordDefaultsNode getKeywordDefaultsNode) {
175-
return getKeywordDefaultsNode.execute(builtinMethod.getFunction());
175+
@Specialization
176+
PKeyword[] doBuiltinMethod(PBuiltinMethod builtinMethod) {
177+
return builtinMethod.getFunction().getKwDefaults();
176178
}
177179

178180
public static GetKeywordDefaultsNode create() {
@@ -182,6 +184,7 @@ public static GetKeywordDefaultsNode create() {
182184

183185
@GenerateUncached
184186
public abstract static class GetFunctionCodeNode extends PNodeWithContext {
187+
185188
public abstract PCode execute(PFunction function);
186189

187190
@Specialization(guards = {"self == cachedSelf"}, assumptions = {"singleContextAssumption()", "codeStableAssumption"})
@@ -196,43 +199,41 @@ PCode getCodeCached(@SuppressWarnings("unused") PFunction self,
196199
PCode getCodeUncached(PFunction self) {
197200
return self.getCode();
198201
}
199-
200-
public static GetFunctionCodeNode create() {
201-
return GetFunctionCodeNodeGen.create();
202-
}
203202
}
204203

205204
@ImportStatic(PGuards.class)
206205
@GenerateUncached
207206
public abstract static class GetSignatureNode extends PNodeWithContext {
208-
private static Signature doFunctionInternal(GetFunctionCodeNode getFunctionCodeNode, PFunction function) {
209-
PCode code = getFunctionCodeNode.execute(function);
210-
return code.getSignature();
211-
}
212207

213208
public abstract Signature execute(Object function);
214209

215210
@Specialization
216211
Signature doFunction(PFunction function,
217-
@Cached("create()") GetFunctionCodeNode getFunctionCodeNode) {
218-
return doFunctionInternal(getFunctionCodeNode, function);
212+
@Shared("get") @Cached GetFunctionCodeNode getFunctionCodeNode) {
213+
return getFunctionCodeNode.execute(function).getSignature();
219214
}
220215

221216
@Specialization
222217
Signature doBuiltinFunction(PBuiltinFunction builtinFunction) {
223218
return builtinFunction.getSignature();
224219
}
225220

226-
@Specialization(guards = "!isSameObject(method, method.getFunction())")
227-
Signature doMethod(PMethod method,
228-
@Cached("create()") GetSignatureNode getSignatureNode) {
229-
return getSignatureNode.execute(method.getFunction());
221+
@Specialization(guards = "isPFunction(function)")
222+
Signature doMethod(@SuppressWarnings("unused") PMethod method,
223+
@Bind("method.getFunction()") Object function,
224+
@Shared("get") @Cached GetFunctionCodeNode getFunctionCodeNode) {
225+
return getFunctionCodeNode.execute((PFunction) function).getSignature();
230226
}
231227

232-
@Specialization(guards = "!isSameObject(builtinMethod, builtinMethod.getFunction())")
233-
Signature doBuiltinMethod(PBuiltinMethod builtinMethod,
234-
@Cached("create()") GetSignatureNode getSignatureNode) {
235-
return getSignatureNode.execute(builtinMethod.getFunction());
228+
@Specialization(guards = "isPBuiltinFunction(method.getFunction())")
229+
Signature doMethod(@SuppressWarnings("unused") PMethod method,
230+
@Bind("method.getFunction()") Object function) {
231+
return ((PBuiltinFunction) function).getSignature();
232+
}
233+
234+
@Specialization
235+
Signature doBuiltinMethod(PBuiltinMethod builtinMethod) {
236+
return builtinMethod.getFunction().getSignature();
236237
}
237238

238239
public static GetSignatureNode create() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/CallTargetInvokeNode.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5555
import com.oracle.truffle.api.RootCallTarget;
5656
import com.oracle.truffle.api.Truffle;
57+
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
5758
import com.oracle.truffle.api.dsl.Cached;
5859
import com.oracle.truffle.api.dsl.CachedContext;
5960
import com.oracle.truffle.api.dsl.Specialization;
@@ -107,7 +108,7 @@ public static CallTargetInvokeNode create(CallTarget callTarget, boolean isBuilt
107108
protected Object doNoClosure(VirtualFrame frame, PFunction callee, @SuppressWarnings("unused") PythonObject globals, @SuppressWarnings("unused") PCell[] closure, Object[] arguments,
108109
@Cached("createBinaryProfile()") ConditionProfile classBodyProfile,
109110
@Cached("createBinaryProfile()") ConditionProfile generatorFunctionProfile,
110-
@CachedContext(PythonLanguage.class) PythonContext context) {
111+
@CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
111112
RootCallTarget ct = (RootCallTarget) callNode.getCurrentCallTarget();
112113
optionallySetClassBodySpecial(arguments, ct, classBodyProfile);
113114
optionallySetGeneratorFunction(arguments, ct, generatorFunctionProfile, callee);
@@ -117,6 +118,7 @@ protected Object doNoClosure(VirtualFrame frame, PFunction callee, @SuppressWarn
117118
// 2. This invoke node is (indirectly) used behind a TruffleBoundary.
118119
// This is preferably prepared using 'IndirectCallContext.enter'.
119120
if (profileIsNullFrame(frame == null)) {
121+
PythonContext context = contextRef.get();
120122
PFrame.Reference frameInfo = IndirectCalleeContext.enter(context, arguments, ct);
121123
try {
122124
return callNode.call(arguments);
@@ -133,10 +135,10 @@ protected Object doNoClosure(VirtualFrame frame, PFunction callee, @SuppressWarn
133135
protected Object doGeneric(VirtualFrame frame, PFunction callee, PythonObject globals, PCell[] closure, Object[] arguments,
134136
@Cached("createBinaryProfile()") ConditionProfile classBodyProfile,
135137
@Cached("createBinaryProfile()") ConditionProfile generatorFunctionProfile,
136-
@CachedContext(PythonLanguage.class) PythonContext context) {
138+
@CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
137139
PArguments.setGlobals(arguments, globals);
138140
PArguments.setClosure(arguments, closure);
139-
return doNoClosure(frame, callee, null, null, arguments, classBodyProfile, generatorFunctionProfile, context);
141+
return doNoClosure(frame, callee, null, null, arguments, classBodyProfile, generatorFunctionProfile, contextRef);
140142
}
141143

142144
public final CallTarget getCallTarget() {

0 commit comments

Comments
 (0)