Skip to content

Commit

Permalink
Reduce libgraal image size by removing other architectures
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrodriguez committed Mar 13, 2020
1 parent 2988f47 commit ee1d95e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.nio.file.Files;
Expand Down Expand Up @@ -125,14 +126,17 @@
import com.oracle.svm.hosted.ImageClassLoader;
import com.oracle.svm.jni.hosted.JNIFeature;
import com.oracle.svm.reflect.hosted.ReflectionFeature;
import com.oracle.svm.util.ReflectionUtil;

import jdk.vm.ci.common.NativeImageReinitialize;
import jdk.vm.ci.hotspot.HotSpotJVMCIBackendFactory;
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
import jdk.vm.ci.hotspot.HotSpotSignature;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.services.Services;

public final class LibGraalFeature implements com.oracle.svm.core.graal.GraalFeature {

Expand Down Expand Up @@ -402,7 +406,7 @@ private void registerMethodSubstitutions(DebugContext debug, InvocationPlugins i
}
}

@SuppressWarnings("try")
@SuppressWarnings({"try", "unchecked"})
@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {

Expand All @@ -427,6 +431,30 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
} catch (Throwable t) {
throw debug.handle(t);
}

try {
HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) HotSpotJVMCIRuntime.runtime().getCompiler();
String osArch = compiler.getGraalRuntime().getVMConfig().osArch;
String archPackage = "." + osArch + ".";

final Field servicesCacheField = ReflectionUtil.lookupField(Services.class, "servicesCache");
Map<Class<Object>, List<Object>> services = (Map<Class<Object>, List<Object>>) servicesCacheField.get(null);
for (List<Object> list : services.values()) {
list.removeIf(o -> {
String name = o.getClass().getName();
if (name.contains(".aarch64.") || name.contains(".sparc.") || name.contains(".amd64.")) {
return !name.contains(archPackage);
}
return false;
});
}

Field cachedHotSpotJVMCIBackendFactoriesField = ReflectionUtil.lookupField(HotSpotJVMCIRuntime.class, "cachedHotSpotJVMCIBackendFactories");
List<HotSpotJVMCIBackendFactory> cachedHotSpotJVMCIBackendFactories = (List<HotSpotJVMCIBackendFactory>) cachedHotSpotJVMCIBackendFactoriesField.get(null);
cachedHotSpotJVMCIBackendFactories.removeIf(factory -> !factory.getArchitecture().equalsIgnoreCase(osArch));
} catch (ReflectiveOperationException ex) {
throw VMError.shouldNotReachHere(ex);
}
}

@SuppressWarnings("try")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ final class Target_org_graalvm_compiler_debug_KeyRegistry {
private static List<MetricKey> keys = new ArrayList<>();
}

@TargetClass(value = org.graalvm.compiler.core.match.MatchRuleRegistry.class, onlyWith = GraalFeature.IsEnabledAndNotLibgraal.class)
@TargetClass(value = org.graalvm.compiler.core.match.MatchRuleRegistry.class, onlyWith = GraalFeature.IsEnabled.class)
final class Target_org_graalvm_compiler_core_match_MatchRuleRegistry {

@Substitute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
import org.graalvm.compiler.api.runtime.GraalRuntime;
import org.graalvm.compiler.core.common.spi.ForeignCallsProvider;
import org.graalvm.compiler.hotspot.HotSpotBackendFactory;
import org.graalvm.compiler.nodes.FieldLocationIdentity;
import org.graalvm.nativeimage.c.function.RelocatedPointer;
import org.graalvm.nativeimage.hosted.Feature.CompilationAccess;
Expand Down Expand Up @@ -65,6 +66,7 @@
import com.oracle.svm.hosted.meta.HostedType;
import com.oracle.svm.hosted.meta.HostedUniverse;

import jdk.vm.ci.code.Architecture;
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
import jdk.vm.ci.hotspot.HotSpotResolvedJavaField;
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
Expand Down Expand Up @@ -125,6 +127,12 @@ public Object apply(Object source) {
dest = providerReplacements.getMetaAccessProvider();
} else if (source instanceof HotSpotJVMCIRuntime) {
throw new UnsupportedFeatureException("HotSpotJVMCIRuntime should not appear in the image: " + source);
} else if (source instanceof HotSpotBackendFactory) {
HotSpotBackendFactory factory = (HotSpotBackendFactory) source;
Architecture hostArch = HotSpotJVMCIRuntime.runtime().getHostJVMCIBackend().getTarget().arch;
if (!factory.getArchitecture().equals(hostArch.getClass())) {
throw new UnsupportedFeatureException("non host archtecture HotSpotBackendFactory should not appear in the image: " + source);
}
} else if (source instanceof GraalRuntime) {
dest = sGraalRuntime;
} else if (source instanceof AnalysisConstantReflectionProvider) {
Expand Down

0 comments on commit ee1d95e

Please sign in to comment.