scala-cli icon indicating copy to clipboard operation
scala-cli copied to clipboard

Java compilation with `jdk.incubator.vector` module hangs indefinitely

Open Gedochao opened this issue 1 year ago • 2 comments

Version(s) v1.1.2

Describe the bug The --add-modules=jdk.incubator.vector javac option seems to hang the compilation with the following code:

//> using javacOpt --add-modules=jdk.incubator.vector

import jdk.incubator.vector.FloatVector;
import jdk.incubator.vector.VectorSpecies;

public class Main {

    static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_256;

    static void vectorComputation(float[] a, float[] b, float[] c) {

        for (int i = 0; i < a.length; i += SPECIES.length()) {
            var m = SPECIES.indexInRange(i, a.length);

            var va = FloatVector.fromArray(SPECIES, a, i, m);
            var vb = FloatVector.fromArray(SPECIES, b, i, m);
            var vc = va.mul(va).
                    add(vb.mul(vb)).
                    neg();
            vc.intoArray(c, i, m);
        }
    }

    public static void main(String[] args) {
        float[] a = new float[]{1.0f, 3.0f, 2.0f};
        float[] b = {1.0f, -1.0f, 5.0f};
        float[] c = {1.0f, 6.0f, 1.0f};
        Main.vectorComputation(a, b, c);
        System.out.println(c);
    }
}

To Reproduce Just compile the code mentioned above with Scala CLI. The implicit Java version is 17 here, the behaviour is similar when testing with Java 21.

scala-cli compile Main.java
# Compiling project (Java)
# Warning: using incubating module(s): jdk.incubator.vector
# Warning: using incubating module(s): jdk.incubator.vector
# Warning: using incubating module(s): jdk.incubator.vector

This causes the compilation to hang indefinitely. Also note that the module warning gets printed thrice, where it should only happen once. 🤔

Expected behaviour The compilation should be successful. The code works when compiled with the vanilla Java compiler:

javac --add-modules jdk.incubator.vector Main.java
# warning: using incubating module(s): jdk.incubator.vector
# 1 warning

Gedochao avatar Jan 22 '24 08:01 Gedochao

Note: this may be Bloop-related, as --server=false makes the compilation pass.

scala-cli compile Main.java --server=false
# warning: using incubating module(s): jdk.incubator.vector
# 1 warning

Gedochao avatar Jan 22 '24 09:01 Gedochao

I narrowed this slightly to run in pure scala;

vec.scala

import jdk.incubator.vector.DoubleVector;
import jdk.incubator.vector.VectorSpecies;


def vecMe(v1: Array[Double], v2: Array[Double]): Array[Double] =
  var i = 0
  var out = new Array[Double](v1.length)
  val SPECIES = DoubleVector.SPECIES_PREFERRED

  while (i < v1.length) {
    var m = SPECIES.indexInRange(i, v1.length);

    var va = DoubleVector.fromArray(SPECIES, v1, i, m);
    var vb = DoubleVector.fromArray(SPECIES, v2, i, m);
    var vc = va.mul(va).
            add(vb.mul(vb)).
            neg();
    vc.intoArray(out, i, m);
    i += SPECIES.length()
  }

  out

@main def run =
  println(vecMe(Array(1.0, 2.0, 3.0), Array(4.0, 5.0, 6.0)).mkString(","))

Here is project.scala

//> using scala 3.3.1
//> using javaOpt --add-modules=jdk.incubator.vector

For me, this command

scala-cli run . 

Works as expected;

simon@Simons-Mac-mini scripts % scala-cli run . 
Compiling project (Scala 3.3.1, JVM (21))
Compiled project (Scala 3.3.1, JVM (21))
WARNING: Using incubator modules: jdk.incubator.vector
-17.0,-29.0,-45.0

But scala-cli doesn't like it via the code lenses,

xecuting task: /"opt"/"homebrew"/"Cellar"/"openjdk"/"21.0.1"/"libexec"/"openjdk.jdk"/"Contents"/"Home"/"bin"/"java"  -classpath "/Users/simon/Code/scripts/.metals/.tmp/classpath_2F8C966C18BA319ED39FA5CEDB80166B.jar" run  

Exception in thread "main" java.lang.NoClassDefFoundError: jdk/incubator/vector/Vector
        at run.main(breeze.scala:24)
Caused by: java.lang.ClassNotFoundException: jdk.incubator.vector.Vector
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
        ... 1 more

 *  The terminal process "/bin/zsh '-l', '-c', '/"opt"/"homebrew"/"Cellar"/"openjdk"/"21.0.1"/"libexec"/"openjdk.jdk"/"Contents"/"Home"/"bin"/"java"  -classpath "/Users/simon/Code/scripts/.metals/.tmp/classpath_2F8C966C18BA319ED39FA5CEDB80166B.jar" run '" failed to launch (exit code: 1). 
 *  Terminal will be reused by tasks, press any key to close it. 

I claim, that the invocation path through the code tense, ignores that javaOpt directive.

Quafadas avatar Jan 22 '24 09:01 Quafadas