Skip to content

Commit 4aaf7e3

Browse files
committed
[GR-53552] Remove Future from ImageLayerLoader
PullRequest: graal/17663
2 parents 99ceff5 + 61a8c4d commit 4aaf7e3

File tree

10 files changed

+236
-310
lines changed

10 files changed

+236
-310
lines changed

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/heap/ImageLayerLoader.java

Lines changed: 168 additions & 267 deletions
Large diffs are not rendered by default.

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/heap/ImageLayerSnapshotUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class ImageLayerSnapshotUtil {
5858
public static final String SUPER_CLASS_TAG = "super class";
5959
public static final String INTERFACES_TAG = "interfaces";
6060
public static final String CONSTANTS_TAG = "constants";
61+
public static final String CONSTANTS_TO_RELINK_TAG = "constants to relink";
6162
public static final String TID_TAG = "tid";
6263
public static final String IDENTITY_HASH_CODE_TAG = "identityHashCode";
6364
public static final String ID_TAG = "id";

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/heap/ImageLayerWriter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.CLASS_NAME_TAG;
3030
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.COMPONENT_TYPE_TAG;
3131
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.CONSTANTS_TAG;
32+
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.CONSTANTS_TO_RELINK_TAG;
3233
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.CONSTANT_TYPE_TAG;
3334
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.DATA_TAG;
3435
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.ENCLOSING_TYPE_TAG;
@@ -102,6 +103,7 @@ public class ImageLayerWriter {
102103
private String[] imageInternedStrings;
103104

104105
protected EconomicMap<String, Object> jsonMap = EconomicMap.create();
106+
protected List<Integer> constantsToRelink;
105107
FileInfo fileInfo;
106108

107109
private record FileInfo(Path layerSnapshotPath, String fileName, String suffix) {
@@ -113,6 +115,7 @@ public ImageLayerWriter() {
113115

114116
public ImageLayerWriter(ImageLayerSnapshotUtil imageLayerSnapshotUtil) {
115117
this.imageLayerSnapshotUtil = imageLayerSnapshotUtil;
118+
this.constantsToRelink = new ArrayList<>();
116119
}
117120

118121
public void setImageInternedStrings(String[] imageInternedStrings) {
@@ -190,6 +193,7 @@ public void persistAnalysisInfo(Universe hostedUniverse, AnalysisUniverse analys
190193
}
191194
}
192195
jsonMap.put(CONSTANTS_TAG, constantsMap);
196+
jsonMap.put(CONSTANTS_TO_RELINK_TAG, constantsToRelink);
193197
}
194198

195199
/**
@@ -319,12 +323,12 @@ public void persistConstantRelinkingInfo(EconomicMap<String, Object> constantMap
319323
boolean simulated = hostedObject == null;
320324
constantMap.put(SIMULATED_TAG, simulated);
321325
if (!simulated) {
322-
persistConstantRelinkingInfo(constantMap, bb, clazz, hostedObject);
326+
persistConstantRelinkingInfo(constantMap, bb, clazz, hostedObject, imageHeapConstant.constantData.id);
323327
}
324328
}
325329

326330
@SuppressFBWarnings(value = "ES", justification = "Reference equality check needed to detect intern status")
327-
public void persistConstantRelinkingInfo(EconomicMap<String, Object> constantMap, BigBang bb, Class<?> clazz, JavaConstant hostedObject) {
331+
public void persistConstantRelinkingInfo(EconomicMap<String, Object> constantMap, BigBang bb, Class<?> clazz, JavaConstant hostedObject, int id) {
328332
if (clazz.equals(String.class)) {
329333
String value = bb.getSnippetReflectionProvider().asObject(String.class, hostedObject);
330334
int stringIndex = Arrays.binarySearch(imageInternedStrings, value);
@@ -334,11 +338,13 @@ public void persistConstantRelinkingInfo(EconomicMap<String, Object> constantMap
334338
*/
335339
if (stringIndex >= 0 && imageInternedStrings[stringIndex] == value) {
336340
constantMap.put(VALUE_TAG, value);
341+
constantsToRelink.add(id);
337342
}
338343
} else if (Enum.class.isAssignableFrom(clazz)) {
339344
Enum<?> value = bb.getSnippetReflectionProvider().asObject(Enum.class, hostedObject);
340345
constantMap.put(ENUM_CLASS_TAG, value.getDeclaringClass().getName());
341346
constantMap.put(ENUM_NAME_TAG, value.name());
347+
constantsToRelink.add(id);
342348
}
343349
}
344350

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisType.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,7 @@ public AnalysisType(AnalysisUniverse universe, ResolvedJavaType javaType, JavaKi
274274
}
275275

276276
/* Set id after accessing super types, so that all these types get a lower id number. */
277-
if (wrapped instanceof BaseLayerType baseLayerType) {
278-
this.id = baseLayerType.getId();
279-
this.isInBaseLayer = true;
280-
} else if (universe.hostVM().useBaseLayer()) {
277+
if (universe.hostVM().useBaseLayer()) {
281278
int tid = universe.getImageLayerLoader().lookupHostedTypeInBaseLayer(this);
282279
if (tid != -1) {
283280
/*
@@ -288,7 +285,13 @@ public AnalysisType(AnalysisUniverse universe, ResolvedJavaType javaType, JavaKi
288285
this.isInBaseLayer = true;
289286
} else {
290287
this.id = universe.computeNextTypeId();
291-
this.isInBaseLayer = false;
288+
/*
289+
* If both the BaseLayerType and the complete type are created at the same time,
290+
* there can be a race for the base layer id. It is possible that the complete type
291+
* gets the base layer id even though the BaseLayerType is created. In this case,
292+
* the AnalysisType should still be marked as isInBaseLayer.
293+
*/
294+
this.isInBaseLayer = wrapped instanceof BaseLayerType;
292295
}
293296
} else {
294297
this.id = universe.computeNextTypeId();

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisUniverse.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,6 @@ public JavaType lookupAllowUnresolved(JavaType rawType) {
222222
if (result == null) {
223223
result = createType(type);
224224
}
225-
if (result.isInBaseLayer()) {
226-
/*
227-
* The constants can only be relinked after the type is registered as the dynamic hub is
228-
* not available otherwise.
229-
*/
230-
getImageLayerLoader().loadAndRelinkTypeConstants(result);
231-
}
232225
assert typesById[result.getId()].equals(result) : result;
233226
return result;
234227
}

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/BaseLayerType.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class BaseLayerType implements ResolvedJavaType, OriginalClassProvider {
5151
*/
5252
public static final String BASE_LAYER_SUFFIX = "_BaseLayer;";
5353
private final String name;
54-
private final int id;
54+
private final int baseLayerId;
5555
private final int modifiers;
5656
private final boolean isInterface;
5757
private final boolean isEnum;
@@ -64,10 +64,10 @@ public class BaseLayerType implements ResolvedJavaType, OriginalClassProvider {
6464
private final ResolvedJavaType[] interfaces;
6565
private final ResolvedJavaType objectType;
6666

67-
public BaseLayerType(String name, int id, int modifiers, boolean isInterface, boolean isEnum, boolean isInitialized, boolean isLinked, String sourceFileName, ResolvedJavaType enclosingType,
68-
ResolvedJavaType componentType, ResolvedJavaType superClass, ResolvedJavaType[] interfaces, ResolvedJavaType objectType) {
67+
public BaseLayerType(String name, int baseLayerId, int modifiers, boolean isInterface, boolean isEnum, boolean isInitialized, boolean isLinked, String sourceFileName,
68+
ResolvedJavaType enclosingType, ResolvedJavaType componentType, ResolvedJavaType superClass, ResolvedJavaType[] interfaces, ResolvedJavaType objectType) {
6969
this.name = name.substring(0, name.length() - 1) + BASE_LAYER_SUFFIX;
70-
this.id = id;
70+
this.baseLayerId = baseLayerId;
7171
this.modifiers = modifiers;
7272
this.isInterface = isInterface;
7373
this.isEnum = isEnum;
@@ -328,7 +328,7 @@ public ResolvedJavaType unwrapTowardsOriginalType() {
328328
return objectType;
329329
}
330330

331-
public int getId() {
332-
return id;
331+
public int getBaseLayerId() {
332+
return baseLayerId;
333333
}
334334
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@
225225
import com.oracle.svm.hosted.code.CEntryPointData;
226226
import com.oracle.svm.hosted.code.CFunctionSubstitutionProcessor;
227227
import com.oracle.svm.hosted.code.CompileQueue;
228-
import com.oracle.svm.hosted.code.ObjectFileTransformer;
229228
import com.oracle.svm.hosted.code.HostedRuntimeConfigurationBuilder;
230229
import com.oracle.svm.hosted.code.NativeMethodSubstitutionProcessor;
230+
import com.oracle.svm.hosted.code.ObjectFileTransformer;
231231
import com.oracle.svm.hosted.code.RestrictHeapAccessCalleesImpl;
232232
import com.oracle.svm.hosted.code.SubstrateGraphMakerFactory;
233233
import com.oracle.svm.hosted.heap.ObservableImageHeapMapProviderImpl;
@@ -824,10 +824,6 @@ protected boolean runPointsToAnalysis(String imageName, OptionValues options, De
824824
bb.getHostVM().getClassInitializationSupport().setConfigurationSealed(true);
825825
}
826826

827-
if (ImageLayerBuildingSupport.buildingExtensionLayer()) {
828-
HostedImageLayerBuildingSupport.singleton().getLoader().loadLayerConstants();
829-
}
830-
831827
try (ReporterClosable c = ProgressReporter.singleton().printAnalysis(bb.getUniverse(), nativeLibraries.getLibraries())) {
832828
DuringAnalysisAccessImpl config = new DuringAnalysisAccessImpl(featureHandler, loader, bb, nativeLibraries, debug);
833829
try {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.oracle.graal.pointsto.meta.AnalysisMethod;
6565
import com.oracle.graal.pointsto.meta.AnalysisType;
6666
import com.oracle.graal.pointsto.meta.AnalysisUniverse;
67+
import com.oracle.graal.pointsto.meta.BaseLayerType;
6768
import com.oracle.graal.pointsto.meta.HostedProviders;
6869
import com.oracle.graal.pointsto.phases.InlineBeforeAnalysisGraphDecoder;
6970
import com.oracle.graal.pointsto.phases.InlineBeforeAnalysisPolicy;
@@ -408,7 +409,11 @@ private DynamicHub createHub(AnalysisType type) {
408409
ClassLoader hubClassLoader = javaClass.getClassLoader();
409410

410411
/* Class names must be interned strings according to the Java specification. */
411-
String className = type.toClassName().intern();
412+
String name = type.toClassName();
413+
if (name.endsWith(BaseLayerType.BASE_LAYER_SUFFIX.substring(0, BaseLayerType.BASE_LAYER_SUFFIX.length() - 1))) {
414+
name = name.substring(0, name.length() - BaseLayerType.BASE_LAYER_SUFFIX.length() + 1);
415+
}
416+
String className = name.intern();
412417
/*
413418
* There is no need to have file names and simple binary names as interned strings. So we
414419
* perform our own de-duplication.

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/heap/SVMImageLayerLoader.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ public SVMImageLayerLoader(List<Path> loaderPaths) {
7474
dynamicHubArrayHubField = ReflectionUtil.lookupField(DynamicHub.class, "arrayHub");
7575
}
7676

77+
@Override
78+
protected void prepareConstantRelinking(EconomicMap<String, Object> constantData, int id) {
79+
Integer tid = get(constantData, CLASS_ID_TAG);
80+
if (tid != null) {
81+
typeToConstant.put(tid, id);
82+
} else {
83+
super.prepareConstantRelinking(constantData, id);
84+
}
85+
}
86+
7787
@Override
7888
protected boolean delegateProcessing(String constantType, Object constantValue, Object[] values, int i) {
7989
if (constantType.equals(METHOD_POINTER_TAG)) {
@@ -112,31 +122,22 @@ protected boolean delegateProcessing(String constantType, Object constantValue,
112122
}
113123

114124
@Override
115-
protected void relinkConstant(ImageHeapInstance imageHeapInstance, EconomicMap<String, Object> baseLayerConstant, Class<?> clazz) {
125+
protected JavaConstant getHostedObject(EconomicMap<String, Object> baseLayerConstant, Class<?> clazz) {
116126
if (clazz.equals(Class.class)) {
117127
Integer tid = get(baseLayerConstant, CLASS_ID_TAG);
118128
/* DynamicHub corresponding to $$TypeSwitch classes are not relinked */
119129
if (tid != null) {
120-
if (universe.isTypeCreated(tid)) {
121-
relinkDynamicHub(imageHeapInstance, tid);
122-
} else {
123-
/*
124-
* If the DynamicHub is not created yet, we create a task that will be executed
125-
* on the DynamicHub creation
126-
*/
127-
AnalysisFuture<Void> task = new AnalysisFuture<>(() -> relinkDynamicHub(imageHeapInstance, tid));
128-
missingTypeTasks.computeIfAbsent(tid, unused -> ConcurrentHashMap.newKeySet()).add(task);
129-
}
130+
return getDynamicHub(tid);
130131
}
131-
} else {
132-
super.relinkConstant(imageHeapInstance, baseLayerConstant, clazz);
133132
}
133+
return super.getHostedObject(baseLayerConstant, clazz);
134134
}
135135

136-
private void relinkDynamicHub(ImageHeapInstance imageHeapInstance, int tid) {
136+
private JavaConstant getDynamicHub(int tid) {
137+
getAnalysisType(tid);
137138
AnalysisType type = universe.getType(tid);
138139
DynamicHub hub = ((SVMHost) universe.hostVM()).dynamicHub(type);
139-
relinkConstant(hub, imageHeapInstance);
140+
return getHostedObject(hub);
140141
}
141142

142143
@Override
@@ -185,6 +186,25 @@ public void rescanHub(AnalysisType type, Object hubObject) {
185186
}
186187
}
187188

189+
@Override
190+
protected boolean hasValueForObject(Object object) {
191+
if (object instanceof DynamicHub dynamicHub) {
192+
AnalysisType type = ((SVMHost) universe.hostVM()).lookupType(dynamicHub);
193+
return typeToConstant.containsKey(type.getId());
194+
}
195+
return super.hasValueForObject(object);
196+
}
197+
198+
@Override
199+
protected ImageHeapConstant getValueForObject(Object object) {
200+
if (object instanceof DynamicHub dynamicHub) {
201+
AnalysisType type = ((SVMHost) universe.hostVM()).lookupType(dynamicHub);
202+
int id = typeToConstant.get(type.getId());
203+
return getOrCreateConstant(id);
204+
}
205+
return super.getValueForObject(object);
206+
}
207+
188208
public Map<Object, Set<Class<?>>> loadImageSingletons(Object forbiddenObject) {
189209
loadJsonMap();
190210
return loadImageSingletons0(forbiddenObject);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected void persistConstant(AnalysisUniverse analysisUniverse, ImageHeapConst
158158
}
159159

160160
@Override
161-
public void persistConstantRelinkingInfo(EconomicMap<String, Object> constantMap, BigBang bb, Class<?> clazz, JavaConstant hostedObject) {
161+
public void persistConstantRelinkingInfo(EconomicMap<String, Object> constantMap, BigBang bb, Class<?> clazz, JavaConstant hostedObject, int id) {
162162
ResolvedJavaType type = bb.getConstantReflectionProvider().asJavaType(hostedObject);
163163
if (type instanceof AnalysisType analysisType) {
164164
/*
@@ -170,9 +170,10 @@ public void persistConstantRelinkingInfo(EconomicMap<String, Object> constantMap
170170
*/
171171
if (!isTypeSwitch(analysisType)) {
172172
constantMap.put(CLASS_ID_TAG, analysisType.getId());
173+
constantsToRelink.add(id);
173174
}
174175
} else {
175-
super.persistConstantRelinkingInfo(constantMap, bb, clazz, hostedObject);
176+
super.persistConstantRelinkingInfo(constantMap, bb, clazz, hostedObject, id);
176177
}
177178
}
178179

0 commit comments

Comments
 (0)