< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot/src/org/graalvm/compiler/hotspot/stubs/SnippetStub.java

Print this page




   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.stubs;
  26 
  27 import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
  28 
  29 import java.lang.reflect.Method;
  30 
  31 import org.graalvm.compiler.api.replacements.Snippet;
  32 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
  33 import org.graalvm.compiler.api.replacements.Snippet.NonNullParameter;
  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.core.common.type.StampFactory;
  38 import org.graalvm.compiler.debug.DebugContext;
  39 import org.graalvm.compiler.debug.GraalError;
  40 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
  41 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  42 import org.graalvm.compiler.java.GraphBuilderPhase;
  43 import org.graalvm.compiler.nodes.NodeView;
  44 import org.graalvm.compiler.nodes.ParameterNode;
  45 import org.graalvm.compiler.nodes.StructuredGraph;
  46 import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;
  47 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  48 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  49 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
  50 import org.graalvm.compiler.nodes.spi.LoweringTool;
  51 import org.graalvm.compiler.options.OptionValues;
  52 import org.graalvm.compiler.phases.OptimisticOptimizations;
  53 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  54 import org.graalvm.compiler.phases.common.LoweringPhase;
  55 import org.graalvm.compiler.phases.common.RemoveValueProxyPhase;
  56 import org.graalvm.compiler.phases.tiers.PhaseContext;
  57 import org.graalvm.compiler.replacements.ConstantBindingParameterPlugin;
  58 import org.graalvm.compiler.replacements.SnippetTemplate;
  59 import org.graalvm.compiler.replacements.Snippets;
  60 
  61 import jdk.vm.ci.meta.Local;
  62 import jdk.vm.ci.meta.LocalVariableTable;
  63 import jdk.vm.ci.meta.MetaAccessProvider;
  64 import jdk.vm.ci.meta.ResolvedJavaMethod;
  65 
  66 /**
  67  * Base class for a stub defined by a snippet.
  68  */
  69 public abstract class SnippetStub extends Stub implements Snippets {
  70 
  71     protected final ResolvedJavaMethod method;
  72 
  73     /**
  74      * Creates a new snippet stub.
  75      *
  76      * @param snippetMethodName name of the single {@link Snippet} annotated method in the class of
  77      *            this object
  78      * @param linkage linkage details for a call to the stub
  79      */
  80     public SnippetStub(String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
  81         this(null, snippetMethodName, options, providers, linkage);
  82     }
  83 
  84     /**
  85      * Creates a new snippet stub.
  86      *
  87      * @param snippetDeclaringClass this class in which the {@link Snippet} annotated method is
  88      *            declared. If {@code null}, this the class of this object is used.
  89      * @param snippetMethodName name of the single {@link Snippet} annotated method in
  90      *            {@code snippetDeclaringClass}
  91      * @param linkage linkage details for a call to the stub
  92      */
  93     public SnippetStub(Class<? extends Snippets> snippetDeclaringClass, String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
  94         super(options, providers, linkage);
  95         Method javaMethod = SnippetTemplate.AbstractTemplates.findMethod(snippetDeclaringClass == null ? getClass() : snippetDeclaringClass, snippetMethodName, null);
  96         this.method = providers.getMetaAccess().lookupJavaMethod(javaMethod);




  97     }
  98 
  99     @Override
 100     @SuppressWarnings("try")
 101     protected StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId) {
 102         Plugins defaultPlugins = providers.getGraphBuilderPlugins();
 103         MetaAccessProvider metaAccess = providers.getMetaAccess();
 104         SnippetReflectionProvider snippetReflection = providers.getSnippetReflection();
 105 
 106         Plugins plugins = new Plugins(defaultPlugins);
 107         plugins.prependParameterPlugin(new ConstantBindingParameterPlugin(makeConstArgs(), metaAccess, snippetReflection));
 108         GraphBuilderConfiguration config = GraphBuilderConfiguration.getSnippetDefault(plugins);
 109 
 110         // @formatter:off
 111         // Stubs cannot have optimistic assumptions since they have
 112         // to be valid for the entire run of the VM.
 113         final StructuredGraph graph = new StructuredGraph.Builder(options, debug).
 114                         method(method).
 115                         compilationId(compilationId).
 116                         setIsSubstitution(true).
 117                         build();
 118         // @formatter:on
 119         try (DebugContext.Scope outer = debug.scope("SnippetStub", graph)) {
 120             graph.disableUnsafeAccessTracking();
 121 
 122             IntrinsicContext initialIntrinsicContext = new IntrinsicContext(method, method, getReplacementsBytecodeProvider(), INLINE_AFTER_PARSING);
 123             GraphBuilderPhase.Instance instance = new GraphBuilderPhase.Instance(metaAccess, providers.getStampProvider(),
 124                             providers.getConstantReflection(), providers.getConstantFieldProvider(),
 125                             config, OptimisticOptimizations.NONE,
 126                             initialIntrinsicContext);
 127             instance.apply(graph);
 128 
 129             for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) {
 130                 int index = param.index();
 131                 if (method.getParameterAnnotation(NonNullParameter.class, index) != null) {
 132                     param.setStamp(param.stamp(NodeView.DEFAULT).join(StampFactory.objectNonNull()));
 133                 }
 134             }
 135 
 136             new RemoveValueProxyPhase().apply(graph);
 137             graph.setGuardsStage(GuardsStage.FLOATING_GUARDS);
 138             CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 139             PhaseContext context = new PhaseContext(providers);
 140             canonicalizer.apply(graph, context);
 141             new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 142         } catch (Throwable e) {
 143             throw debug.handle(e);
 144         }
 145 
 146         return graph;
 147     }
 148 
 149     protected BytecodeProvider getReplacementsBytecodeProvider() {
 150         return providers.getReplacements().getDefaultReplacementBytecodeProvider();
 151     }
 152 
 153     protected boolean checkConstArg(int index, String expectedName) {
 154         assert method.getParameterAnnotation(ConstantParameter.class, index) != null : String.format("parameter %d of %s is expected to be constant", index, method.format("%H.%n(%p)"));
 155         LocalVariableTable lvt = method.getLocalVariableTable();
 156         if (lvt != null) {
 157             Local local = lvt.getLocal(index, 0);
 158             assert local != null;
 159             String actualName = local.getName();
 160             assert actualName.equals(expectedName) : String.format("parameter %d of %s is expected to be named %s, not %s", index, method.format("%H.%n(%p)"), expectedName, actualName);
 161         }
 162         return true;
 163     }
 164 
 165     protected Object[] makeConstArgs() {
 166         int count = method.getSignature().getParameterCount(false);
 167         Object[] args = new Object[count];
 168         for (int i = 0; i < args.length; i++) {
 169             if (method.getParameterAnnotation(ConstantParameter.class, i) != null) {
 170                 args[i] = getConstantParameterValue(i, null);


 174     }
 175 
 176     protected Object getConstantParameterValue(int index, String name) {
 177         throw new GraalError("%s must override getConstantParameterValue() to provide a value for parameter %d%s", getClass().getName(), index, name == null ? "" : " (" + name + ")");
 178     }
 179 
 180     @Override
 181     protected Object debugScopeContext() {
 182         return getInstalledCodeOwner();
 183     }
 184 
 185     @Override
 186     public ResolvedJavaMethod getInstalledCodeOwner() {
 187         return method;
 188     }
 189 
 190     @Override
 191     public String toString() {
 192         return "Stub<" + getInstalledCodeOwner().format("%h.%n") + ">";
 193     }




 194 }


   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.stubs;
  26 




  27 import org.graalvm.compiler.api.replacements.Snippet;
  28 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
  29 import org.graalvm.compiler.api.replacements.Snippet.NonNullParameter;


  30 import org.graalvm.compiler.core.common.CompilationIdentifier;
  31 import org.graalvm.compiler.core.common.type.StampFactory;
  32 import org.graalvm.compiler.debug.DebugContext;
  33 import org.graalvm.compiler.debug.GraalError;
  34 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
  35 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;

  36 import org.graalvm.compiler.nodes.NodeView;
  37 import org.graalvm.compiler.nodes.ParameterNode;
  38 import org.graalvm.compiler.nodes.StructuredGraph;
  39 import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;



  40 import org.graalvm.compiler.nodes.spi.LoweringTool;
  41 import org.graalvm.compiler.options.OptionValues;

  42 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  43 import org.graalvm.compiler.phases.common.LoweringPhase;
  44 import org.graalvm.compiler.phases.common.RemoveValueProxyPhase;
  45 import org.graalvm.compiler.phases.tiers.PhaseContext;

  46 import org.graalvm.compiler.replacements.SnippetTemplate;
  47 import org.graalvm.compiler.replacements.Snippets;
  48 
  49 import jdk.vm.ci.meta.Local;
  50 import jdk.vm.ci.meta.LocalVariableTable;

  51 import jdk.vm.ci.meta.ResolvedJavaMethod;
  52 
  53 /**
  54  * Base class for a stub defined by a snippet.
  55  */
  56 public abstract class SnippetStub extends Stub implements Snippets {
  57 
  58     protected final ResolvedJavaMethod method;
  59 
  60     /**
  61      * Creates a new snippet stub.
  62      *
  63      * @param snippetMethodName name of the single {@link Snippet} annotated method in the class of
  64      *            this object
  65      * @param linkage linkage details for a call to the stub
  66      */
  67     public SnippetStub(String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
  68         this(null, snippetMethodName, options, providers, linkage);
  69     }
  70 
  71     /**
  72      * Creates a new snippet stub.
  73      *
  74      * @param snippetDeclaringClass this class in which the {@link Snippet} annotated method is
  75      *            declared. If {@code null}, this the class of this object is used.
  76      * @param snippetMethodName name of the single {@link Snippet} annotated method in
  77      *            {@code snippetDeclaringClass}
  78      * @param linkage linkage details for a call to the stub
  79      */
  80     public SnippetStub(Class<? extends Snippets> snippetDeclaringClass, String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
  81         super(options, providers, linkage);
  82         this.method = SnippetTemplate.AbstractTemplates.findMethod(providers.getMetaAccess(), snippetDeclaringClass == null ? getClass() : snippetDeclaringClass, snippetMethodName);
  83         registerSnippet();
  84     }
  85 
  86     protected void registerSnippet() {
  87         providers.getReplacements().registerSnippet(method, null, null, false);
  88     }
  89 
  90     @Override
  91     @SuppressWarnings("try")
  92     protected StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId) {









  93         // Stubs cannot have optimistic assumptions since they have
  94         // to be valid for the entire run of the VM.
  95         final StructuredGraph graph = buildInitialGraph(debug, compilationId, makeConstArgs());





  96         try (DebugContext.Scope outer = debug.scope("SnippetStub", graph)) {









  97             for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) {
  98                 int index = param.index();
  99                 if (method.getParameterAnnotation(NonNullParameter.class, index) != null) {
 100                     param.setStamp(param.stamp(NodeView.DEFAULT).join(StampFactory.objectNonNull()));
 101                 }
 102             }
 103 
 104             new RemoveValueProxyPhase().apply(graph);
 105             graph.setGuardsStage(GuardsStage.FLOATING_GUARDS);
 106             CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 107             PhaseContext context = new PhaseContext(providers);
 108             canonicalizer.apply(graph, context);
 109             new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 110         } catch (Throwable e) {
 111             throw debug.handle(e);
 112         }
 113 
 114         return graph;
 115     }
 116 
 117     protected StructuredGraph buildInitialGraph(DebugContext debug, CompilationIdentifier compilationId, Object[] args) {
 118         return providers.getReplacements().getSnippet(method, args, false, null).copyWithIdentifier(compilationId, debug);
 119     }
 120 
 121     protected boolean checkConstArg(int index, String expectedName) {
 122         assert method.getParameterAnnotation(ConstantParameter.class, index) != null : String.format("parameter %d of %s is expected to be constant", index, method.format("%H.%n(%p)"));
 123         LocalVariableTable lvt = method.getLocalVariableTable();
 124         if (lvt != null) {
 125             Local local = lvt.getLocal(index, 0);
 126             assert local != null;
 127             String actualName = local.getName();
 128             assert actualName.equals(expectedName) : String.format("parameter %d of %s is expected to be named %s, not %s", index, method.format("%H.%n(%p)"), expectedName, actualName);
 129         }
 130         return true;
 131     }
 132 
 133     protected Object[] makeConstArgs() {
 134         int count = method.getSignature().getParameterCount(false);
 135         Object[] args = new Object[count];
 136         for (int i = 0; i < args.length; i++) {
 137             if (method.getParameterAnnotation(ConstantParameter.class, i) != null) {
 138                 args[i] = getConstantParameterValue(i, null);


 142     }
 143 
 144     protected Object getConstantParameterValue(int index, String name) {
 145         throw new GraalError("%s must override getConstantParameterValue() to provide a value for parameter %d%s", getClass().getName(), index, name == null ? "" : " (" + name + ")");
 146     }
 147 
 148     @Override
 149     protected Object debugScopeContext() {
 150         return getInstalledCodeOwner();
 151     }
 152 
 153     @Override
 154     public ResolvedJavaMethod getInstalledCodeOwner() {
 155         return method;
 156     }
 157 
 158     @Override
 159     public String toString() {
 160         return "Stub<" + getInstalledCodeOwner().format("%h.%n") + ">";
 161     }
 162 
 163     public ResolvedJavaMethod getMethod() {
 164         return method;
 165     }
 166 }
< prev index next >