RFR: 8315771: [JVMCI] Resolution of bootstrap methods with int[] static arguments [v11]
Doug Simon
dnsimon at openjdk.org
Wed Sep 20 09:51:44 UTC 2023
On Tue, 19 Sep 2023 11:37:09 GMT, Sacha Coppey <duke at openjdk.org> wrote:
>> Currently, `jdk.vm.ci.meta.ConstantPool.lookupBootstrapMethodInvocation` does not support static arguments of type `int[]`.
>>
>> Supporting those static arguments allows to correctly lookup the `BootstrapMethodInvocation` of some `InvokeDynamic` and `DynamicConstant`.
>>
>> To lookup the constant at the index in the static arguments index list, a new class is introduced, allowing to lazily resolve the constant or obtain the constant pool index of the arguments:
>>
>>
>> static class CachedBSMArgs extends AbstractList<JavaConstant> {
>> private final JavaConstant[] cache;
>> private final HotSpotConstantPool cp;
>> private final int bssIndex;
>>
>> CachedBSMArgs(HotSpotConstantPool cp, int bssIndex, int size) {
>> this.cp = cp;
>> this.bssIndex = bssIndex;
>> this.cache = new JavaConstant[size];
>> }
>>
>> @Override
>> public JavaConstant get(int index) {
>> JavaConstant res = cache[index];
>> if (res == null) {
>> int argCpi = compilerToVM().bootstrapArgumentIndexAt(cp, bssIndex, index);
>> res = compilerToVM().lookupConstantInPool(cp, argCpi, false);
>> if (res == null) {
>> res = JavaConstant.forInt(argCpi);
>> }
>> cache[index] = res;
>> }
>> return res;
>> }
>>
>> @Override
>> public int size() {
>> return cache.length;
>> }
>> }
>
> Sacha Coppey has updated the pull request incrementally with one additional commit since the last revision:
>
> Add an extra run configuration for TestDynamicConstant instead of replacing it
While it's tempting to introduce a new type to distinguish between a primitive constant and an index in the constant pool, it would involve changing API that is already used in Graal.
What's more, we're (ab)using the fact that `java.lang.invoke.ConstantGroup.get(int index)` always returns a boxed value. Boxed Java values in JVMCI are always represented by a `JavaConstant` for which `c.getJavaKind() == JavaKind.Object`. So we can use `PrimitiveConstant` here as a side channel to represent a constant pool index.
@Zeavee can you please update the javadoc of `BootstrapMethodInvocation.getStaticArguments()` one more time to make this crystal clear:
/**
* Gets the static arguments with which the bootstrap method will be invoked.
*
* The {@linkplain JavaConstant#getJavaKind kind} of each argument will be
* {@link JavaKind#Object} or {@link JavaKind#Int}. The latter represents an
* unresolved {@code CONSTANT_Dynamic_info} entry. To resolve this entry, the
* corresponding bootstrap method has to be called first:
*
* <pre>
* List<JavaConstant> args = bmi.getStaticArguments();
* List<JavaConstant> resolvedArgs = new ArrayList<>(args.size());
* for (JavaConstant c : args) {
* JavaConstant r = c;
* if (c.getJavaKind() == JavaKind.Int) {
* // If needed, access corresponding BootstrapMethodInvocation using
* // cp.lookupBootstrapMethodInvocation(pc.asInt(), -1)
* r = cp.lookupConstant(c.asInt(), true);
* } else {
* assert c.getJavaKind() == JavaKind.Object;
* }
* resolvedArgs.append(r);
* }
* </pre>
No need to re-run testing after this change as it's only a doc change.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/15588#issuecomment-1727370299
More information about the hotspot-compiler-dev
mailing list