Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-60088] Add jdk.graal.nativeimage module. #10380

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4dfdd8e
use class literals where possible
dougxc Nov 4, 2024
4e98059
remove LibGraal dependency on java.management and some SVM internals
dougxc Nov 8, 2024
d927ed5
added jdk.graal.nativeimage module
dougxc Nov 4, 2024
4464f5a
added GlobalData API and re-implemented GlobalAtomicLong on it
dougxc Nov 4, 2024
f63555c
folded project com.oracle.svm.graal.hotspot into project com.oracle.s…
dougxc Nov 29, 2024
84b3037
removed use of com.oracle.svm.util in libgraal
dougxc Nov 30, 2024
7850342
tmp
dougxc Nov 30, 2024
9fdeb36
remove reference to SVM options
dougxc Dec 1, 2024
a851544
move libgraal into compiler
dougxc Dec 3, 2024
e27626a
remove reflection in non-truffle libgraal
dougxc Dec 8, 2024
77237b2
remove reflection in truffle libgraal
dougxc Dec 10, 2024
c5c0ceb
removed more reflection
dougxc Dec 23, 2024
c412d27
fix hotspot issue
dougxc Dec 29, 2024
cc5d561
putting jargraal on the --upgrade-module-path now also needs graal-na…
dougxc Jan 5, 2025
a68973a
fixed @since tags
dougxc Jan 6, 2025
8e423ef
moved LibGraalFeatureComponent out of jdk.graal.nativeimage and renam…
dougxc Jan 7, 2025
f754d76
moved NATIVE_IMAGE_SETTING_KEY_PREFIX from LibGraalRuntime to LibGraa…
dougxc Jan 7, 2025
b9c2f4a
moved LibGraalFeatureComponent out of jdk.graal.nativeimage and renam…
dougxc Jan 8, 2025
97af33c
reduce and better document jdk.graal.nativeimage API
dougxc Jan 8, 2025
9793c0d
added missing headers
dougxc Jan 8, 2025
7542c2f
added exception for importing libgraal into NodeClass and Fields
dougxc Jan 9, 2025
0a42666
added CHANGELOG entry
dougxc Jan 9, 2025
16d6066
removed references to non-existing org.graalvm.libgraal package
dougxc Jan 9, 2025
8de06d3
fixed checkstyle warnings and applied some ItelliJ tips
dougxc Jan 9, 2025
ab482b5
removed unnecessary exporting of internal packages
dougxc Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This changelog summarizes newly introduced optimizations and other compiler related changes.

## GraalVM for JDK 25 (Internal Version 25.0.0)
* (GR-60088): This PR adds the `jdk.graal.nativeimage` SDK module. With this module, all logic for building
libgraal has been moved into the core compiler. There is no dependency on Native Image internals. This
is required for Galahad CE where libgraal must be buildable from the Graal compiler sources in the OpenJDK
while using Native Image as an external tool.

## GraalVM for JDK 24 (Internal Version 24.2.0)
* (GR-57209): The default number of JVMCI threads is now the same as the number of C2 threads (`-XX:JVMCINativeLibraryThreadFraction=0.66`).
This benefits the program warmup but could increase the maximum RSS.
Expand Down
8 changes: 7 additions & 1 deletion compiler/mx.compiler/mx_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,12 @@ def _check_forbidden_imports(projects, package_substrings, exceptions=None):
def compiler_gate_runner(suites, unit_test_runs, bootstrap_tests, tasks, extraVMarguments=None, extraUnitTestArguments=None):
with Task('CheckForbiddenImports:Compiler', tasks, tags=['style']) as t:
# Ensure HotSpot-independent compiler classes do not import HotSpot-specific classes
if t: _check_forbidden_imports([mx.project('jdk.graal.compiler')], ('hotspot', 'libgraal'))
if t:
exceptions = {
'Fields.java': 'need to register libgraal feature components',
'NodeClass.java': 'need to register libgraal feature components'
}
_check_forbidden_imports([mx.project('jdk.graal.compiler')], ('hotspot', 'libgraal'), exceptions)

