Skip to content

Commit f907f3f

Browse files
committed
[GR-64959] Support InitialLayerOnlyImageSingleton with different key
PullRequest: graal/20887
2 parents 5cb3049 + db7bc9a commit f907f3f

File tree

7 files changed

+48
-24
lines changed

7 files changed

+48
-24
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,7 @@ static LayeredImageSingletonSupport singleton() {
5353

5454
void freezeLayeredImageSingletonMetadata();
5555

56+
boolean isInitialLayerOnlyImageSingleton(Class<?> key);
57+
5658
JavaConstant getInitialLayerOnlyImageSingleton(Class<?> key);
5759
}

substratevm/src/com.oracle.svm.hosted/resources/SharedLayerSnapshotCapnProtoSchema.capnp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ struct ImageSingletonKey {
230230
persistFlag @1 :Int32;
231231
objectId @2 :SingletonObjId;
232232
constantId @3 :ConstantId;
233+
isInitialLayerOnly @4 :Bool;
233234
}
234235

235236
struct ImageSingletonObject {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ImageSingletonsSupportImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ public void freezeLayeredImageSingletonMetadata() {
8282
HostedManagement.getAndAssertExists().freezeLayeredImageSingletonMetadata();
8383
}
8484

85+
@Override
86+
public boolean isInitialLayerOnlyImageSingleton(Class<?> key) {
87+
var loader = HostedImageLayerBuildingSupport.singleton().getSingletonLoader();
88+
return loader.isInitialLayerOnlyImageSingleton(key);
89+
}
90+
8591
@Override
8692
public JavaConstant getInitialLayerOnlyImageSingleton(Class<?> key) {
8793
var loader = HostedImageLayerBuildingSupport.singleton().getSingletonLoader();

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerSingletonLoader.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.graalvm.collections.UnmodifiableEconomicMap;
3737

3838
import com.oracle.svm.core.layeredimagesingleton.ImageSingletonLoader;
39-
import com.oracle.svm.core.layeredimagesingleton.InitialLayerOnlyImageSingleton;
4039
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingleton.PersistFlags;
4140
import com.oracle.svm.core.util.UserError;
4241
import com.oracle.svm.core.util.VMError;
@@ -107,11 +106,9 @@ public Map<Object, Set<Class<?>>> loadImageSingletons(Object forbiddenObject) {
107106
Class<?> clazz = imageLayerBuildingSupport.lookupClass(false, className);
108107
singletonInitializationMap.computeIfAbsent(forbiddenObject, (k) -> new HashSet<>());
109108
singletonInitializationMap.get(forbiddenObject).add(clazz);
110-
if (InitialLayerOnlyImageSingleton.class.isAssignableFrom(clazz)) {
109+
if (entry.getIsInitialLayerOnly()) {
111110
int constantId = entry.getConstantId();
112-
if (constantId != -1) {
113-
initialLayerKeyToIdMap.put(clazz, constantId);
114-
}
111+
initialLayerKeyToIdMap.put(clazz, constantId);
115112
}
116113
} else {
117114
assert persistInfo == PersistFlags.NOTHING : "Unexpected PersistFlags value: " + persistInfo;
@@ -124,6 +121,10 @@ public Map<Object, Set<Class<?>>> loadImageSingletons(Object forbiddenObject) {
124121
return singletonInitializationMap;
125122
}
126123

124+
public boolean isInitialLayerOnlyImageSingleton(Class<?> key) {
125+
return initialLayerOnlySingletonConstantIds.containsKey(key);
126+
}
127+
127128
public JavaConstant loadInitialLayerOnlyImageSingleton(Class<?> key) {
128129
int constantId = initialLayerOnlySingletonConstantIds.getOrDefault(key, -1);
129130
if (constantId != -1) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,7 @@ public void writeImageSingletonInfo(List<Map.Entry<Class<?>, Object>> layeredIma
11171117
constantId = initialLayerOnlySingletonMap.getOrDefault(initialLayerOnlyImageSingleton, -1);
11181118
}
11191119
sb.setConstantId(constantId);
1120+
sb.setIsInitialLayerOnly(singleton instanceof InitialLayerOnlyImageSingleton);
11201121
}
11211122

11221123
var sortedByIDs = singletonInfoMap.entrySet().stream()

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SharedLayerSnapshotCapnProtoSchemaHolder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3948,6 +3948,13 @@ public final void setConstantId(int value) {
39483948
_setIntField(2, value);
39493949
}
39503950

3951+
public final boolean getIsInitialLayerOnly() {
3952+
return _getBooleanField(96);
3953+
}
3954+
public final void setIsInitialLayerOnly(boolean value) {
3955+
_setBooleanField(96, value);
3956+
}
3957+
39513958
}
39523959

39533960
public static final class Reader extends com.oracle.svm.shaded.org.capnproto.StructReader {
@@ -3974,6 +3981,10 @@ public final int getConstantId() {
39743981
return _getIntField(2);
39753982
}
39763983

3984+
public final boolean getIsInitialLayerOnly() {
3985+
return _getBooleanField(96);
3986+
}
3987+
39773988
}
39783989

39793990
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/snippets/SubstrateGraphBuilderPlugins.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
import com.oracle.svm.core.imagelayer.LoadImageSingletonFactory;
8989
import com.oracle.svm.core.jdk.proxy.DynamicProxyRegistry;
9090
import com.oracle.svm.core.layeredimagesingleton.ApplicationLayerOnlyImageSingleton;
91-
import com.oracle.svm.core.layeredimagesingleton.InitialLayerOnlyImageSingleton;
9291
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonBuilderFlags;
9392
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonSupport;
9493
import com.oracle.svm.core.layeredimagesingleton.MultiLayeredImageSingleton;
@@ -1204,27 +1203,30 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
12041203
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unused, ValueNode classNode) {
12051204
Class<?> key = constantObjectParameter(b, targetMethod, 0, Class.class, classNode);
12061205

1207-
if (ApplicationLayerOnlyImageSingleton.isAssignableFrom(key) &&
1208-
ImageLayerBuildingSupport.buildingSharedLayer()) {
1209-
/*
1210-
* This singleton is only installed in the application layer heap. All other
1211-
* layers looks refer to this singleton.
1212-
*/
1213-
b.addPush(JavaKind.Object, LoadImageSingletonFactory.loadApplicationOnlyImageSingleton(key, b.getMetaAccess()));
1214-
return true;
1215-
}
1206+
var layeredSingletonSupport = LayeredImageSingletonSupport.singleton();
1207+
if (ImageLayerBuildingSupport.buildingImageLayer()) {
1208+
if (ApplicationLayerOnlyImageSingleton.isAssignableFrom(key) &&
1209+
ImageLayerBuildingSupport.buildingSharedLayer()) {
1210+
/*
1211+
* This singleton is only installed in the application layer heap. All other
1212+
* layers looks refer to this singleton.
1213+
*/
1214+
b.addPush(JavaKind.Object, LoadImageSingletonFactory.loadApplicationOnlyImageSingleton(key, b.getMetaAccess()));
1215+
return true;
1216+
}
12161217

1217-
if (InitialLayerOnlyImageSingleton.class.isAssignableFrom(key) && ImageLayerBuildingSupport.buildingExtensionLayer()) {
1218-
/*
1219-
* This singleton is only installed in the initial layer heap. When allowed, all
1220-
* other layers lookups refer to this singleton.
1221-
*/
1222-
JavaConstant initialSingleton = LayeredImageSingletonSupport.singleton().getInitialLayerOnlyImageSingleton(key);
1223-
b.addPush(JavaKind.Object, ConstantNode.forConstant(initialSingleton, b.getMetaAccess(), b.getGraph()));
1224-
return true;
1218+
if (ImageLayerBuildingSupport.buildingExtensionLayer() && layeredSingletonSupport.isInitialLayerOnlyImageSingleton(key)) {
1219+
/*
1220+
* This singleton is only installed in the initial layer heap. When allowed,
1221+
* all other layers lookups refer to this singleton.
1222+
*/
1223+
JavaConstant initialSingleton = layeredSingletonSupport.getInitialLayerOnlyImageSingleton(key);
1224+
b.addPush(JavaKind.Object, ConstantNode.forConstant(initialSingleton, b.getMetaAccess(), b.getGraph()));
1225+
return true;
1226+
}
12251227
}
12261228

1227-
Object singleton = LayeredImageSingletonSupport.singleton().lookup(key, true, true);
1229+
Object singleton = layeredSingletonSupport.lookup(key, true, true);
12281230
LayeredImageSingletonBuilderFlags.validateRuntimeLookup(singleton);
12291231
b.addPush(JavaKind.Object, ConstantNode.forConstant(b.getSnippetReflection().forObject(singleton), b.getMetaAccess()));
12301232
return true;

0 commit comments

Comments
 (0)