43972
|
1 |
/*
|
52910
|
2 |
* Copyright (c) 2012, 2018, 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 |
|
49873
|
27 |
import static org.graalvm.compiler.core.CompilationWrapper.ExceptionAction.Diagnose;
|
46680
|
28 |
import static org.graalvm.compiler.core.CompilationWrapper.ExceptionAction.ExitVM;
|
54204
|
29 |
import static org.graalvm.compiler.core.GraalCompilerOptions.CompilationBailoutAsFailure;
|
46680
|
30 |
import static org.graalvm.compiler.core.GraalCompilerOptions.CompilationFailureAction;
|
43972
|
31 |
import static org.graalvm.compiler.core.phases.HighTier.Options.Inline;
|
46344
|
32 |
import static org.graalvm.compiler.java.BytecodeParserOptions.InlineDuringParsing;
|
46371
|
33 |
|
52578
|
34 |
import java.io.PrintStream;
|
43972
|
35 |
import java.util.List;
|
|
36 |
|
49873
|
37 |
import jdk.internal.vm.compiler.collections.EconomicMap;
|
46640
|
38 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
43972
|
39 |
import org.graalvm.compiler.code.CompilationResult;
|
46680
|
40 |
import org.graalvm.compiler.core.CompilationPrinter;
|
|
41 |
import org.graalvm.compiler.core.CompilationWrapper;
|
46640
|
42 |
import org.graalvm.compiler.core.common.CompilationIdentifier;
|
46680
|
43 |
import org.graalvm.compiler.debug.Assertions;
|
46640
|
44 |
import org.graalvm.compiler.debug.CounterKey;
|
43972
|
45 |
import org.graalvm.compiler.debug.DebugCloseable;
|
46640
|
46 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
47 |
import org.graalvm.compiler.debug.DebugDumpScope;
|
|
48 |
import org.graalvm.compiler.debug.GraalError;
|
46640
|
49 |
import org.graalvm.compiler.debug.TimerKey;
|
46344
|
50 |
import org.graalvm.compiler.options.OptionKey;
|
|
51 |
import org.graalvm.compiler.options.OptionValues;
|
46640
|
52 |
import org.graalvm.compiler.printer.GraalDebugHandlersFactory;
|
43972
|
53 |
|
|
54 |
import jdk.vm.ci.code.BailoutException;
|
|
55 |
import jdk.vm.ci.code.CodeCacheProvider;
|
|
56 |
import jdk.vm.ci.hotspot.EventProvider;
|
|
57 |
import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
|
|
58 |
import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult;
|
|
59 |
import jdk.vm.ci.hotspot.HotSpotInstalledCode;
|
50858
|
60 |
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
|
43972
|
61 |
import jdk.vm.ci.hotspot.HotSpotNmethod;
|
|
62 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
|
|
63 |
import jdk.vm.ci.runtime.JVMCICompiler;
|
|
64 |
import jdk.vm.ci.services.JVMCIServiceLocator;
|
|
65 |
|
|
66 |
public class CompilationTask {
|
|
67 |
|
|
68 |
private static final EventProvider eventProvider;
|
|
69 |
|
|
70 |
static {
|
|
71 |
List<EventProvider> providers = JVMCIServiceLocator.getProviders(EventProvider.class);
|
|
72 |
if (providers.size() > 1) {
|
|
73 |
throw new GraalError("Multiple %s providers found: %s", EventProvider.class.getName(), providers);
|
|
74 |
} else if (providers.isEmpty()) {
|
|
75 |
eventProvider = EventProvider.createEmptyEventProvider();
|
|
76 |
} else {
|
|
77 |
eventProvider = providers.get(0);
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
50858
|
81 |
private final HotSpotJVMCIRuntime jvmciRuntime;
|
43972
|
82 |
|
|
83 |
private final HotSpotGraalCompiler compiler;
|
|
84 |
private final HotSpotCompilationIdentifier compilationId;
|
|
85 |
|
|
86 |
private HotSpotInstalledCode installedCode;
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Specifies whether the compilation result is installed as the
|
|
90 |
* {@linkplain HotSpotNmethod#isDefault() default} nmethod for the compiled method.
|
|
91 |
*/
|
|
92 |
private final boolean installAsDefault;
|
|
93 |
|
|
94 |
private final boolean useProfilingInfo;
|
54328
|
95 |
private final boolean shouldRetainLocalVariables;
|
46344
|
96 |
private final OptionValues options;
|
43972
|
97 |
|
46680
|
98 |
final class HotSpotCompilationWrapper extends CompilationWrapper<HotSpotCompilationRequestResult> {
|
46371
|
99 |
private final EventProvider.CompilationEvent compilationEvent;
|
|
100 |
CompilationResult result;
|
|
101 |
|
46680
|
102 |
HotSpotCompilationWrapper(EventProvider.CompilationEvent compilationEvent) {
|
46807
|
103 |
super(compiler.getGraalRuntime().getOutputDirectory(), compiler.getGraalRuntime().getCompilationProblemsPerAction());
|
46371
|
104 |
this.compilationEvent = compilationEvent;
|
|
105 |
}
|
|
106 |
|
46459
|
107 |
@Override
|
52578
|
108 |
protected DebugContext createRetryDebugContext(OptionValues retryOptions, PrintStream logStream) {
|
46680
|
109 |
SnippetReflectionProvider snippetReflection = compiler.getGraalRuntime().getHostProviders().getSnippetReflection();
|
52578
|
110 |
return DebugContext.create(retryOptions, logStream, new GraalDebugHandlersFactory(snippetReflection));
|
46680
|
111 |
}
|
|
112 |
|
|
113 |
@Override
|
46459
|
114 |
public String toString() {
|
46680
|
115 |
return getMethod().format("%H.%n(%p)");
|
|
116 |
}
|
|
117 |
|
|
118 |
@Override
|
|
119 |
protected HotSpotCompilationRequestResult handleException(Throwable t) {
|
|
120 |
if (t instanceof BailoutException) {
|
|
121 |
BailoutException bailout = (BailoutException) t;
|
|
122 |
/*
|
|
123 |
* Handling of permanent bailouts: Permanent bailouts that can happen for example
|
|
124 |
* due to unsupported unstructured control flow in the bytecodes of a method must
|
|
125 |
* not be retried. Hotspot compile broker will ensure that no recompilation at the
|
|
126 |
* given tier will happen if retry is false.
|
|
127 |
*/
|
|
128 |
return HotSpotCompilationRequestResult.failure(bailout.getMessage(), !bailout.isPermanent());
|
|
129 |
}
|
|
130 |
// Log a failure event.
|
|
131 |
EventProvider.CompilerFailureEvent event = eventProvider.newCompilerFailureEvent();
|
|
132 |
if (event.shouldWrite()) {
|
|
133 |
event.setCompileId(getId());
|
|
134 |
event.setMessage(t.getMessage());
|
|
135 |
event.commit();
|
|
136 |
}
|
|
137 |
|
|
138 |
/*
|
|
139 |
* Treat random exceptions from the compiler as indicating a problem compiling this
|
|
140 |
* method. Report the result of toString instead of getMessage to ensure that the
|
|
141 |
* exception type is included in the output in case there's no detail mesage.
|
|
142 |
*/
|
|
143 |
return HotSpotCompilationRequestResult.failure(t.toString(), false);
|
|
144 |
}
|
|
145 |
|
|
146 |
@Override
|
54204
|
147 |
protected ExceptionAction lookupAction(OptionValues values, Throwable cause) {
|
|
148 |
if (cause instanceof BailoutException) {
|
|
149 |
BailoutException bailout = (BailoutException) cause;
|
|
150 |
if (bailout.isPermanent()) {
|
|
151 |
// Respect current action if it has been explicitly set.
|
|
152 |
if (!CompilationBailoutAsFailure.hasBeenSet(values)) {
|
|
153 |
// Get more info for permanent bailouts during bootstrap
|
|
154 |
// or when assertions are enabled.
|
|
155 |
if (Assertions.assertionsEnabled() || compiler.getGraalRuntime().isBootstrapping()) {
|
|
156 |
return Diagnose;
|
|
157 |
}
|
|
158 |
|
49873
|
159 |
}
|
46680
|
160 |
}
|
54204
|
161 |
if (!CompilationBailoutAsFailure.getValue(values)) {
|
|
162 |
return super.lookupAction(values, cause);
|
|
163 |
}
|
46680
|
164 |
}
|
54204
|
165 |
|
|
166 |
// Respect current action if it has been explicitly set.
|
|
167 |
if (!CompilationFailureAction.hasBeenSet(values)) {
|
|
168 |
// Automatically exit on failure during bootstrap
|
|
169 |
// or when assertions are enabled.
|
|
170 |
if (Assertions.assertionsEnabled() || compiler.getGraalRuntime().isBootstrapping()) {
|
|
171 |
return ExitVM;
|
|
172 |
}
|
|
173 |
}
|
|
174 |
return super.lookupAction(values, cause);
|
46459
|
175 |
}
|
|
176 |
|
46371
|
177 |
@SuppressWarnings("try")
|
|
178 |
@Override
|
46680
|
179 |
protected HotSpotCompilationRequestResult performCompilation(DebugContext debug) {
|
46371
|
180 |
HotSpotResolvedJavaMethod method = getMethod();
|
|
181 |
int entryBCI = getEntryBCI();
|
|
182 |
final boolean isOSR = entryBCI != JVMCICompiler.INVOCATION_ENTRY_BCI;
|
|
183 |
CompilationStatistics stats = CompilationStatistics.create(options, method, isOSR);
|
|
184 |
|
46680
|
185 |
final CompilationPrinter printer = CompilationPrinter.begin(options, compilationId, method, entryBCI);
|
46371
|
186 |
|
46640
|
187 |
try (DebugContext.Scope s = debug.scope("Compiling", new DebugDumpScope(getIdString(), true))) {
|
46371
|
188 |
// Begin the compilation event.
|
|
189 |
compilationEvent.begin();
|
54328
|
190 |
result = compiler.compile(method, entryBCI, useProfilingInfo, shouldRetainLocalVariables, compilationId, options, debug);
|
46371
|
191 |
} catch (Throwable e) {
|
46640
|
192 |
throw debug.handle(e);
|
46371
|
193 |
} finally {
|
|
194 |
// End the compilation event.
|
|
195 |
compilationEvent.end();
|
|
196 |
}
|
|
197 |
|
|
198 |
if (result != null) {
|
46640
|
199 |
try (DebugCloseable b = CodeInstallationTime.start(debug)) {
|
|
200 |
installMethod(debug, result);
|
46371
|
201 |
}
|
46680
|
202 |
// Installation is included in compilation time and memory usage reported by printer
|
|
203 |
printer.finish(result);
|
46371
|
204 |
}
|
|
205 |
stats.finish(method, installedCode);
|
|
206 |
if (result != null) {
|
|
207 |
return HotSpotCompilationRequestResult.success(result.getBytecodeSize() - method.getCodeSize());
|
|
208 |
}
|
|
209 |
return null;
|
|
210 |
}
|
49873
|
211 |
|
46371
|
212 |
}
|
|
213 |
|
50858
|
214 |
public CompilationTask(HotSpotJVMCIRuntime jvmciRuntime, HotSpotGraalCompiler compiler, HotSpotCompilationRequest request, boolean useProfilingInfo, boolean installAsDefault,
|
46344
|
215 |
OptionValues options) {
|
54328
|
216 |
this(jvmciRuntime, compiler, request, useProfilingInfo, false, installAsDefault, options);
|
|
217 |
}
|
|
218 |
|
|
219 |
public CompilationTask(HotSpotJVMCIRuntime jvmciRuntime, HotSpotGraalCompiler compiler, HotSpotCompilationRequest request, boolean useProfilingInfo, boolean shouldRetainLocalVariables,
|
|
220 |
boolean installAsDefault, OptionValues options) {
|
43972
|
221 |
this.jvmciRuntime = jvmciRuntime;
|
|
222 |
this.compiler = compiler;
|
|
223 |
this.compilationId = new HotSpotCompilationIdentifier(request);
|
|
224 |
this.useProfilingInfo = useProfilingInfo;
|
54328
|
225 |
this.shouldRetainLocalVariables = shouldRetainLocalVariables;
|
43972
|
226 |
this.installAsDefault = installAsDefault;
|
46344
|
227 |
|
|
228 |
/*
|
|
229 |
* Disable inlining if HotSpot has it disabled unless it's been explicitly set in Graal.
|
|
230 |
*/
|
|
231 |
HotSpotGraalRuntimeProvider graalRuntime = compiler.getGraalRuntime();
|
|
232 |
GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
|
|
233 |
OptionValues newOptions = options;
|
|
234 |
if (!config.inline) {
|
|
235 |
EconomicMap<OptionKey<?>, Object> m = OptionValues.newOptionMap();
|
|
236 |
if (Inline.getValue(options) && !Inline.hasBeenSet(options)) {
|
|
237 |
m.put(Inline, false);
|
|
238 |
}
|
|
239 |
if (InlineDuringParsing.getValue(options) && !InlineDuringParsing.hasBeenSet(options)) {
|
|
240 |
m.put(InlineDuringParsing, false);
|
|
241 |
}
|
|
242 |
if (!m.isEmpty()) {
|
|
243 |
newOptions = new OptionValues(options, m);
|
|
244 |
}
|
|
245 |
}
|
|
246 |
this.options = newOptions;
|
43972
|
247 |
}
|
|
248 |
|
|
249 |
public HotSpotResolvedJavaMethod getMethod() {
|
|
250 |
return getRequest().getMethod();
|
|
251 |
}
|
|
252 |
|
46640
|
253 |
CompilationIdentifier getCompilationIdentifier() {
|
|
254 |
return compilationId;
|
|
255 |
}
|
|
256 |
|
43972
|
257 |
/**
|
46680
|
258 |
* Returns the HotSpot id of this compilation.
|
43972
|
259 |
*
|
46640
|
260 |
* @return HotSpot compile id
|
43972
|
261 |
*/
|
|
262 |
public int getId() {
|
|
263 |
return getRequest().getId();
|
|
264 |
}
|
|
265 |
|
|
266 |
public int getEntryBCI() {
|
|
267 |
return getRequest().getEntryBCI();
|
|
268 |
}
|
|
269 |
|
|
270 |
/**
|
47667
|
271 |
* @return the compilation id plus a trailing '%' if the compilation is an OSR to match
|
43972
|
272 |
* PrintCompilation style output
|
|
273 |
*/
|
|
274 |
public String getIdString() {
|
|
275 |
if (getEntryBCI() != JVMCICompiler.INVOCATION_ENTRY_BCI) {
|
|
276 |
return getId() + "%";
|
|
277 |
} else {
|
|
278 |
return Integer.toString(getId());
|
|
279 |
}
|
|
280 |
}
|
|
281 |
|
|
282 |
public HotSpotInstalledCode getInstalledCode() {
|
|
283 |
return installedCode;
|
|
284 |
}
|
|
285 |
|
|
286 |
/**
|
|
287 |
* Time spent in compilation.
|
|
288 |
*/
|
46640
|
289 |
private static final TimerKey CompilationTime = DebugContext.timer("CompilationTime").doc("Time spent in compilation and code installation.");
|
43972
|
290 |
|
|
291 |
/**
|
|
292 |
* Counts the number of compiled {@linkplain CompilationResult#getBytecodeSize() bytecodes}.
|
|
293 |
*/
|
46640
|
294 |
private static final CounterKey CompiledBytecodes = DebugContext.counter("CompiledBytecodes");
|
43972
|
295 |
|
|
296 |
/**
|
|
297 |
* Counts the number of compiled {@linkplain CompilationResult#getBytecodeSize() bytecodes} for
|
|
298 |
* which {@linkplain CompilationResult#getTargetCode()} code was installed.
|
|
299 |
*/
|
46640
|
300 |
private static final CounterKey CompiledAndInstalledBytecodes = DebugContext.counter("CompiledAndInstalledBytecodes");
|
43972
|
301 |
|
|
302 |
/**
|
|
303 |
* Counts the number of installed {@linkplain CompilationResult#getTargetCodeSize()} bytes.
|
|
304 |
*/
|
46640
|
305 |
private static final CounterKey InstalledCodeSize = DebugContext.counter("InstalledCodeSize");
|
43972
|
306 |
|
|
307 |
/**
|
|
308 |
* Time spent in code installation.
|
|
309 |
*/
|
46640
|
310 |
public static final TimerKey CodeInstallationTime = DebugContext.timer("CodeInstallation");
|
|
311 |
|
|
312 |
public HotSpotCompilationRequestResult runCompilation() {
|
|
313 |
SnippetReflectionProvider snippetReflection = compiler.getGraalRuntime().getHostProviders().getSnippetReflection();
|
|
314 |
try (DebugContext debug = DebugContext.create(options, new GraalDebugHandlersFactory(snippetReflection))) {
|
|
315 |
return runCompilation(debug);
|
|
316 |
}
|
|
317 |
}
|
43972
|
318 |
|
|
319 |
@SuppressWarnings("try")
|
46640
|
320 |
public HotSpotCompilationRequestResult runCompilation(DebugContext debug) {
|
43972
|
321 |
HotSpotGraalRuntimeProvider graalRuntime = compiler.getGraalRuntime();
|
|
322 |
GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
|
|
323 |
int entryBCI = getEntryBCI();
|
46371
|
324 |
boolean isOSR = entryBCI != JVMCICompiler.INVOCATION_ENTRY_BCI;
|
43972
|
325 |
HotSpotResolvedJavaMethod method = getMethod();
|
|
326 |
|
|
327 |
// Log a compilation event.
|
|
328 |
EventProvider.CompilationEvent compilationEvent = eventProvider.newCompilationEvent();
|
|
329 |
|
54328
|
330 |
if (installAsDefault || isOSR) {
|
46536
|
331 |
// If there is already compiled code for this method on our level we simply return.
|
|
332 |
// JVMCI compiles are always at the highest compile level, even in non-tiered mode so we
|
|
333 |
// only need to check for that value.
|
|
334 |
if (method.hasCodeAtLevel(entryBCI, config.compilationLevelFullOptimization)) {
|
|
335 |
return HotSpotCompilationRequestResult.failure("Already compiled", false);
|
|
336 |
}
|
54328
|
337 |
if (HotSpotGraalCompilerFactory.shouldExclude(method)) {
|
46762
|
338 |
return HotSpotCompilationRequestResult.failure("GraalCompileOnly excluded", false);
|
|
339 |
}
|
43972
|
340 |
}
|
|
341 |
|
46680
|
342 |
HotSpotCompilationWrapper compilation = new HotSpotCompilationWrapper(compilationEvent);
|
46640
|
343 |
try (DebugCloseable a = CompilationTime.start(debug)) {
|
46680
|
344 |
return compilation.run(debug);
|
43972
|
345 |
} finally {
|
|
346 |
try {
|
|
347 |
int compiledBytecodes = 0;
|
|
348 |
int codeSize = 0;
|
46371
|
349 |
|
|
350 |
if (compilation.result != null) {
|
|
351 |
compiledBytecodes = compilation.result.getBytecodeSize();
|
46640
|
352 |
CompiledBytecodes.add(debug, compiledBytecodes);
|
43972
|
353 |
if (installedCode != null) {
|
|
354 |
codeSize = installedCode.getSize();
|
46640
|
355 |
CompiledAndInstalledBytecodes.add(debug, compiledBytecodes);
|
|
356 |
InstalledCodeSize.add(debug, codeSize);
|
43972
|
357 |
}
|
|
358 |
}
|
|
359 |
|
|
360 |
// Log a compilation event.
|
|
361 |
if (compilationEvent.shouldWrite()) {
|
|
362 |
compilationEvent.setMethod(method.format("%H.%n(%p)"));
|
|
363 |
compilationEvent.setCompileId(getId());
|
|
364 |
compilationEvent.setCompileLevel(config.compilationLevelFullOptimization);
|
46371
|
365 |
compilationEvent.setSucceeded(compilation.result != null && installedCode != null);
|
43972
|
366 |
compilationEvent.setIsOsr(isOSR);
|
|
367 |
compilationEvent.setCodeSize(codeSize);
|
|
368 |
compilationEvent.setInlinedBytes(compiledBytecodes);
|
|
369 |
compilationEvent.commit();
|
|
370 |
}
|
|
371 |
} catch (Throwable t) {
|
46680
|
372 |
return compilation.handleException(t);
|
43972
|
373 |
}
|
|
374 |
}
|
|
375 |
}
|
|
376 |
|
|
377 |
@SuppressWarnings("try")
|
46640
|
378 |
private void installMethod(DebugContext debug, final CompilationResult compResult) {
|
43972
|
379 |
final CodeCacheProvider codeCache = jvmciRuntime.getHostJVMCIBackend().getCodeCache();
|
46551
|
380 |
HotSpotBackend backend = compiler.getGraalRuntime().getHostBackend();
|
43972
|
381 |
installedCode = null;
|
|
382 |
Object[] context = {new DebugDumpScope(getIdString(), true), codeCache, getMethod(), compResult};
|
46640
|
383 |
try (DebugContext.Scope s = debug.scope("CodeInstall", context)) {
|
54084
|
384 |
HotSpotCompilationRequest request = getRequest();
|
|
385 |
installedCode = (HotSpotInstalledCode) backend.createInstalledCode(debug,
|
|
386 |
request.getMethod(),
|
|
387 |
request,
|
|
388 |
compResult,
|
|
389 |
null,
|
|
390 |
installAsDefault,
|
|
391 |
context);
|
46640
|
392 |
} catch (Throwable e) {
|
|
393 |
throw debug.handle(e);
|
|
394 |
}
|
43972
|
395 |
}
|
|
396 |
|
|
397 |
@Override
|
|
398 |
public String toString() {
|
|
399 |
return "Compilation[id=" + getId() + ", " + getMethod().format("%H.%n(%p)") + (getEntryBCI() == JVMCICompiler.INVOCATION_ENTRY_BCI ? "" : "@" + getEntryBCI()) + "]";
|
|
400 |
}
|
|
401 |
|
|
402 |
private HotSpotCompilationRequest getRequest() {
|
|
403 |
return compilationId.getRequest();
|
|
404 |
}
|
|
405 |
}
|