43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2013, 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.hotspot;
|
|
26 |
|
54084
|
27 |
import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
|
|
28 |
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
|
54601
|
29 |
import static org.graalvm.compiler.core.common.GraalOptions.UseEncodedGraphs;
|
|
30 |
import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
|
|
31 |
import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.ROOT_COMPILATION;
|
54084
|
32 |
|
|
33 |
import java.util.Set;
|
|
34 |
|
|
35 |
import jdk.internal.vm.compiler.collections.EconomicSet;
|
43972
|
36 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
|
37 |
import org.graalvm.compiler.bytecode.BytecodeProvider;
|
54084
|
38 |
import org.graalvm.compiler.core.common.CompilationIdentifier;
|
|
39 |
import org.graalvm.compiler.debug.DebugContext;
|
|
40 |
import org.graalvm.compiler.debug.GraalError;
|
|
41 |
import org.graalvm.compiler.graph.NodeSourcePosition;
|
52956
|
42 |
import org.graalvm.compiler.hotspot.meta.HotSpotWordOperationPlugin;
|
43972
|
43 |
import org.graalvm.compiler.hotspot.word.HotSpotOperation;
|
55509
|
44 |
import org.graalvm.compiler.nodes.Cancellable;
|
54084
|
45 |
import org.graalvm.compiler.nodes.Invoke;
|
|
46 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
47 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
|
52956
|
48 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderPlugin;
|
54601
|
49 |
import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
|
54084
|
50 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
|
|
51 |
import org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin;
|
46344
|
52 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
53 |
import org.graalvm.compiler.phases.util.Providers;
|
46640
|
54 |
import org.graalvm.compiler.printer.GraalDebugHandlersFactory;
|
43972
|
55 |
import org.graalvm.compiler.replacements.ReplacementsImpl;
|
|
56 |
|
|
57 |
import jdk.vm.ci.code.TargetDescription;
|
54084
|
58 |
import jdk.vm.ci.common.NativeImageReinitialize;
|
43972
|
59 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
60 |
|
|
61 |
/**
|
|
62 |
* Filters certain method substitutions based on whether there is underlying hardware support for
|
|
63 |
* them.
|
|
64 |
*/
|
|
65 |
public class HotSpotReplacementsImpl extends ReplacementsImpl {
|
54601
|
66 |
public HotSpotReplacementsImpl(Providers providers, SnippetReflectionProvider snippetReflection, BytecodeProvider bytecodeProvider, TargetDescription target) {
|
|
67 |
super(new GraalDebugHandlersFactory(snippetReflection), providers, snippetReflection, bytecodeProvider, target);
|
43972
|
68 |
}
|
|
69 |
|
54601
|
70 |
HotSpotReplacementsImpl(HotSpotReplacementsImpl replacements, Providers providers) {
|
|
71 |
super(new GraalDebugHandlersFactory(replacements.snippetReflection), providers, replacements.snippetReflection,
|
54084
|
72 |
replacements.getDefaultReplacementBytecodeProvider(), replacements.target);
|
|
73 |
}
|
|
74 |
|
43972
|
75 |
@Override
|
52956
|
76 |
public Class<? extends GraphBuilderPlugin> getIntrinsifyingPlugin(ResolvedJavaMethod method) {
|
|
77 |
return method.getAnnotation(HotSpotOperation.class) != null ? HotSpotWordOperationPlugin.class : super.getIntrinsifyingPlugin(method);
|
|
78 |
}
|
|
79 |
|
54601
|
80 |
@Override
|
|
81 |
public void registerMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context, OptionValues options) {
|
54084
|
82 |
if (!IS_IN_NATIVE_IMAGE) {
|
54601
|
83 |
if (IS_BUILDING_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
|
54084
|
84 |
synchronized (HotSpotReplacementsImpl.class) {
|
|
85 |
if (snippetEncoder == null) {
|
|
86 |
snippetEncoder = new SymbolicSnippetEncoder(this);
|
|
87 |
}
|
54601
|
88 |
snippetEncoder.registerMethodSubstitution(plugin, original, context, options);
|
54084
|
89 |
}
|
|
90 |
}
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
@Override
|
55509
|
95 |
public StructuredGraph getIntrinsicGraph(ResolvedJavaMethod method, CompilationIdentifier compilationId, DebugContext debug, Cancellable cancellable) {
|
54601
|
96 |
boolean useEncodedGraphs = UseEncodedGraphs.getValue(debug.getOptions());
|
|
97 |
if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
|
54084
|
98 |
HotSpotReplacementsImpl replacements = (HotSpotReplacementsImpl) providers.getReplacements();
|
|
99 |
InvocationPlugin plugin = replacements.getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(method);
|
|
100 |
if (plugin instanceof MethodSubstitutionPlugin) {
|
|
101 |
MethodSubstitutionPlugin msp = (MethodSubstitutionPlugin) plugin;
|
54601
|
102 |
if (useEncodedGraphs) {
|
|
103 |
replacements.registerMethodSubstitution(msp, method, ROOT_COMPILATION, debug.getOptions());
|
|
104 |
}
|
55509
|
105 |
StructuredGraph methodSubstitution = replacements.getMethodSubstitution(msp, method, ROOT_COMPILATION, StructuredGraph.AllowAssumptions.YES, cancellable, debug.getOptions());
|
54601
|
106 |
methodSubstitution.resetDebug(debug);
|
|
107 |
return methodSubstitution;
|
54084
|
108 |
}
|
|
109 |
return null;
|
|
110 |
}
|
55509
|
111 |
return super.getIntrinsicGraph(method, compilationId, debug, cancellable);
|
54084
|
112 |
}
|
|
113 |
|
|
114 |
@Override
|
54601
|
115 |
public StructuredGraph getSubstitution(ResolvedJavaMethod targetMethod, int invokeBci, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition, OptionValues options) {
|
|
116 |
boolean useEncodedGraphs = UseEncodedGraphs.getValue(options);
|
|
117 |
if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
|
|
118 |
InvocationPlugin plugin = getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(targetMethod);
|
|
119 |
if (plugin instanceof MethodSubstitutionPlugin && (!plugin.inlineOnly() || invokeBci >= 0)) {
|
|
120 |
MethodSubstitutionPlugin msPlugin = (MethodSubstitutionPlugin) plugin;
|
|
121 |
if (!IS_IN_NATIVE_IMAGE && useEncodedGraphs) {
|
|
122 |
registerMethodSubstitution(msPlugin, targetMethod, INLINE_AFTER_PARSING, options);
|
|
123 |
}
|
|
124 |
// This assumes the normal path creates the graph using
|
|
125 |
// GraphBuilderConfiguration.getSnippetDefault with omits exception edges
|
55509
|
126 |
StructuredGraph subst = getMethodSubstitution(msPlugin, targetMethod, INLINE_AFTER_PARSING, StructuredGraph.AllowAssumptions.NO, null, options);
|
54601
|
127 |
return subst;
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
return super.getSubstitution(targetMethod, invokeBci, trackNodeSourcePosition, replaceePosition, options);
|
|
132 |
}
|
|
133 |
|
|
134 |
@Override
|
54084
|
135 |
public void notifyNotInlined(GraphBuilderContext b, ResolvedJavaMethod method, Invoke invoke) {
|
|
136 |
if (b.parsingIntrinsic() && snippetEncoder != null) {
|
|
137 |
if (getIntrinsifyingPlugin(method) != null) {
|
|
138 |
snippetEncoder.addDelayedInvocationPluginMethod(method);
|
|
139 |
return;
|
|
140 |
}
|
|
141 |
}
|
|
142 |
super.notifyNotInlined(b, method, invoke);
|
|
143 |
}
|
|
144 |
|
|
145 |
// When assertions are enabled, these fields are used to ensure all snippets are
|
|
146 |
// registered during Graal initialization which in turn ensures that native image
|
|
147 |
// building will not miss any snippets.
|
|
148 |
@NativeImageReinitialize private EconomicSet<ResolvedJavaMethod> registeredSnippets = EconomicSet.create();
|
52956
|
149 |
private boolean snippetRegistrationClosed;
|
|
150 |
|
|
151 |
@Override
|
54601
|
152 |
public void registerSnippet(ResolvedJavaMethod method, ResolvedJavaMethod original, Object receiver, boolean trackNodeSourcePosition, OptionValues options) {
|
54084
|
153 |
if (!IS_IN_NATIVE_IMAGE) {
|
|
154 |
assert !snippetRegistrationClosed : "Cannot register snippet after registration is closed: " + method.format("%H.%n(%p)");
|
|
155 |
assert registeredSnippets.add(method) : "Cannot register snippet twice: " + method.format("%H.%n(%p)");
|
54601
|
156 |
if (IS_BUILDING_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
|
54084
|
157 |
synchronized (HotSpotReplacementsImpl.class) {
|
|
158 |
if (snippetEncoder == null) {
|
|
159 |
snippetEncoder = new SymbolicSnippetEncoder(this);
|
|
160 |
}
|
54601
|
161 |
snippetEncoder.registerSnippet(method, original, receiver, trackNodeSourcePosition, options);
|
54084
|
162 |
}
|
|
163 |
}
|
|
164 |
}
|
52956
|
165 |
}
|
|
166 |
|
|
167 |
@Override
|
|
168 |
public void closeSnippetRegistration() {
|
|
169 |
snippetRegistrationClosed = true;
|
43972
|
170 |
}
|
54084
|
171 |
|
54601
|
172 |
private static SymbolicSnippetEncoder.EncodedSnippets getEncodedSnippets() {
|
54084
|
173 |
return encodedSnippets;
|
|
174 |
}
|
|
175 |
|
|
176 |
public Set<ResolvedJavaMethod> getSnippetMethods() {
|
|
177 |
if (snippetEncoder != null) {
|
|
178 |
return snippetEncoder.getSnippetMethods();
|
|
179 |
}
|
|
180 |
return null;
|
|
181 |
}
|
|
182 |
|
|
183 |
static void setEncodedSnippets(SymbolicSnippetEncoder.EncodedSnippets encodedSnippets) {
|
|
184 |
HotSpotReplacementsImpl.encodedSnippets = encodedSnippets;
|
|
185 |
}
|
|
186 |
|
54601
|
187 |
public boolean encode(OptionValues options) {
|
54084
|
188 |
SymbolicSnippetEncoder encoder = HotSpotReplacementsImpl.snippetEncoder;
|
|
189 |
if (encoder != null) {
|
54601
|
190 |
return encoder.encode(options);
|
54084
|
191 |
}
|
|
192 |
return false;
|
|
193 |
}
|
|
194 |
|
|
195 |
private static volatile SymbolicSnippetEncoder.EncodedSnippets encodedSnippets;
|
|
196 |
|
54601
|
197 |
@NativeImageReinitialize private static SymbolicSnippetEncoder snippetEncoder;
|
54084
|
198 |
|
|
199 |
@Override
|
54601
|
200 |
public StructuredGraph getSnippet(ResolvedJavaMethod method, ResolvedJavaMethod recursiveEntry, Object[] args, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition,
|
|
201 |
OptionValues options) {
|
|
202 |
StructuredGraph graph = getEncodedSnippet(method, args, StructuredGraph.AllowAssumptions.NO, options);
|
54084
|
203 |
if (graph != null) {
|
|
204 |
return graph;
|
|
205 |
}
|
|
206 |
|
|
207 |
assert !IS_IN_NATIVE_IMAGE : "should be using encoded snippets";
|
54601
|
208 |
return super.getSnippet(method, recursiveEntry, args, trackNodeSourcePosition, replaceePosition, options);
|
54084
|
209 |
}
|
|
210 |
|
57537
|
211 |
@SuppressWarnings("try")
|
54601
|
212 |
private StructuredGraph getEncodedSnippet(ResolvedJavaMethod method, Object[] args, StructuredGraph.AllowAssumptions allowAssumptions, OptionValues options) {
|
|
213 |
boolean useEncodedGraphs = UseEncodedGraphs.getValue(options);
|
|
214 |
if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
|
54084
|
215 |
synchronized (HotSpotReplacementsImpl.class) {
|
54601
|
216 |
if (!IS_IN_NATIVE_IMAGE) {
|
|
217 |
snippetEncoder.encode(options);
|
54084
|
218 |
}
|
|
219 |
|
|
220 |
if (getEncodedSnippets() == null) {
|
|
221 |
throw GraalError.shouldNotReachHere("encoded snippets not found");
|
|
222 |
}
|
57537
|
223 |
// Snippets graphs can contain foreign object reference and
|
|
224 |
// outlive a single compilation.
|
|
225 |
try (CompilationContext scope = HotSpotGraalServices.enterGlobalCompilationContext()) {
|
|
226 |
StructuredGraph graph = getEncodedSnippets().getEncodedSnippet(method, this, args, allowAssumptions, options);
|
|
227 |
if (graph == null) {
|
|
228 |
throw GraalError.shouldNotReachHere("snippet not found: " + method.format("%H.%n(%p)"));
|
|
229 |
}
|
|
230 |
return graph;
|
54084
|
231 |
}
|
|
232 |
}
|
54601
|
233 |
} else {
|
|
234 |
assert registeredSnippets == null || registeredSnippets.contains(method) : "Asking for snippet method that was never registered: " + method.format("%H.%n(%p)");
|
54084
|
235 |
}
|
|
236 |
return null;
|
|
237 |
}
|
|
238 |
|
54601
|
239 |
@Override
|
|
240 |
public StructuredGraph getMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context,
|
55509
|
241 |
StructuredGraph.AllowAssumptions allowAssumptions, Cancellable cancellable, OptionValues options) {
|
54601
|
242 |
boolean useEncodedGraphs = UseEncodedGraphs.getValue(options);
|
|
243 |
if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
|
|
244 |
if (!IS_IN_NATIVE_IMAGE) {
|
|
245 |
snippetEncoder.encode(options);
|
|
246 |
}
|
|
247 |
|
54084
|
248 |
if (getEncodedSnippets() == null) {
|
|
249 |
throw GraalError.shouldNotReachHere("encoded snippets not found");
|
|
250 |
}
|
55509
|
251 |
return getEncodedSnippets().getMethodSubstitutionGraph(plugin, original, this, context, allowAssumptions, cancellable, options);
|
54084
|
252 |
}
|
|
253 |
return null;
|
|
254 |
}
|
|
255 |
|
43972
|
256 |
}
|