Skip to content

Commit 8ad4c83

Browse files
committed
Remove reflection for constant pool accesses
1 parent 731c7d7 commit 8ad4c83

File tree

6 files changed

+13
-120
lines changed

6 files changed

+13
-120
lines changed

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/ResolveDynamicConstantTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
*/
2525
package jdk.graal.compiler.core.test;
2626

27-
import jdk.graal.compiler.serviceprovider.GraalServices;
27+
import java.lang.reflect.Method;
28+
import java.time.Clock;
29+
import java.time.Instant;
30+
2831
import org.junit.Assert;
29-
import org.junit.Assume;
3032
import org.junit.Test;
3133
import org.objectweb.asm.ClassWriter;
3234
import org.objectweb.asm.ConstantDynamic;
@@ -37,17 +39,12 @@
3739

3840
import jdk.vm.ci.meta.ResolvedJavaMethod;
3941

40-
import java.lang.reflect.Method;
41-
import java.time.Clock;
42-
import java.time.Instant;
43-
4442
public class ResolveDynamicConstantTest extends CustomizedBytecodePatternTest {
4543

4644
private static final int PUBLIC_STATIC = ACC_PUBLIC | ACC_STATIC;
4745

4846
@Test
4947
public void test00601m001() throws Throwable {
50-
Assume.assumeTrue(GraalServices.supportsNonresolvingLookupConstant());
5148
runTest("test.resolveDynamicConstant00601m001");
5249
}
5350

@@ -64,7 +61,6 @@ public static void main(String[] args) {
6461

6562
@Test
6663
public void test00602m008() throws Throwable {
67-
Assume.assumeTrue(GraalServices.supportsNonresolvingLookupConstant());
6864
runTest("test.resolveDynamicConstant00602m008");
6965
}
7066

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/jfr/TestGetEventWriter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.lang.reflect.InvocationTargetException;
3030

3131
import org.junit.Assert;
32-
import org.junit.Assume;
3332
import org.junit.Test;
3433
import org.objectweb.asm.AnnotationVisitor;
3534
import org.objectweb.asm.ClassWriter;
@@ -38,7 +37,6 @@
3837

3938
import jdk.graal.compiler.core.common.PermanentBailoutException;
4039
import jdk.graal.compiler.core.test.SubprocessTest;
41-
import jdk.graal.compiler.serviceprovider.GraalServices;
4240
import jdk.graal.compiler.test.AddExports;
4341
import jdk.graal.compiler.test.SubprocessUtil;
4442
import jdk.jfr.Event;
@@ -60,7 +58,6 @@
6058
public class TestGetEventWriter extends SubprocessTest {
6159

6260
private static void initializeJFR() {
63-
Assume.assumeTrue("Requires JDK-8290075", GraalServices.hasLookupMethodWithCaller());
6461
try (Recording r = new Recording()) {
6562
r.start();
6663
// Unlocks access to jdk.jfr.internal.event

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/bytecode/BytecodeDisassembler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
import static jdk.graal.compiler.bytecode.Bytecodes.TABLESWITCH;
8080

8181
import org.graalvm.collections.EconomicMap;
82-
import jdk.graal.compiler.serviceprovider.GraalServices;
8382

8483
import jdk.vm.ci.meta.ConstantPool;
8584
import jdk.vm.ci.meta.JavaConstant;
@@ -354,7 +353,7 @@ private void decodeOperand(StringBuilder buf, BytecodeStream stream, ConstantPoo
354353
case LDC_W :
355354
case LDC2_W : {
356355
int cpi = stream.readCPI();
357-
Object constant = GraalServices.lookupConstant(cp, cpi, false);
356+
Object constant = cp.lookupConstant(cpi, false);
358357
String desc = null;
359358
if (constant == null) {
360359
desc = "<unresolved>";

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/java/BytecodeParser.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@
435435
import jdk.graal.compiler.phases.OptimisticOptimizations;
436436
import jdk.graal.compiler.phases.util.ValueMergeUtil;
437437
import jdk.graal.compiler.replacements.nodes.MacroInvokable;
438-
import jdk.graal.compiler.serviceprovider.GraalServices;
439438
import jdk.graal.compiler.serviceprovider.SpeculationReasonGroup;
440439
import jdk.vm.ci.code.BailoutException;
441440
import jdk.vm.ci.code.BytecodeFrame;
@@ -4393,14 +4392,11 @@ private JavaMethod lookupMethod(int cpi, int opcode) {
43934392
}
43944393

43954394
protected JavaMethod lookupMethodInPool(int cpi, int opcode) {
4396-
if (GraalServices.hasLookupMethodWithCaller()) {
4397-
try {
4398-
return GraalServices.lookupMethodWithCaller(constantPool, cpi, opcode, method);
4399-
} catch (IllegalAccessError e) {
4400-
throw new PermanentBailoutException(e, "cannot link call from %s", method.format("%H.%n(%p)"));
4401-
}
4395+
try {
4396+
return constantPool.lookupMethod(cpi, opcode, method);
4397+
} catch (IllegalAccessError e) {
4398+
throw new PermanentBailoutException(e, "cannot link call from %s", method.format("%H.%n(%p)"));
44024399
}
4403-
return constantPool.lookupMethod(cpi, opcode);
44044400
}
44054401

44064402
protected JavaField lookupField(int cpi, int opcode) {
@@ -4436,7 +4432,7 @@ protected JavaField lookupField(JavaField result) {
44364432
*/
44374433
protected Object lookupConstant(int cpi, int opcode, boolean allowBootstrapMethodInvocation) {
44384434
maybeEagerlyResolve(cpi, opcode);
4439-
Object result = GraalServices.lookupConstant(constantPool, cpi, allowBootstrapMethodInvocation);
4435+
Object result = constantPool.lookupConstant(cpi, allowBootstrapMethodInvocation);
44404436
if (result != null) {
44414437
assert !graphBuilderConfig.unresolvedIsError() || !(result instanceof JavaType) || (result instanceof ResolvedJavaType) : result;
44424438
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/serviceprovider/GraalServices.java

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030

3131
import java.io.IOException;
3232
import java.io.InputStream;
33-
import java.lang.reflect.InvocationTargetException;
34-
import java.lang.reflect.Method;
3533
import java.util.ArrayList;
3634
import java.util.HashMap;
3735
import java.util.Iterator;
@@ -40,11 +38,7 @@
4038
import java.util.ServiceConfigurationError;
4139
import java.util.ServiceLoader;
4240

43-
import jdk.graal.compiler.debug.GraalError;
44-
import jdk.vm.ci.meta.ConstantPool;
4541
import jdk.vm.ci.meta.EncodedSpeculationReason;
46-
import jdk.vm.ci.meta.JavaMethod;
47-
import jdk.vm.ci.meta.ResolvedJavaMethod;
4842
import jdk.vm.ci.meta.SpeculationLog.SpeculationReason;
4943
import jdk.vm.ci.runtime.JVMCI;
5044
import jdk.vm.ci.services.Services;
@@ -54,32 +48,8 @@
5448
*/
5549
public final class GraalServices {
5650

57-
// NOTE: The use of reflection to access JVMCI API is to support
58-
// compiling on JDKs with varying versions of JVMCI.
59-
6051
private static final Map<Class<?>, List<?>> servicesCache = IS_BUILDING_NATIVE_IMAGE ? new HashMap<>() : null;
6152

62-
private static final Method constantPoolLookupMethodWithCaller;
63-
private static final Method constantPoolLookupConstantWithResolve;
64-
65-
static {
66-
Method lookupMethodWithCaller = null;
67-
Method lookupConstantWithResolve = null;
68-
69-
try {
70-
lookupMethodWithCaller = ConstantPool.class.getDeclaredMethod("lookupMethod", Integer.TYPE, Integer.TYPE, ResolvedJavaMethod.class);
71-
} catch (NoSuchMethodException e) {
72-
}
73-
74-
try {
75-
lookupConstantWithResolve = ConstantPool.class.getDeclaredMethod("lookupConstant", Integer.TYPE, Boolean.TYPE);
76-
} catch (NoSuchMethodException e) {
77-
}
78-
79-
constantPoolLookupMethodWithCaller = lookupMethodWithCaller;
80-
constantPoolLookupConstantWithResolve = lookupConstantWithResolve;
81-
}
82-
8353
private GraalServices() {
8454
}
8555

@@ -449,55 +419,6 @@ public static int getJavaUpdateVersion() {
449419
return Runtime.version().update();
450420
}
451421

452-
/**
453-
* Calls {@code ConstantPool#lookupMethod(int, int, ResolvedJavaMethod)}.
454-
*/
455-
public static JavaMethod lookupMethodWithCaller(ConstantPool constantPool, int cpi, int opcode, ResolvedJavaMethod caller) {
456-
if (constantPoolLookupMethodWithCaller != null) {
457-
try {
458-
return (JavaMethod) constantPoolLookupMethodWithCaller.invoke(constantPool, cpi, opcode, caller);
459-
} catch (InvocationTargetException e) {
460-
throw rethrow(e.getCause());
461-
} catch (IllegalAccessException e) {
462-
throw GraalError.shouldNotReachHere(e, "The method lookupMethod should be accessible.");
463-
}
464-
}
465-
throw new InternalError("This JDK doesn't support ConstantPool.lookupMethod(int, int, ResolvedJavaMethod)");
466-
}
467-
468-
public static Object lookupConstant(ConstantPool constantPool, int cpi, boolean resolve) {
469-
if (constantPoolLookupConstantWithResolve != null) {
470-
try {
471-
return constantPoolLookupConstantWithResolve.invoke(constantPool, cpi, resolve);
472-
} catch (InvocationTargetException e) {
473-
throw rethrow(e.getCause());
474-
} catch (IllegalAccessException e) {
475-
throw GraalError.shouldNotReachHere(e, "The method lookupConstant should be accessible.");
476-
}
477-
}
478-
return constantPool.lookupConstant(cpi);
479-
}
480-
481-
@SuppressWarnings("unchecked")
482-
static <E extends Throwable> RuntimeException rethrow(Throwable ex) throws E {
483-
throw (E) ex;
484-
}
485-
486-
/**
487-
* Returns true if the JDK includes {@code ConstantPool.lookupConstant(int, boolean)}.
488-
*/
489-
public static boolean supportsNonresolvingLookupConstant() {
490-
return constantPoolLookupConstantWithResolve != null;
491-
}
492-
493-
/**
494-
* Returns true if the JDK includes
495-
* {@code ConstantPool.lookupMethod(int, int, ResolvedJavaMethod)}.
496-
*/
497-
public static boolean hasLookupMethodWithCaller() {
498-
return constantPoolLookupMethodWithCaller != null;
499-
}
500-
501422
/**
502423
* Notifies that the compiler is at a point where memory usage is expected to be minimal like
503424
* after the completion of compilation.

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/infrastructure/WrappedConstantPool.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,14 @@
2626

2727
import static jdk.vm.ci.common.JVMCIError.unimplemented;
2828

29-
import java.lang.reflect.InvocationTargetException;
30-
import java.lang.reflect.Method;
3129
import java.util.List;
3230
import java.util.stream.Collectors;
3331

3432
import com.oracle.graal.pointsto.constraints.UnresolvedElementException;
3533
import com.oracle.graal.pointsto.util.GraalAccess;
36-
import com.oracle.svm.util.ReflectionUtil;
3734

3835
import jdk.graal.compiler.api.replacements.SnippetReflectionProvider;
3936
import jdk.graal.compiler.debug.GraalError;
40-
import jdk.graal.compiler.serviceprovider.GraalServices;
4137
import jdk.vm.ci.meta.ConstantPool;
4238
import jdk.vm.ci.meta.JavaConstant;
4339
import jdk.vm.ci.meta.JavaField;
@@ -87,12 +83,6 @@ public JavaConstant extractResolvedType(JavaConstant constant) {
8783
return constant;
8884
}
8985

90-
/**
91-
* {@code jdk.vm.ci.meta.ConstantPool.lookupMethod(int cpi, int opcode, ResolvedJavaMethod caller)}
92-
* was introduced in JVMCI 22.3.
93-
*/
94-
private static final Method lookupMethodWithCaller = ReflectionUtil.lookupMethod(true, ConstantPool.class, "lookupMethod", int.class, int.class, ResolvedJavaMethod.class);
95-
9686
@Override
9787
public void loadReferencedType(int cpi, int opcode, boolean initialize) {
9888
GraalError.guarantee(!initialize, "Must not initialize classes");
@@ -127,23 +117,17 @@ public JavaMethod lookupMethod(int cpi, int opcode) {
127117

128118
@Override
129119
public JavaMethod lookupMethod(int cpi, int opcode, ResolvedJavaMethod caller) {
130-
if (lookupMethodWithCaller == null) {
131-
/* Resort to version without caller. */
132-
return lookupMethod(cpi, opcode);
133-
}
134120
try {
135121
/* Unwrap the caller method. */
136122
ResolvedJavaMethod substCaller = universe.resolveSubstitution(((WrappedJavaMethod) caller).getWrapped());
137123
/*
138124
* Delegate to the lookup with caller method of the wrapped constant pool (via
139125
* reflection).
140126
*/
141-
return universe.lookupAllowUnresolved((JavaMethod) lookupMethodWithCaller.invoke(wrapped, cpi, opcode, substCaller));
127+
return universe.lookupAllowUnresolved(wrapped.lookupMethod(cpi, opcode, substCaller));
142128
} catch (Throwable ex) {
143129
Throwable cause = ex;
144-
if (ex instanceof InvocationTargetException && ex.getCause() != null) {
145-
cause = ex.getCause();
146-
} else if (ex instanceof ExceptionInInitializerError && ex.getCause() != null) {
130+
if (ex instanceof ExceptionInInitializerError && ex.getCause() != null) {
147131
cause = ex.getCause();
148132
}
149133
throw new UnresolvedElementException("Error loading a referenced type: " + cause.toString(), cause);
@@ -177,7 +161,7 @@ public Object lookupConstant(int cpi) {
177161

178162
@Override
179163
public Object lookupConstant(int cpi, boolean resolve) {
180-
Object con = GraalServices.lookupConstant(wrapped, cpi, resolve);
164+
Object con = wrapped.lookupConstant(cpi, resolve);
181165
if (con instanceof JavaType) {
182166
if (con instanceof ResolvedJavaType) {
183167
return universe.lookup((ResolvedJavaType) con);

0 commit comments

Comments
 (0)