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