43972
|
1 |
/*
|
54328
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.hotspot;
|
|
26 |
|
44836
|
27 |
import static jdk.vm.ci.common.InitTimer.timer;
|
54328
|
28 |
import static jdk.vm.ci.hotspot.HotSpotJVMCICompilerFactory.CompilationLevelAdjustment.None;
|
54084
|
29 |
import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
|
54328
|
30 |
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
|
46344
|
31 |
import static org.graalvm.compiler.hotspot.HotSpotGraalOptionValues.GRAAL_OPTION_PROPERTY_PREFIX;
|
43972
|
32 |
|
|
33 |
import java.io.PrintStream;
|
|
34 |
|
49873
|
35 |
import org.graalvm.compiler.api.runtime.GraalRuntime;
|
43972
|
36 |
import org.graalvm.compiler.debug.MethodFilter;
|
|
37 |
import org.graalvm.compiler.options.Option;
|
46344
|
38 |
import org.graalvm.compiler.options.OptionKey;
|
43972
|
39 |
import org.graalvm.compiler.options.OptionType;
|
46344
|
40 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
41 |
import org.graalvm.compiler.options.OptionsParser;
|
|
42 |
import org.graalvm.compiler.phases.tiers.CompilerConfiguration;
|
|
43 |
|
|
44 |
import jdk.vm.ci.common.InitTimer;
|
44836
|
45 |
import jdk.vm.ci.hotspot.HotSpotJVMCICompilerFactory;
|
43972
|
46 |
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
|
54328
|
47 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
|
|
48 |
import jdk.vm.ci.meta.Signature;
|
43972
|
49 |
import jdk.vm.ci.runtime.JVMCIRuntime;
|
54328
|
50 |
import jdk.vm.ci.services.Services;
|
43972
|
51 |
|
|
52 |
public final class HotSpotGraalCompilerFactory extends HotSpotJVMCICompilerFactory {
|
|
53 |
|
|
54 |
private static MethodFilter[] graalCompileOnlyFilter;
|
46371
|
55 |
private static boolean compileGraalWithC1Only;
|
43972
|
56 |
|
49873
|
57 |
private IsGraalPredicate isGraalPredicate;
|
49451
|
58 |
|
43972
|
59 |
private final HotSpotGraalJVMCIServiceLocator locator;
|
|
60 |
|
|
61 |
HotSpotGraalCompilerFactory(HotSpotGraalJVMCIServiceLocator locator) {
|
|
62 |
this.locator = locator;
|
|
63 |
}
|
|
64 |
|
|
65 |
@Override
|
|
66 |
public String getCompilerName() {
|
|
67 |
return "graal";
|
|
68 |
}
|
|
69 |
|
46344
|
70 |
/**
|
|
71 |
* Initialized when this factory is {@linkplain #onSelection() selected}.
|
|
72 |
*/
|
|
73 |
private OptionValues options;
|
|
74 |
|
43972
|
75 |
@Override
|
|
76 |
public void onSelection() {
|
54328
|
77 |
JVMCIVersionCheck.check(Services.getSavedProperties(), false);
|
46344
|
78 |
assert options == null : "cannot select " + getClass() + " service more than once";
|
54084
|
79 |
options = HotSpotGraalOptionValues.defaultOptions();
|
46371
|
80 |
initializeGraalCompilePolicyFields(options);
|
49873
|
81 |
isGraalPredicate = compileGraalWithC1Only ? new IsGraalPredicate() : null;
|
46371
|
82 |
/*
|
|
83 |
* Exercise this code path early to encourage loading now. This doesn't solve problem of
|
|
84 |
* deadlock during class loading but seems to eliminate it in practice.
|
|
85 |
*/
|
54328
|
86 |
if (isGraalPredicate != null && isGraalPredicate.getCompilationLevelAdjustment() != None) {
|
|
87 |
adjustCompilationLevelInternal(Object.class, CompilationLevel.FullOptimization);
|
|
88 |
adjustCompilationLevelInternal(Object.class, CompilationLevel.Simple);
|
|
89 |
}
|
54084
|
90 |
if (IS_BUILDING_NATIVE_IMAGE) {
|
|
91 |
// Triggers initialization of all option descriptors
|
|
92 |
Options.CompileGraalWithC1Only.getName();
|
|
93 |
}
|
46344
|
94 |
}
|
|
95 |
|
46371
|
96 |
private static void initializeGraalCompilePolicyFields(OptionValues options) {
|
54328
|
97 |
compileGraalWithC1Only = Options.CompileGraalWithC1Only.getValue(options) && !IS_IN_NATIVE_IMAGE;
|
46344
|
98 |
String optionValue = Options.GraalCompileOnly.getValue(options);
|
|
99 |
if (optionValue != null) {
|
|
100 |
MethodFilter[] filter = MethodFilter.parse(optionValue);
|
|
101 |
if (filter.length == 0) {
|
|
102 |
filter = null;
|
|
103 |
}
|
|
104 |
graalCompileOnlyFilter = filter;
|
|
105 |
}
|
43972
|
106 |
}
|
|
107 |
|
|
108 |
@Override
|
|
109 |
public void printProperties(PrintStream out) {
|
|
110 |
out.println("[Graal properties]");
|
46344
|
111 |
options.printHelp(OptionsParser.getOptionsLoader(), out, GRAAL_OPTION_PROPERTY_PREFIX);
|
43972
|
112 |
}
|
|
113 |
|
|
114 |
static class Options {
|
|
115 |
|
|
116 |
// @formatter:off
|
|
117 |
@Option(help = "In tiered mode compile Graal and JVMCI using optimized first tier code.", type = OptionType.Expert)
|
46344
|
118 |
public static final OptionKey<Boolean> CompileGraalWithC1Only = new OptionKey<>(true);
|
43972
|
119 |
|
49451
|
120 |
@Option(help = "A filter applied to a method the VM has selected for compilation by Graal. " +
|
|
121 |
"A method not matching the filter is redirected to a lower tier compiler. " +
|
|
122 |
"The filter format is the same as for the MethodFilter option.", type = OptionType.Expert)
|
46344
|
123 |
public static final OptionKey<String> GraalCompileOnly = new OptionKey<>(null);
|
43972
|
124 |
// @formatter:on
|
|
125 |
|
|
126 |
}
|
|
127 |
|
|
128 |
@Override
|
|
129 |
public HotSpotGraalCompiler createCompiler(JVMCIRuntime runtime) {
|
49451
|
130 |
CompilerConfigurationFactory factory = CompilerConfigurationFactory.selectFactory(null, options);
|
49873
|
131 |
if (isGraalPredicate != null) {
|
54328
|
132 |
isGraalPredicate.onCompilerConfigurationFactorySelection((HotSpotJVMCIRuntime) runtime, factory);
|
49451
|
133 |
}
|
49873
|
134 |
HotSpotGraalCompiler compiler = createCompiler("VM", runtime, options, factory);
|
43972
|
135 |
// Only the HotSpotGraalRuntime associated with the compiler created via
|
|
136 |
// jdk.vm.ci.runtime.JVMCIRuntime.getCompiler() is registered for receiving
|
|
137 |
// VM events.
|
|
138 |
locator.onCompilerCreation(compiler);
|
|
139 |
return compiler;
|
|
140 |
}
|
|
141 |
|
|
142 |
/**
|
|
143 |
* Creates a new {@link HotSpotGraalRuntime} object and a new {@link HotSpotGraalCompiler} and
|
|
144 |
* returns the latter.
|
|
145 |
*
|
49873
|
146 |
* @param runtimeNameQualifier a qualifier to be added to the {@linkplain GraalRuntime#getName()
|
|
147 |
* name} of the {@linkplain HotSpotGraalCompiler#getGraalRuntime() runtime} created
|
|
148 |
* by this method
|
43972
|
149 |
* @param runtime the JVMCI runtime on which the {@link HotSpotGraalRuntime} is built
|
|
150 |
* @param compilerConfigurationFactory factory for the {@link CompilerConfiguration}
|
|
151 |
*/
|
|
152 |
@SuppressWarnings("try")
|
49873
|
153 |
public static HotSpotGraalCompiler createCompiler(String runtimeNameQualifier, JVMCIRuntime runtime, OptionValues options, CompilerConfigurationFactory compilerConfigurationFactory) {
|
43972
|
154 |
HotSpotJVMCIRuntime jvmciRuntime = (HotSpotJVMCIRuntime) runtime;
|
|
155 |
try (InitTimer t = timer("HotSpotGraalRuntime.<init>")) {
|
49873
|
156 |
HotSpotGraalRuntime graalRuntime = new HotSpotGraalRuntime(runtimeNameQualifier, jvmciRuntime, compilerConfigurationFactory, options);
|
46536
|
157 |
return new HotSpotGraalCompiler(jvmciRuntime, graalRuntime, graalRuntime.getOptions());
|
43972
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
@Override
|
|
162 |
public CompilationLevelAdjustment getCompilationLevelAdjustment() {
|
54328
|
163 |
return isGraalPredicate != null ? isGraalPredicate.getCompilationLevelAdjustment() : None;
|
43972
|
164 |
}
|
|
165 |
|
|
166 |
@Override
|
50858
|
167 |
public CompilationLevel adjustCompilationLevel(Object declaringClassObject, String name, String signature, boolean isOsr, CompilationLevel level) {
|
|
168 |
Class<?> declaringClass = (Class<?>) declaringClassObject;
|
54328
|
169 |
return adjustCompilationLevelInternal(declaringClass, level);
|
43972
|
170 |
}
|
|
171 |
|
46344
|
172 |
static {
|
|
173 |
// Fail-fast detection for package renaming to guard use of package
|
|
174 |
// prefixes in adjustCompilationLevelInternal.
|
46459
|
175 |
assert jdk.vm.ci.services.Services.class.getName().equals("jdk.vm.ci.services.Services");
|
46344
|
176 |
assert HotSpotGraalCompilerFactory.class.getName().equals("org.graalvm.compiler.hotspot.HotSpotGraalCompilerFactory");
|
|
177 |
}
|
|
178 |
|
54328
|
179 |
private CompilationLevel adjustCompilationLevelInternal(Class<?> declaringClass, CompilationLevel level) {
|
|
180 |
assert isGraalPredicate != null;
|
|
181 |
if (level.ordinal() > CompilationLevel.Simple.ordinal()) {
|
|
182 |
if (isGraalPredicate.apply(declaringClass)) {
|
43972
|
183 |
return CompilationLevel.Simple;
|
|
184 |
}
|
|
185 |
}
|
|
186 |
return level;
|
|
187 |
}
|
54328
|
188 |
|
|
189 |
static boolean shouldExclude(HotSpotResolvedJavaMethod method) {
|
|
190 |
if (graalCompileOnlyFilter != null) {
|
|
191 |
String javaClassName = method.getDeclaringClass().toJavaName();
|
|
192 |
String name = method.getName();
|
|
193 |
Signature signature = method.getSignature();
|
|
194 |
for (MethodFilter filter : graalCompileOnlyFilter) {
|
|
195 |
if (filter.matches(javaClassName, name, signature)) {
|
|
196 |
return false;
|
|
197 |
}
|
|
198 |
}
|
|
199 |
return true;
|
|
200 |
}
|
|
201 |
return false;
|
|
202 |
}
|
43972
|
203 |
}
|