author | iignatyev |
Tue, 18 Dec 2018 13:37:06 -0800 | |
changeset 53071 | 281c85f43f79 |
parent 51436 | 091c0d22e735 |
child 54084 | 84f10bbf993f |
permissions | -rw-r--r-- |
42650 | 1 |
/* |
51206
14b870bda24f
8207069: [AOT] we should check that VM uses the same GC as one used for AOT library generation.
kvn
parents:
49873
diff
changeset
|
2 |
* Copyright (c) 2016, 2018, 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 static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; |
|
29 |
import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode; |
|
30 |
import static org.graalvm.compiler.hotspot.meta.HotSpotAOTProfilingPlugin.Options.TieredAOT; |
|
31 |
||
53071 | 32 |
import java.io.IOException; |
42650 | 33 |
import java.io.PrintWriter; |
34 |
import java.text.MessageFormat; |
|
53071 | 35 |
import java.util.ArrayList; |
36 |
import java.util.Collections; |
|
42650 | 37 |
import java.util.List; |
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
38 |
import java.util.ListIterator; |
42650 | 39 |
import java.util.Set; |
53071 | 40 |
import java.util.StringTokenizer; |
41 |
import java.util.stream.Stream; |
|
42 |
import java.nio.file.Files; |
|
43 |
import java.nio.file.Paths; |
|
42650 | 44 |
|
46640 | 45 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; |
42650 | 46 |
import org.graalvm.compiler.api.runtime.GraalJVMCICompiler; |
46640 | 47 |
import org.graalvm.compiler.debug.DebugContext; |
48 |
import org.graalvm.compiler.debug.DebugContext.Activation; |
|
46344 | 49 |
import org.graalvm.compiler.hotspot.CompilerConfigurationFactory; |
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
50 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; |
46344 | 51 |
import org.graalvm.compiler.hotspot.HotSpotGraalCompilerFactory; |
52 |
import org.graalvm.compiler.hotspot.HotSpotGraalOptionValues; |
|
51206
14b870bda24f
8207069: [AOT] we should check that VM uses the same GC as one used for AOT library generation.
kvn
parents:
49873
diff
changeset
|
53 |
import org.graalvm.compiler.hotspot.HotSpotGraalRuntime; |
42650 | 54 |
import org.graalvm.compiler.hotspot.HotSpotHostBackend; |
47668 | 55 |
import org.graalvm.compiler.hotspot.meta.HotSpotInvokeDynamicPlugin; |
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
56 |
import org.graalvm.compiler.java.GraphBuilderPhase; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
57 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; |
46344 | 58 |
import org.graalvm.compiler.options.OptionValues; |
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
59 |
import org.graalvm.compiler.phases.BasePhase; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
60 |
import org.graalvm.compiler.phases.PhaseSuite; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
61 |
import org.graalvm.compiler.phases.tiers.HighTierContext; |
46640 | 62 |
import org.graalvm.compiler.printer.GraalDebugHandlersFactory; |
42650 | 63 |
import org.graalvm.compiler.runtime.RuntimeProvider; |
64 |
||
51436 | 65 |
import jdk.tools.jaotc.Options.Option; |
66 |
import jdk.tools.jaotc.binformat.BinaryContainer; |
|
42650 | 67 |
import jdk.vm.ci.meta.MetaAccessProvider; |
68 |
import jdk.vm.ci.meta.ResolvedJavaMethod; |
|
69 |
import jdk.vm.ci.runtime.JVMCI; |
|
70 |
||
46949 | 71 |
public final class Main { |
42650 | 72 |
|
46949 | 73 |
final Options options = new Options(); |
74 |
private PrintWriter log; |
|
75 |
LogPrinter printer; |
|
76 |
GraalFilters filters; |
|
42650 | 77 |
|
78 |
private static final int EXIT_OK = 0; // No errors. |
|
79 |
private static final int EXIT_CMDERR = 2; // Bad command-line arguments and/or switches. |
|
80 |
private static final int EXIT_ABNORMAL = 4; // Terminated abnormally. |
|
81 |
||
82 |
private static final String PROGNAME = "jaotc"; |
|
83 |
||
84 |
private static final String JVM_VERSION = System.getProperty("java.runtime.version"); |
|
85 |
||
86 |
public static void main(String[] args) throws Exception { |
|
87 |
Main t = new Main(); |
|
53071 | 88 |
final int exitCode = t.run(parse(args)); |
42650 | 89 |
System.exit(exitCode); |
90 |
} |
|
91 |
||
53071 | 92 |
/** |
93 |
* Expands '@file' in command line arguments by replacing '@file' with the content of 'file' |
|
94 |
* parsed by StringTokenizer. '@' character can be quoted as '@@'. |
|
95 |
*/ |
|
96 |
private static String[] parse(String[] args) throws IOException { |
|
97 |
List<String> result = new ArrayList<>(); |
|
98 |
for (String arg : args) { |
|
99 |
if (arg.length() > 1 && arg.charAt(0) == '@') { |
|
100 |
String v = arg.substring(1); |
|
101 |
if (v.charAt(0) == '@') { |
|
102 |
result.add(v); |
|
103 |
} else { |
|
104 |
try (Stream<String> file = Files.lines(Paths.get(v))) { |
|
105 |
file.map(StringTokenizer::new).map(Collections::list).flatMap(l -> l.stream().map(o -> (String) o)).forEachOrdered(result::add); |
|
106 |
} |
|
107 |
} |
|
108 |
} else { |
|
109 |
result.add(arg); |
|
110 |
} |
|
111 |
} |
|
112 |
return result.toArray(String[]::new); |
|
113 |
} |
|
114 |
||
42650 | 115 |
private int run(String[] args) { |
46949 | 116 |
log = new PrintWriter(System.out); |
117 |
printer = new LogPrinter(this, log); |
|
42650 | 118 |
|
119 |
try { |
|
46949 | 120 |
Options.handleOptions(this, args); |
42650 | 121 |
if (options.help) { |
122 |
showHelp(); |
|
123 |
return EXIT_OK; |
|
124 |
} |
|
125 |
if (options.version) { |
|
126 |
showVersion(); |
|
127 |
return EXIT_OK; |
|
128 |
} |
|
129 |
||
46949 | 130 |
printer.printlnInfo("Compiling " + options.outputName + "..."); |
42650 | 131 |
final long start = System.currentTimeMillis(); |
43464
f38fde4a6b52
8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
43405
diff
changeset
|
132 |
if (!run()) { |
46344 | 133 |
return EXIT_ABNORMAL; |
43464
f38fde4a6b52
8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
43405
diff
changeset
|
134 |
} |
42650 | 135 |
final long end = System.currentTimeMillis(); |
46949 | 136 |
printer.printlnInfo("Total time: " + (end - start) + " ms"); |
42650 | 137 |
|
138 |
return EXIT_OK; |
|
46949 | 139 |
} catch (Options.BadArgs e) { |
140 |
printer.reportError(e.key, e.args); |
|
42650 | 141 |
if (e.showUsage) { |
142 |
showUsage(); |
|
143 |
} |
|
144 |
return EXIT_CMDERR; |
|
145 |
} catch (Exception e) { |
|
146 |
e.printStackTrace(); |
|
147 |
return EXIT_ABNORMAL; |
|
148 |
} finally { |
|
149 |
log.flush(); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
@SuppressWarnings("try") |
|
43464
f38fde4a6b52
8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
43405
diff
changeset
|
154 |
private boolean run() throws Exception { |
46949 | 155 |
LogPrinter.openLog(); |
42650 | 156 |
|
157 |
try { |
|
158 |
||
46949 | 159 |
final Linker linker = new Linker(this); |
160 |
final String objectFileName = linker.objFile(); |
|
161 |
final Collector collector = new Collector(this); |
|
162 |
Set<Class<?>> classesToCompile; |
|
42650 | 163 |
|
164 |
try (Timer t = new Timer(this, "")) { |
|
46949 | 165 |
classesToCompile = collector.collectClassesToCompile(); |
166 |
printer.printInfo(classesToCompile.size() + " classes found"); |
|
42650 | 167 |
} |
168 |
||
46344 | 169 |
OptionValues graalOptions = HotSpotGraalOptionValues.HOTSPOT_OPTIONS; |
170 |
// Setting -Dgraal.TieredAOT overrides --compile-for-tiered |
|
171 |
if (!TieredAOT.hasBeenSet(graalOptions)) { |
|
172 |
graalOptions = new OptionValues(graalOptions, TieredAOT, options.tiered); |
|
173 |
} |
|
46557 | 174 |
graalOptions = new OptionValues(graalOptions, GeneratePIC, true, ImmutableCode, true); |
49873 | 175 |
GraalJVMCICompiler graalCompiler = HotSpotGraalCompilerFactory.createCompiler("JAOTC", JVMCI.getRuntime(), graalOptions, CompilerConfigurationFactory.selectFactory(null, graalOptions)); |
51206
14b870bda24f
8207069: [AOT] we should check that VM uses the same GC as one used for AOT library generation.
kvn
parents:
49873
diff
changeset
|
176 |
HotSpotGraalRuntime runtime = (HotSpotGraalRuntime) graalCompiler.getGraalRuntime(); |
42650 | 177 |
HotSpotHostBackend backend = (HotSpotHostBackend) runtime.getCapability(RuntimeProvider.class).getHostBackend(); |
178 |
MetaAccessProvider metaAccess = backend.getProviders().getMetaAccess(); |
|
46949 | 179 |
filters = new GraalFilters(metaAccess); |
42650 | 180 |
|
181 |
List<AOTCompiledClass> classes; |
|
182 |
||
183 |
try (Timer t = new Timer(this, "")) { |
|
46949 | 184 |
classes = collector.collectMethodsToCompile(classesToCompile, metaAccess); |
42650 | 185 |
} |
186 |
||
187 |
// Free memory! |
|
188 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
46949 | 189 |
printer.printMemoryUsage(); |
42650 | 190 |
classesToCompile = null; |
191 |
System.gc(); |
|
192 |
} |
|
193 |
||
47668 | 194 |
AOTDynamicTypeStore dynoStore = new AOTDynamicTypeStore(); |
195 |
AOTCompiledClass.setDynamicTypeStore(dynoStore); |
|
196 |
||
197 |
AOTBackend aotBackend = new AOTBackend(this, graalOptions, backend, new HotSpotInvokeDynamicPlugin(dynoStore)); |
|
46640 | 198 |
SnippetReflectionProvider snippetReflection = aotBackend.getProviders().getSnippetReflection(); |
46344 | 199 |
AOTCompiler compiler = new AOTCompiler(this, graalOptions, aotBackend, options.threads); |
42650 | 200 |
classes = compiler.compileClasses(classes); |
201 |
||
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
202 |
GraalHotSpotVMConfig graalHotSpotVMConfig = runtime.getVMConfig(); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
203 |
PhaseSuite<HighTierContext> graphBuilderSuite = aotBackend.getGraphBuilderSuite(); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
204 |
ListIterator<BasePhase<? super HighTierContext>> iterator = graphBuilderSuite.findPhase(GraphBuilderPhase.class); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
205 |
GraphBuilderConfiguration graphBuilderConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig(); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
206 |
|
42650 | 207 |
// Free memory! |
208 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
46949 | 209 |
printer.printMemoryUsage(); |
42650 | 210 |
aotBackend = null; |
211 |
compiler = null; |
|
212 |
System.gc(); |
|
213 |
} |
|
214 |
||
51436 | 215 |
int gc = runtime.getGarbageCollector().ordinal() + 1; |
51206
14b870bda24f
8207069: [AOT] we should check that VM uses the same GC as one used for AOT library generation.
kvn
parents:
49873
diff
changeset
|
216 |
BinaryContainer binaryContainer = new BinaryContainer(graalOptions, graalHotSpotVMConfig, graphBuilderConfig, gc, JVM_VERSION); |
42650 | 217 |
DataBuilder dataBuilder = new DataBuilder(this, backend, classes, binaryContainer); |
46640 | 218 |
|
219 |
try (DebugContext debug = DebugContext.create(graalOptions, new GraalDebugHandlersFactory(snippetReflection)); Activation a = debug.activate()) { |
|
220 |
dataBuilder.prepareData(debug); |
|
221 |
} |
|
42650 | 222 |
|
223 |
// Print information about section sizes |
|
46949 | 224 |
printer.containersInfo(binaryContainer); |
42650 | 225 |
|
226 |
// Free memory! |
|
227 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
46949 | 228 |
printer.printMemoryUsage(); |
42650 | 229 |
backend = null; |
230 |
for (AOTCompiledClass aotCompClass : classes) { |
|
231 |
aotCompClass.clear(); |
|
232 |
} |
|
233 |
classes.clear(); |
|
234 |
classes = null; |
|
235 |
dataBuilder = null; |
|
236 |
binaryContainer.freeMemory(); |
|
237 |
System.gc(); |
|
238 |
} |
|
239 |
||
240 |
try (Timer t = new Timer(this, "Creating binary: " + objectFileName)) { |
|
46949 | 241 |
binaryContainer.createBinary(objectFileName); |
42650 | 242 |
} |
243 |
||
244 |
// Free memory! |
|
245 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
46949 | 246 |
printer.printMemoryUsage(); |
42650 | 247 |
binaryContainer = null; |
248 |
System.gc(); |
|
249 |
} |
|
250 |
||
46949 | 251 |
try (Timer t = new Timer(this, "Creating shared library: " + linker.libFile())) { |
252 |
linker.link(); |
|
42650 | 253 |
} |
254 |
||
46949 | 255 |
printer.printVerbose("Final memory "); |
256 |
printer.printMemoryUsage(); |
|
257 |
printer.printlnVerbose(""); |
|
42650 | 258 |
|
259 |
} finally { |
|
46949 | 260 |
LogPrinter.closeLog(); |
42650 | 261 |
} |
43464
f38fde4a6b52
8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
43405
diff
changeset
|
262 |
return true; |
42650 | 263 |
} |
264 |
||
46949 | 265 |
void handleError(ResolvedJavaMethod resolvedMethod, Throwable e, String message) { |
266 |
String methodName = JavaMethodInfo.uniqueMethodName(resolvedMethod); |
|
42650 | 267 |
|
46949 | 268 |
if (options.debug) { |
269 |
printer.printError("Failed compilation: " + methodName + ": " + e); |
|
42650 | 270 |
} |
46949 | 271 |
|
272 |
// Ignore some exceptions when meta-compiling Graal. |
|
273 |
if (GraalFilters.shouldIgnoreException(e)) { |
|
42650 | 274 |
return; |
275 |
} |
|
276 |
||
46949 | 277 |
LogPrinter.writeLog("Failed compilation of method " + methodName + message); |
42650 | 278 |
|
46949 | 279 |
if (!options.debug) { |
280 |
printer.printError("Failed compilation: " + methodName + ": " + e); |
|
42650 | 281 |
} |
282 |
||
46949 | 283 |
if (options.verbose) { |
284 |
e.printStackTrace(log); |
|
285 |
} |
|
42650 | 286 |
|
46949 | 287 |
if (options.exitOnError) { |
288 |
System.exit(1); |
|
42650 | 289 |
} |
290 |
} |
|
291 |
||
46949 | 292 |
void warning(String key, Object... args) { |
42650 | 293 |
log.println("Warning: " + MessageFormat.format(key, args)); |
294 |
log.flush(); |
|
295 |
} |
|
296 |
||
297 |
private void showUsage() { |
|
43479 | 298 |
log.println("Usage: " + PROGNAME + " <options> list"); |
42650 | 299 |
log.println("use --help for a list of possible options"); |
46949 | 300 |
log.flush(); |
42650 | 301 |
} |
302 |
||
303 |
private void showHelp() { |
|
43479 | 304 |
log.println("Usage: " + PROGNAME + " <options> list"); |
42650 | 305 |
log.println(); |
43479 | 306 |
log.println(" list A : separated list of class names, modules, jar files"); |
307 |
log.println(" or directories which contain class files."); |
|
42650 | 308 |
log.println(); |
43479 | 309 |
log.println("where options include:"); |
46949 | 310 |
for (Option o : Options.recognizedOptions) { |
42650 | 311 |
String name = o.aliases[0].substring(1); // there must always be at least one name |
312 |
name = name.charAt(0) == '-' ? name.substring(1) : name; |
|
313 |
if (o.isHidden() || name.equals("h")) { |
|
314 |
continue; |
|
315 |
} |
|
316 |
log.println(o.help); |
|
317 |
} |
|
46949 | 318 |
log.flush(); |
42650 | 319 |
} |
320 |
||
321 |
private void showVersion() { |
|
322 |
log.println(PROGNAME + " " + JVM_VERSION); |
|
323 |
} |
|
324 |
} |