@@ -119,38 +119,9 @@ public class ImageLayerWriter {
119
119
private final boolean useSharedLayerGraphs ;
120
120
private final boolean useSharedLayerStrengthenedGraphs ;
121
121
122
- /*
123
- * Types, members and constants to persist even when they are not considered reachable by the
124
- * analysis, or referenced from the image heap. Typically, these elements would be reachable
125
- * from a persisted graph.
126
- */
127
- private boolean sealed = false ;
128
- private final Set <AnalysisType > typesToPersist = ConcurrentHashMap .newKeySet ();
129
- private final Set <AnalysisMethod > methodsToPersist = ConcurrentHashMap .newKeySet ();
130
- private final Set <AnalysisField > fieldsToPersist = ConcurrentHashMap .newKeySet ();
131
122
private final Set <ImageHeapConstant > constantsToPersist = ConcurrentHashMap .newKeySet ();
132
123
133
- public void ensureTypePersisted (AnalysisType type ) {
134
- assert !sealed ;
135
- if (typesToPersist .add (type )) {
136
- afterTypeAdded (type );
137
- }
138
- }
139
-
140
- public void ensureMethodPersisted (AnalysisMethod method ) {
141
- assert !sealed ;
142
- if (methodsToPersist .add (method )) {
143
- afterMethodAdded (method );
144
- }
145
- }
146
-
147
- public void ensureFieldPersisted (AnalysisField field ) {
148
- assert !sealed ;
149
- fieldsToPersist .add (field );
150
- }
151
-
152
124
public void ensureConstantPersisted (ImageHeapConstant constant ) {
153
- assert !sealed ;
154
125
constantsToPersist .add (constant );
155
126
afterConstantAdded (constant );
156
127
}
@@ -162,7 +133,8 @@ protected record ConstantParent(int constantId, int index) {
162
133
private record FileInfo (Path layerFilePath , String fileName , String suffix ) {
163
134
}
164
135
165
- protected record MethodGraphsInfo (String analysisGraphLocation , boolean analysisGraphIsIntrinsic , String strengthenedGraphLocation ) {
136
+ protected record MethodGraphsInfo (String analysisGraphLocation , boolean analysisGraphIsIntrinsic ,
137
+ String strengthenedGraphLocation ) {
166
138
167
139
static final MethodGraphsInfo NO_GRAPHS = new MethodGraphsInfo (null , false , null );
168
140
@@ -252,6 +224,11 @@ public void setAnalysisUniverse(AnalysisUniverse aUniverse) {
252
224
this .aUniverse = aUniverse ;
253
225
}
254
226
227
+ @ SuppressWarnings ("unused" )
228
+ public void onTrackedAcrossLayer (AnalysisMethod method , Object reason ) {
229
+ imageLayerWriterHelper .onTrackedAcrossLayer (method , reason );
230
+ }
231
+
255
232
public void dumpFiles () {
256
233
graphsOutput .finish ();
257
234
@@ -291,6 +268,10 @@ public void persistAnalysisInfo() {
291
268
snapshotBuilder .setNextFieldId (aUniverse .getNextFieldId ());
292
269
snapshotBuilder .setNextConstantId (ImageHeapConstant .getCurrentId ());
293
270
271
+ List <AnalysisType > typesToPersist = aUniverse .getTypes ().stream ().filter (AnalysisType ::isTrackedAcrossLayers ).toList ();
272
+ List <AnalysisMethod > methodsToPersist = aUniverse .getMethods ().stream ().filter (AnalysisMethod ::isTrackedAcrossLayers ).toList ();
273
+ List <AnalysisField > fieldsToPersist = aUniverse .getFields ().stream ().filter (AnalysisField ::isTrackedAcrossLayers ).toList ();
274
+
294
275
initSortedList (snapshotBuilder ::initTypes , typesToPersist , Comparator .comparingInt (AnalysisType ::getId ), this ::persistType );
295
276
initSortedList (snapshotBuilder ::initMethods , methodsToPersist , Comparator .comparingInt (AnalysisMethod ::getId ), this ::persistMethod );
296
277
initSortedList (snapshotBuilder ::initFields , fieldsToPersist , Comparator .comparingInt (AnalysisField ::getId ), this ::persistField );
@@ -345,7 +326,7 @@ protected void persistHook() {
345
326
}
346
327
347
328
public boolean isTypePersisted (AnalysisType type ) {
348
- return typesToPersist . contains ( type );
329
+ return type . isTrackedAcrossLayers ( );
349
330
}
350
331
351
332
private void persistType (AnalysisType type , Supplier <PersistedAnalysisType .Builder > builderSupplier ) {
@@ -373,12 +354,6 @@ protected void persistType(AnalysisType type, String typeDescriptor, PersistedAn
373
354
if (enclosingType != null ) {
374
355
builder .setEnclosingTypeId (enclosingType .getId ());
375
356
}
376
- } catch (AnalysisError .TypeNotFoundError e ) {
377
- /*
378
- * GR-59571: The enclosing type is not automatically created when the inner type is
379
- * created. If the enclosing type is missing, it is ignored for now. This try/catch
380
- * block could be removed after the trackAcrossLayers is fully implemented.
381
- */
382
357
} catch (InternalError | TypeNotPresentException | LinkageError e ) {
383
358
/* Ignore missing type errors. */
384
359
}
@@ -399,6 +374,8 @@ protected void persistType(AnalysisType type, String typeDescriptor, PersistedAn
399
374
builder .setIsReachable (type .isReachable ());
400
375
401
376
imageLayerWriterHelper .persistType (type , builder );
377
+
378
+ afterTypeAdded (type );
402
379
}
403
380
404
381
protected static void initInts (IntFunction <PrimitiveList .Int .Builder > builderSupplier , IntStream ids ) {
@@ -417,31 +394,17 @@ protected static void initStringList(IntFunction<TextList.Builder> builderSuppli
417
394
}
418
395
}
419
396
397
+ @ SuppressWarnings ("unused" )
420
398
protected void afterTypeAdded (AnalysisType type ) {
421
- /*
422
- * Some persisted types are not reachable. In this case, the super class and interfaces have
423
- * to be persisted manually as well.
424
- */
425
- if (type .getSuperclass () != null ) {
426
- ensureTypePersisted (type .getSuperclass ());
427
- }
428
- for (AnalysisType iface : type .getInterfaces ()) {
429
- ensureTypePersisted (iface );
430
- }
431
- }
432
-
433
- protected void afterMethodAdded (AnalysisMethod method ) {
434
- ensureTypePersisted (method .getSignature ().getReturnType ());
435
- imageLayerWriterHelper .afterMethodAdded (method );
436
399
}
437
400
438
401
private void afterConstantAdded (ImageHeapConstant constant ) {
439
- ensureTypePersisted ( constant .getType ());
402
+ constant .getType (). registerAsTrackedAcrossLayers ( constant );
440
403
/* If this is a Class constant persist the corresponding type. */
441
404
ConstantReflectionProvider constantReflection = aUniverse .getBigbang ().getConstantReflectionProvider ();
442
405
AnalysisType typeFromClassConstant = (AnalysisType ) constantReflection .asJavaType (constant );
443
406
if (typeFromClassConstant != null ) {
444
- ensureTypePersisted ( typeFromClassConstant );
407
+ typeFromClassConstant . registerAsTrackedAcrossLayers ( constant );
445
408
}
446
409
}
447
410
@@ -535,31 +498,25 @@ public boolean isMethodPersisted(AnalysisMethod method) {
535
498
return methodsMap .containsKey (name );
536
499
}
537
500
538
- public void persistMethodGraphs () {
539
- assert aUniverse .sealed ();
540
-
541
- aUniverse .getTypes ().stream ().filter (AnalysisType ::isTrackedAcrossLayers )
542
- .forEach (this ::ensureTypePersisted );
543
-
544
- aUniverse .getMethods ().stream ().filter (AnalysisMethod ::isTrackedAcrossLayers )
545
- .forEach (this ::ensureMethodPersisted );
546
-
547
- aUniverse .getFields ().stream ().filter (AnalysisField ::isTrackedAcrossLayers )
548
- .forEach (this ::ensureFieldPersisted );
549
-
501
+ public void persistAnalysisParsedGraphs () {
550
502
// Persisting graphs discovers additional types, members and constants that need persisting
551
503
Set <AnalysisMethod > persistedGraphMethods = new HashSet <>();
504
+ boolean modified ;
552
505
do {
553
- for (AnalysisMethod method : methodsToPersist ) {
506
+ modified = false ;
507
+ /*
508
+ * GR-60503: It would be better to mark all the elements as trackedAcrossLayers before
509
+ * the end of the analysis and only iterate only once over all methods.
510
+ */
511
+ for (AnalysisMethod method : aUniverse .getMethods ().stream ().filter (AnalysisMethod ::isTrackedAcrossLayers ).toList ()) {
554
512
if (persistedGraphMethods .add (method )) {
513
+ modified = true ;
555
514
persistAnalysisParsedGraph (method );
556
515
}
557
516
}
558
- } while (! persistedGraphMethods . equals ( methodsToPersist ) );
517
+ } while (modified );
559
518
560
519
// Note that constants are scanned late so all values are available.
561
-
562
- sealed = true ;
563
520
}
564
521
565
522
private void persistAnalysisParsedGraph (AnalysisMethod method ) {
@@ -647,7 +604,9 @@ protected void persistField(AnalysisField field, PersistedAnalysisField.Builder
647
604
protected void persistConstant (ImageHeapConstant imageHeapConstant , ConstantParent parent , PersistedConstant .Builder builder , Set <Integer > constantsToRelink ) {
648
605
int id = getConstantId (imageHeapConstant );
649
606
builder .setId (id );
650
- builder .setTypeId (imageHeapConstant .getType ().getId ());
607
+ AnalysisType type = imageHeapConstant .getType ();
608
+ AnalysisError .guarantee (type .isTrackedAcrossLayers (), "Type %s from constant %s should have been marked as trackedAcrossLayers, but was not" , type , imageHeapConstant );
609
+ builder .setTypeId (type .getId ());
651
610
652
611
IdentityHashCodeProvider identityHashCodeProvider = (IdentityHashCodeProvider ) aUniverse .getBigbang ().getConstantReflectionProvider ();
653
612
int identityHashCode = identityHashCodeProvider .identityHashCode (imageHeapConstant );
0 commit comments