Skip to content

Commit e21355d

Browse files
committed
Fix overloaded method caching in HostExecuteNode.
1 parent cd08f59 commit e21355d

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

truffle/src/com.oracle.truffle.host/src/com/oracle/truffle/host/HostExecuteNode.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -152,14 +152,14 @@ static Object doVarArgs(Node node, SingleMethod method, Object obj, Object[] arg
152152
@Exclusive @Cached InlinedExactClassProfile receiverProfile,
153153
@Shared("errorBranch") @Cached InlinedBranchProfile errorBranch,
154154
@Shared("seenScope") @Cached InlinedBranchProfile seenDynamicScope,
155-
@Shared @Cached InlinedBranchProfile seenVargArgs,
155+
@Shared @Cached InlinedBranchProfile seenVarArgs,
156156
@Exclusive @Cached HostTargetMappingNode varArgsMappingNode,
157157
@Exclusive @CachedLibrary(limit = "3") InteropLibrary varArgsMappingInterop,
158158
@Cached(value = "hostContext.getGuestToHostCache()", allowUncached = true) GuestToHostCodeCache cache) throws ArityException, UnsupportedTypeException {
159159
boolean asVarArgs;
160160
int parameterCount = cachedMethod.getParameterCount();
161161
if (args.length == parameterCount) {
162-
seenVargArgs.enter(node);
162+
seenVarArgs.enter(node);
163163
Class<?> varArgParamType = cachedMethod.getParameterTypes()[parameterCount - 1];
164164
asVarArgs = !HostToTypeNode.canConvert(node, args[parameterCount - 1], varArgParamType,
165165
cachedMethod.getGenericParameterTypes()[parameterCount - 1],
@@ -256,33 +256,37 @@ static Object doSingleUncached(Node node, SingleMethod method, Object obj, Objec
256256
}
257257

258258
// Note: checkArgTypes must be evaluated after selectOverload.
259-
@SuppressWarnings({"unused", "static-method", "truffle-static-method"})
260259
@ExplodeLoop
261-
@Specialization(guards = {"method == cachedMethod", "checkArgTypes(args, cachedArgTypes, interop, hostContext)"}, limit = "LIMIT")
260+
@Specialization(guards = {"method == cachedMethod", "checkArgTypes(args, cachedArgTypes, interop, hostContext, overload)"}, limit = "LIMIT")
262261
static final Object doOverloadedCached(Node node, OverloadedMethod method, Object obj, Object[] args, HostContext hostContext,
263-
@Cached("method") OverloadedMethod cachedMethod,
262+
@Cached("method") @SuppressWarnings("unused") OverloadedMethod cachedMethod,
264263
@Exclusive @Cached HostToTypeNode toJavaNode,
265264
@Exclusive @Cached ToGuestValueNode toGuest,
266-
@CachedLibrary(limit = "LIMIT") InteropLibrary interop,
265+
@CachedLibrary(limit = "LIMIT") @SuppressWarnings("unused") InteropLibrary interop,
267266
@Cached("createArgTypesArray(args)") TypeCheckNode[] cachedArgTypes,
268267
@Cached("selectOverload(node, method, args, hostContext, cachedArgTypes)") SingleMethod overload,
269268
@Exclusive @Cached InlinedExactClassProfile receiverProfile,
270269
@Shared("errorBranch") @Cached InlinedBranchProfile errorBranch,
271270
@Shared("seenScope") @Cached InlinedBranchProfile seenVariableScope,
272-
@Shared @Cached InlinedBranchProfile seenVargArgs,
271+
@Shared @Cached InlinedBranchProfile seenVarArgs,
273272
@Exclusive @Cached HostTargetMappingNode varArgsMappingNode,
274273
@Exclusive @CachedLibrary(limit = "3") InteropLibrary varArgsMappingInterop,
275274
@Cached(value = "hostContext.getGuestToHostCache()", allowUncached = true) GuestToHostCodeCache cache) throws ArityException, UnsupportedTypeException {
275+
assert args.length == cachedArgTypes.length;
276276

277277
boolean asVarArgs;
278-
int parameterCount = cachedArgTypes.length;
278+
int parameterCount = overload.getParameterCount();
279279
if (overload.isVarArgs()) {
280-
seenVargArgs.enter(node);
281-
Class<?> varArgParamType = overload.getParameterTypes()[parameterCount - 1];
282-
asVarArgs = !HostToTypeNode.canConvert(node, args[parameterCount - 1], varArgParamType,
283-
overload.getGenericParameterTypes()[parameterCount - 1],
284-
null, hostContext, HostToTypeNode.COERCE,
285-
varArgsMappingInterop, varArgsMappingNode);
280+
seenVarArgs.enter(node);
281+
if (cachedArgTypes.length == parameterCount) {
282+
Class<?> varArgParamType = overload.getParameterTypes()[parameterCount - 1];
283+
asVarArgs = !HostToTypeNode.canConvert(node, args[parameterCount - 1], varArgParamType,
284+
overload.getGenericParameterTypes()[parameterCount - 1],
285+
null, hostContext, HostToTypeNode.COERCE,
286+
varArgsMappingInterop, varArgsMappingNode);
287+
} else {
288+
asVarArgs = true;
289+
}
286290
} else {
287291
asVarArgs = false;
288292
}
@@ -323,7 +327,6 @@ static final Object doOverloadedCached(Node node, OverloadedMethod method, Objec
323327
}
324328
}
325329

326-
@SuppressWarnings("static-method")
327330
@Specialization(replaces = "doOverloadedCached")
328331
static final Object doOverloadedUncached(Node node, OverloadedMethod method, Object obj, Object[] args, HostContext hostContext,
329332
@Shared("toHost") @Cached HostToTypeNode toJavaNode,
@@ -446,7 +449,7 @@ private static void fillArgTypesArray(Node node, Object[] args, TypeCheckNode[]
446449
cachedArgTypes[i] = node.insert(argType);
447450
}
448451

449-
assert checkArgTypes(args, cachedArgTypes, InteropLibrary.getFactory().getUncached(), context) : Arrays.toString(cachedArgTypes);
452+
assert checkArgTypes(args, cachedArgTypes, InteropLibrary.getFactory().getUncached(), context, selected) : Arrays.toString(cachedArgTypes);
450453
}
451454

452455
private static TypeCheckNode createPrimitiveTargetCheck(List<SingleMethod> applicable, SingleMethod selected, Object arg, Class<?> targetType, int parameterIndex, int priority, boolean varArgs,
@@ -482,8 +485,13 @@ private static TypeCheckNode createPrimitiveTargetCheck(List<SingleMethod> appli
482485
return new PrimitiveType(currentTargetType, otherPossibleTypes.toArray(EMPTY_CLASS_ARRAY), priority);
483486
}
484487

488+
/**
489+
* @param overload dummy argument used to ensure this guard is not evaluated until
490+
* {@link #fillArgTypesArray} has been called by {@link #selectOverload}.
491+
* @see #doOverloadedCached
492+
*/
485493
@ExplodeLoop
486-
static boolean checkArgTypes(Object[] args, TypeCheckNode[] argTypes, InteropLibrary interop, HostContext context) {
494+
static boolean checkArgTypes(Object[] args, TypeCheckNode[] argTypes, InteropLibrary interop, HostContext context, SingleMethod overload) {
487495
if (args.length != argTypes.length) {
488496
return false;
489497
}

0 commit comments

Comments
 (0)