Skip to content

Commit 96a908e

Browse files
committed
ensure that signatures for constant PCode objects are constant, too
1 parent 23ee323 commit 96a908e

File tree

4 files changed

+39
-38
lines changed

4 files changed

+39
-38
lines changed

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

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -233,44 +233,28 @@ static Signature doCached(Node inliningTarget, @SuppressWarnings("unused") PCode
233233
return getInSingleContextMode(inliningTarget, cachedCode);
234234
}
235235

236-
public static Signature getInSingleContextMode(Node inliningTarget, PFunction fun, InlinedBranchProfile firstExecution) {
236+
public static Signature getInSingleContextMode(Node inliningTarget, PFunction fun) {
237237
assert isSingleContext(inliningTarget);
238-
if (CompilerDirectives.inCompiledCode() && CompilerDirectives.isPartialEvaluationConstant(fun.getCode())) {
239-
getInSingleContextMode(inliningTarget, fun.getCode());
240-
}
241-
PCode code = fun.getCode();
242-
Signature signature = code.signature;
243-
if (signature == null) {
244-
firstExecution.enter(inliningTarget);
245-
RootCallTarget ct = code.initializeCallTarget();
246-
signature = code.initializeSignature(ct);
238+
CompilerAsserts.partialEvaluationConstant(fun);
239+
if (CompilerDirectives.inCompiledCode()) {
240+
return getInSingleContextMode(inliningTarget, fun.getCode());
241+
} else {
242+
PCode code = fun.getCode();
243+
return code.getSignature();
247244
}
248-
return signature;
249245
}
250246

251247
public static Signature getInSingleContextMode(Node inliningTarget, PCode code) {
252248
assert isSingleContext(inliningTarget);
253249
CompilerAsserts.partialEvaluationConstant(code);
254-
if (code.signature == null) {
255-
CompilerDirectives.transferToInterpreterAndInvalidate();
256-
code.initializeSignature(code.initializeCallTarget());
257-
}
258-
return code.signature;
250+
return code.getSignature();
259251
}
260252

261253
@Specialization(replaces = "doCached")
262254
static Signature getGeneric(Node inliningTarget, PCode code,
263255
@Cached InlinedConditionProfile signatureProfile,
264256
@Cached InlinedConditionProfile ctProfile) {
265-
Signature signature = code.signature;
266-
if (signatureProfile.profile(inliningTarget, signature == null)) {
267-
RootCallTarget ct = code.callTarget;
268-
if (ctProfile.profile(inliningTarget, ct == null)) {
269-
ct = code.initializeCallTarget();
270-
}
271-
signature = code.initializeSignature(ct);
272-
}
273-
return signature;
257+
return code.getSignature(inliningTarget, signatureProfile);
274258
}
275259
}
276260

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
import com.oracle.graal.python.util.Supplier;
7777
import com.oracle.truffle.api.CallTarget;
7878
import com.oracle.truffle.api.CompilerAsserts;
79+
import com.oracle.truffle.api.CompilerDirectives;
80+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
7981
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
8082
import com.oracle.truffle.api.RootCallTarget;
8183
import com.oracle.truffle.api.dsl.Cached;
@@ -84,8 +86,10 @@
8486
import com.oracle.truffle.api.interop.UnsupportedMessageException;
8587
import com.oracle.truffle.api.library.ExportLibrary;
8688
import com.oracle.truffle.api.library.ExportMessage;
89+
import com.oracle.truffle.api.nodes.Node;
8790
import com.oracle.truffle.api.nodes.RootNode;
8891
import com.oracle.truffle.api.object.Shape;
92+
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
8993
import com.oracle.truffle.api.source.SourceSection;
9094
import com.oracle.truffle.api.strings.TruffleString;
9195

@@ -109,7 +113,7 @@ public final class PCode extends PythonBuiltinObject {
109113
// set. Otherwise, these are lazily created from the supplier.
110114
private Supplier<CallTarget> callTargetSupplier;
111115
RootCallTarget callTarget;
112-
Signature signature;
116+
@CompilationFinal private Signature signature;
113117

114118
// number of local variables
115119
private int nlocals = -1;
@@ -559,6 +563,23 @@ public boolean takesVarKeywordArgs() {
559563
return PCode.takesVarKeywordArgs(getFlags());
560564
}
561565

566+
public Signature getSignature() {
567+
return getSignature(null, InlinedConditionProfile.getUncached());
568+
}
569+
570+
public Signature getSignature(Node inliningTarget, InlinedConditionProfile signatureProfile) {
571+
if (signatureProfile.profile(inliningTarget, signature == null)) {
572+
if (CompilerDirectives.isPartialEvaluationConstant(this)) {
573+
CompilerDirectives.transferToInterpreterAndInvalidate();
574+
}
575+
if (callTarget == null) {
576+
callTarget = initializeCallTarget();
577+
}
578+
signature = initializeSignature(callTarget);
579+
}
580+
return signature;
581+
}
582+
562583
@TruffleBoundary
563584
synchronized Signature initializeSignature(RootCallTarget rootCallTarget) {
564585
if (signature == null) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/argument/CreateArgumentsNode.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,11 @@ public abstract class CreateArgumentsNode extends PNodeWithContext {
113113
Object[] doMethodCached(@SuppressWarnings("unused") PMethodBase method, Object[] userArguments, PKeyword[] keywords,
114114
@Bind("this") Node inliningTarget,
115115
@Cached CreateAndCheckArgumentsNode createAndCheckArgumentsNode,
116-
@Cached InlinedBranchProfile wasFirst,
117116
@Cached(value = "method", weak = true) PMethodBase cachedMethod) {
118117

119118
CompilerAsserts.partialEvaluationConstant(getFunction(cachedMethod));
120119
// Following getters should fold since getFunction(cachedMethod) is constant
121-
Signature signature = GetSignatureNode.getMethodSignatureSingleContext(cachedMethod, inliningTarget, wasFirst);
120+
Signature signature = GetSignatureNode.getMethodSignatureSingleContext(cachedMethod, inliningTarget);
122121
Object[] defaults = GetDefaultsNode.getMethodDefaults(cachedMethod);
123122
PKeyword[] kwdefaults = GetKeywordDefaultsNode.getMethodKeywords(cachedMethod);
124123
Object self = cachedMethod.getSelf();
@@ -133,12 +132,11 @@ Object[] doMethodCached(@SuppressWarnings("unused") PMethodBase method, Object[]
133132
Object[] doMethodFunctionAndSelfCached(PMethodBase method, Object[] userArguments, PKeyword[] keywords,
134133
@Bind("this") Node inliningTarget,
135134
@Cached CreateAndCheckArgumentsNode createAndCheckArgumentsNode,
136-
@Cached InlinedBranchProfile wasFirst,
137135
@Cached(value = "getFunction(method)", weak = true) @SuppressWarnings("unused") Object cachedFunction,
138136
@Cached(value = "method.getSelf()", weak = true) Object cachedSelf,
139137
@Cached(value = "getClassObject(method)", weak = true) Object cachedClassObject) {
140138
// Following getters should fold since getFunction(cachedMethod) is constant
141-
Signature signature = GetSignatureNode.getFunctionSignatureSingleContext(inliningTarget, wasFirst, cachedFunction);
139+
Signature signature = GetSignatureNode.getFunctionSignatureSingleContext(inliningTarget, cachedFunction);
142140
Object[] defaults = GetDefaultsNode.getFunctionDefaults(cachedFunction);
143141
PKeyword[] kwdefaults = GetKeywordDefaultsNode.getFunctionKeywords(cachedFunction);
144142
return createAndCheckArgumentsNode.execute(inliningTarget, method, userArguments, keywords, signature, cachedSelf, cachedClassObject, defaults, kwdefaults, isMethodCall(cachedSelf));
@@ -147,11 +145,10 @@ Object[] doMethodFunctionAndSelfCached(PMethodBase method, Object[] userArgument
147145
@Specialization(guards = {"isSingleContext()", "getFunction(method) == cachedFunction"}, limit = "getVariableArgumentInlineCacheLimit()", replaces = "doMethodFunctionAndSelfCached")
148146
Object[] doMethodFunctionCached(PMethodBase method, Object[] userArguments, PKeyword[] keywords,
149147
@Bind("this") Node inliningTarget,
150-
@Cached InlinedBranchProfile wasFirst,
151148
@Cached CreateAndCheckArgumentsNode createAndCheckArgumentsNode,
152149
@Cached(value = "getFunction(method)", weak = true) @SuppressWarnings("unused") Object cachedFunction) {
153150
// Following getters should fold since getFunction(cachedMethod) is constant
154-
Signature signature = GetSignatureNode.getFunctionSignatureSingleContext(inliningTarget, wasFirst, cachedFunction);
151+
Signature signature = GetSignatureNode.getFunctionSignatureSingleContext(inliningTarget, cachedFunction);
155152
Object[] defaults = GetDefaultsNode.getFunctionDefaults(cachedFunction);
156153
PKeyword[] kwdefaults = GetKeywordDefaultsNode.getFunctionKeywords(cachedFunction);
157154
Object self = method.getSelf();
@@ -163,9 +160,8 @@ Object[] doMethodFunctionCached(PMethodBase method, Object[] userArguments, PKey
163160
Object[] doFunctionCached(PFunction callable, Object[] userArguments, PKeyword[] keywords,
164161
@Bind("this") Node inliningTarget,
165162
@Cached CreateAndCheckArgumentsNode createAndCheckArgumentsNode,
166-
@Cached InlinedBranchProfile firstExecution,
167163
@Cached(value = "callable", weak = true) @SuppressWarnings("unused") PFunction cachedCallable) {
168-
Signature signature = CodeNodes.GetCodeSignatureNode.getInSingleContextMode(inliningTarget, cachedCallable, firstExecution);
164+
Signature signature = CodeNodes.GetCodeSignatureNode.getInSingleContextMode(inliningTarget, cachedCallable);
169165
Object[] defaults = cachedCallable.getDefaults();
170166
PKeyword[] kwdefaults = cachedCallable.getKwDefaults();
171167
return createAndCheckArgumentsNode.execute(inliningTarget, callable, userArguments, keywords, signature, null, null, defaults, kwdefaults, false);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,18 @@ public static Signature executeUncached(Object function) {
226226
/**
227227
* Fast-path if method is a partial evaluation constant, and we are in single context mode.
228228
*/
229-
public static Signature getMethodSignatureSingleContext(PMethodBase method, Node inliningTarget, InlinedBranchProfile wasFirst) {
229+
public static Signature getMethodSignatureSingleContext(PMethodBase method, Node inliningTarget) {
230230
CompilerAsserts.partialEvaluationConstant(method);
231-
return getFunctionSignatureSingleContext(inliningTarget, wasFirst, method.getFunction());
231+
return getFunctionSignatureSingleContext(inliningTarget, method.getFunction());
232232
}
233233

234234
/**
235235
* Fast-path if method is a partial evaluation constant, and we are in single context mode.
236236
*/
237-
public static Signature getFunctionSignatureSingleContext(Node inliningTarget, InlinedBranchProfile wasFirst, Object fun) {
237+
public static Signature getFunctionSignatureSingleContext(Node inliningTarget, Object fun) {
238238
CompilerAsserts.partialEvaluationConstant(fun);
239239
if (fun instanceof PFunction f) {
240-
return CodeNodes.GetCodeSignatureNode.getInSingleContextMode(inliningTarget, f, wasFirst);
240+
return CodeNodes.GetCodeSignatureNode.getInSingleContextMode(inliningTarget, f);
241241
} else if (fun instanceof PBuiltinFunction f) {
242242
return f.getSignature();
243243
}

0 commit comments

Comments
 (0)