Skip to content

Commit 293c339

Browse files
committed
[GR-70556] Removed usages of AnnotatedElement in Truffler compiler and runtime.
PullRequest: graal/22232
2 parents a9a4116 + cfd8bf1 commit 293c339

File tree

24 files changed

+771
-135
lines changed

24 files changed

+771
-135
lines changed

compiler/src/jdk.graal.compiler.libgraal/src/jdk/graal/compiler/libgraal/truffle/HSTruffleCompilerRuntime.java

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,36 @@
2424
*/
2525
package jdk.graal.compiler.libgraal.truffle;
2626

27+
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.CreateStringSupplier;
28+
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.GetPartialEvaluationMethodInfo;
29+
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.IsSuppressedFailure;
30+
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.IsValueType;
31+
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.Log;
32+
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.OnCodeInstallation;
33+
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.OnIsolateShutdown;
34+
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.RegisterOptimizedAssumptionDependency;
35+
import static org.graalvm.jniutils.JNIMethodScope.env;
36+
import static org.graalvm.jniutils.JNIMethodScope.scope;
37+
38+
import java.util.function.Consumer;
39+
import java.util.function.Supplier;
40+
41+
import org.graalvm.jniutils.HSObject;
42+
import org.graalvm.jniutils.JNI.JClass;
43+
import org.graalvm.jniutils.JNI.JNIEnv;
44+
import org.graalvm.jniutils.JNI.JObject;
45+
import org.graalvm.jniutils.JNI.JString;
46+
import org.graalvm.jniutils.JNIMethodScope;
47+
import org.graalvm.jniutils.JNIUtil;
48+
2749
import com.oracle.truffle.compiler.ConstantFieldInfo;
2850
import com.oracle.truffle.compiler.HostMethodInfo;
2951
import com.oracle.truffle.compiler.OptimizedAssumptionDependency;
3052
import com.oracle.truffle.compiler.PartialEvaluationMethodInfo;
3153
import com.oracle.truffle.compiler.TruffleCompilable;
3254
import com.oracle.truffle.compiler.TruffleCompilerRuntime;
33-
3455
import com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal;
56+
3557
import jdk.graal.compiler.libgraal.LibGraalFeature;
3658
import jdk.graal.compiler.serviceprovider.IsolateUtil;
3759
import jdk.graal.compiler.truffle.hotspot.HotSpotTruffleCompilationSupport;
@@ -47,33 +69,8 @@
4769
import jdk.vm.ci.meta.ResolvedJavaMethod;
4870
import jdk.vm.ci.meta.ResolvedJavaType;
4971
import jdk.vm.ci.meta.UnresolvedJavaType;
50-
import org.graalvm.jniutils.HSObject;
51-
import org.graalvm.jniutils.JNI.JByteArray;
52-
import org.graalvm.jniutils.JNI.JClass;
53-
import org.graalvm.jniutils.JNI.JNIEnv;
54-
import org.graalvm.jniutils.JNI.JObject;
55-
import org.graalvm.jniutils.JNI.JString;
56-
import org.graalvm.jniutils.JNIMethodScope;
57-
import org.graalvm.jniutils.JNIUtil;
58-
import org.graalvm.nativeimage.StackValue;
5972
import org.graalvm.nativeimage.c.type.CCharPointer;
6073

