Skip to content

Commit bbe9880

Browse files
committed
move use of GlobalData to implement GlobalAtomicLong directly into the latter instead of using a FieldValueTransformer
1 parent ab482b5 commit bbe9880

File tree

6 files changed

+43
-53
lines changed

6 files changed

+43
-53
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/core/common/Fields.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
import java.util.Map;
3333

3434
import jdk.graal.compiler.debug.GraalError;
35+
import jdk.graal.compiler.graph.Edges;
3536
import jdk.graal.compiler.libgraal.LibGraalFeature;
3637
import jdk.internal.misc.Unsafe;
3738
import org.graalvm.nativeimage.ImageInfo;
3839
import org.graalvm.nativeimage.hosted.Feature;
40+
import org.graalvm.nativeimage.hosted.Feature.BeforeCompilationAccess;
3941

4042
/**
4143
* Describes fields in a class, primarily for access via {@link Unsafe}.
@@ -91,6 +93,15 @@ private Field getField(int i) {
9193
}
9294
}
9395

96+
/**
97+
* Recomputes the {@link Unsafe} based field offsets and the {@link Edges#getIterationMask()}
98+
* derived from them.
99+
*
100+
* @param access provides the new offsets via {@link BeforeCompilationAccess#objectFieldOffset}
101+
* @return a pair (represented as a map entry) where the key is the new offsets and the value is
102+
* the iteration mask
103+
*
104+
*/
94105
public Map.Entry<long[], Long> recomputeOffsetsAndIterationMask(Feature.BeforeCompilationAccess access) {
95106
long[] newOffsets = new long[offsets.length];
96107
for (int i = 0; i < offsets.length; i++) {
@@ -293,7 +304,7 @@ public String getName(int index) {
293304
/**
294305
* Gets the type of a field.
295306
*
296-
* @param index index of a field
307+
* @param index index of the field
297308
*/
298309
public Class<?> getType(int index) {
299310
return types[index];

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/graph/NodeClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public void duringAnalysis(Feature feature, Feature.DuringAnalysisAccess access)
418418
/**
419419
* Describes a field representing an input or successor edge in a node.
420420
*/
421-
protected static class EdgeInfo extends FieldsScanner.FieldInfo {
421+
public static class EdgeInfo extends FieldsScanner.FieldInfo {
422422

423423
public EdgeInfo(long offset, String name, Class<?> type, Class<?> declaringClass) {
424424
super(offset, name, type, declaringClass);

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/libgraal/LibGraalFeature.java

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@
6767
import jdk.graal.compiler.graph.Edges;
6868
import jdk.graal.compiler.options.OptionDescriptor;
6969
import jdk.graal.compiler.options.OptionKey;
70-
import jdk.graal.compiler.serviceprovider.GlobalAtomicLong;
7170
import jdk.graal.compiler.core.common.FeatureComponent;
7271
import jdk.graal.nativeimage.LibGraalLoader;
73-
import jdk.graal.nativeimage.hosted.GlobalData;
7472
import jdk.vm.ci.hotspot.HotSpotModifiers;
7573
import org.graalvm.nativeimage.hosted.RuntimeSystemProperties;
7674

@@ -187,11 +185,8 @@ public void duringSetup(DuringSetupAccess access) {
187185
private OptionCollector optionCollector;
188186

189187
/**
190-
* Collects all options that are reachable at run time. Reachable options are the
191-
* {@link OptionKey} instances reached by the static analysis. The VM options are instances of
192-
* {@link OptionKey} loaded by the {@code com.oracle.svm.hosted.NativeImageClassLoader} and
193-
* compiler options are instances of {@link OptionKey} loaded by the
194-
* {@code HostedLibGraalClassLoader}.
188+
* Collects all instances of the LibGraalLoader loaded {@link OptionKey} class reached by the
189+
* static analysis.
195190
*/
196191
private class OptionCollector implements Consumer<OptionKey<?>> {
197192
private final Set<OptionKey<?>> options = Collections.newSetFromMap(new ConcurrentHashMap<>());
@@ -289,35 +284,11 @@ private Map.Entry<long[], Long> computeReplacement(Object receiver) {
289284
}
290285
}
291286

292-
/**
293-
* Transforms {@code GlobalAtomicLong.addressSupplier} by replacing it with a {@link GlobalData}
294-
* backed address supplier.
295-
*/
296-
static class GlobalAtomicLongTransformer implements FieldValueTransformer {
297-
298-
void register(BeforeAnalysisAccess access) {
299-
Field addressSupplierField = lookupField(GlobalAtomicLong.class, "addressSupplier");
300-
access.registerFieldValueTransformer(addressSupplierField, this);
301-
}
302-
303-
@Override
304-
public Object transform(Object receiver, Object originalValue) {
305-
long initialValue;
306-
try {
307-
initialValue = ((GlobalAtomicLong) receiver).getInitialValue();
308-
} catch (Throwable e) {
309-
throw GraalError.shouldNotReachHere(e);
310-
}
311-
return GlobalData.createGlobal(initialValue);
312-
}
313-
}
314-
315287
@SuppressWarnings("unchecked")
316288
@Override
317289
public void beforeAnalysis(BeforeAnalysisAccess access) {
318290

319291
new FieldOffsetsTransformer().register(access);
320-
new GlobalAtomicLongTransformer().register(access);
321292

322293
/* Contains static fields that depend on HotSpotJVMCIRuntime */
323294
RuntimeClassInitialization.initializeAtRunTime(HotSpotModifiers.class);

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/options/OptionsParser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public class OptionsParser {
5454
*
5555
* @param descriptors set of compiler options available in libgraal. These correspond to the
5656
* reachable {@link OptionKey}s discovered during Native Image static analysis. This
57-
* field is only non-null when {@link OptionsParser} is loaded by the
58-
* LibGraalClassLoader.
57+
* field is only non-null when {@link OptionsParser} is loaded by a
58+
* {@link jdk.graal.nativeimage.LibGraalLoader}.
5959
* @param enterpriseOptions {@linkplain OptionKey#getName() names} of enterprise options
6060
*/
6161
public record LibGraalOptionsInfo(EconomicMap<String, OptionDescriptor> descriptors, Set<String> enterpriseOptions) {
@@ -66,7 +66,7 @@ public static LibGraalOptionsInfo create() {
6666

6767
/**
6868
* Compiler options info available in libgraal. This field is only non-null when
69-
* {@link OptionsParser} is loaded by the LibGraalClassLoader.
69+
* {@link OptionsParser} is loaded by a {@link jdk.graal.nativeimage.LibGraalLoader}.
7070
*/
7171
private static LibGraalOptionsInfo libgraalOptions;
7272

@@ -81,8 +81,8 @@ public static Iterable<OptionDescriptors> getOptionsLoader() {
8181
boolean inLibGraal = libgraalOptions != null;
8282
if (inLibGraal && inImageBuildtimeCode()) {
8383
/*
84-
* Graal code is being run in the context of the LibGraalClassLoader while building
85-
* libgraal so use the LibGraalClassLoader to load the OptionDescriptors.
84+
* Graal code is being run is loaded by a LibGraalLoader while building libgraal so use
85+
* the loader to load the OptionDescriptors.
8686
*/
8787
ClassLoader myCL = OptionsParser.class.getClassLoader();
8888
return ServiceLoader.load(OptionDescriptors.class, myCL);

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
import java.util.function.Supplier;
3030

3131
import jdk.graal.compiler.debug.GraalError;
32+
import jdk.graal.nativeimage.LibGraalLoader;
3233
import jdk.graal.nativeimage.hosted.GlobalData;
3334
import jdk.internal.misc.Unsafe;
3435
import org.graalvm.nativeimage.ImageInfo;
36+
import org.graalvm.nativeimage.Platform;
37+
import org.graalvm.nativeimage.Platforms;
3538

3639
/**
3740
* A shareable long value in the JVM process that is updated atomically. The long value is stored in
@@ -48,6 +51,7 @@ public class GlobalAtomicLong {
4851
/**
4952
* Cleaner for freeing {@link #address}.
5053
*/
54+
@Platforms(Platform.HOSTED_ONLY.class) //
5155
private static Cleaner cleaner;
5256

5357
/**
@@ -83,20 +87,24 @@ public GlobalAtomicLong(String name, long initialValue) {
8387
if (ImageInfo.inImageRuntimeCode()) {
8488
throw GraalError.shouldNotReachHere("Cannot create " + getClass().getName() + " objects in native image runtime");
8589
} else {
86-
addressSupplier = () -> {
87-
if (ImageInfo.inImageRuntimeCode()) {
88-
throw GraalError.shouldNotReachHere("The addressSupplier field value should have been replaced at image build time");
89-
}
90-
long addr = UNSAFE.allocateMemory(Long.BYTES);
91-
synchronized (GlobalAtomicLong.class) {
92-
if (cleaner == null) {
93-
cleaner = Cleaner.create();
90+
if (ImageInfo.inImageBuildtimeCode() && GlobalAtomicLong.class.getClassLoader() instanceof LibGraalLoader) {
91+
addressSupplier = GlobalData.createGlobal(initialValue);
92+
} else {
93+
addressSupplier = () -> {
94+
if (ImageInfo.inImageRuntimeCode()) {
95+
throw GraalError.shouldNotReachHere("The addressSupplier field value should have been replaced at image build time");
9496
}
95-
cleaner.register(GlobalAtomicLong.this, () -> UNSAFE.freeMemory(addr));
96-
}
97-
UNSAFE.putLongVolatile(null, addr, initialValue);
98-
return addr;
99-
};
97+
long addr = UNSAFE.allocateMemory(Long.BYTES);
98+
synchronized (GlobalAtomicLong.class) {
99+
if (cleaner == null) {
100+
cleaner = Cleaner.create();
101+
}
102+
cleaner.register(GlobalAtomicLong.this, () -> UNSAFE.freeMemory(addr));
103+
}
104+
UNSAFE.putLongVolatile(null, addr, initialValue);
105+
return addr;
106+
};
107+
}
100108
}
101109
}
102110

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static boolean isInLibgraal() {
7070

7171
/**
7272
* The set of services available in libgraal. This field is only non-null when
73-
* {@link GraalServices} is loaded by the LibGraalClassLoader.
73+
* {@link GraalServices} is loaded by a {@link LibGraalLoader}.
7474
*/
7575
private static final Map<Class<?>, List<?>> libgraalServices;
7676

@@ -173,7 +173,7 @@ public static Properties getSystemProperties(String justification) {
173173
public static Map<String, String> getSavedProperties() {
174174
if (inImageBuildtimeCode()) {
175175
// Avoid calling down to JVMCI native methods as they will fail to
176-
// link in a copy of JVMCI loaded by the LibGraalClassLoader.
176+
// link in a copy of JVMCI loaded by a LibGraalLoader.
177177
return jdk.internal.misc.VM.getSavedProperties();
178178
}
179179
return Services.getSavedProperties();

0 commit comments

Comments
 (0)