From 4d1c860e2b97492ef43fa6f4db9cbebb76f78f80 Mon Sep 17 00:00:00 2001 From: Christian Haeubl Date: Tue, 4 Mar 2025 09:27:03 +0100 Subject: [PATCH] Disable libgraal-specific API if it isn't needed. --- .../svm/core/libgraal/LibGraalBuild.java | 36 +++++++++++++++++++ .../libgraal/LibGraalRuntimeSupportImpl.java | 7 ++-- .../graal/hosted/GraalCompilerFeature.java | 19 +++++++--- .../svm/hosted/GlobalDataSupportImpl.java | 7 ++-- 4 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/libgraal/LibGraalBuild.java diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/libgraal/LibGraalBuild.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/libgraal/LibGraalBuild.java new file mode 100644 index 000000000000..9b8fd93eeff7 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/libgraal/LibGraalBuild.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.svm.core.libgraal; + +import java.util.function.BooleanSupplier; + +import com.oracle.svm.core.SubstrateOptions; + +public class LibGraalBuild implements BooleanSupplier { + @Override + public boolean getAsBoolean() { + return !SubstrateOptions.LibGraalClassLoader.getValue().isBlank(); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/libgraal/LibGraalRuntimeSupportImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/libgraal/LibGraalRuntimeSupportImpl.java index 67c1cddf93fd..b4a12f213c5f 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/libgraal/LibGraalRuntimeSupportImpl.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/libgraal/LibGraalRuntimeSupportImpl.java @@ -24,15 +24,14 @@ */ package com.oracle.svm.core.libgraal; -import com.oracle.svm.core.Isolates; +import org.graalvm.nativeimage.libgraal.impl.LibGraalRuntimeSupport; +import com.oracle.svm.core.Isolates; import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton; import com.oracle.svm.core.heap.Heap; import com.oracle.svm.core.util.VMError; -import org.graalvm.nativeimage.libgraal.impl.LibGraalRuntimeSupport; - -@AutomaticallyRegisteredImageSingleton({LibGraalRuntimeSupport.class}) +@AutomaticallyRegisteredImageSingleton(value = LibGraalRuntimeSupport.class, onlyWith = LibGraalBuild.class) public final class LibGraalRuntimeSupportImpl implements LibGraalRuntimeSupport { @Override diff --git a/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/GraalCompilerFeature.java b/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/GraalCompilerFeature.java index b659e1c70cf4..3d9cc0ab0bad 100644 --- a/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/GraalCompilerFeature.java +++ b/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/GraalCompilerFeature.java @@ -28,20 +28,21 @@ import java.util.List; import java.util.function.BooleanSupplier; -import com.oracle.svm.util.ReflectionUtil; -import jdk.graal.compiler.serviceprovider.GlobalAtomicLong; -import org.graalvm.nativeimage.libgraal.hosted.GlobalData; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.hosted.Feature; import org.graalvm.nativeimage.hosted.FieldValueTransformer; import org.graalvm.nativeimage.hosted.RuntimeReflection; +import com.oracle.svm.core.c.GlobalLongSupplier; import com.oracle.svm.core.feature.InternalFeature; import com.oracle.svm.core.graal.GraalConfiguration; +import com.oracle.svm.core.graal.RuntimeCompilation; import com.oracle.svm.graal.GraalCompilerSupport; import com.oracle.svm.hosted.FeatureImpl; +import com.oracle.svm.util.ReflectionUtil; import jdk.graal.compiler.debug.DebugContext; +import jdk.graal.compiler.serviceprovider.GlobalAtomicLong; import jdk.vm.ci.meta.JavaKind; /** @@ -64,8 +65,11 @@ public List> getRequiredFeatures() { @Override public void duringSetup(DuringSetupAccess c) { - ImageSingletons.add(GraalCompilerSupport.class, new GraalCompilerSupport()); + if (!RuntimeCompilation.isEnabled()) { + return; + } + ImageSingletons.add(GraalCompilerSupport.class, new GraalCompilerSupport()); ((FeatureImpl.DuringSetupAccessImpl) c).registerClassReachabilityListener(GraalCompilerSupport::registerPhaseStatistics); } @@ -76,12 +80,17 @@ void register(BeforeAnalysisAccess access) { @Override public Object transform(Object receiver, Object originalValue) { - return GlobalData.createGlobal(((GlobalAtomicLong) receiver).getInitialValue()); + long initialValue = ((GlobalAtomicLong) receiver).getInitialValue(); + return new GlobalLongSupplier(initialValue); } } @Override public void beforeAnalysis(BeforeAnalysisAccess c) { + if (!RuntimeCompilation.isEnabled()) { + return; + } + DebugContext debug = DebugContext.forCurrentThread(); new GlobalAtomicLongTransformer().register(c); diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/GlobalDataSupportImpl.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/GlobalDataSupportImpl.java index 8a8d427789c0..7fa7d92b9c5d 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/GlobalDataSupportImpl.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/GlobalDataSupportImpl.java @@ -26,12 +26,13 @@ import java.util.function.Supplier; +import org.graalvm.nativeimage.libgraal.impl.GlobalDataSupport; + import com.oracle.svm.core.c.GlobalLongSupplier; import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton; +import com.oracle.svm.core.libgraal.LibGraalBuild; -import org.graalvm.nativeimage.libgraal.impl.GlobalDataSupport; - -@AutomaticallyRegisteredImageSingleton(GlobalDataSupport.class) +@AutomaticallyRegisteredImageSingleton(value = GlobalDataSupport.class, onlyWith = LibGraalBuild.class) public final class GlobalDataSupportImpl implements GlobalDataSupport { @Override public Supplier createGlobal(long initialValue) {