Skip to content

Commit 5a5b342

Browse files
committed
8352020: [CompileFramework] enable compilation for VectorAPI
Reviewed-by: chagedorn, kvn
1 parent 6ac9299 commit 5a5b342

File tree

3 files changed

+105
-6
lines changed

3 files changed

+105
-6
lines changed

test/hotspot/jtreg/compiler/lib/compile_framework/Compile.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.nio.file.Files;
3131
import java.nio.file.Path;
3232
import java.util.ArrayList;
33+
import java.util.Arrays;
3334
import java.util.concurrent.TimeUnit;
3435
import java.util.List;
3536
import jdk.test.lib.JDKToolFinder;
@@ -47,26 +48,29 @@ class Compile {
4748
* Compile all sources in {@code javaSources}. First write them to the {@code sourceDir},
4849
* then compile them to class-files which are stored in {@code classesDir}.
4950
*/
50-
public static void compileJavaSources(List<SourceCode> javaSources, Path sourceDir, Path classesDir) {
51+
public static void compileJavaSources(List<SourceCode> javaSources, Path sourceDir, Path classesDir, String[] javacFlags) {
5152
if (javaSources.isEmpty()) {
5253
Utils.printlnVerbose("No java sources to compile.");
5354
return;
5455
}
5556
Utils.printlnVerbose("Compiling Java sources: " + javaSources.size());
5657

5758
List<Path> javaFilePaths = writeSourcesToFiles(javaSources, sourceDir);
58-
compileJavaFiles(javaFilePaths, classesDir);
59+
compileJavaFiles(javaFilePaths, classesDir, javacFlags);
5960
Utils.printlnVerbose("Java sources compiled.");
6061
}
6162

6263
/**
6364
* Compile a list of files (i.e. {@code paths}) using javac and store
6465
* them in {@code classesDir}.
6566
*/
66-
private static void compileJavaFiles(List<Path> paths, Path classesDir) {
67+
private static void compileJavaFiles(List<Path> paths, Path classesDir, String[] javacFlags) {
6768
List<String> command = new ArrayList<>();
6869

6970
command.add(JAVAC_PATH);
71+
if (javacFlags != null) {
72+
command.addAll(Arrays.asList(javacFlags));
73+
}
7074
command.add("-classpath");
7175
// Note: the backslashes from windows paths must be escaped!
7276
command.add(Utils.getEscapedClassPathAndClassesDir(classesDir));
@@ -192,8 +196,10 @@ private static void executeCompileCommand(List<String> command) {
192196
throw new CompileFrameworkException("InterruptedException during compilation", e);
193197
}
194198

195-
if (exitCode != 0 || !output.isEmpty()) {
199+
// Note: the output can be non-empty even if the compilation succeeds, e.g. for warnings.
200+
if (exitCode != 0) {
196201
System.err.println("Compilation failed.");
202+
System.err.println("Command: " + command);
197203
System.err.println("Exit code: " + exitCode);
198204
System.err.println("Output: '" + output + "'");
199205
throw new CompileFrameworkException("Compilation failed.");

test/hotspot/jtreg/compiler/lib/compile_framework/CompileFramework.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ public void addJasmSourceCode(String className, String code) {
7171
* Compile all sources: store the sources to the {@link sourceDir} directory, compile
7272
* Java and Jasm sources and store the generated class-files in the {@link classesDir}
7373
* directory.
74+
*
75+
* @param javacFlags: optional, list of additional flags for javac, e.g. to make modules
76+
* visible.
7477
*/
75-
public void compile() {
78+
public void compile(String... javacFlags) {
7679
if (classLoader != null) {
7780
throw new CompileFrameworkException("Cannot compile twice!");
7881
}
@@ -86,7 +89,7 @@ public void compile() {
8689
System.out.println("Classes directory: " + classesDir);
8790

8891
Compile.compileJasmSources(jasmSources, sourceDir, classesDir);
89-
Compile.compileJavaSources(javaSources, sourceDir, classesDir);
92+
Compile.compileJavaSources(javaSources, sourceDir, classesDir, javacFlags);
9093
classLoader = ClassLoaderBuilder.build(classesDir);
9194
}
9295

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2025, 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+
* @test
26+
* @summary Example test to use the Compile Framework together with the IR Framework (i.e. TestFramework),
27+
* and the VectorAPI.
28+
* @modules java.base/jdk.internal.misc
29+
* @modules jdk.incubator.vector
30+
* @library /test/lib /
31+
* @compile ../../../compiler/lib/ir_framework/TestFramework.java
32+
* @run driver compile_framework.examples.IRFrameworkWithVectorAPIExample
33+
*/
34+
35+
package compile_framework.examples;
36+
37+
import compiler.lib.compile_framework.*;
38+
import jdk.test.lib.Utils;
39+
import jdk.incubator.vector.IntVector;
40+
import jdk.test.lib.Platform;
41+
import java.lang.reflect.InvocationTargetException;
42+
43+
/**
44+
* This test shows that the IR verification can be done on code compiled by the Compile Framework.
45+
* The "@compile" command for JTREG is required so that the IRFramework is compiled, other javac
46+
* might not compile it because it is not present in the class, only in the dynamically compiled
47+
* code.
48+
* <p>
49+
* Additionally, we must set the classpath for the Test-VM, so that it has access to all compiled
50+
* classes (see {@link CompileFramework#getEscapedClassPathOfCompiledClasses}).
51+
*/
52+
public class IRFrameworkWithVectorAPIExample {
53+
54+
public static void main(String[] args) {
55+
// Create a new CompileFramework instance.
56+
CompileFramework comp = new CompileFramework();
57+
58+
// Add a java source file.
59+
comp.addJavaSourceCode("InnerTest", generateInnerTest(comp));
60+
61+
// Compile the source file. "javac" needs to know that it is ok to compile with the
62+
// VectorAPI module.
63+
comp.compile("--add-modules=jdk.incubator.vector");
64+
65+
// InnerTest.main();
66+
comp.invoke("InnerTest", "main", new Object[] {null});
67+
}
68+
69+
// Generate a source java file as String
70+
public static String generateInnerTest(CompileFramework comp) {
71+
return String.format("""
72+
import compiler.lib.ir_framework.*;
73+
import jdk.incubator.vector.*;
74+
75+
public class InnerTest {
76+
public static void main(String args[]) {
77+
TestFramework framework = new TestFramework(InnerTest.class);
78+
// Also the TestFramework must allow the test VM to see the VectorAPI module.
79+
framework.addFlags("-classpath", "%s", "--add-modules=jdk.incubator.vector");
80+
framework.start();
81+
}
82+
83+
@Test
84+
static Object test() {
85+
return IntVector.broadcast(IntVector.SPECIES_64, 42);
86+
}
87+
}
88+
""", comp.getEscapedClassPathOfCompiledClasses());
89+
}
90+
}

0 commit comments

Comments
 (0)