43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2009, 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.core.target;
|
|
26 |
|
46640
|
27 |
import java.util.ArrayList;
|
|
28 |
|
43972
|
29 |
import org.graalvm.compiler.code.CompilationResult;
|
|
30 |
import org.graalvm.compiler.core.common.CompilationIdentifier;
|
|
31 |
import org.graalvm.compiler.core.common.LIRKind;
|
|
32 |
import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
|
|
33 |
import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
|
|
34 |
import org.graalvm.compiler.core.common.spi.ForeignCallsProvider;
|
54084
|
35 |
import org.graalvm.compiler.core.gen.LIRCompilerBackend;
|
46640
|
36 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
37 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
|
54084
|
38 |
import org.graalvm.compiler.lir.phases.LIRSuites;
|
43972
|
39 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
52578
|
40 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
41 |
import org.graalvm.compiler.phases.tiers.SuitesProvider;
|
|
42 |
import org.graalvm.compiler.phases.tiers.TargetProvider;
|
|
43 |
import org.graalvm.compiler.phases.util.Providers;
|
|
44 |
|
|
45 |
import jdk.vm.ci.code.BailoutException;
|
|
46 |
import jdk.vm.ci.code.CodeCacheProvider;
|
|
47 |
import jdk.vm.ci.code.CompilationRequest;
|
|
48 |
import jdk.vm.ci.code.CompiledCode;
|
|
49 |
import jdk.vm.ci.code.InstalledCode;
|
|
50 |
import jdk.vm.ci.code.RegisterConfig;
|
|
51 |
import jdk.vm.ci.code.TargetDescription;
|
|
52 |
import jdk.vm.ci.code.ValueKindFactory;
|
|
53 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
|
54 |
import jdk.vm.ci.meta.JavaKind;
|
|
55 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
56 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
57 |
|
|
58 |
/**
|
|
59 |
* Represents a compiler backend for Graal.
|
|
60 |
*/
|
|
61 |
public abstract class Backend implements TargetProvider, ValueKindFactory<LIRKind> {
|
|
62 |
|
|
63 |
private final Providers providers;
|
46551
|
64 |
private final ArrayList<CodeInstallationTaskFactory> codeInstallationTaskFactories;
|
43972
|
65 |
|
|
66 |
public static final ForeignCallDescriptor ARITHMETIC_FREM = new ForeignCallDescriptor("arithmeticFrem", float.class, float.class, float.class);
|
|
67 |
public static final ForeignCallDescriptor ARITHMETIC_DREM = new ForeignCallDescriptor("arithmeticDrem", double.class, double.class, double.class);
|
|
68 |
|
|
69 |
protected Backend(Providers providers) {
|
|
70 |
this.providers = providers;
|
46551
|
71 |
this.codeInstallationTaskFactories = new ArrayList<>();
|
|
72 |
}
|
|
73 |
|
|
74 |
public synchronized void addCodeInstallationTask(CodeInstallationTaskFactory factory) {
|
|
75 |
this.codeInstallationTaskFactories.add(factory);
|
43972
|
76 |
}
|
|
77 |
|
|
78 |
public Providers getProviders() {
|
|
79 |
return providers;
|
|
80 |
}
|
|
81 |
|
|
82 |
public CodeCacheProvider getCodeCache() {
|
|
83 |
return providers.getCodeCache();
|
|
84 |
}
|
|
85 |
|
|
86 |
public MetaAccessProvider getMetaAccess() {
|
|
87 |
return providers.getMetaAccess();
|
|
88 |
}
|
|
89 |
|
|
90 |
public ConstantReflectionProvider getConstantReflection() {
|
|
91 |
return providers.getConstantReflection();
|
|
92 |
}
|
|
93 |
|
|
94 |
public ForeignCallsProvider getForeignCalls() {
|
|
95 |
return providers.getForeignCalls();
|
|
96 |
}
|
|
97 |
|
|
98 |
public abstract SuitesProvider getSuites();
|
|
99 |
|
|
100 |
@Override
|
|
101 |
public TargetDescription getTarget() {
|
|
102 |
return providers.getCodeCache().getTarget();
|
|
103 |
}
|
|
104 |
|
|
105 |
@Override
|
|
106 |
public LIRKind getValueKind(JavaKind javaKind) {
|
|
107 |
return LIRKind.fromJavaKind(getTarget().arch, javaKind);
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
46344
|
111 |
* Creates a new configuration for register allocation.
|
|
112 |
*
|
|
113 |
* @param allocationRestrictedTo if not {@code null}, register allocation will be restricted to
|
|
114 |
* registers whose names appear in this array
|
|
115 |
*/
|
|
116 |
public abstract RegisterAllocationConfig newRegisterAllocationConfig(RegisterConfig registerConfig, String[] allocationRestrictedTo);
|
43972
|
117 |
|
|
118 |
/**
|
|
119 |
* Turns a Graal {@link CompilationResult} into a {@link CompiledCode} object that can be passed
|
|
120 |
* to the VM for code installation.
|
|
121 |
*/
|
52578
|
122 |
protected abstract CompiledCode createCompiledCode(ResolvedJavaMethod method, CompilationRequest compilationRequest, CompilationResult compilationResult, boolean isDefault, OptionValues options);
|
43972
|
123 |
|
|
124 |
/**
|
46640
|
125 |
* @see #createInstalledCode(DebugContext, ResolvedJavaMethod, CompilationRequest,
|
54084
|
126 |
* CompilationResult, InstalledCode, boolean, Object[])
|
43972
|
127 |
*/
|
54084
|
128 |
public InstalledCode createInstalledCode(DebugContext debug,
|
|
129 |
ResolvedJavaMethod method,
|
|
130 |
CompilationResult compilationResult,
|
|
131 |
InstalledCode predefinedInstalledCode,
|
|
132 |
boolean isDefault) {
|
|
133 |
return createInstalledCode(debug, method, null, compilationResult, predefinedInstalledCode, isDefault, null);
|
43972
|
134 |
}
|
|
135 |
|
|
136 |
/**
|
46640
|
137 |
* @see #createInstalledCode(DebugContext, ResolvedJavaMethod, CompilationRequest,
|
54084
|
138 |
* CompilationResult, InstalledCode, boolean, Object[])
|
46551
|
139 |
*/
|
|
140 |
@SuppressWarnings("try")
|
54084
|
141 |
public InstalledCode createInstalledCode(DebugContext debug,
|
|
142 |
ResolvedJavaMethod method,
|
|
143 |
CompilationRequest compilationRequest,
|
|
144 |
CompilationResult compilationResult,
|
|
145 |
InstalledCode predefinedInstalledCode,
|
|
146 |
boolean isDefault) {
|
|
147 |
return createInstalledCode(debug, method, compilationRequest, compilationResult, predefinedInstalledCode, isDefault, null);
|
46551
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
43972
|
151 |
* Installs code based on a given compilation result.
|
|
152 |
*
|
|
153 |
* @param method the method compiled to produce {@code compiledCode} or {@code null} if the
|
|
154 |
* input to {@code compResult} was not a {@link ResolvedJavaMethod}
|
|
155 |
* @param compilationRequest the compilation request or {@code null}
|
49451
|
156 |
* @param compilationResult the code to be installed
|
43972
|
157 |
* @param predefinedInstalledCode a pre-allocated {@link InstalledCode} object to use as a
|
|
158 |
* reference to the installed code. If {@code null}, a new {@link InstalledCode}
|
|
159 |
* object will be created.
|
|
160 |
* @param isDefault specifies if the installed code should be made the default implementation of
|
|
161 |
* {@code compRequest.getMethod()}. The default implementation for a method is the
|
|
162 |
* code executed for standard calls to the method. This argument is ignored if
|
|
163 |
* {@code compRequest == null}.
|
46640
|
164 |
* @param context a custom debug context to use for the code installation
|
43972
|
165 |
* @return a reference to the compiled and ready-to-run installed code
|
|
166 |
* @throws BailoutException if the code installation failed
|
52578
|
167 |
* @throws IllegalArgumentException if {@code installedCode != null} and this platform does not
|
|
168 |
* {@linkplain CodeCacheProvider#installCode support} a predefined
|
|
169 |
* {@link InstalledCode} object
|
43972
|
170 |
*/
|
|
171 |
@SuppressWarnings("try")
|
54084
|
172 |
public InstalledCode createInstalledCode(DebugContext debug,
|
|
173 |
ResolvedJavaMethod method,
|
|
174 |
CompilationRequest compilationRequest,
|
|
175 |
CompilationResult compilationResult,
|
|
176 |
InstalledCode predefinedInstalledCode,
|
|
177 |
boolean isDefault,
|
|
178 |
Object[] context) {
|
46551
|
179 |
Object[] debugContext = context != null ? context : new Object[]{getProviders().getCodeCache(), method, compilationResult};
|
47798
|
180 |
CodeInstallationTask[] tasks;
|
|
181 |
synchronized (this) {
|
|
182 |
tasks = new CodeInstallationTask[codeInstallationTaskFactories.size()];
|
|
183 |
for (int i = 0; i < codeInstallationTaskFactories.size(); i++) {
|
|
184 |
tasks[i] = codeInstallationTaskFactories.get(i).create();
|
|
185 |
}
|
46551
|
186 |
}
|
46640
|
187 |
try (DebugContext.Scope s2 = debug.scope("CodeInstall", debugContext);
|
|
188 |
DebugContext.Activation a = debug.activate()) {
|
48861
|
189 |
|
|
190 |
InstalledCode installedCode;
|
|
191 |
try {
|
52578
|
192 |
preCodeInstallationTasks(tasks, compilationResult);
|
|
193 |
CompiledCode compiledCode = createCompiledCode(method, compilationRequest, compilationResult, isDefault, debug.getOptions());
|
54084
|
194 |
installedCode = getProviders().getCodeCache().installCode(method, compiledCode, predefinedInstalledCode, compilationResult.getSpeculationLog(), isDefault);
|
49451
|
195 |
assert predefinedInstalledCode == null || installedCode == predefinedInstalledCode;
|
48861
|
196 |
} catch (Throwable t) {
|
|
197 |
failCodeInstallationTasks(tasks, t);
|
|
198 |
throw t;
|
46551
|
199 |
}
|
|
200 |
|
52578
|
201 |
postCodeInstallationTasks(tasks, compilationResult, installedCode);
|
46551
|
202 |
|
|
203 |
return installedCode;
|
43972
|
204 |
} catch (Throwable e) {
|
46640
|
205 |
throw debug.handle(e);
|
43972
|
206 |
}
|
|
207 |
}
|
|
208 |
|
48861
|
209 |
private static void failCodeInstallationTasks(CodeInstallationTask[] tasks, Throwable t) {
|
|
210 |
for (CodeInstallationTask task : tasks) {
|
|
211 |
task.installFailed(t);
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
52578
|
215 |
private static void preCodeInstallationTasks(CodeInstallationTask[] tasks, CompilationResult compilationResult) {
|
48861
|
216 |
for (CodeInstallationTask task : tasks) {
|
52578
|
217 |
task.preProcess(compilationResult);
|
48861
|
218 |
}
|
|
219 |
}
|
|
220 |
|
52578
|
221 |
private static void postCodeInstallationTasks(CodeInstallationTask[] tasks, CompilationResult compilationResult, InstalledCode installedCode) {
|
48861
|
222 |
try {
|
|
223 |
for (CodeInstallationTask task : tasks) {
|
52578
|
224 |
task.postProcess(compilationResult, installedCode);
|
48861
|
225 |
}
|
|
226 |
} catch (Throwable t) {
|
|
227 |
installedCode.invalidate();
|
|
228 |
throw t;
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
43972
|
232 |
/**
|
|
233 |
* Installs code based on a given compilation result.
|
|
234 |
*
|
|
235 |
* @param method the method compiled to produce {@code compiledCode} or {@code null} if the
|
|
236 |
* input to {@code compResult} was not a {@link ResolvedJavaMethod}
|
|
237 |
* @param compilationRequest the request or {@code null}
|
54084
|
238 |
* @param compilationResult the compiled code
|
43972
|
239 |
* @return a reference to the compiled and ready-to-run installed code
|
|
240 |
* @throws BailoutException if the code installation failed
|
|
241 |
*/
|
54084
|
242 |
public InstalledCode addInstalledCode(DebugContext debug,
|
|
243 |
ResolvedJavaMethod method,
|
|
244 |
CompilationRequest compilationRequest,
|
|
245 |
CompilationResult compilationResult) {
|
|
246 |
return createInstalledCode(debug, method, compilationRequest, compilationResult, null, false);
|
43972
|
247 |
}
|
|
248 |
|
|
249 |
/**
|
|
250 |
* Installs code based on a given compilation result and sets it as the default code to be used
|
|
251 |
* when {@code method} is invoked.
|
|
252 |
*
|
|
253 |
* @param method the method compiled to produce {@code compiledCode} or {@code null} if the
|
|
254 |
* input to {@code compResult} was not a {@link ResolvedJavaMethod}
|
54084
|
255 |
* @param compilationResult the compiled code
|
43972
|
256 |
* @return a reference to the compiled and ready-to-run installed code
|
|
257 |
* @throws BailoutException if the code installation failed
|
|
258 |
*/
|
46640
|
259 |
public InstalledCode createDefaultInstalledCode(DebugContext debug, ResolvedJavaMethod method, CompilationResult compilationResult) {
|
54084
|
260 |
return createInstalledCode(debug, method, compilationResult, null, true);
|
43972
|
261 |
}
|
|
262 |
|
|
263 |
/**
|
|
264 |
* Gets the compilation id for a given {@link ResolvedJavaMethod}. Returns
|
|
265 |
* {@code CompilationIdentifier#INVALID_COMPILATION_ID} in case there is no such id.
|
|
266 |
*
|
|
267 |
* @param resolvedJavaMethod
|
|
268 |
*/
|
|
269 |
public CompilationIdentifier getCompilationIdentifier(ResolvedJavaMethod resolvedJavaMethod) {
|
|
270 |
return CompilationIdentifier.INVALID_COMPILATION_ID;
|
|
271 |
}
|
46551
|
272 |
|
54084
|
273 |
public void emitBackEnd(StructuredGraph graph,
|
|
274 |
Object stub,
|
|
275 |
ResolvedJavaMethod installedCodeOwner,
|
|
276 |
CompilationResult compilationResult,
|
|
277 |
CompilationResultBuilderFactory factory,
|
|
278 |
RegisterConfig config, LIRSuites lirSuites) {
|
|
279 |
LIRCompilerBackend.emitBackEnd(graph, stub, installedCodeOwner, this, compilationResult, factory, config, lirSuites);
|
|
280 |
}
|
|
281 |
|
46551
|
282 |
/**
|
|
283 |
* Encapsulates custom tasks done before and after code installation.
|
|
284 |
*/
|
|
285 |
public abstract static class CodeInstallationTask {
|
|
286 |
/**
|
|
287 |
* Task to run before code installation.
|
49451
|
288 |
*
|
|
289 |
* @param compilationResult the code about to be installed
|
|
290 |
*
|
46551
|
291 |
*/
|
52578
|
292 |
public void preProcess(CompilationResult compilationResult) {
|
46551
|
293 |
}
|
|
294 |
|
|
295 |
/**
|
|
296 |
* Task to run after the code is installed.
|
49451
|
297 |
*
|
52578
|
298 |
* @param compilationResult the code about to be installed
|
49451
|
299 |
* @param installedCode a reference to the installed code
|
46551
|
300 |
*/
|
52578
|
301 |
public void postProcess(CompilationResult compilationResult, InstalledCode installedCode) {
|
46551
|
302 |
}
|
|
303 |
|
|
304 |
/**
|
48861
|
305 |
* Invoked after {@link #preProcess} when code installation fails.
|
49451
|
306 |
*
|
|
307 |
* @param cause the cause of the installation failure
|
46551
|
308 |
*/
|
49451
|
309 |
public void installFailed(Throwable cause) {
|
46551
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
/**
|
|
314 |
* Creates code installation tasks.
|
|
315 |
*/
|
|
316 |
public abstract static class CodeInstallationTaskFactory {
|
|
317 |
public abstract CodeInstallationTask create();
|
|
318 |
}
|
43972
|
319 |
}
|