43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
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 |
*/
|
|
23 |
package org.graalvm.compiler.hotspot;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.core.common.GraalOptions.OptAssumptions;
|
|
26 |
import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.ROOT_COMPILATION;
|
46371
|
27 |
|
43972
|
28 |
import java.io.ByteArrayOutputStream;
|
|
29 |
import java.io.PrintStream;
|
46640
|
30 |
import java.util.Collections;
|
43972
|
31 |
import java.util.Formattable;
|
|
32 |
import java.util.Formatter;
|
46640
|
33 |
import java.util.List;
|
43972
|
34 |
|
|
35 |
import org.graalvm.compiler.api.runtime.GraalJVMCICompiler;
|
46459
|
36 |
import org.graalvm.compiler.bytecode.Bytecode;
|
43972
|
37 |
import org.graalvm.compiler.code.CompilationResult;
|
|
38 |
import org.graalvm.compiler.core.GraalCompiler;
|
|
39 |
import org.graalvm.compiler.core.common.CompilationIdentifier;
|
|
40 |
import org.graalvm.compiler.core.common.util.CompilationAlarm;
|
46640
|
41 |
import org.graalvm.compiler.debug.DebugContext;
|
|
42 |
import org.graalvm.compiler.debug.DebugContext.Activation;
|
49873
|
43 |
import org.graalvm.compiler.debug.DebugHandlersFactory;
|
46640
|
44 |
import org.graalvm.compiler.debug.DebugOptions;
|
43972
|
45 |
import org.graalvm.compiler.hotspot.CompilationCounters.Options;
|
|
46 |
import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
|
|
47 |
import org.graalvm.compiler.hotspot.phases.OnStackReplacementPhase;
|
|
48 |
import org.graalvm.compiler.java.GraphBuilderPhase;
|
|
49 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
|
|
50 |
import org.graalvm.compiler.lir.phases.LIRSuites;
|
|
51 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
52 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
|
53 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
|
|
54 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
|
|
55 |
import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
|
|
56 |
import org.graalvm.compiler.nodes.spi.Replacements;
|
46344
|
57 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
58 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
59 |
import org.graalvm.compiler.phases.OptimisticOptimizations.Optimization;
|
|
60 |
import org.graalvm.compiler.phases.PhaseSuite;
|
|
61 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
62 |
import org.graalvm.compiler.phases.tiers.Suites;
|
46640
|
63 |
import org.graalvm.compiler.printer.GraalDebugHandlersFactory;
|
43972
|
64 |
|
|
65 |
import jdk.vm.ci.code.CompilationRequest;
|
|
66 |
import jdk.vm.ci.code.CompilationRequestResult;
|
|
67 |
import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
|
|
68 |
import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult;
|
|
69 |
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
|
|
70 |
import jdk.vm.ci.meta.DefaultProfilingInfo;
|
|
71 |
import jdk.vm.ci.meta.JavaMethod;
|
|
72 |
import jdk.vm.ci.meta.ProfilingInfo;
|
|
73 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
74 |
import jdk.vm.ci.meta.SpeculationLog;
|
|
75 |
import jdk.vm.ci.meta.TriState;
|
|
76 |
import jdk.vm.ci.runtime.JVMCICompiler;
|
|
77 |
|
|
78 |
public class HotSpotGraalCompiler implements GraalJVMCICompiler {
|
|
79 |
|
|
80 |
private final HotSpotJVMCIRuntimeProvider jvmciRuntime;
|
|
81 |
private final HotSpotGraalRuntimeProvider graalRuntime;
|
|
82 |
private final CompilationCounters compilationCounters;
|
|
83 |
private final BootstrapWatchDog bootstrapWatchDog;
|
46640
|
84 |
private List<DebugHandlersFactory> factories;
|
43972
|
85 |
|
46536
|
86 |
HotSpotGraalCompiler(HotSpotJVMCIRuntimeProvider jvmciRuntime, HotSpotGraalRuntimeProvider graalRuntime, OptionValues options) {
|
43972
|
87 |
this.jvmciRuntime = jvmciRuntime;
|
|
88 |
this.graalRuntime = graalRuntime;
|
|
89 |
// It is sufficient to have one compilation counter object per Graal compiler object.
|
46344
|
90 |
this.compilationCounters = Options.CompilationCountLimit.getValue(options) > 0 ? new CompilationCounters(options) : null;
|
46640
|
91 |
this.bootstrapWatchDog = graalRuntime.isBootstrapping() && !DebugOptions.BootstrapInitializeOnly.getValue(options) ? BootstrapWatchDog.maybeCreate(graalRuntime) : null;
|
|
92 |
}
|
|
93 |
|
|
94 |
public List<DebugHandlersFactory> getDebugHandlersFactories() {
|
|
95 |
if (factories == null) {
|
|
96 |
factories = Collections.singletonList(new GraalDebugHandlersFactory(graalRuntime.getHostProviders().getSnippetReflection()));
|
|
97 |
}
|
|
98 |
return factories;
|
43972
|
99 |
}
|
|
100 |
|
|
101 |
@Override
|
|
102 |
public HotSpotGraalRuntimeProvider getGraalRuntime() {
|
|
103 |
return graalRuntime;
|
|
104 |
}
|
|
105 |
|
|
106 |
@Override
|
46536
|
107 |
public CompilationRequestResult compileMethod(CompilationRequest request) {
|
49873
|
108 |
return compileMethod(request, true, graalRuntime.getOptions());
|
46536
|
109 |
}
|
|
110 |
|
43972
|
111 |
@SuppressWarnings("try")
|
49873
|
112 |
CompilationRequestResult compileMethod(CompilationRequest request, boolean installAsDefault, OptionValues options) {
|
46371
|
113 |
if (graalRuntime.isShutdown()) {
|
|
114 |
return HotSpotCompilationRequestResult.failure(String.format("Shutdown entered"), false);
|
|
115 |
}
|
46640
|
116 |
|
|
117 |
ResolvedJavaMethod method = request.getMethod();
|
46459
|
118 |
|
46344
|
119 |
if (graalRuntime.isBootstrapping()) {
|
46640
|
120 |
if (DebugOptions.BootstrapInitializeOnly.getValue(options)) {
|
|
121 |
return HotSpotCompilationRequestResult.failure(String.format("Skip compilation because %s is enabled", DebugOptions.BootstrapInitializeOnly.getName()), true);
|
46344
|
122 |
}
|
|
123 |
if (bootstrapWatchDog != null) {
|
|
124 |
if (bootstrapWatchDog.hitCriticalCompilationRateOrTimeout()) {
|
|
125 |
// Drain the compilation queue to expedite completion of the bootstrap
|
|
126 |
return HotSpotCompilationRequestResult.failure("hit critical bootstrap compilation rate or timeout", true);
|
|
127 |
}
|
43972
|
128 |
}
|
|
129 |
}
|
|
130 |
HotSpotCompilationRequest hsRequest = (HotSpotCompilationRequest) request;
|
46344
|
131 |
try (CompilationWatchDog w1 = CompilationWatchDog.watch(method, hsRequest.getId(), options);
|
43972
|
132 |
BootstrapWatchDog.Watch w2 = bootstrapWatchDog == null ? null : bootstrapWatchDog.watch(request);
|
46344
|
133 |
CompilationAlarm alarm = CompilationAlarm.trackCompilationPeriod(options);) {
|
43972
|
134 |
if (compilationCounters != null) {
|
|
135 |
compilationCounters.countCompilation(method);
|
|
136 |
}
|
46536
|
137 |
CompilationTask task = new CompilationTask(jvmciRuntime, this, hsRequest, true, installAsDefault, options);
|
46640
|
138 |
CompilationRequestResult r = null;
|
|
139 |
try (DebugContext debug = graalRuntime.openDebugContext(options, task.getCompilationIdentifier(), method, getDebugHandlersFactories());
|
|
140 |
Activation a = debug.activate()) {
|
|
141 |
r = task.runCompilation(debug);
|
43972
|
142 |
}
|
|
143 |
assert r != null;
|
|
144 |
return r;
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
46807
|
148 |
public StructuredGraph createGraph(ResolvedJavaMethod method, int entryBCI, boolean useProfilingInfo, CompilationIdentifier compilationId, OptionValues options, DebugContext debug) {
|
43972
|
149 |
HotSpotBackend backend = graalRuntime.getHostBackend();
|
|
150 |
HotSpotProviders providers = backend.getProviders();
|
|
151 |
final boolean isOSR = entryBCI != JVMCICompiler.INVOCATION_ENTRY_BCI;
|
46640
|
152 |
StructuredGraph graph = method.isNative() || isOSR ? null : getIntrinsicGraph(method, providers, compilationId, options, debug);
|
43972
|
153 |
|
|
154 |
if (graph == null) {
|
|
155 |
SpeculationLog speculationLog = method.getSpeculationLog();
|
|
156 |
if (speculationLog != null) {
|
|
157 |
speculationLog.collectFailedSpeculations();
|
|
158 |
}
|
46640
|
159 |
graph = new StructuredGraph.Builder(options, debug, AllowAssumptions.ifTrue(OptAssumptions.getValue(options))).method(method).entryBCI(entryBCI).speculationLog(
|
46344
|
160 |
speculationLog).useProfilingInfo(useProfilingInfo).compilationId(compilationId).build();
|
43972
|
161 |
}
|
46807
|
162 |
return graph;
|
|
163 |
}
|
|
164 |
|
|
165 |
public CompilationResult compileHelper(CompilationResultBuilderFactory crbf, CompilationResult result, StructuredGraph graph, ResolvedJavaMethod method, int entryBCI, boolean useProfilingInfo,
|
|
166 |
OptionValues options) {
|
|
167 |
|
|
168 |
HotSpotBackend backend = graalRuntime.getHostBackend();
|
|
169 |
HotSpotProviders providers = backend.getProviders();
|
|
170 |
final boolean isOSR = entryBCI != JVMCICompiler.INVOCATION_ENTRY_BCI;
|
43972
|
171 |
|
46344
|
172 |
Suites suites = getSuites(providers, options);
|
|
173 |
LIRSuites lirSuites = getLIRSuites(providers, options);
|
43972
|
174 |
ProfilingInfo profilingInfo = useProfilingInfo ? method.getProfilingInfo(!isOSR, isOSR) : DefaultProfilingInfo.get(TriState.FALSE);
|
46344
|
175 |
OptimisticOptimizations optimisticOpts = getOptimisticOpts(profilingInfo, options);
|
|
176 |
|
|
177 |
/*
|
|
178 |
* Cut off never executed code profiles if there is code, e.g. after the osr loop, that is
|
|
179 |
* never executed.
|
|
180 |
*/
|
|
181 |
if (isOSR && !OnStackReplacementPhase.Options.DeoptAfterOSR.getValue(options)) {
|
43972
|
182 |
optimisticOpts.remove(Optimization.RemoveNeverExecutedCode);
|
|
183 |
}
|
46807
|
184 |
|
43972
|
185 |
result.setEntryBCI(entryBCI);
|
|
186 |
boolean shouldDebugNonSafepoints = providers.getCodeCache().shouldDebugNonSafepoints();
|
|
187 |
PhaseSuite<HighTierContext> graphBuilderSuite = configGraphBuilderSuite(providers.getSuites().getDefaultGraphBuilderSuite(), shouldDebugNonSafepoints, isOSR);
|
50330
|
188 |
GraalCompiler.compileGraph(graph, method, providers, backend, graphBuilderSuite, optimisticOpts, profilingInfo, suites, lirSuites, result, crbf, true);
|
43972
|
189 |
|
|
190 |
if (!isOSR && useProfilingInfo) {
|
|
191 |
ProfilingInfo profile = profilingInfo;
|
|
192 |
profile.setCompilerIRSize(StructuredGraph.class, graph.getNodeCount());
|
|
193 |
}
|
|
194 |
|
|
195 |
return result;
|
|
196 |
}
|
|
197 |
|
46807
|
198 |
public CompilationResult compile(ResolvedJavaMethod method, int entryBCI, boolean useProfilingInfo, CompilationIdentifier compilationId, OptionValues options, DebugContext debug) {
|
|
199 |
StructuredGraph graph = createGraph(method, entryBCI, useProfilingInfo, compilationId, options, debug);
|
47084
|
200 |
CompilationResult result = new CompilationResult(compilationId);
|
46807
|
201 |
return compileHelper(CompilationResultBuilderFactory.Default, result, graph, method, entryBCI, useProfilingInfo, options);
|
|
202 |
}
|
|
203 |
|
43972
|
204 |
/**
|
|
205 |
* Gets a graph produced from the intrinsic for a given method that can be compiled and
|
|
206 |
* installed for the method.
|
|
207 |
*
|
|
208 |
* @param method
|
|
209 |
* @param compilationId
|
46344
|
210 |
* @param options
|
46640
|
211 |
* @param debug
|
43972
|
212 |
* @return an intrinsic graph that can be compiled and installed for {@code method} or null
|
|
213 |
*/
|
|
214 |
@SuppressWarnings("try")
|
46640
|
215 |
public StructuredGraph getIntrinsicGraph(ResolvedJavaMethod method, HotSpotProviders providers, CompilationIdentifier compilationId, OptionValues options, DebugContext debug) {
|
43972
|
216 |
Replacements replacements = providers.getReplacements();
|
46459
|
217 |
Bytecode subst = replacements.getSubstitutionBytecode(method);
|
|
218 |
if (subst != null) {
|
|
219 |
ResolvedJavaMethod substMethod = subst.getMethod();
|
43972
|
220 |
assert !substMethod.equals(method);
|
46640
|
221 |
StructuredGraph graph = new StructuredGraph.Builder(options, debug, AllowAssumptions.YES).method(substMethod).compilationId(compilationId).build();
|
|
222 |
try (DebugContext.Scope scope = debug.scope("GetIntrinsicGraph", graph)) {
|
43972
|
223 |
Plugins plugins = new Plugins(providers.getGraphBuilderPlugins());
|
|
224 |
GraphBuilderConfiguration config = GraphBuilderConfiguration.getSnippetDefault(plugins);
|
46459
|
225 |
IntrinsicContext initialReplacementContext = new IntrinsicContext(method, substMethod, subst.getOrigin(), ROOT_COMPILATION);
|
43972
|
226 |
new GraphBuilderPhase.Instance(providers.getMetaAccess(), providers.getStampProvider(), providers.getConstantReflection(), providers.getConstantFieldProvider(), config,
|
|
227 |
OptimisticOptimizations.NONE, initialReplacementContext).apply(graph);
|
|
228 |
assert !graph.isFrozen();
|
|
229 |
return graph;
|
|
230 |
} catch (Throwable e) {
|
46640
|
231 |
debug.handle(e);
|
43972
|
232 |
}
|
|
233 |
}
|
|
234 |
return null;
|
|
235 |
}
|
|
236 |
|
46344
|
237 |
protected OptimisticOptimizations getOptimisticOpts(ProfilingInfo profilingInfo, OptionValues options) {
|
|
238 |
return new OptimisticOptimizations(profilingInfo, options);
|
43972
|
239 |
}
|
|
240 |
|
46344
|
241 |
protected Suites getSuites(HotSpotProviders providers, OptionValues options) {
|
|
242 |
return providers.getSuites().getDefaultSuites(options);
|
43972
|
243 |
}
|
|
244 |
|
46344
|
245 |
protected LIRSuites getLIRSuites(HotSpotProviders providers, OptionValues options) {
|
|
246 |
return providers.getSuites().getDefaultLIRSuites(options);
|
43972
|
247 |
}
|
|
248 |
|
|
249 |
/**
|
|
250 |
* Reconfigures a given graph builder suite (GBS) if one of the given GBS parameter values is
|
|
251 |
* not the default.
|
|
252 |
*
|
|
253 |
* @param suite the graph builder suite
|
|
254 |
* @param shouldDebugNonSafepoints specifies if extra debug info should be generated (default is
|
|
255 |
* false)
|
|
256 |
* @param isOSR specifies if extra OSR-specific post-processing is required (default is false)
|
|
257 |
* @return a new suite derived from {@code suite} if any of the GBS parameters did not have a
|
|
258 |
* default value otherwise {@code suite}
|
|
259 |
*/
|
|
260 |
protected PhaseSuite<HighTierContext> configGraphBuilderSuite(PhaseSuite<HighTierContext> suite, boolean shouldDebugNonSafepoints, boolean isOSR) {
|
|
261 |
if (shouldDebugNonSafepoints || isOSR) {
|
|
262 |
PhaseSuite<HighTierContext> newGbs = suite.copy();
|
|
263 |
|
|
264 |
if (shouldDebugNonSafepoints) {
|
|
265 |
GraphBuilderPhase graphBuilderPhase = (GraphBuilderPhase) newGbs.findPhase(GraphBuilderPhase.class).previous();
|
|
266 |
GraphBuilderConfiguration graphBuilderConfig = graphBuilderPhase.getGraphBuilderConfig();
|
|
267 |
graphBuilderConfig = graphBuilderConfig.withNodeSourcePosition(true);
|
|
268 |
GraphBuilderPhase newGraphBuilderPhase = new GraphBuilderPhase(graphBuilderConfig);
|
|
269 |
newGbs.findPhase(GraphBuilderPhase.class).set(newGraphBuilderPhase);
|
|
270 |
}
|
|
271 |
if (isOSR) {
|
46344
|
272 |
// We must not clear non liveness for OSR compilations.
|
|
273 |
GraphBuilderPhase graphBuilderPhase = (GraphBuilderPhase) newGbs.findPhase(GraphBuilderPhase.class).previous();
|
|
274 |
GraphBuilderConfiguration graphBuilderConfig = graphBuilderPhase.getGraphBuilderConfig();
|
|
275 |
GraphBuilderPhase newGraphBuilderPhase = new GraphBuilderPhase(graphBuilderConfig);
|
|
276 |
newGbs.findPhase(GraphBuilderPhase.class).set(newGraphBuilderPhase);
|
43972
|
277 |
newGbs.appendPhase(new OnStackReplacementPhase());
|
|
278 |
}
|
|
279 |
return newGbs;
|
|
280 |
}
|
|
281 |
return suite;
|
|
282 |
}
|
|
283 |
|
|
284 |
/**
|
|
285 |
* Converts {@code method} to a String with {@link JavaMethod#format(String)} and the format
|
|
286 |
* string {@code "%H.%n(%p)"}.
|
|
287 |
*/
|
|
288 |
static String str(JavaMethod method) {
|
|
289 |
return method.format("%H.%n(%p)");
|
|
290 |
}
|
|
291 |
|
|
292 |
/**
|
|
293 |
* Wraps {@code obj} in a {@link Formatter} that standardizes formatting for certain objects.
|
|
294 |
*/
|
|
295 |
static Formattable fmt(Object obj) {
|
|
296 |
return new Formattable() {
|
|
297 |
@Override
|
|
298 |
public void formatTo(Formatter buf, int flags, int width, int precision) {
|
|
299 |
if (obj instanceof Throwable) {
|
|
300 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
301 |
((Throwable) obj).printStackTrace(new PrintStream(baos));
|
|
302 |
buf.format("%s", baos.toString());
|
|
303 |
} else if (obj instanceof StackTraceElement[]) {
|
|
304 |
for (StackTraceElement e : (StackTraceElement[]) obj) {
|
|
305 |
buf.format("\t%s%n", e);
|
|
306 |
}
|
|
307 |
} else if (obj instanceof JavaMethod) {
|
|
308 |
buf.format("%s", str((JavaMethod) obj));
|
|
309 |
} else {
|
|
310 |
buf.format("%s", obj);
|
|
311 |
}
|
|
312 |
}
|
|
313 |
};
|
|
314 |
}
|
|
315 |
}
|