2525package com .oracle .svm .core ;
2626
2727import java .util .EnumSet ;
28+ import java .util .List ;
2829
2930import org .graalvm .nativeimage .Platform ;
3031import org .graalvm .nativeimage .Platforms ;
3132
3233import com .oracle .svm .core .code .RuntimeCodeCache ;
34+ import com .oracle .svm .core .layeredimagesingleton .ImageSingletonLoader ;
35+ import com .oracle .svm .core .layeredimagesingleton .ImageSingletonWriter ;
36+ import com .oracle .svm .core .layeredimagesingleton .LayeredImageSingleton ;
37+ import com .oracle .svm .core .layeredimagesingleton .LayeredImageSingleton .PersistFlags ;
38+ import com .oracle .svm .core .traits .BuiltinTraits .AllAccess ;
39+ import com .oracle .svm .core .traits .SingletonLayeredCallbacks ;
40+ import com .oracle .svm .core .traits .SingletonLayeredCallbacksSupplier ;
41+ import com .oracle .svm .core .traits .SingletonLayeredInstallationKind .Independent ;
42+ import com .oracle .svm .core .traits .SingletonTrait ;
43+ import com .oracle .svm .core .traits .SingletonTraitKind ;
44+ import com .oracle .svm .core .traits .SingletonTraits ;
45+ import com .oracle .svm .core .util .VMError ;
3346
3447import jdk .vm .ci .code .Architecture ;
3548import jdk .vm .ci .code .TargetDescription ;
3649
50+ @ SingletonTraits (access = AllAccess .class , layeredCallbacks = SubstrateTargetDescription .LayeredCallbacks .class , layeredInstallationKind = Independent .class )
3751public class SubstrateTargetDescription extends TargetDescription {
52+ private static final String RUNTIME_CHECKED_CPU_FEATURES = "runtimeCheckedCPUFeatures" ;
53+
3854 @ Platforms (Platform .HOSTED_ONLY .class )
3955 public static boolean shouldInlineObjectsInImageCode () {
4056 return SubstrateOptions .SpawnIsolates .getValue ();
@@ -55,4 +71,33 @@ public SubstrateTargetDescription(Architecture arch, boolean isMP, int stackAlig
5571 public EnumSet <?> getRuntimeCheckedCPUFeatures () {
5672 return runtimeCheckedCPUFeatures ;
5773 }
74+
75+ static class LayeredCallbacks extends SingletonLayeredCallbacksSupplier {
76+ @ Override
77+ public SingletonTrait getLayeredCallbacksTrait () {
78+ SingletonLayeredCallbacks action = new SingletonLayeredCallbacks () {
79+ @ Override
80+ public LayeredImageSingleton .PersistFlags doPersist (ImageSingletonWriter writer , Object singleton ) {
81+ SubstrateTargetDescription substrateTargetDescription = (SubstrateTargetDescription ) singleton ;
82+ writer .writeStringList (RUNTIME_CHECKED_CPU_FEATURES , getCPUFeaturesList (substrateTargetDescription ));
83+ return PersistFlags .CALLBACK_ON_REGISTRATION ;
84+ }
85+
86+ @ Override
87+ public void onSingletonRegistration (ImageSingletonLoader loader , Object singleton ) {
88+ SubstrateTargetDescription substrateTargetDescription = (SubstrateTargetDescription ) singleton ;
89+ List <String > previousLayerRuntimeCheckedCPUFeatures = loader .readStringList (RUNTIME_CHECKED_CPU_FEATURES );
90+ List <String > currentLayerRuntimeCheckedCPUFeatures = getCPUFeaturesList (substrateTargetDescription );
91+ VMError .guarantee (previousLayerRuntimeCheckedCPUFeatures .equals (currentLayerRuntimeCheckedCPUFeatures ),
92+ "The runtime checked CPU Features should be consistent across layers. The previous layer CPU Features were %s, but the current layer are %s" ,
93+ previousLayerRuntimeCheckedCPUFeatures , currentLayerRuntimeCheckedCPUFeatures );
94+ }
95+ };
96+ return new SingletonTrait (SingletonTraitKind .LAYERED_CALLBACKS , action );
97+ }
98+ }
99+
100+ private static List <String > getCPUFeaturesList (SubstrateTargetDescription substrateTargetDescription ) {
101+ return substrateTargetDescription .runtimeCheckedCPUFeatures .stream ().map (Enum ::toString ).toList ();
102+ }
58103}
0 commit comments