src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot/src/org/graalvm/compiler/hotspot/HotSpotReplacementsImpl.java
changeset 58877 aec7bf35d6f5
parent 58299 6df94ce3ab2f
equal deleted inserted replaced
58876:1a8d65e71a66 58877:aec7bf35d6f5
    70     HotSpotReplacementsImpl(HotSpotReplacementsImpl replacements, Providers providers) {
    70     HotSpotReplacementsImpl(HotSpotReplacementsImpl replacements, Providers providers) {
    71         super(new GraalDebugHandlersFactory(replacements.snippetReflection), providers, replacements.snippetReflection,
    71         super(new GraalDebugHandlersFactory(replacements.snippetReflection), providers, replacements.snippetReflection,
    72                         replacements.getDefaultReplacementBytecodeProvider(), replacements.target);
    72                         replacements.getDefaultReplacementBytecodeProvider(), replacements.target);
    73     }
    73     }
    74 
    74 
       
    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 
    75     @Override
    85     @Override
    76     public Class<? extends GraphBuilderPlugin> getIntrinsifyingPlugin(ResolvedJavaMethod method) {
    86     public Class<? extends GraphBuilderPlugin> getIntrinsifyingPlugin(ResolvedJavaMethod method) {
    77         return method.getAnnotation(HotSpotOperation.class) != null ? HotSpotWordOperationPlugin.class : super.getIntrinsifyingPlugin(method);
    87         return method.getAnnotation(HotSpotOperation.class) != null ? HotSpotWordOperationPlugin.class : super.getIntrinsifyingPlugin(method);
    78     }
    88     }
    79 
    89 
    80     @Override
    90     @Override
    81     public void registerMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context, OptionValues options) {
    91     public void registerMethodSubstitution(MethodSubstitutionPlugin plugin) {
    82         if (!IS_IN_NATIVE_IMAGE) {
    92         if (snippetEncoder != null) {
    83             if (IS_BUILDING_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
    93             snippetEncoder.registerMethodSubstitution(plugin);
    84                 synchronized (HotSpotReplacementsImpl.class) {
    94         }
    85                     if (snippetEncoder == null) {
    95     }
    86                         snippetEncoder = new SymbolicSnippetEncoder(this);
    96 
    87                     }
    97     @Override
    88                     snippetEncoder.registerMethodSubstitution(plugin, original, context, options);
    98     public void registerConditionalPlugin(InvocationPlugin plugin) {
    89                 }
    99         if (snippetEncoder != null) {
    90             }
   100             snippetEncoder.registerConditionalPlugin(plugin);
    91         }
   101         }
       
   102     }
       
   103 
       
   104     public void checkRegistered(MethodSubstitutionPlugin plugin) {
       
   105         snippetEncoder.checkRegistered(plugin);
    92     }
   106     }
    93 
   107 
    94     @Override
   108     @Override
    95     public StructuredGraph getIntrinsicGraph(ResolvedJavaMethod method, CompilationIdentifier compilationId, DebugContext debug, Cancellable cancellable) {
   109     public StructuredGraph getIntrinsicGraph(ResolvedJavaMethod method, CompilationIdentifier compilationId, DebugContext debug, Cancellable cancellable) {
    96         boolean useEncodedGraphs = UseEncodedGraphs.getValue(debug.getOptions());
   110         boolean useEncodedGraphs = UseEncodedGraphs.getValue(debug.getOptions());
    97         if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
   111         if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
    98             HotSpotReplacementsImpl replacements = (HotSpotReplacementsImpl) providers.getReplacements();
   112             HotSpotReplacementsImpl replacements = (HotSpotReplacementsImpl) providers.getReplacements();
    99             InvocationPlugin plugin = replacements.getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(method);
   113             InvocationPlugin plugin = replacements.getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(method);
   100             if (plugin instanceof MethodSubstitutionPlugin) {
   114             if (plugin instanceof MethodSubstitutionPlugin) {
   101                 MethodSubstitutionPlugin msp = (MethodSubstitutionPlugin) plugin;
   115                 MethodSubstitutionPlugin msp = (MethodSubstitutionPlugin) plugin;
   102                 if (useEncodedGraphs) {
   116                 if (!IS_IN_NATIVE_IMAGE && useEncodedGraphs) {
   103                     replacements.registerMethodSubstitution(msp, method, ROOT_COMPILATION, debug.getOptions());
   117                     replacements.maybeInitializeEncoder(debug.getOptions());
       
   118                     replacements.registerMethodSubstitution(msp);
   104                 }
   119                 }
   105                 StructuredGraph methodSubstitution = replacements.getMethodSubstitution(msp, method, ROOT_COMPILATION, StructuredGraph.AllowAssumptions.YES, cancellable, debug.getOptions());
   120                 StructuredGraph methodSubstitution = replacements.getMethodSubstitution(msp, method, ROOT_COMPILATION, StructuredGraph.AllowAssumptions.YES, cancellable, debug.getOptions());
   106                 methodSubstitution.resetDebug(debug);
   121                 methodSubstitution.resetDebug(debug);
   107                 return methodSubstitution;
   122                 return methodSubstitution;
   108             }
   123             }
   117         if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
   132         if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
   118             InvocationPlugin plugin = getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(targetMethod);
   133             InvocationPlugin plugin = getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(targetMethod);
   119             if (plugin instanceof MethodSubstitutionPlugin && (!plugin.inlineOnly() || invokeBci >= 0)) {
   134             if (plugin instanceof MethodSubstitutionPlugin && (!plugin.inlineOnly() || invokeBci >= 0)) {
   120                 MethodSubstitutionPlugin msPlugin = (MethodSubstitutionPlugin) plugin;
   135                 MethodSubstitutionPlugin msPlugin = (MethodSubstitutionPlugin) plugin;
   121                 if (!IS_IN_NATIVE_IMAGE && useEncodedGraphs) {
   136                 if (!IS_IN_NATIVE_IMAGE && useEncodedGraphs) {
   122                     registerMethodSubstitution(msPlugin, targetMethod, INLINE_AFTER_PARSING, options);
   137                     maybeInitializeEncoder(options);
       
   138                     registerMethodSubstitution(msPlugin);
   123                 }
   139                 }
   124                 // This assumes the normal path creates the graph using
   140                 // This assumes the normal path creates the graph using
   125                 // GraphBuilderConfiguration.getSnippetDefault with omits exception edges
   141                 // GraphBuilderConfiguration.getSnippetDefault with omits exception edges
   126                 StructuredGraph subst = getMethodSubstitution(msPlugin, targetMethod, INLINE_AFTER_PARSING, StructuredGraph.AllowAssumptions.NO, null, options);
   142                 StructuredGraph subst = getMethodSubstitution(msPlugin, targetMethod, INLINE_AFTER_PARSING, StructuredGraph.AllowAssumptions.NO, null, options);
   127                 return subst;
   143                 return subst;
   153         if (!IS_IN_NATIVE_IMAGE) {
   169         if (!IS_IN_NATIVE_IMAGE) {
   154             assert !snippetRegistrationClosed : "Cannot register snippet after registration is closed: " + method.format("%H.%n(%p)");
   170             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)");
   171             assert registeredSnippets.add(method) : "Cannot register snippet twice: " + method.format("%H.%n(%p)");
   156             if (IS_BUILDING_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
   172             if (IS_BUILDING_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
   157                 synchronized (HotSpotReplacementsImpl.class) {
   173                 synchronized (HotSpotReplacementsImpl.class) {
   158                     if (snippetEncoder == null) {
       
   159                         snippetEncoder = new SymbolicSnippetEncoder(this);
       
   160                     }
       
   161                     snippetEncoder.registerSnippet(method, original, receiver, trackNodeSourcePosition, options);
   174                     snippetEncoder.registerSnippet(method, original, receiver, trackNodeSourcePosition, options);
   162                 }
   175                 }
   163             }
   176             }
   164         }
   177         }
   165     }
   178     }
   167     @Override
   180     @Override
   168     public void closeSnippetRegistration() {
   181     public void closeSnippetRegistration() {
   169         snippetRegistrationClosed = true;
   182         snippetRegistrationClosed = true;
   170     }
   183     }
   171 
   184 
   172     private static SymbolicSnippetEncoder.EncodedSnippets getEncodedSnippets() {
   185     private static SymbolicSnippetEncoder.EncodedSnippets getEncodedSnippets(OptionValues options) {
       
   186         if (!IS_IN_NATIVE_IMAGE && snippetEncoder != null) {
       
   187             snippetEncoder.encode(options);
       
   188         }
   173         return encodedSnippets;
   189         return encodedSnippets;
   174     }
   190     }
   175 
   191 
   176     public Set<ResolvedJavaMethod> getSnippetMethods() {
   192     public Set<ResolvedJavaMethod> getSnippetMethods() {
   177         if (snippetEncoder != null) {
   193         if (snippetEncoder != null) {
   208         return super.getSnippet(method, recursiveEntry, args, trackNodeSourcePosition, replaceePosition, options);
   224         return super.getSnippet(method, recursiveEntry, args, trackNodeSourcePosition, replaceePosition, options);
   209     }
   225     }
   210 
   226 
   211     @SuppressWarnings("try")
   227     @SuppressWarnings("try")
   212     private StructuredGraph getEncodedSnippet(ResolvedJavaMethod method, Object[] args, StructuredGraph.AllowAssumptions allowAssumptions, OptionValues options) {
   228     private StructuredGraph getEncodedSnippet(ResolvedJavaMethod method, Object[] args, StructuredGraph.AllowAssumptions allowAssumptions, OptionValues options) {
   213         boolean useEncodedGraphs = UseEncodedGraphs.getValue(options);
   229         if (IS_IN_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
   214         if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
       
   215             synchronized (HotSpotReplacementsImpl.class) {
   230             synchronized (HotSpotReplacementsImpl.class) {
   216                 if (!IS_IN_NATIVE_IMAGE) {
   231                 if (getEncodedSnippets(options) == null) {
   217                     snippetEncoder.encode(options);
       
   218                 }
       
   219 
       
   220                 if (getEncodedSnippets() == null) {
       
   221                     throw GraalError.shouldNotReachHere("encoded snippets not found");
   232                     throw GraalError.shouldNotReachHere("encoded snippets not found");
   222                 }
   233                 }
   223                 // Snippets graphs can contain foreign object reference and
   234                 // Snippets graphs can contain foreign object reference and
   224                 // outlive a single compilation.
   235                 // outlive a single compilation.
   225                 try (CompilationContext scope = HotSpotGraalServices.enterGlobalCompilationContext()) {
   236                 try (CompilationContext scope = HotSpotGraalServices.enterGlobalCompilationContext()) {
   226                     StructuredGraph graph = getEncodedSnippets().getEncodedSnippet(method, this, args, allowAssumptions, options);
   237                     StructuredGraph graph = getEncodedSnippets(options).getEncodedSnippet(method, this, args, allowAssumptions, options);
   227                     if (graph == null) {
   238                     if (graph == null) {
   228                         throw GraalError.shouldNotReachHere("snippet not found: " + method.format("%H.%n(%p)"));
   239                         throw GraalError.shouldNotReachHere("snippet not found: " + method.format("%H.%n(%p)"));
   229                     }
   240                     }
   230                     return graph;
   241                     return graph;
   231                 }
   242                 }
   237     }
   248     }
   238 
   249 
   239     @Override
   250     @Override
   240     public StructuredGraph getMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context,
   251     public StructuredGraph getMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context,
   241                     StructuredGraph.AllowAssumptions allowAssumptions, Cancellable cancellable, OptionValues options) {
   252                     StructuredGraph.AllowAssumptions allowAssumptions, Cancellable cancellable, OptionValues options) {
   242         boolean useEncodedGraphs = UseEncodedGraphs.getValue(options);
   253         if (IS_IN_NATIVE_IMAGE || UseEncodedGraphs.getValue(options)) {
   243         if (IS_IN_NATIVE_IMAGE || useEncodedGraphs) {
   254             if (getEncodedSnippets(options) == null) {
   244             if (!IS_IN_NATIVE_IMAGE) {
       
   245                 snippetEncoder.encode(options);
       
   246             }
       
   247 
       
   248             if (getEncodedSnippets() == null) {
       
   249                 throw GraalError.shouldNotReachHere("encoded snippets not found");
   255                 throw GraalError.shouldNotReachHere("encoded snippets not found");
   250             }
   256             }
   251             return getEncodedSnippets().getMethodSubstitutionGraph(plugin, original, this, context, allowAssumptions, cancellable, options);
   257             return getEncodedSnippets(options).getMethodSubstitutionGraph(plugin, original, this, context, allowAssumptions, cancellable, options);
   252         }
   258         }
   253         return null;
   259         return null;
   254     }
   260     }
   255 
   261 
   256 }
   262 }