with Task('JDK_java_base_test', tasks, tags=['javabasetest'], report=True) as t:
if t: java_base_unittest(_remove_empty_entries(extraVMarguments) + [])
Expand Down Expand Up @@ -1483,6 +1488,7 @@ def _jvmci_jars():
return [
'compiler:GRAAL',
'compiler:GRAAL_MANAGEMENT',
'compiler:GRAAL_NATIVEIMAGE',
]

# The community compiler component
Expand Down
50 changes: 47 additions & 3 deletions compiler/mx.compiler/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,27 @@

# ------------- Graal -------------

# Native Image API extensions for libgraal.
"jdk.graal.nativeimage" : {
"subDir" : "src",
"sourceDirs" : ["src"],
"dependencies" : [
"sdk:NATIVEIMAGE"
],
"checkstyle" : "jdk.graal.compiler",
"javaCompliance" : "21+"
},

"jdk.graal.compiler" : {
"subDir" : "src",
"sourceDirs" : ["src"],
"dependencies" : [
"GRAAL_NATIVEIMAGE",
"sdk:JNIUTILS",
"sdk:WORD",
"sdk:COLLECTIONS",
"sdk:NATIVEIMAGE",
"sdk:NATIVEBRIDGE",
"truffle:TRUFFLE_COMPILER",
],
"requires" : [
Expand All @@ -179,6 +193,7 @@
],
"requiresConcealed" : {
"java.base" : [
"jdk.internal.module",
"jdk.internal.misc"
],
"jdk.internal.vm.ci" : [
Expand Down Expand Up @@ -209,7 +224,8 @@
"jdk.graal.compiler.truffle.substitutions.GraphDecoderInvocationPluginProvider"
],
"annotationProcessors" : [
"GRAAL_PROCESSOR"
"GRAAL_PROCESSOR",
"truffle:TRUFFLE_LIBGRAAL_PROCESSOR",
],
"checkPackagePrefix": "false",
"checkstyleVersion" : "10.21.0",
Expand All @@ -218,7 +234,6 @@
"jacoco" : "include",
"jacocoExcludePackages" : [
"jdk.graal.compiler.test",
"org.graalvm.libgraal.jni",
"jdk.graal.compiler.replacements",
"jdk.graal.compiler.hotspot.test",
"jdk.graal.compiler.replacements.test",
Expand Down Expand Up @@ -478,7 +493,7 @@
"workingSets" : "Graal",
"javaCompliance" : "21+",
"dependencies" : [
"jdk.graal.compiler",
"jdk.graal.nativeimage",
],
"requiresConcealed" : {
"java.base" : [
Expand Down Expand Up @@ -552,6 +567,32 @@
"maven": False,
},

"GRAAL_NATIVEIMAGE" : {
"subDir" : "src",
"dependencies" : [
"jdk.graal.nativeimage",
],
"distDependencies" : ["sdk:NATIVEIMAGE"],
"javadocType": "api",
"moduleInfo" : {
"name" : "jdk.graal.nativeimage",
"requires" : [
"transitive org.graalvm.nativeimage",
],
"exports" : [
"jdk.graal.nativeimage",
"jdk.graal.nativeimage.hosted",
"jdk.graal.nativeimage.impl to org.graalvm.nativeimage.builder",
],
"uses" : [],
"opens" : [],
},
"description" : "Native Image API extensions for libgraal.",
"maven": {
"tag": ["default", "public"],
},
},

"GRAAL" : {
# This distribution defines a module.
"moduleInfo" : {
Expand Down Expand Up @@ -603,9 +644,12 @@
"GRAAL_VERSION",
],
"distDependencies" : [
"GRAAL_NATIVEIMAGE",
"sdk:COLLECTIONS",
"sdk:WORD",
"sdk:NATIVEIMAGE",
"sdk:NATIVEBRIDGE",
"sdk:JNIUTILS",
"truffle:TRUFFLE_COMPILER",
],
"allowsJavadocWarnings": True,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 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
Expand All @@ -22,7 +22,15 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.graal.compiler.hotspot.libgraal;
package jdk.graal.compiler.libgraal.loader;

import jdk.graal.nativeimage.LibGraalLoader;
import jdk.internal.jimage.BasicImageReader;
import jdk.internal.jimage.ImageLocation;
import jdk.internal.module.Modules;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.Feature;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -46,24 +54,17 @@
import java.util.Objects;
import java.util.Set;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.Feature;

import jdk.graal.compiler.debug.GraalError;
import jdk.internal.jimage.BasicImageReader;
import jdk.internal.jimage.ImageLocation;
import jdk.internal.module.Modules;

/**
* A classloader, that reads class files and resources from a jimage file at image build time.
* A classloader that reads class files and resources from a jimage file at image build time.
*/
@SuppressWarnings("unused")
@Platforms(Platform.HOSTED_ONLY.class)
final class HostedLibGraalClassLoader extends ClassLoader implements LibGraalClassLoaderBase {
public final class HostedLibGraalClassLoader extends ClassLoader implements LibGraalLoader {

private static final String JAVA_HOME_PROPERTY_KEY = "jdk.graal.internal.libgraal.javahome";
private static final String JAVA_HOME_PROPERTY_VALUE = System.getProperty(JAVA_HOME_PROPERTY_KEY, System.getProperty("java.home"));
/**
* Name of the system property specifying the {@code java.home} of the JDK whose runtime image
* contains the Graal and JVMCI classes from which libgraal will be built.
*/
private static final String LIBGRAAL_JAVA_HOME_PROPERTY_NAME = "libgraal.java.home";

/**
* Reader for the image.
Expand All @@ -75,12 +76,6 @@ final class HostedLibGraalClassLoader extends ClassLoader implements LibGraalCla
*/
private final Map<String, String> resources = new HashMap<>();

/**
* Map from the {@linkplain Class#forName(String) name} of a class to the image path of its
* class file.
*/
private final Map<String, String> classes;

/**
* Map from a service name to a list of providers.
*/
Expand All @@ -93,12 +88,18 @@ final class HostedLibGraalClassLoader extends ClassLoader implements LibGraalCla
private final Map<String, String> modules;

/**
* Modules in which Graal classes and their dependencies are defined.
* Modules in which Graal and JVMCI classes are defined.
*/
private static final Set<String> LIBGRAAL_MODULES = Set.of(
"jdk.internal.vm.ci",
"org.graalvm.collections",
"org.graalvm.word",
"jdk.graal.compiler",
"org.graalvm.truffle.compiler",
"com.oracle.graal.graal_enterprise");

/**
* Modules containing classes that can be annotated by {@code LibGraalService}.
*/
private static final Set<String> LIBGRAAL_SERVICES_MODULES = Set.of(
"jdk.graal.compiler",
"org.graalvm.truffle.compiler",
"com.oracle.graal.graal_enterprise");
Expand All @@ -107,11 +108,16 @@ final class HostedLibGraalClassLoader extends ClassLoader implements LibGraalCla
ClassLoader.registerAsParallelCapable();
}

public final Path libGraalJavaHome;
private final Path libgraalJavaHome = Path.of(System.getProperty(LIBGRAAL_JAVA_HOME_PROPERTY_NAME, System.getProperty("java.home")));

@Override
public Path getJavaHome() {
return libgraalJavaHome;
}

@SuppressWarnings("unused")
public HostedLibGraalClassLoader() {
super(LibGraalClassLoader.LOADER_NAME, Feature.class.getClassLoader());
libGraalJavaHome = Path.of(JAVA_HOME_PROPERTY_VALUE);

try {
/*
Expand All @@ -129,9 +135,8 @@ public HostedLibGraalClassLoader() {
Modules.addExports(javaBaseModule, "jdk.internal.misc", unnamedModuleOfThisLoader);

Map<String, String> modulesMap = new HashMap<>();
Map<String, String> classesMap = new HashMap<>();

Path imagePath = libGraalJavaHome.resolve(Path.of("lib", "modules"));
Path imagePath = libgraalJavaHome.resolve(Path.of("lib", "modules"));
this.imageReader = BasicImageReader.open(imagePath);
for (var entry : imageReader.getEntryNames()) {
int secondSlash = entry.indexOf('/', 1);
Expand All @@ -148,7 +153,6 @@ public HostedLibGraalClassLoader() {
services.computeIfAbsent(p.service(), k -> new ArrayList<>()).addAll(p.providers());
}
} else {
classesMap.put(className, entry);
modulesMap.put(className, module);
}
}
Expand All @@ -157,28 +161,29 @@ public HostedLibGraalClassLoader() {
}

modules = Map.copyOf(modulesMap);
classes = Map.copyOf(classesMap);

} catch (IOException e) {
throw GraalError.shouldNotReachHere(e);
throw new RuntimeException(e);
}
}

/**
* Gets an unmodifiable map from the {@linkplain Class#forName(String) name} of a class to the
* name of its enclosing module.
*/
@Override
public Map<String, String> getModules() {
public Map<String, String> getModuleMap() {
return modules;
}

/* Allow image builder to perform registration action on each class this loader provides. */

@Override
public Set<String> getAllClassNames() {
return classes.keySet();
public Set<String> getServicesModules() {
return LIBGRAAL_SERVICES_MODULES;
}

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (!classes.containsKey(name)) {
if (!modules.containsKey(name)) {
return super.loadClass(name, resolve);
}
synchronized (getClassLoadingLock(name)) {
Expand Down Expand Up @@ -261,7 +266,7 @@ protected Enumeration<URL> findResources(String name) throws IOException {

/**
* A {@link URLStreamHandler} for use with URLs returned by
* {@link HostedLibGraalClassLoader#findResource(java.lang.String)}.
* {@link HostedLibGraalClassLoader#findResource(String)}.
*/
private class ImageURLStreamHandler extends URLStreamHandler {
@Override
Expand Down Expand Up @@ -318,22 +323,7 @@ public String getContentType() {
}

@Override
public HostedLibGraalClassLoader getClassLoader() {
return this;
}

@Override
public LibGraalClassLoader getRuntimeClassLoader() {
public ClassLoader getRuntimeClassLoader() {
return LibGraalClassLoader.singleton;
}
}

public final class LibGraalClassLoader extends ClassLoader {

static final String LOADER_NAME = "LibGraalClassLoader";
static final LibGraalClassLoader singleton = new LibGraalClassLoader();

private LibGraalClassLoader() {
super(LOADER_NAME, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2024, 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 jdk.graal.compiler.libgraal.loader;

import org.graalvm.nativeimage.hosted.Feature.DuringSetupAccess;

/**
* The image runtime class loader that {@linkplain DuringSetupAccess#registerObjectReplacer
* replaces} the build-time instance of {@link HostedLibGraalClassLoader}.
*/
public final class LibGraalClassLoader extends ClassLoader {

static final String LOADER_NAME = "LibGraalClassLoader";
static final LibGraalClassLoader singleton = new LibGraalClassLoader();

private LibGraalClassLoader() {
super(LOADER_NAME, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void onStuckCompilation(CompilationWatchDog watchDog, Thread watched, Com

};

CompilationWatchDog watch = CompilationWatchDog.watch(compilation, options, false, longCompilationHandler);
CompilationWatchDog watch = CompilationWatchDog.watch(compilation, options, false, longCompilationHandler, null);
try (CompilationWatchDog watchScope = watch) {
event("start compiling");
try (TTY.Filter f = new TTY.Filter()) {
Expand Down
Loading
Loading