46 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; |
46 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; |
47 import org.graalvm.compiler.hotspot.phases.OnStackReplacementPhase; |
47 import org.graalvm.compiler.hotspot.phases.OnStackReplacementPhase; |
48 import org.graalvm.compiler.java.GraphBuilderPhase; |
48 import org.graalvm.compiler.java.GraphBuilderPhase; |
49 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory; |
49 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory; |
50 import org.graalvm.compiler.lir.phases.LIRSuites; |
50 import org.graalvm.compiler.lir.phases.LIRSuites; |
|
51 import org.graalvm.compiler.nodes.Cancellable; |
51 import org.graalvm.compiler.nodes.StructuredGraph; |
52 import org.graalvm.compiler.nodes.StructuredGraph; |
52 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; |
53 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; |
53 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; |
54 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; |
54 import org.graalvm.compiler.options.OptionValues; |
55 import org.graalvm.compiler.options.OptionValues; |
55 import org.graalvm.compiler.phases.OptimisticOptimizations; |
56 import org.graalvm.compiler.phases.OptimisticOptimizations; |
72 import jdk.vm.ci.meta.SpeculationLog; |
73 import jdk.vm.ci.meta.SpeculationLog; |
73 import jdk.vm.ci.meta.TriState; |
74 import jdk.vm.ci.meta.TriState; |
74 import jdk.vm.ci.runtime.JVMCICompiler; |
75 import jdk.vm.ci.runtime.JVMCICompiler; |
75 import sun.misc.Unsafe; |
76 import sun.misc.Unsafe; |
76 |
77 |
77 public class HotSpotGraalCompiler implements GraalJVMCICompiler { |
78 public class HotSpotGraalCompiler implements GraalJVMCICompiler, Cancellable { |
78 |
79 |
79 private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); |
80 private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); |
80 private final HotSpotJVMCIRuntime jvmciRuntime; |
81 private final HotSpotJVMCIRuntime jvmciRuntime; |
81 private final HotSpotGraalRuntimeProvider graalRuntime; |
82 private final HotSpotGraalRuntimeProvider graalRuntime; |
82 private final CompilationCounters compilationCounters; |
83 private final CompilationCounters compilationCounters; |
84 private List<DebugHandlersFactory> factories; |
85 private List<DebugHandlersFactory> factories; |
85 |
86 |
86 HotSpotGraalCompiler(HotSpotJVMCIRuntime jvmciRuntime, HotSpotGraalRuntimeProvider graalRuntime, OptionValues options) { |
87 HotSpotGraalCompiler(HotSpotJVMCIRuntime jvmciRuntime, HotSpotGraalRuntimeProvider graalRuntime, OptionValues options) { |
87 this.jvmciRuntime = jvmciRuntime; |
88 this.jvmciRuntime = jvmciRuntime; |
88 this.graalRuntime = graalRuntime; |
89 this.graalRuntime = graalRuntime; |
89 // It is sufficient to have one compilation counter object per Graal compiler object. |
90 // It is sufficient to have one compilation counter object per compiler object. |
90 this.compilationCounters = Options.CompilationCountLimit.getValue(options) > 0 ? new CompilationCounters(options) : null; |
91 this.compilationCounters = Options.CompilationCountLimit.getValue(options) > 0 ? new CompilationCounters(options) : null; |
91 this.bootstrapWatchDog = graalRuntime.isBootstrapping() && !DebugOptions.BootstrapInitializeOnly.getValue(options) ? BootstrapWatchDog.maybeCreate(graalRuntime) : null; |
92 this.bootstrapWatchDog = graalRuntime.isBootstrapping() && !DebugOptions.BootstrapInitializeOnly.getValue(options) ? BootstrapWatchDog.maybeCreate(graalRuntime) : null; |
92 } |
93 } |
93 |
94 |
94 public List<DebugHandlersFactory> getDebugHandlersFactories() { |
95 public List<DebugHandlersFactory> getDebugHandlersFactories() { |
108 return compileMethod(request, true, graalRuntime.getOptions()); |
109 return compileMethod(request, true, graalRuntime.getOptions()); |
109 } |
110 } |
110 |
111 |
111 @SuppressWarnings("try") |
112 @SuppressWarnings("try") |
112 CompilationRequestResult compileMethod(CompilationRequest request, boolean installAsDefault, OptionValues initialOptions) { |
113 CompilationRequestResult compileMethod(CompilationRequest request, boolean installAsDefault, OptionValues initialOptions) { |
113 if (graalRuntime.isShutdown()) { |
114 try (CompilationContext scope = HotSpotGraalServices.openLocalCompilationContext(request)) { |
114 return HotSpotCompilationRequestResult.failure(String.format("Shutdown entered"), false); |
115 if (graalRuntime.isShutdown()) { |
115 } |
116 return HotSpotCompilationRequestResult.failure(String.format("Shutdown entered"), true); |
116 |
117 } |
117 ResolvedJavaMethod method = request.getMethod(); |
118 |
118 |
119 ResolvedJavaMethod method = request.getMethod(); |
119 if (graalRuntime.isBootstrapping()) { |
120 |
120 if (DebugOptions.BootstrapInitializeOnly.getValue(initialOptions)) { |
121 if (graalRuntime.isBootstrapping()) { |
121 return HotSpotCompilationRequestResult.failure(String.format("Skip compilation because %s is enabled", DebugOptions.BootstrapInitializeOnly.getName()), true); |
122 if (DebugOptions.BootstrapInitializeOnly.getValue(initialOptions)) { |
122 } |
123 return HotSpotCompilationRequestResult.failure(String.format("Skip compilation because %s is enabled", DebugOptions.BootstrapInitializeOnly.getName()), true); |
123 if (bootstrapWatchDog != null) { |
124 } |
124 if (bootstrapWatchDog.hitCriticalCompilationRateOrTimeout()) { |
125 if (bootstrapWatchDog != null) { |
125 // Drain the compilation queue to expedite completion of the bootstrap |
126 if (bootstrapWatchDog.hitCriticalCompilationRateOrTimeout()) { |
126 return HotSpotCompilationRequestResult.failure("hit critical bootstrap compilation rate or timeout", true); |
127 // Drain the compilation queue to expedite completion of the bootstrap |
127 } |
128 return HotSpotCompilationRequestResult.failure("hit critical bootstrap compilation rate or timeout", true); |
128 } |
129 } |
129 } |
130 } |
130 HotSpotCompilationRequest hsRequest = (HotSpotCompilationRequest) request; |
131 } |
131 CompilationTask task = new CompilationTask(jvmciRuntime, this, hsRequest, true, shouldRetainLocalVariables(hsRequest.getJvmciEnv()), installAsDefault); |
132 HotSpotCompilationRequest hsRequest = (HotSpotCompilationRequest) request; |
132 OptionValues options = task.filterOptions(initialOptions); |
133 CompilationTask task = new CompilationTask(jvmciRuntime, this, hsRequest, true, shouldRetainLocalVariables(hsRequest.getJvmciEnv()), installAsDefault); |
133 try (CompilationWatchDog w1 = CompilationWatchDog.watch(method, hsRequest.getId(), options); |
134 OptionValues options = task.filterOptions(initialOptions); |
134 BootstrapWatchDog.Watch w2 = bootstrapWatchDog == null ? null : bootstrapWatchDog.watch(request); |
135 try (CompilationWatchDog w1 = CompilationWatchDog.watch(method, hsRequest.getId(), options); |
135 CompilationAlarm alarm = CompilationAlarm.trackCompilationPeriod(options);) { |
136 BootstrapWatchDog.Watch w2 = bootstrapWatchDog == null ? null : bootstrapWatchDog.watch(request); |
136 if (compilationCounters != null) { |
137 CompilationAlarm alarm = CompilationAlarm.trackCompilationPeriod(options);) { |
137 compilationCounters.countCompilation(method); |
138 if (compilationCounters != null) { |
138 } |
139 compilationCounters.countCompilation(method); |
139 CompilationRequestResult r = null; |
140 } |
140 try (DebugContext debug = graalRuntime.openDebugContext(options, task.getCompilationIdentifier(), method, getDebugHandlersFactories(), DebugContext.DEFAULT_LOG_STREAM); |
141 CompilationRequestResult r = null; |
141 Activation a = debug.activate()) { |
142 try (DebugContext debug = graalRuntime.openDebugContext(options, task.getCompilationIdentifier(), method, getDebugHandlersFactories(), DebugContext.DEFAULT_LOG_STREAM); |
142 r = task.runCompilation(debug); |
143 Activation a = debug.activate()) { |
143 } |
144 r = task.runCompilation(debug); |
144 assert r != null; |
145 } |
145 return r; |
146 assert r != null; |
|
147 return r; |
|
148 } |
146 } |
149 } |
147 } |
150 } |
148 |
151 |
149 private boolean shouldRetainLocalVariables(long envAddress) { |
152 private boolean shouldRetainLocalVariables(long envAddress) { |
150 GraalHotSpotVMConfig config = graalRuntime.getVMConfig(); |
153 GraalHotSpotVMConfig config = graalRuntime.getVMConfig(); |
162 } |
165 } |
163 } |
166 } |
164 return false; |
167 return false; |
165 } |
168 } |
166 |
169 |
|
170 @Override |
|
171 public boolean isCancelled() { |
|
172 return graalRuntime.isShutdown(); |
|
173 } |
|
174 |
167 public StructuredGraph createGraph(ResolvedJavaMethod method, int entryBCI, boolean useProfilingInfo, CompilationIdentifier compilationId, OptionValues options, DebugContext debug) { |
175 public StructuredGraph createGraph(ResolvedJavaMethod method, int entryBCI, boolean useProfilingInfo, CompilationIdentifier compilationId, OptionValues options, DebugContext debug) { |
168 HotSpotBackend backend = graalRuntime.getHostBackend(); |
176 HotSpotBackend backend = graalRuntime.getHostBackend(); |
169 HotSpotProviders providers = backend.getProviders(); |
177 HotSpotProviders providers = backend.getProviders(); |
170 final boolean isOSR = entryBCI != JVMCICompiler.INVOCATION_ENTRY_BCI; |
178 final boolean isOSR = entryBCI != JVMCICompiler.INVOCATION_ENTRY_BCI; |
171 StructuredGraph graph = method.isNative() || isOSR ? null : providers.getReplacements().getIntrinsicGraph(method, compilationId, debug); |
179 StructuredGraph graph = method.isNative() || isOSR ? null : providers.getReplacements().getIntrinsicGraph(method, compilationId, debug, this); |
172 |
180 |
173 if (graph == null) { |
181 if (graph == null) { |
174 SpeculationLog speculationLog = method.getSpeculationLog(); |
182 SpeculationLog speculationLog = method.getSpeculationLog(); |
175 if (speculationLog != null) { |
183 if (speculationLog != null) { |
176 speculationLog.collectFailedSpeculations(); |
184 speculationLog.collectFailedSpeculations(); |
177 } |
185 } |
178 graph = new StructuredGraph.Builder(options, debug, AllowAssumptions.ifTrue(OptAssumptions.getValue(options))).method(method).entryBCI(entryBCI).speculationLog( |
186 // @formatter:off |
179 speculationLog).useProfilingInfo(useProfilingInfo).compilationId(compilationId).build(); |
187 graph = new StructuredGraph.Builder(options, debug, AllowAssumptions.ifTrue(OptAssumptions.getValue(options))). |
|
188 method(method). |
|
189 cancellable(this). |
|
190 entryBCI(entryBCI). |
|
191 speculationLog(speculationLog). |
|
192 useProfilingInfo(useProfilingInfo). |
|
193 compilationId(compilationId).build(); |
|
194 // @formatter:on |
180 } |
195 } |
181 return graph; |
196 return graph; |
182 } |
197 } |
183 |
198 |
184 public CompilationResult compileHelper(CompilationResultBuilderFactory crbf, CompilationResult result, StructuredGraph graph, ResolvedJavaMethod method, int entryBCI, boolean useProfilingInfo, |
199 public CompilationResult compileHelper(CompilationResultBuilderFactory crbf, CompilationResult result, StructuredGraph graph, ResolvedJavaMethod method, int entryBCI, boolean useProfilingInfo, |
217 } |
232 } |
218 |
233 |
219 return result; |
234 return result; |
220 } |
235 } |
221 |
236 |
222 public CompilationResult compile(ResolvedJavaMethod method, int entryBCI, boolean useProfilingInfo, boolean shouldRetainLocalVariables, CompilationIdentifier compilationId, |
237 public CompilationResult compile(ResolvedJavaMethod method, |
|
238 int entryBCI, |
|
239 boolean useProfilingInfo, |
|
240 boolean shouldRetainLocalVariables, |
|
241 CompilationIdentifier compilationId, |
223 DebugContext debug) { |
242 DebugContext debug) { |
224 StructuredGraph graph = createGraph(method, entryBCI, useProfilingInfo, compilationId, debug.getOptions(), debug); |
243 StructuredGraph graph = createGraph(method, entryBCI, useProfilingInfo, compilationId, debug.getOptions(), debug); |
225 CompilationResult result = new CompilationResult(compilationId); |
244 CompilationResult result = new CompilationResult(compilationId); |
226 return compileHelper(CompilationResultBuilderFactory.Default, result, graph, method, entryBCI, useProfilingInfo, shouldRetainLocalVariables, debug.getOptions()); |
245 return compileHelper(CompilationResultBuilderFactory.Default, result, graph, method, entryBCI, useProfilingInfo, shouldRetainLocalVariables, debug.getOptions()); |
227 } |
246 } |