61-
import java.util.Arrays;
62-
import java.util.function.Consumer;
63-
import java.util.function.Supplier;
64-
65-
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.CreateStringSupplier;
66-
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.GetConstantFieldInfo;
67-
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.GetPartialEvaluationMethodInfo;
68-
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.IsSuppressedFailure;
69-
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.IsValueType;
70-
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.Log;
71-
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.OnCodeInstallation;
72-
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.OnIsolateShutdown;
73-
import static com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id.RegisterOptimizedAssumptionDependency;
74-
import static org.graalvm.jniutils.JNIMethodScope.env;
75-
import static org.graalvm.jniutils.JNIMethodScope.scope;
76-
7774
public final class HSTruffleCompilerRuntime extends HSObject implements TruffleCompilerRuntime {
7875

7976
static final String COMPILER_VERSION = HotSpotTruffleCompilationSupport.readCompilerVersion();
@@ -82,31 +79,23 @@ public final class HSTruffleCompilerRuntime extends HSObject implements TruffleC
8279
private static final Class<?> TRANSLATED_EXCEPTION = LibGraalFeature.lookupClass("jdk.internal.vm.TranslatedException");
8380

8481
private final ResolvedJavaType classLoaderDelegate;
82+
private final CCharPointer javaInstrumentationActive;
8583
final TruffleFromLibGraalCalls calls;
8684

87-
HSTruffleCompilerRuntime(JNIEnv env, JObject handle, ResolvedJavaType classLoaderDelegate, JClass peer) {
85+
HSTruffleCompilerRuntime(JNIEnv env, JObject handle, ResolvedJavaType classLoaderDelegate, JClass peer, CCharPointer javaInstrumentationActive) {
8886
/*
8987
* Note global duplicates may happen if the compiler is initialized by a host compilation.
9088
*/
9189
super(env, handle, true, false);
9290
this.classLoaderDelegate = classLoaderDelegate;
91+
this.javaInstrumentationActive = javaInstrumentationActive;
9392
this.calls = new TruffleFromLibGraalCalls(env, peer);
9493
}
9594

9695
@TruffleFromLibGraal(GetPartialEvaluationMethodInfo)
9796
@Override
9897
public PartialEvaluationMethodInfo getPartialEvaluationMethodInfo(ResolvedJavaMethod method) {
99-
long methodHandle = HotSpotJVMCIRuntime.runtime().translate(method);
100-
JByteArray hsByteArray = HSTruffleCompilerRuntimeGen.callGetPartialEvaluationMethodInfo(calls, env(), getHandle(), methodHandle);
101-
CCharPointer buffer = StackValue.get(5);
102-
JNIUtil.GetByteArrayRegion(env(), hsByteArray, 0, 5, buffer);
103-
BinaryInput in = BinaryInput.create(buffer, 5);
104-
LoopExplosionKind loopExplosionKind = LoopExplosionKind.values()[in.readByte()];
105-
InlineKind peInlineKind = InlineKind.values()[in.readByte()];
106-
InlineKind inlineKind = InlineKind.values()[in.readByte()];
107-
boolean inlineable = in.readBoolean();
108-
boolean isSpecializationMethod = in.readBoolean();
109-
return new PartialEvaluationMethodInfo(loopExplosionKind, peInlineKind, inlineKind, inlineable, isSpecializationMethod);
98+
throw new UnsupportedOperationException("Use HotSpotPartialEvaluator#getMethodInfo()");
11099
}
111100

112101
@Override
@@ -151,35 +140,9 @@ public boolean isValueType(ResolvedJavaType type) {
151140
return HSTruffleCompilerRuntimeGen.callIsValueType(calls, env(), getHandle(), typeHandle);
152141
}
153142

154-
@TruffleFromLibGraal(GetConstantFieldInfo)
155143
@Override
156144
public ConstantFieldInfo getConstantFieldInfo(ResolvedJavaField field) {
157-
ResolvedJavaType enclosingType = field.getDeclaringClass();
158-
boolean isStatic = field.isStatic();
159-
ResolvedJavaField[] declaredFields = isStatic ? enclosingType.getStaticFields() : enclosingType.getInstanceFields(false);
160-
int fieldIndex = -1;
161-
for (int i = 0; i < declaredFields.length; i++) {
162-
if (field.equals(declaredFields[i])) {
163-
fieldIndex = i;
164-
break;
165-
}
166-
}
167-
if (fieldIndex == -1) {
168-
throw new IllegalStateException(String.format(
169-
"%s field: %s declared in: %s is not in declared fields: %s",
170-
isStatic ? "Static" : "Instance",
171-
field,
172-
enclosingType,
173-
Arrays.toString(declaredFields)));
174-
}
175-
long typeHandle = HotSpotJVMCIRuntime.runtime().translate(enclosingType);
176-
int rawValue = HSTruffleCompilerRuntimeGen.callGetConstantFieldInfo(calls, env(), getHandle(), typeHandle, isStatic, fieldIndex);
177-
return switch (rawValue) {
178-
case Integer.MIN_VALUE -> null;
179-
case -1 -> ConstantFieldInfo.CHILD;
180-
case -2 -> ConstantFieldInfo.CHILDREN;
181-
default -> ConstantFieldInfo.forDimensions(rawValue);
182-
};
145+
throw new UnsupportedOperationException("Use HotSpotPartialEvaluator#getConstantFieldInfo()");
183146
}
184147

185148
@Override
@@ -252,4 +215,9 @@ public boolean isSuppressedFailure(TruffleCompilable compilable, Supplier<String
252215
public void notifyShutdown(JNIEnv env) {
253216
HSTruffleCompilerRuntimeGen.callOnIsolateShutdown(calls, env, IsolateUtil.getIsolateID());
254217
}
218+
219+
@Override
220+
public boolean isJavaInstrumentationActive() {
221+
return javaInstrumentationActive.isNonNull() && javaInstrumentationActive.read() != 0;
222+
}
255223
}

compiler/src/jdk.graal.compiler.libgraal/src/jdk/graal/compiler/libgraal/truffle/LibGraalTruffleEntryPoints.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.graalvm.nativeimage.ObjectHandles;
4444
import org.graalvm.nativeimage.c.function.CEntryPoint;
4545
import org.graalvm.nativeimage.c.function.CEntryPoint.IsolateThreadContext;
46+
import org.graalvm.nativeimage.c.type.CCharPointer;
4647
import org.graalvm.nativeimage.c.type.CLongPointer;
4748
import org.graalvm.word.PointerBase;
4849

@@ -105,9 +106,23 @@ public static void initializeIsolate(JNIEnv env, JClass hsClazz, @IsolateThreadC
105106
@CEntryPoint(name = "Java_com_oracle_truffle_runtime_hotspot_libgraal_TruffleToLibGraalCalls_registerRuntime", include = LibGraalFeature.IsEnabled.class)
106107
@TruffleToLibGraal(Id.RegisterRuntime)
107108
public static boolean registerRuntime(JNIEnv env, JClass hsClazz, @IsolateThreadContext long isolateThreadAddress, JObject truffleRuntime) {
109+
try (JNIMethodScope s = openScope(Id.RegisterRuntime, env)) {
110+
throw new UnsupportedOperationException("Operation not supported on Graal compiler 25.1 and later");
111+
} catch (Throwable t) {
112+
JNIExceptionWrapper.throwInHotSpot(env, t);
113+
return false;
114+
}
115+
}
116+
117+
@SuppressWarnings({"unused", "try"})
118+
@CEntryPoint(name = "Java_com_oracle_truffle_runtime_hotspot_libgraal_TruffleToLibGraalCalls3_registerRuntime", include = LibGraalFeature.IsEnabled.class)
119+
@TruffleToLibGraal(Id.RegisterRuntime)
120+
public static boolean registerRuntime(JNIEnv env, JClass hsClazz, @IsolateThreadContext long isolateThreadAddress, JObject truffleRuntime,
121+
JObject javaInstrumentationActive) {
108122
try (JNIMethodScope s = openScope(Id.RegisterRuntime, env)) {
109123
long truffleRuntimeWeakRef = JNIUtil.NewWeakGlobalRef(env, truffleRuntime, "TruffleCompilerRuntime").rawValue();
110-
return LibGraalTruffleHostEnvironmentLookup.registerRuntime(truffleRuntimeWeakRef);
124+
long activeAddress = JNIUtil.GetDirectBufferAddress(env, javaInstrumentationActive).rawValue();
125+
return LibGraalTruffleHostEnvironmentLookup.registerRuntime(truffleRuntimeWeakRef, activeAddress);
111126
} catch (Throwable t) {
112127
JNIExceptionWrapper.throwInHotSpot(env, t);
113128
return false;
@@ -119,9 +134,23 @@ public static boolean registerRuntime(JNIEnv env, JClass hsClazz, @IsolateThread
119134
@TruffleToLibGraal(Id.InitializeRuntime)
120135
public static long initializeRuntime(JNIEnv env, JClass hsClazz, @IsolateThreadContext long isolateThreadAddress,
121136
JObject truffleRuntime, JClass hsClassLoaderDelegate) {
137+
try (JNIMethodScope s = openScope(Id.InitializeRuntime, env)) {
138+
throw new UnsupportedOperationException("Operation not supported on Graal compiler 25.1 and later");
139+
} catch (Throwable t) {
140+
JNIExceptionWrapper.throwInHotSpot(env, t);
141+
return 0L;
142+
}
143+
}
144+
145+
@SuppressWarnings({"unused", "try"})
146+
@CEntryPoint(name = "Java_com_oracle_truffle_runtime_hotspot_libgraal_TruffleToLibGraalCalls3_initializeRuntime", include = LibGraalFeature.IsEnabled.class)
147+
@TruffleToLibGraal(Id.InitializeRuntime)
148+
public static long initializeRuntime(JNIEnv env, JClass hsClazz, @IsolateThreadContext long isolateThreadAddress,
149+
JObject truffleRuntime, JClass hsClassLoaderDelegate, JObject javaInstrumentationActive) {
122150
try (JNIMethodScope s = openScope(Id.InitializeRuntime, env)) {
123151
ResolvedJavaType classLoaderDelegate = HotSpotJVMCIRuntime.runtime().asResolvedJavaType(hsClassLoaderDelegate.rawValue());
124-
HSTruffleCompilerRuntime hsTruffleRuntime = new HSTruffleCompilerRuntime(env, truffleRuntime, classLoaderDelegate, hsClassLoaderDelegate);
152+
CCharPointer activeAddress = (CCharPointer) JNIUtil.GetDirectBufferAddress(env, javaInstrumentationActive);
153+
HSTruffleCompilerRuntime hsTruffleRuntime = new HSTruffleCompilerRuntime(env, truffleRuntime, classLoaderDelegate, hsClassLoaderDelegate, activeAddress);
125154
LibGraalSupportImpl.registerTruffleCompilerRuntime(hsTruffleRuntime);
126155
return LibGraalObjectHandles.create(hsTruffleRuntime);
127156
} catch (Throwable t) {

compiler/src/jdk.graal.compiler.libgraal/src/jdk/graal/compiler/libgraal/truffle/LibGraalTruffleHostEnvironmentLookup.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public final class LibGraalTruffleHostEnvironmentLookup implements TruffleHostEn
5454

5555
private static final int NO_TRUFFLE_REGISTERED = 0;
5656
private static final GlobalAtomicLong WEAK_TRUFFLE_RUNTIME_INSTANCE = new GlobalAtomicLong("WEAK_TRUFFLE_RUNTIME_INSTANCE", NO_TRUFFLE_REGISTERED);
57+
private static final GlobalAtomicLong JAVA_INSTRUMENTATION_ACTIVE_ADDRESS = new GlobalAtomicLong("JAVA_INSTRUMENTATION_ACTIVE_ADDRESS", NO_TRUFFLE_REGISTERED);
5758

5859
private TruffleHostEnvironment previousRuntime;
5960

@@ -89,7 +90,7 @@ public TruffleHostEnvironment lookup(ResolvedJavaType forType) {
8990
* per type. So in theory multiple truffle runtimes can be loaded.
9091
*/
9192
try (TruffleRuntimeScope truffleRuntimeScope = LibGraalTruffleHostEnvironment.openTruffleRuntimeScopeImpl()) {
92-
HSTruffleCompilerRuntime runtime = new HSTruffleCompilerRuntime(env, runtimeLocalRef, runtimeType, runtimeClass);
93+
HSTruffleCompilerRuntime runtime = new HSTruffleCompilerRuntime(env, runtimeLocalRef, runtimeType, runtimeClass, Word.pointer(JAVA_INSTRUMENTATION_ACTIVE_ADDRESS.get()));
9394
this.previousRuntime = environment = new LibGraalTruffleHostEnvironment(runtime, HotSpotJVMCIRuntime.runtime().getHostJVMCIBackend().getMetaAccess());
9495
assert JNIUtil.IsSameObject(env, hsRuntime(environment).getHandle(), runtimeLocalRef);
9596
return environment;
@@ -100,8 +101,12 @@ private static HSTruffleCompilerRuntime hsRuntime(TruffleHostEnvironment environ
100101
return (HSTruffleCompilerRuntime) environment.runtime();
101102
}
102103

103-
public static boolean registerRuntime(long truffleRuntimeWeakRef) {
104+
public static boolean registerRuntime(long truffleRuntimeWeakRef, long javaInstrumentationActiveAddress) {
104105
// TODO GR-44222 support multiple runtimes.
105-
return WEAK_TRUFFLE_RUNTIME_INSTANCE.compareAndSet(0, truffleRuntimeWeakRef);
106+
boolean registered = WEAK_TRUFFLE_RUNTIME_INSTANCE.compareAndSet(0, truffleRuntimeWeakRef);
107+
if (registered) {
108+
JAVA_INSTRUMENTATION_ACTIVE_ADDRESS.set(javaInstrumentationActiveAddress);
109+
}
110+
return registered;
106111
}
107112
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/AbstractKnownTruffleTypes.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ protected AbstractKnownTruffleTypes(TruffleCompilerRuntime runtime, MetaAccessPr
5959
this.metaAccess = metaAccess;
6060
}
6161

62+
/**
63+
* Resolves {@code className} to a type. Use {@link #lookupTypeCached(String)} instead for types
64+
* that will have methods or fields looked up as well.
65+
*/
6266
public final ResolvedJavaType lookupType(String className) {
6367
ResolvedJavaType type = runtime.resolveType(metaAccess, className);
6468
onTypeLookup(type);
@@ -77,6 +81,10 @@ protected final ResolvedJavaType lookupType(Class<?> c) {
7781
return type;
7882
}
7983

84+
/**
85+
* Resolves {@code className} to a type. In contrast to {@link #lookupType(String)}, this
86+
* version creates a {@link TypeCache} for efficient access to fields and methods in the type.
87+
*/
8088
protected final ResolvedJavaType lookupTypeCached(String className) {
8189
ResolvedJavaType type = runtime.resolveType(metaAccess, className);
8290
onTypeLookup(type);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.graal.compiler.truffle;
26+
27+
import com.oracle.truffle.compiler.TruffleCompilerRuntime;
28+
import jdk.vm.ci.meta.JavaMethod;
29+
import jdk.vm.ci.meta.ResolvedJavaMethod;
30+
import jdk.vm.ci.meta.ResolvedJavaType;
31+
32+
import java.util.LinkedHashSet;
33+
import java.util.Set;
34+
35+
final class FlightRecorderInstrumentation {
36+
37+
private static final Set<InstrumentedMethodPattern> instrumentedMethodPatterns = createInstrumentedPatterns();
38+
39+
private static Set<InstrumentedMethodPattern> createInstrumentedPatterns() {
40+
Set<InstrumentedMethodPattern> patterns = new LinkedHashSet<>();
41+
patterns.add(new InstrumentedMethodPattern("begin", "()V"));
42+
patterns.add(new InstrumentedMethodPattern("commit", "()V"));
43+
patterns.add(new InstrumentedMethodPattern("end", "()V"));
44+
patterns.add(new InstrumentedMethodPattern("isEnabled", "()Z"));
45+
patterns.add(new InstrumentedMethodPattern("set", "(ILjava/lang/Object;)V"));
46+
patterns.add(new InstrumentedMethodPattern("shouldCommit", "()Z"));
47+
return patterns;
48+
}
49+
50+
private FlightRecorderInstrumentation() {
51+
}
52+
53+
static boolean isInstrumented(TruffleCompilerRuntime runtime, ResolvedJavaMethod method, KnownTruffleTypes types) {
54+
if (!runtime.isJavaInstrumentationActive()) {
55+
return false;
56+
}
57+
if (("traceThrowable".equals(method.getName()) || "traceError".equals(method.getName())) && "Ljdk/jfr/internal/instrument/ThrowableTracer;".equals(method.getDeclaringClass().getName())) {
58+
return true;
59+
}
60+
61+
// Fast check, the JFR instrumented methods are marked as synthetic.
62+
if (!method.isSynthetic() || method.isBridge() || method.isStatic()) {
63+
return false;
64+
}
65+
66+
if (!instrumentedMethodPatterns.contains(InstrumentedMethodPattern.forJavaMethod(method))) {
67+
return false;
68+
}
69+
70+
ResolvedJavaType methodOwner = method.getDeclaringClass();
71+
return types.Event != null && types.Event.isAssignableFrom(methodOwner);
72+
}
73+
74+
private record InstrumentedMethodPattern(String name, String signature) {
75+
76+
static InstrumentedMethodPattern forJavaMethod(JavaMethod method) {
77+
return new InstrumentedMethodPattern(method.getName(), method.getSignature().toMethodDescriptor());
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)