Skip to content

Commit

Permalink
[WIP] [GR-54456] Do not attempt constant folding of instance fields w…
Browse files Browse the repository at this point in the history
…hose class is initialized at run time.

PullRequest: graal/17898
  • Loading branch information
Christian Wimmer committed Jun 5, 2024
2 parents 9f1232f + ea0f1b0 commit bb21ab1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ public boolean isReachable() {
return isReachableHandle.get(constantData) != null;
}

public boolean allowConstantFolding() {
/*
* An object whose type is initialized at run time does not have hosted field values. Only
* simulated objects can be used for constant folding.
*/
return constantData.type.isInitialized() || constantData.hostedObject == null;
}

public Object getReachableReason() {
return constantData.isReachable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,21 @@ private ImageHeapInstance createImageHeapInstance(JavaConstant constant, Analysi
for (ResolvedJavaField javaField : instanceFields) {
AnalysisField field = (AnalysisField) javaField;
ValueSupplier<JavaConstant> rawFieldValue;
try {
rawFieldValue = readHostedFieldValue(field, constant);
} catch (InternalError | TypeNotPresentException | LinkageError e) {
/* Ignore missing type errors. */
continue;
if (!type.isInitialized()) {
/*
* We cannot read the hosted value of an object whose type is initialized at run
* time. If the object is marked as reachable later on, it will be reported as
* an unsupported feature. But we must not fail here earlier with an internal
* error.
*/
rawFieldValue = ValueSupplier.lazyValue(() -> null, () -> false);
} else {
try {
rawFieldValue = readHostedFieldValue(field, constant);
} catch (InternalError | TypeNotPresentException | LinkageError e) {
/* Ignore missing type errors. */
continue;
}
}
hostedFieldValues[field.getPosition()] = new AnalysisFuture<>(() -> {
ScanReason fieldReason = new FieldScan(field, instance, reason);
Expand Down Expand Up @@ -578,7 +588,7 @@ protected void onObjectReachable(ImageHeapConstant imageHeapConstant, ScanReason
/* Enhance the unsupported feature message with the object trace and rethrow. */
StringBuilder backtrace = new StringBuilder();
ObjectScanner.buildObjectBacktrace(bb, reason, backtrace);
throw new UnsupportedFeatureException(e.getMessage() + System.lineSeparator() + backtrace);
throw new UnsupportedFeatureException(e.getMessage() + System.lineSeparator() + backtrace, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

import com.oracle.graal.pointsto.heap.ImageHeapConstant;
import com.oracle.graal.pointsto.infrastructure.UniverseMetaAccess;
import com.oracle.graal.pointsto.meta.AnalysisField;
import com.oracle.svm.core.meta.MethodPointer;
Expand Down Expand Up @@ -56,7 +57,7 @@ public SharedConstantFieldProvider(MetaAccessProvider metaAccess, SVMHost hostVM

@Override
public boolean isFinalField(ResolvedJavaField field, ConstantFieldTool<?> tool) {
return super.isFinalField(field, tool) && allowConstantFolding(field);
return super.isFinalField(field, tool) && allowConstantFolding(field, tool);
}

@Override
Expand All @@ -72,12 +73,21 @@ public boolean isStableField(ResolvedJavaField field, ConstantFieldTool<?> tool)
} else {
stable = super.isStableField(field, tool);
}
return stable && allowConstantFolding(field);
return stable && allowConstantFolding(field, tool);
}

private boolean allowConstantFolding(ResolvedJavaField field) {
private boolean allowConstantFolding(ResolvedJavaField field, ConstantFieldTool<?> tool) {
var aField = asAnalysisField(field);

/*
* During compiler optimizations, it is possible to see field loads with a constant receiver
* of a wrong type that might not even be an ImageHeapConstant. Also, we need to ensure that
* the ImageHeapConstant allows constant folding of its fields.
*/
if (!field.isStatic() && (!(tool.getReceiver() instanceof ImageHeapConstant receiver) || !receiver.allowConstantFolding())) {
return false;
}

/*
* This code should run as late as possible, because it has side effects. So we only do it
* after we have already checked that the field is `final` or `stable`. It marks the
Expand Down

0 comments on commit bb21ab1

Please sign in to comment.