author | iveresov |
Fri, 17 Aug 2018 13:20:53 -0700 | |
changeset 51436 | 091c0d22e735 |
parent 50330 | 2cbc42a5764b |
child 52578 | 7dd81e82d083 |
permissions | -rw-r--r-- |
42650 | 1 |
/* |
46949 | 2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
42650 | 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 |
||
51436 | 24 |
|
25 |
||
42650 | 26 |
package jdk.tools.jaotc; |
27 |
||
28 |
import java.util.ListIterator; |
|
29 |
||
30 |
import org.graalvm.compiler.code.CompilationResult; |
|
31 |
import org.graalvm.compiler.core.GraalCompiler; |
|
47084 | 32 |
import org.graalvm.compiler.core.common.CompilationIdentifier; |
46640 | 33 |
import org.graalvm.compiler.debug.DebugContext; |
42650 | 34 |
import org.graalvm.compiler.hotspot.HotSpotBackend; |
35 |
import org.graalvm.compiler.hotspot.HotSpotCompiledCodeBuilder; |
|
36 |
import org.graalvm.compiler.hotspot.meta.HotSpotProviders; |
|
47668 | 37 |
import org.graalvm.compiler.hotspot.meta.HotSpotInvokeDynamicPlugin; |
42650 | 38 |
import org.graalvm.compiler.java.GraphBuilderPhase; |
39 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory; |
|
40 |
import org.graalvm.compiler.lir.phases.LIRSuites; |
|
41 |
import org.graalvm.compiler.nodes.StructuredGraph; |
|
42 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; |
|
43 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins; |
|
46344 | 44 |
import org.graalvm.compiler.options.OptionValues; |
42650 | 45 |
import org.graalvm.compiler.phases.BasePhase; |
46 |
import org.graalvm.compiler.phases.OptimisticOptimizations; |
|
47 |
import org.graalvm.compiler.phases.PhaseSuite; |
|
48 |
import org.graalvm.compiler.phases.tiers.HighTierContext; |
|
49 |
import org.graalvm.compiler.phases.tiers.Suites; |
|
50 |
||
51 |
import jdk.vm.ci.code.InstalledCode; |
|
52 |
import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider; |
|
53 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; |
|
54 |
import jdk.vm.ci.meta.DefaultProfilingInfo; |
|
55 |
import jdk.vm.ci.meta.ProfilingInfo; |
|
56 |
import jdk.vm.ci.meta.ResolvedJavaMethod; |
|
57 |
import jdk.vm.ci.meta.TriState; |
|
58 |
||
46949 | 59 |
final class AOTBackend { |
42650 | 60 |
private final Main main; |
46344 | 61 |
private final OptionValues graalOptions; |
42650 | 62 |
private final HotSpotBackend backend; |
63 |
private final HotSpotProviders providers; |
|
64 |
private final HotSpotCodeCacheProvider codeCache; |
|
65 |
private final PhaseSuite<HighTierContext> graphBuilderSuite; |
|
66 |
private final HighTierContext highTierContext; |
|
67 |
||
47668 | 68 |
AOTBackend(Main main, OptionValues graalOptions, HotSpotBackend backend, HotSpotInvokeDynamicPlugin inokeDynamicPlugin) { |
42650 | 69 |
this.main = main; |
46344 | 70 |
this.graalOptions = graalOptions; |
42650 | 71 |
this.backend = backend; |
72 |
providers = backend.getProviders(); |
|
73 |
codeCache = providers.getCodeCache(); |
|
47668 | 74 |
graphBuilderSuite = initGraphBuilderSuite(backend, main.options.compileWithAssertions, inokeDynamicPlugin); |
42650 | 75 |
highTierContext = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.ALL); |
76 |
} |
|
77 |
||
46949 | 78 |
PhaseSuite<HighTierContext> getGraphBuilderSuite() { |
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
79 |
return graphBuilderSuite; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
80 |
} |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
81 |
|
46949 | 82 |
HotSpotBackend getBackend() { |
46371 | 83 |
return backend; |
84 |
} |
|
85 |
||
46949 | 86 |
HotSpotProviders getProviders() { |
46640 | 87 |
return providers; |
88 |
} |
|
89 |
||
42650 | 90 |
private Suites getSuites() { |
91 |
// create suites every time, as we modify options for the compiler |
|
46344 | 92 |
return backend.getSuites().getDefaultSuites(graalOptions); |
42650 | 93 |
} |
94 |
||
95 |
private LIRSuites getLirSuites() { |
|
96 |
// create suites every time, as we modify options for the compiler |
|
46344 | 97 |
return backend.getSuites().getDefaultLIRSuites(graalOptions); |
42650 | 98 |
} |
99 |
||
100 |
@SuppressWarnings("try") |
|
46949 | 101 |
CompilationResult compileMethod(ResolvedJavaMethod resolvedMethod, DebugContext debug) { |
46640 | 102 |
StructuredGraph graph = buildStructuredGraph(resolvedMethod, debug); |
46344 | 103 |
if (graph != null) { |
46640 | 104 |
return compileGraph(resolvedMethod, graph, debug); |
42650 | 105 |
} |
46344 | 106 |
return null; |
42650 | 107 |
} |
108 |
||
109 |
/** |
|
110 |
* Build a structured graph for the member. |
|
111 |
* |
|
112 |
* @param javaMethod method for whose code the graph is to be created |
|
46640 | 113 |
* @param debug |
42650 | 114 |
* @return structured graph |
115 |
*/ |
|
116 |
@SuppressWarnings("try") |
|
46640 | 117 |
private StructuredGraph buildStructuredGraph(ResolvedJavaMethod javaMethod, DebugContext debug) { |
118 |
try (DebugContext.Scope s = debug.scope("AOTParseMethod")) { |
|
119 |
StructuredGraph graph = new StructuredGraph.Builder(graalOptions, debug).method(javaMethod).useProfilingInfo(false).build(); |
|
42650 | 120 |
graphBuilderSuite.apply(graph, highTierContext); |
121 |
return graph; |
|
122 |
} catch (Throwable e) { |
|
46949 | 123 |
main.handleError(javaMethod, e, " (building graph)"); |
42650 | 124 |
} |
125 |
return null; |
|
126 |
} |
|
127 |
||
128 |
@SuppressWarnings("try") |
|
46640 | 129 |
private CompilationResult compileGraph(ResolvedJavaMethod resolvedMethod, StructuredGraph graph, DebugContext debug) { |
130 |
try (DebugContext.Scope s = debug.scope("AOTCompileMethod")) { |
|
42650 | 131 |
ProfilingInfo profilingInfo = DefaultProfilingInfo.get(TriState.FALSE); |
132 |
||
133 |
final boolean isImmutablePIC = true; |
|
47084 | 134 |
CompilationIdentifier id = new CompilationIdentifier() { |
135 |
@Override |
|
136 |
public String toString(Verbosity verbosity) { |
|
137 |
return resolvedMethod.getName(); |
|
138 |
} |
|
139 |
}; |
|
140 |
CompilationResult compilationResult = new CompilationResult(id, isImmutablePIC); |
|
42650 | 141 |
|
142 |
return GraalCompiler.compileGraph(graph, resolvedMethod, providers, backend, graphBuilderSuite, OptimisticOptimizations.ALL, profilingInfo, getSuites(), getLirSuites(), |
|
50330 | 143 |
compilationResult, CompilationResultBuilderFactory.Default, true); |
42650 | 144 |
|
145 |
} catch (Throwable e) { |
|
46949 | 146 |
main.handleError(resolvedMethod, e, " (compiling graph)"); |
42650 | 147 |
} |
148 |
return null; |
|
149 |
} |
|
150 |
||
47668 | 151 |
private static PhaseSuite<HighTierContext> initGraphBuilderSuite(HotSpotBackend backend, boolean compileWithAssertions, HotSpotInvokeDynamicPlugin inokeDynamicPlugin) { |
42650 | 152 |
PhaseSuite<HighTierContext> graphBuilderSuite = backend.getSuites().getDefaultGraphBuilderSuite().copy(); |
153 |
ListIterator<BasePhase<? super HighTierContext>> iterator = graphBuilderSuite.findPhase(GraphBuilderPhase.class); |
|
154 |
GraphBuilderConfiguration baseConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig(); |
|
155 |
||
156 |
// Use all default plugins. |
|
157 |
Plugins plugins = baseConfig.getPlugins(); |
|
47668 | 158 |
plugins.setInvokeDynamicPlugin(inokeDynamicPlugin); |
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
159 |
GraphBuilderConfiguration aotConfig = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true).withOmitAssertions(!compileWithAssertions); |
42650 | 160 |
|
161 |
iterator.next(); |
|
162 |
iterator.remove(); |
|
163 |
iterator.add(new GraphBuilderPhase(aotConfig)); |
|
164 |
||
165 |
return graphBuilderSuite; |
|
166 |
} |
|
167 |
||
46949 | 168 |
void printCompiledMethod(HotSpotResolvedJavaMethod resolvedMethod, CompilationResult compResult) { |
42650 | 169 |
// This is really not installing the method. |
48443 | 170 |
InstalledCode installedCode = codeCache.addCode(resolvedMethod, HotSpotCompiledCodeBuilder.createCompiledCode(codeCache, resolvedMethod, null, compResult), null, null); |
42650 | 171 |
String disassembly = codeCache.disassemble(installedCode); |
172 |
if (disassembly != null) { |
|
46949 | 173 |
main.printer.printlnDebug(disassembly); |
42650 | 174 |
} |
175 |
} |
|
176 |
} |