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