Skip to content

Commit 4447c7c

Browse files
committed
[GR-66214] Switch Application Layer Only Image Singletons to use new trait.
PullRequest: graal/21898
2 parents dfbe2f1 + a02ac5f commit 4447c7c

19 files changed

+326
-277
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMainWrapper.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.ArrayList;
3333
import java.util.Arrays;
3434
import java.util.Collections;
35-
import java.util.EnumSet;
3635
import java.util.List;
3736
import java.util.function.BooleanSupplier;
3837

@@ -70,15 +69,16 @@
7069
import com.oracle.svm.core.jdk.RuntimeSupport;
7170
import com.oracle.svm.core.jni.JNIJavaVMList;
7271
import com.oracle.svm.core.jni.functions.JNIFunctionTables;
73-
import com.oracle.svm.core.layeredimagesingleton.ApplicationLayerOnlyImageSingleton;
74-
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonBuilderFlags;
75-
import com.oracle.svm.core.layeredimagesingleton.UnsavedSingleton;
7672
import com.oracle.svm.core.log.Log;
7773
import com.oracle.svm.core.thread.JavaThreads;
7874
import com.oracle.svm.core.thread.PlatformThreads;
7975
import com.oracle.svm.core.thread.RecurringCallbackSupport;
8076
import com.oracle.svm.core.thread.VMThreads;
8177
import com.oracle.svm.core.thread.VMThreads.OSThreadHandle;
78+
import com.oracle.svm.core.traits.BuiltinTraits.AllAccess;
79+
import com.oracle.svm.core.traits.BuiltinTraits.NoLayeredCallbacks;
80+
import com.oracle.svm.core.traits.SingletonLayeredInstallationKind.ApplicationLayerOnly;
81+
import com.oracle.svm.core.traits.SingletonTraits;
8282
import com.oracle.svm.core.util.UserError;
8383
import com.oracle.svm.core.util.VMError;
8484
import com.oracle.svm.util.ClassUtil;
@@ -101,7 +101,8 @@ public class JavaMainWrapper {
101101
* that uses it may be compiled as part of the base layer, e.g., such as
102102
* {@link CEntryPointSnippets}.
103103
*/
104-
public static class JavaMainSupport implements ApplicationLayerOnlyImageSingleton, UnsavedSingleton {
104+
@SingletonTraits(access = AllAccess.class, layeredCallbacks = NoLayeredCallbacks.class, layeredInstallationKind = ApplicationLayerOnly.class)
105+
public static class JavaMainSupport {
105106
private final MethodHandle javaMainHandle;
106107
private final MethodHandle javaMainClassCtorHandle;
107108
final String javaMainClassName;
@@ -158,11 +159,6 @@ public List<String> getInputArguments() {
158159
}
159160
return Collections.emptyList();
160161
}
161-
162-
@Override
163-
public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() {
164-
return LayeredImageSingletonBuilderFlags.ALL_ACCESS;
165-
}
166162
}
167163

168164
public static void invokeMain(String[] args) throws Throwable {

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/registry/TypeIDs.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package com.oracle.svm.core.hub.registry;
2626

27-
import java.util.EnumSet;
2827
import java.util.concurrent.atomic.AtomicInteger;
2928

3029
import org.graalvm.nativeimage.ImageSingletons;
@@ -38,13 +37,15 @@
3837
import com.oracle.svm.core.hub.DynamicHub;
3938
import com.oracle.svm.core.hub.DynamicHubSupport;
4039
import com.oracle.svm.core.imagelayer.ImageLayerBuildingSupport;
41-
import com.oracle.svm.core.layeredimagesingleton.ApplicationLayerOnlyImageSingleton;
42-
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonBuilderFlags;
43-
import com.oracle.svm.core.layeredimagesingleton.UnsavedSingleton;
40+
import com.oracle.svm.core.traits.BuiltinTraits.AllAccess;
41+
import com.oracle.svm.core.traits.BuiltinTraits.NoLayeredCallbacks;
42+
import com.oracle.svm.core.traits.SingletonLayeredInstallationKind.ApplicationLayerOnly;
43+
import com.oracle.svm.core.traits.SingletonTraits;
4444
import com.oracle.svm.core.util.VMError;
4545

4646
/** Keeps track of type ID information at run-time (see {@link DynamicHub#getTypeID()}). */
47-
public class TypeIDs implements ApplicationLayerOnlyImageSingleton, UnsavedSingleton {
47+
@SingletonTraits(access = AllAccess.class, layeredCallbacks = NoLayeredCallbacks.class, layeredInstallationKind = ApplicationLayerOnly.class)
48+
public class TypeIDs {
4849
private final AtomicInteger nextTypeId = new AtomicInteger();
4950
@UnknownPrimitiveField(availability = AfterCompilation.class) //
5051
private int firstRuntimeTypeId;
@@ -79,11 +80,6 @@ public int nextTypeId() {
7980
public int getNumTypeIds() {
8081
return nextTypeId.get();
8182
}
82-
83-
@Override
84-
public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() {
85-
return LayeredImageSingletonBuilderFlags.ALL_ACCESS;
86-
}
8783
}
8884

8985
@AutomaticallyRegisteredFeature

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/imagelayer/ImageLayerBuildingSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@
6363
*/
6464
public abstract class ImageLayerBuildingSupport {
6565
public final boolean buildingImageLayer;
66-
private final boolean buildingInitialLayer;
67-
private final boolean buildingApplicationLayer;
66+
public final boolean buildingInitialLayer;
67+
public final boolean buildingApplicationLayer;
6868

6969
protected ImageLayerBuildingSupport(boolean buildingImageLayer, boolean buildingInitialLayer, boolean buildingApplicationLayer) {
7070
this.buildingImageLayer = buildingImageLayer;

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/RuntimeModuleSupport.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
*/
2525
package com.oracle.svm.core.jdk;
2626

27-
import java.util.EnumSet;
28-
2927
import org.graalvm.nativeimage.ImageSingletons;
3028
import org.graalvm.nativeimage.Platform;
3129
import org.graalvm.nativeimage.Platforms;
@@ -34,9 +32,10 @@
3432
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
3533
import com.oracle.svm.core.heap.UnknownObjectField;
3634
import com.oracle.svm.core.imagelayer.LastImageBuildPredicate;
37-
import com.oracle.svm.core.layeredimagesingleton.ApplicationLayerOnlyImageSingleton;
38-
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonBuilderFlags;
39-
import com.oracle.svm.core.layeredimagesingleton.UnsavedSingleton;
35+
import com.oracle.svm.core.traits.BuiltinTraits.AllAccess;
36+
import com.oracle.svm.core.traits.BuiltinTraits.NoLayeredCallbacks;
37+
import com.oracle.svm.core.traits.SingletonLayeredInstallationKind.ApplicationLayerOnly;
38+
import com.oracle.svm.core.traits.SingletonTraits;
4039

4140
/**
4241
* Runtime module support singleton, containing the runtime boot module layer. The boot module layer
@@ -48,7 +47,8 @@
4847
* {@code ModuleLayerFeature} for more information.
4948
*/
5049
@AutomaticallyRegisteredImageSingleton(onlyWith = LastImageBuildPredicate.class)
51-
public final class RuntimeModuleSupport implements ApplicationLayerOnlyImageSingleton, UnsavedSingleton {
50+
@SingletonTraits(access = AllAccess.class, layeredCallbacks = NoLayeredCallbacks.class, layeredInstallationKind = ApplicationLayerOnly.class)
51+
public final class RuntimeModuleSupport {
5252
public static RuntimeModuleSupport singleton() {
5353
return ImageSingletons.lookup(RuntimeModuleSupport.class);
5454
}
@@ -64,9 +64,4 @@ public void setBootLayer(ModuleLayer bootLayer) {
6464
public ModuleLayer getBootLayer() {
6565
return bootLayer;
6666
}
67-
68-
@Override
69-
public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() {
70-
return LayeredImageSingletonBuilderFlags.ALL_ACCESS;
71-
}
7267
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/layeredimagesingleton/ApplicationLayerOnlyImageSingleton.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/layeredimagesingleton/LayeredImageSingleton.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,9 @@
3434

3535
/**
3636
* In additional to the traditional singleton model, i.e. a key-value map whose lookups are constant
37-
* folded within generated code, we provide two additional options:
37+
* folded within generated code, we provide one additional option:
3838
*
3939
* <ul>
40-
* <li>{@link ApplicationLayerOnlyImageSingleton}: Instead of having a per-layer singleton, all
41-
* {@link ImageSingletons#lookup} calls refer to a single singleton which will be created in the
42-
* application layer.</li>
43-
*
4440
* <li>{@link MultiLayeredImageSingleton}: {@link ImageSingletons#lookup} should no longer be used.
4541
* Instead, there is the method {@link MultiLayeredImageSingleton#getAllLayers} which returns an
4642
* array with the image singletons corresponding to this key for all layers. The length of this
@@ -52,10 +48,9 @@
5248
* {@link ImageLayerBuildingSupport} for a description of the different layer names.</li>
5349
* </ul>
5450
*
55-
* Note the unique behavior of {@link ApplicationLayerOnlyImageSingleton} applies only when building
56-
* a layered image. Calling {@link MultiLayeredImageSingleton#getAllLayers} during a traditional
57-
* build requires the singleton to be installed in the build and will return an array of length 1
58-
* containing that singleton.
51+
* Calling {@link MultiLayeredImageSingleton#getAllLayers} during a traditional build requires the
52+
* singleton to be installed in the build and will return an array of length 1 containing that
53+
* singleton.
5954
*
6055
* Currently, when using these special singleton types there are additional restrictions:
6156
*
@@ -65,8 +60,6 @@
6560
* between Class<->singleton object</li>
6661
* <li>{@link ImageSingletons#add} must be called before the analysis phase (i.e. these image
6762
* singletons cannot be added at a later point)</li>
68-
* <li>{@link ApplicationLayerOnlyImageSingleton}s can only be installed in the application
69-
* layer</li>
7063
* </ol>
7164
*/
7265
public interface LayeredImageSingleton {

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/layeredimagesingleton/LayeredImageSingletonBuilderFlags.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626

2727
import java.util.EnumSet;
2828

29-
import com.oracle.svm.core.graal.RuntimeCompilation;
30-
import com.oracle.svm.core.util.VMError;
31-
3229
/**
3330
* Flags used during build time to determine how the native image generator handles the layered
3431
* image singleton.
@@ -52,39 +49,4 @@ public enum LayeredImageSingletonBuilderFlags {
5249
public static final EnumSet<LayeredImageSingletonBuilderFlags> RUNTIME_ACCESS_ONLY = EnumSet.of(RUNTIME_ACCESS);
5350

5451
public static final EnumSet<LayeredImageSingletonBuilderFlags> ALL_ACCESS = EnumSet.of(RUNTIME_ACCESS, BUILDTIME_ACCESS);
55-
56-
public static boolean verifyImageBuilderFlags(Object singleton, EnumSet<LayeredImageSingletonBuilderFlags> flags) {
57-
if (!(flags.contains(BUILDTIME_ACCESS) || flags.contains(RUNTIME_ACCESS))) {
58-
assert false : String.format("At least one of the following flags must be set: %s, %s", BUILDTIME_ACCESS, RUNTIME_ACCESS);
59-
}
60-
61-
if (singleton instanceof MultiLayeredImageSingleton || ApplicationLayerOnlyImageSingleton.isSingletonInstanceOf(singleton)) {
62-
assert flags.contains(RUNTIME_ACCESS) : String.format("%s must be set when implementing either %s or %s: %s", RUNTIME_ACCESS, MultiLayeredImageSingleton.class,
63-
ApplicationLayerOnlyImageSingleton.class, singleton);
64-
}
65-
66-
assert !(singleton instanceof MultiLayeredImageSingleton && ApplicationLayerOnlyImageSingleton.isSingletonInstanceOf(singleton)) : String.format("%s can only implement one of %s or %s",
67-
singleton,
68-
MultiLayeredImageSingleton.class, ApplicationLayerOnlyImageSingleton.class);
69-
70-
return true;
71-
}
72-
73-
public static void validateRuntimeLookup(Object singleton) {
74-
if (singleton instanceof LayeredImageSingleton layeredSingleton) {
75-
if (!layeredSingleton.getImageBuilderFlags().contains(LayeredImageSingletonBuilderFlags.RUNTIME_ACCESS)) {
76-
/*
77-
* Runtime compilation installs many singletons into the image which are otherwise
78-
* hosted only. Note the platform checks still apply and can be used to ensure
79-
* certain singleton are not installed into the image.
80-
*/
81-
if (!RuntimeCompilation.isEnabled()) {
82-
throw VMError.shouldNotReachHere("Layered image singleton without runtime access is in runtime graph: " + layeredSingleton);
83-
}
84-
}
85-
if (layeredSingleton instanceof MultiLayeredImageSingleton) {
86-
throw VMError.shouldNotReachHere("Forbidden lookup of MultiLayeredImageSingleton in runtime graph: " + layeredSingleton);
87-
}
88-
}
89-
}
9052
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/layeredimagesingleton/LayeredImageSingletonSupport.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.graalvm.nativeimage.Platforms;
3333

3434
import com.oracle.svm.core.traits.SingletonLayeredInstallationKind;
35+
import com.oracle.svm.core.traits.SingletonTrait;
36+
import com.oracle.svm.core.traits.SingletonTraitKind;
3537

3638
import jdk.vm.ci.meta.JavaConstant;
3739

@@ -58,7 +60,10 @@ static LayeredImageSingletonSupport singleton() {
5860

5961
void freezeLayeredImageSingletonMetadata();
6062

61-
boolean isInitialLayerOnlyImageSingleton(Class<?> key);
62-
6363
JavaConstant getInitialLayerOnlyImageSingleton(Class<?> key);
64+
65+
/**
66+
* @return trait associated with this key if it exists, or else {@code null}.
67+
*/
68+
SingletonTrait getTraitForUninstalledSingleton(Class<?> key, SingletonTraitKind kind);
6469
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/JavaThreads.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;
2828

2929
import java.lang.Thread.UncaughtExceptionHandler;
30-
import java.util.EnumSet;
3130
import java.util.concurrent.atomic.AtomicInteger;
3231
import java.util.concurrent.atomic.AtomicLong;
3332

@@ -46,15 +45,16 @@
4645
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
4746
import com.oracle.svm.core.imagelayer.LastImageBuildPredicate;
4847
import com.oracle.svm.core.jdk.StackTraceUtils;
49-
import com.oracle.svm.core.layeredimagesingleton.ApplicationLayerOnlyImageSingleton;
50-
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonBuilderFlags;
51-
import com.oracle.svm.core.layeredimagesingleton.UnsavedSingleton;
5248
import com.oracle.svm.core.log.Log;
5349
import com.oracle.svm.core.snippets.KnownIntrinsics;
5450
import com.oracle.svm.core.stack.StackFrameVisitor;
5551
import com.oracle.svm.core.threadlocal.FastThreadLocal;
5652
import com.oracle.svm.core.threadlocal.FastThreadLocalFactory;
5753
import com.oracle.svm.core.threadlocal.FastThreadLocalLong;
54+
import com.oracle.svm.core.traits.BuiltinTraits.AllAccess;
55+
import com.oracle.svm.core.traits.BuiltinTraits.NoLayeredCallbacks;
56+
import com.oracle.svm.core.traits.SingletonLayeredInstallationKind.ApplicationLayerOnly;
57+
import com.oracle.svm.core.traits.SingletonTraits;
5858
import com.oracle.svm.util.ReflectionUtil;
5959

6060
import jdk.graal.compiler.api.directives.GraalDirectives;
@@ -93,7 +93,8 @@ public final class JavaThreads {
9393
static final FastThreadLocalLong currentVThreadId = FastThreadLocalFactory.createLong("JavaThreads.currentVThreadId").setMaxOffset(FastThreadLocal.BYTE_OFFSET);
9494

9595
@AutomaticallyRegisteredImageSingleton(onlyWith = LastImageBuildPredicate.class)
96-
public static class JavaThreadNumberSingleton implements ApplicationLayerOnlyImageSingleton, UnsavedSingleton {
96+
@SingletonTraits(access = AllAccess.class, layeredCallbacks = NoLayeredCallbacks.class, layeredInstallationKind = ApplicationLayerOnly.class)
97+
public static class JavaThreadNumberSingleton {
9798

9899
public static JavaThreadNumberSingleton singleton() {
99100
return ImageSingletons.lookup(JavaThreadNumberSingleton.class);
@@ -114,11 +115,6 @@ public void setThreadNumberInfo(long seqNumber, int initNumber) {
114115
this.threadSeqNumber.set(seqNumber);
115116
this.threadInitNumber.set(initNumber);
116117
}
117-
118-
@Override
119-
public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() {
120-
return LayeredImageSingletonBuilderFlags.ALL_ACCESS;
121-
}
122118
}
123119

124120
private JavaThreads() {

0 commit comments

Comments
 (0)