1 /*
2  * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
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  */
23 
24 
25 package org.graalvm.compiler.hotspot;
26 
27 import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
28 import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
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;
32 
33 import jdk.internal.vm.compiler.collections.EconomicSet;
34 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
35 import org.graalvm.compiler.bytecode.BytecodeProvider;
36 import org.graalvm.compiler.core.common.CompilationIdentifier;
37 import org.graalvm.compiler.debug.DebugContext;
38 import org.graalvm.compiler.debug.GraalError;
39 import org.graalvm.compiler.graph.NodeSourcePosition;
40 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
41 import org.graalvm.compiler.hotspot.meta.HotSpotWordOperationPlugin;
42 import org.graalvm.compiler.hotspot.word.HotSpotOperation;
43 import org.graalvm.compiler.nodes.Cancellable;
44 import org.graalvm.compiler.nodes.Invoke;
45 import org.graalvm.compiler.nodes.StructuredGraph;
46 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
47 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
48 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderPlugin;
49 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
50 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
51 import org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin;
52 import org.graalvm.compiler.nodes.spi.SnippetParameterInfo;
53 import org.graalvm.compiler.options.OptionValues;
54 import org.graalvm.compiler.printer.GraalDebugHandlersFactory;
55 import org.graalvm.compiler.replacements.ReplacementsImpl;
56 
57 import jdk.vm.ci.code.TargetDescription;
58 import jdk.vm.ci.common.NativeImageReinitialize;
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 {
HotSpotReplacementsImpl(HotSpotProviders providers, SnippetReflectionProvider snippetReflection, BytecodeProvider bytecodeProvider, TargetDescription target)66     public HotSpotReplacementsImpl(HotSpotProviders providers, SnippetReflectionProvider snippetReflection, BytecodeProvider bytecodeProvider, TargetDescription target) {
67         super(new GraalDebugHandlersFactory(snippetReflection), providers, snippetReflection, bytecodeProvider, target);
68     }
69 
HotSpotReplacementsImpl(HotSpotReplacementsImpl replacements, HotSpotProviders providers)70     HotSpotReplacementsImpl(HotSpotReplacementsImpl replacements, HotSpotProviders providers) {
71         super(new GraalDebugHandlersFactory(replacements.snippetReflection), providers, replacements.snippetReflection,
72                         replacements.getDefaultReplacementBytecodeProvider(), replacements.target);
73     }
74 
75     @Override
getProviders()76     public HotSpotProviders getProviders() {
77         return (HotSpotProviders) super.getProviders();
78     }
79 
maybeInitializeEncoder(OptionValues options)80     public void maybeInitializeEncoder(OptionValues options) {
81         if (IS_IN_NATIVE_IMAGE) {
82             return;
83         }
84         if (IS_BUILDING_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
85             synchronized (HotSpotReplacementsImpl.class) {
86                 if (snippetEncoder == null) {
87                     snippetEncoder = new SymbolicSnippetEncoder(this);
88                 }
89             }
90         }
91     }
92 
93     @Override
getIntrinsifyingPlugin(ResolvedJavaMethod method)94     public Class<? extends GraphBuilderPlugin> getIntrinsifyingPlugin(ResolvedJavaMethod method) {
95         if (!IS_IN_NATIVE_IMAGE) {
96             if (method.getAnnotation(HotSpotOperation.class) != null) {
97                 return HotSpotWordOperationPlugin.class;
98             }
99         }
100         return super.getIntrinsifyingPlugin(method);
101     }
102 
103     @Override
registerMethodSubstitution(MethodSubstitutionPlugin plugin)104     public void registerMethodSubstitution(MethodSubstitutionPlugin plugin) {
105         if (!IS_IN_NATIVE_IMAGE) {
106             if (snippetEncoder != null) {
107                 snippetEncoder.registerMethodSubstitution(plugin);
108             }
109         }
110     }
111 
112     @Override
registerConditionalPlugin(InvocationPlugin plugin)113     public void registerConditionalPlugin(InvocationPlugin plugin) {
114         if (!IS_IN_NATIVE_IMAGE) {
115             if (snippetEncoder != null) {
116                 snippetEncoder.registerConditionalPlugin(plugin);
117             }
118         }
119     }
120 
checkRegistered(MethodSubstitutionPlugin plugin)121     public void checkRegistered(MethodSubstitutionPlugin plugin) {
122         snippetEncoder.checkRegistered(plugin);
123     }
124 
125     @Override
getIntrinsicGraph(ResolvedJavaMethod method, CompilationIdentifier compilationId, DebugContext debug, AllowAssumptions allowAssumptions, Cancellable cancellable)126     public StructuredGraph getIntrinsicGraph(ResolvedJavaMethod method, CompilationIdentifier compilationId, DebugContext debug, AllowAssumptions allowAssumptions, Cancellable cancellable) {
127         boolean useEncodedGraphs = UseEncodedGraphs.getValue(debug.getOptions());
128         if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
129             HotSpotReplacementsImpl replacements = (HotSpotReplacementsImpl) providers.getReplacements();
130             InvocationPlugin plugin = replacements.getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(method);
131             if (plugin instanceof MethodSubstitutionPlugin) {
132                 MethodSubstitutionPlugin msp = (MethodSubstitutionPlugin) plugin;
133                 if (!IS_IN_NATIVE_IMAGE && useEncodedGraphs) {
134                     replacements.maybeInitializeEncoder(debug.getOptions());
135                     replacements.registerMethodSubstitution(msp);
136                 }
137                 StructuredGraph methodSubstitution = replacements.getMethodSubstitution(msp, method, ROOT_COMPILATION, allowAssumptions, cancellable, debug.getOptions());
138                 methodSubstitution.resetDebug(debug);
139                 return methodSubstitution;
140             }
141             return null;
142         }
143         return super.getIntrinsicGraph(method, compilationId, debug, allowAssumptions, cancellable);
144     }
145 
146     @Override
getSubstitution(ResolvedJavaMethod targetMethod, int invokeBci, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition, AllowAssumptions allowAssumptions, OptionValues options)147     public StructuredGraph getSubstitution(ResolvedJavaMethod targetMethod, int invokeBci, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition,
148                     AllowAssumptions allowAssumptions, OptionValues options) {
149         boolean useEncodedGraphs = UseEncodedGraphs.getValue(options);
150         if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
151             InvocationPlugin plugin = getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(targetMethod);
152             if (plugin instanceof MethodSubstitutionPlugin && (!plugin.inlineOnly() || invokeBci >= 0)) {
153                 MethodSubstitutionPlugin msPlugin = (MethodSubstitutionPlugin) plugin;
154                 if (!IS_IN_NATIVE_IMAGE && useEncodedGraphs) {
155                     maybeInitializeEncoder(options);
156                     registerMethodSubstitution(msPlugin);
157                 }
158                 // This assumes the normal path creates the graph using
159                 // GraphBuilderConfiguration.getSnippetDefault with omits exception edges
160                 StructuredGraph subst = getMethodSubstitution(msPlugin, targetMethod, INLINE_AFTER_PARSING, allowAssumptions, null, options);
161                 return subst;
162             }
163         }
164 
165         return super.getSubstitution(targetMethod, invokeBci, trackNodeSourcePosition, replaceePosition, allowAssumptions, options);
166     }
167 
168     @Override
notifyNotInlined(GraphBuilderContext b, ResolvedJavaMethod method, Invoke invoke)169     public void notifyNotInlined(GraphBuilderContext b, ResolvedJavaMethod method, Invoke invoke) {
170         if (!IS_IN_NATIVE_IMAGE) {
171             if (b.parsingIntrinsic() && snippetEncoder != null) {
172                 if (getIntrinsifyingPlugin(method) != null) {
173                     snippetEncoder.addDelayedInvocationPluginMethod(method);
174                     return;
175                 }
176             }
177         }
178         super.notifyNotInlined(b, method, invoke);
179     }
180 
181     // When assertions are enabled, these fields are used to ensure all snippets are
182     // registered during Graal initialization which in turn ensures that native image
183     // building will not miss any snippets.
184     @NativeImageReinitialize private EconomicSet<ResolvedJavaMethod> registeredSnippets = EconomicSet.create();
185     private boolean snippetRegistrationClosed;
186 
187     @Override
registerSnippet(ResolvedJavaMethod method, ResolvedJavaMethod original, Object receiver, boolean trackNodeSourcePosition, OptionValues options)188     public void registerSnippet(ResolvedJavaMethod method, ResolvedJavaMethod original, Object receiver, boolean trackNodeSourcePosition, OptionValues options) {
189         assert method.isStatic() || receiver != null : "must have a constant type for the receiver";
190         if (!IS_IN_NATIVE_IMAGE) {
191             assert !snippetRegistrationClosed : "Cannot register snippet after registration is closed: " + method.format("%H.%n(%p)");
192             assert registeredSnippets.add(method) : "Cannot register snippet twice: " + method.format("%H.%n(%p)");
193             if (IS_BUILDING_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
194                 synchronized (HotSpotReplacementsImpl.class) {
195                     snippetEncoder.registerSnippet(method, original, receiver, trackNodeSourcePosition, options);
196                 }
197             }
198         }
199     }
200 
201     @Override
getSnippetParameterInfo(ResolvedJavaMethod method)202     public SnippetParameterInfo getSnippetParameterInfo(ResolvedJavaMethod method) {
203         if (IS_IN_NATIVE_IMAGE) {
204             OptionValues options = null;
205             if (getEncodedSnippets(options) == null) {
206                 throw GraalError.shouldNotReachHere("encoded snippets not found");
207             }
208             return getEncodedSnippets(options).getSnippetParameterInfo(method);
209         }
210         return super.getSnippetParameterInfo(method);
211     }
212 
213     @Override
isSnippet(ResolvedJavaMethod method)214     public boolean isSnippet(ResolvedJavaMethod method) {
215         if (IS_IN_NATIVE_IMAGE) {
216             if (encodedSnippets == null) {
217                 throw GraalError.shouldNotReachHere("encoded snippets not found");
218             }
219             return encodedSnippets.isSnippet(method);
220         }
221         return super.isSnippet(method);
222     }
223 
224     @Override
closeSnippetRegistration()225     public void closeSnippetRegistration() {
226         snippetRegistrationClosed = true;
227     }
228 
getEncodedSnippets(OptionValues options)229     public static EncodedSnippets getEncodedSnippets(OptionValues options) {
230         if (!IS_IN_NATIVE_IMAGE && snippetEncoder != null) {
231             snippetEncoder.encode(options);
232         }
233         return encodedSnippets;
234     }
235 
clearSnippetParameterNames()236     public void clearSnippetParameterNames() {
237         assert snippetEncoder != null;
238         snippetEncoder.clearSnippetParameterNames();
239     }
240 
setEncodedSnippets(EncodedSnippets encodedSnippets)241     static void setEncodedSnippets(EncodedSnippets encodedSnippets) {
242         HotSpotReplacementsImpl.encodedSnippets = encodedSnippets;
243     }
244 
encode(OptionValues options)245     public boolean encode(OptionValues options) {
246         SymbolicSnippetEncoder encoder = snippetEncoder;
247         if (encoder != null) {
248             return encoder.encode(options);
249         }
250         return false;
251     }
252 
253     private static volatile EncodedSnippets encodedSnippets;
254 
255     @NativeImageReinitialize private static SymbolicSnippetEncoder snippetEncoder;
256 
257     @Override
getSnippet(ResolvedJavaMethod method, ResolvedJavaMethod recursiveEntry, Object[] args, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition, OptionValues options)258     public StructuredGraph getSnippet(ResolvedJavaMethod method, ResolvedJavaMethod recursiveEntry, Object[] args, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition,
259                     OptionValues options) {
260         StructuredGraph graph = getEncodedSnippet(method, args, StructuredGraph.AllowAssumptions.NO, options);
261         if (graph != null) {
262             return graph;
263         }
264 
265         assert !IS_IN_NATIVE_IMAGE : "should be using encoded snippets";
266         return super.getSnippet(method, recursiveEntry, args, trackNodeSourcePosition, replaceePosition, options);
267     }
268 
269     @SuppressWarnings("try")
getEncodedSnippet(ResolvedJavaMethod method, Object[] args, StructuredGraph.AllowAssumptions allowAssumptions, OptionValues options)270     private StructuredGraph getEncodedSnippet(ResolvedJavaMethod method, Object[] args, StructuredGraph.AllowAssumptions allowAssumptions, OptionValues options) {
271         if (IS_IN_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
272             synchronized (HotSpotReplacementsImpl.class) {
273                 if (getEncodedSnippets(options) == null) {
274                     throw GraalError.shouldNotReachHere("encoded snippets not found");
275                 }
276                 // Snippets graphs can contain foreign object reference and
277                 // outlive a single compilation.
278                 try (CompilationContext scope = HotSpotGraalServices.enterGlobalCompilationContext()) {
279                     StructuredGraph graph = getEncodedSnippets(options).getEncodedSnippet(method, this, args, allowAssumptions, options);
280                     if (graph == null) {
281                         throw GraalError.shouldNotReachHere("snippet not found: " + method.format("%H.%n(%p)"));
282                     }
283                     return graph;
284                 }
285             }
286         } else {
287             assert registeredSnippets == null || registeredSnippets.contains(method) : "Asking for snippet method that was never registered: " + method.format("%H.%n(%p)");
288         }
289         return null;
290     }
291 
292     @Override
getMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context, StructuredGraph.AllowAssumptions allowAssumptions, Cancellable cancellable, OptionValues options)293     public StructuredGraph getMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context,
294                     StructuredGraph.AllowAssumptions allowAssumptions, Cancellable cancellable, OptionValues options) {
295         if (IS_IN_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
296             if (getEncodedSnippets(options) == null) {
297                 throw GraalError.shouldNotReachHere("encoded snippets not found");
298             }
299             return getEncodedSnippets(options).getMethodSubstitutionGraph(plugin, original, this, context, allowAssumptions, cancellable, options);
300         }
301         return null;
302     }
303 
304     @SuppressWarnings("unchecked")
305     @Override
getInjectedArgument(Class<T> capability)306     public <T> T getInjectedArgument(Class<T> capability) {
307         if (capability.equals(GraalHotSpotVMConfig.class)) {
308             return (T) getProviders().getConfig();
309         }
310         return super.getInjectedArgument(capability);
311     }
312 }
313